Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: azurerm_private_endpoint expose private_ip_address as computed attribute #5838

Merged
merged 2 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions azurerm/internal/services/network/resource_arm_private_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func resourceArmPrivateEndpoint() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 140),
},
"private_ip_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -166,6 +170,7 @@ func resourceArmPrivateEndpointCreateUpdate(d *schema.ResourceData, meta interfa

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

Expand Down Expand Up @@ -193,7 +198,20 @@ func resourceArmPrivateEndpointRead(d *schema.ResourceData, meta interface{}) er
}

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)
}
}

flattenedConnection := flattenArmPrivateLinkEndpointServiceConnection(props.PrivateLinkServiceConnections, props.ManualPrivateLinkServiceConnections)
for _, item := range flattenedConnection {
v := item.(map[string]interface{})
v["private_ip_address"] = privateIpAddress
}
Comment on lines +211 to +214
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to loop over the returned slice you just need to append the value to it if the privateIpAddress is not empty, something like the below... WDYT?

if privateIpAddress != "" {
	privateIp := make(map[string]interface{})
	privateIp["private_ip_address"] = privateIpAddress 
	flattenedConnection = append(flattenedConnection, privateIp)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WodansSon the flattenedConnection is a list of map, append it with another map will cause flattenedConnection has two maps, not a merged map.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@njuCZ I got the iteration mixed up in my head... you're right... this is fine.

if err := d.Set("private_service_connection", flattenedConnection); err != nil {
return fmt.Errorf("Error setting `private_service_connection`: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccAzureRMPrivateEndpoint_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPrivateEndpointExists(data.ResourceName),
resource.TestCheckResourceAttrSet(data.ResourceName, "subnet_id"),
resource.TestCheckResourceAttrSet(data.ResourceName, "private_service_connection.0.private_ip_address"),
),
},
data.ImportStep(),
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/private_endpoint.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ See the product [documentation](https://docs.microsoft.com/en-us/azure/private-l

* `request_message` - (Optional) A message passed to the owner of the remote resource when the private endpoint attempts to establish the connection to the remote resource. The request message can be a maximum of `140` characters in length. Only valid if `is_manual_connection` is set to `true`.

* `private_ip_address` - The private IP address associated with the private endpoint, note that you will have a private IP address assigned to the private endpoint even if the connection request was `Rejected`.

## Attributes Reference

The following attributes are exported:
Expand Down