Skip to content

Commit

Permalink
RENAME:azurerm_private_link_endpoint to `azurerm_private_endp… (#5150)
Browse files Browse the repository at this point in the history
  • Loading branch information
WodansSon authored and katbyte committed Dec 13, 2019
1 parent 93141f3 commit ffa5618
Show file tree
Hide file tree
Showing 16 changed files with 1,152 additions and 300 deletions.
191 changes: 191 additions & 0 deletions azurerm/data_source_private_endpoint_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package azurerm

import (
"context"
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
aznet "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmPrivateEndpointConnection() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPrivateEndpointConnectionRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: aznet.ValidatePrivateLinkName,
},

"location": azure.SchemaLocationForDataSource(),

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"private_service_connection": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"request_response": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"private_ip_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceArmPrivateEndpointConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Network.PrivateEndpointClient
nicsClient := meta.(*ArmClient).Network.InterfacesClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resourceGroup, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Private Endpoint %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on Private Endpoint %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)
d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if props := resp.PrivateEndpointProperties; props != nil {
privateIpAddress := ""

if nics := props.NetworkInterfaces; nics != nil && len(*nics) > 0 {
nic := (*nics)[0]
if nic.ID != nil && *nic.ID != "" {
privateIpAddress = getPrivateIpAddress(ctx, nicsClient, *nic.ID)
}
}

if err := d.Set("private_service_connection", dataSourceFlattenArmPrivateEndpointServiceConnection(props.PrivateLinkServiceConnections, props.ManualPrivateLinkServiceConnections, privateIpAddress)); err != nil {
return fmt.Errorf("Error setting `private_service_connection`: %+v", err)
}
}

return nil
}

func getPrivateIpAddress(ctx context.Context, client *network.InterfacesClient, networkInterfaceId string) string {
privateIpAddress := ""
id, err := azure.ParseAzureResourceID(networkInterfaceId)
if err != nil {
return privateIpAddress
}
name := id.Path["networkInterfaces"]

resp, err := client.Get(ctx, id.ResourceGroup, name, "")
if err != nil {
return privateIpAddress
}

if props := resp.InterfacePropertiesFormat; props != nil {
if configs := props.IPConfigurations; configs != nil {
for i, config := range *configs {
if propFmt := config.InterfaceIPConfigurationPropertiesFormat; propFmt != nil {
if propFmt.PrivateIPAddress != nil && *propFmt.PrivateIPAddress != "" && i == 0 {
privateIpAddress = *propFmt.PrivateIPAddress
}
break
}
}
}
}

return privateIpAddress
}

func dataSourceFlattenArmPrivateEndpointServiceConnection(serviceConnections *[]network.PrivateLinkServiceConnection, manualServiceConnections *[]network.PrivateLinkServiceConnection, privateIpAddress string) []interface{} {
results := make([]interface{}, 0)
if serviceConnections == nil && manualServiceConnections == nil {
return results
}

if serviceConnections != nil {
for _, item := range *serviceConnections {
result := make(map[string]interface{})
result["private_ip_address"] = privateIpAddress

if v := item.Name; v != nil {
result["name"] = *v
}
if props := item.PrivateLinkServiceConnectionProperties; props != nil {
if v := props.PrivateLinkServiceConnectionState; v != nil {
if s := v.Status; s != nil {
result["status"] = *s
}
if d := v.Description; d != nil {
result["request_response"] = *d
}
}
}

results = append(results, result)
}
}

if manualServiceConnections != nil {
for _, item := range *manualServiceConnections {
result := make(map[string]interface{})
result["private_ip_address"] = privateIpAddress

if v := item.Name; v != nil {
result["name"] = *v
}
if props := item.PrivateLinkServiceConnectionProperties; props != nil {
if v := props.PrivateLinkServiceConnectionState; v != nil {
if s := v.Status; s != nil {
result["status"] = *s
}
if d := v.Description; d != nil {
result["request_response"] = *d
}
}
}

results = append(results, result)
}
}

return results
}
39 changes: 39 additions & 0 deletions azurerm/data_source_private_endpoint_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMPrivateEndpointConnection_complete(t *testing.T) {
dataSourceName := "data.azurerm_private_endpoint_connection.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourcePrivateEndpointConnection_complete(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "private_service_connection.0.status", "Approved"),
),
},
},
})
}

func testAccDataSourcePrivateEndpointConnection_complete(rInt int, location string) string {
return fmt.Sprintf(`
%s
data "azurerm_private_endpoint_connection" "test" {
name = azurerm_private_endpoint.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, testAccAzureRMPrivateEndpoint_basic(rInt, location))
}
37 changes: 7 additions & 30 deletions azurerm/data_source_private_link_endpoint_connection.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package azurerm

import (
"context"
"fmt"
"time"

Expand All @@ -15,6 +14,13 @@ import (

func dataSourceArmPrivateLinkEndpointConnection() *schema.Resource {
return &schema.Resource{
DeprecationMessage: `The 'azurerm_private_link_endpoint_connection' resource is being deprecated in favour of the renamed version 'azurerm_private_endpoint_connection'.
Information on migrating to the renamed resource can be found here: https://terraform.io/docs/providers/azurerm/guides/migrating-between-renamed-resources.html
As such the existing 'azurerm_private_link_endpoint_connection' resource is deprecated and will be removed in the next major version of the AzureRM Provider (2.0).
`,

Read: dataSourceArmPrivateLinkEndpointConnectionRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
Expand Down Expand Up @@ -106,35 +112,6 @@ func dataSourceArmPrivateLinkEndpointConnectionRead(d *schema.ResourceData, meta
return nil
}

func getPrivateIpAddress(ctx context.Context, client *network.InterfacesClient, networkInterfaceId string) string {
privateIpAddress := ""
id, err := azure.ParseAzureResourceID(networkInterfaceId)
if err != nil {
return privateIpAddress
}
name := id.Path["networkInterfaces"]

resp, err := client.Get(ctx, id.ResourceGroup, name, "")
if err != nil {
return privateIpAddress
}

if props := resp.InterfacePropertiesFormat; props != nil {
if configs := props.IPConfigurations; configs != nil {
for i, config := range *configs {
if propFmt := config.InterfaceIPConfigurationPropertiesFormat; propFmt != nil {
if propFmt.PrivateIPAddress != nil && *propFmt.PrivateIPAddress != "" && i == 0 {
privateIpAddress = *propFmt.PrivateIPAddress
}
break
}
}
}
}

return privateIpAddress
}

func dataSourceFlattenArmPrivateLinkEndpointServiceConnection(serviceConnections *[]network.PrivateLinkServiceConnection, manualServiceConnections *[]network.PrivateLinkServiceConnection, privateIpAddress string) []interface{} {
results := make([]interface{}, 0)
if serviceConnections == nil && manualServiceConnections == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ data "azurerm_private_link_endpoint_connection" "test" {
name = azurerm_private_link_endpoint.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, testAccAzureRMPrivateEndpoint_basic(rInt, location))
`, testAccAzureRMPrivateLinkEndpoint_basic(rInt, location))
}
Loading

0 comments on commit ffa5618

Please sign in to comment.