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

network - update private link resources and data sources to use go-azure-sdk #26212

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
73 changes: 38 additions & 35 deletions internal/services/network/private_link_service_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-11-01/privatelinkservices"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

func dataSourcePrivateLinkService() *pluginsdk.Resource {
Expand Down Expand Up @@ -96,72 +96,75 @@ func dataSourcePrivateLinkService() *pluginsdk.Resource {
Computed: true,
},

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}

func dataSourcePrivateLinkServiceRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.PrivateLinkServiceClient
client := meta.(*clients.Client).Network.PrivateLinkServices
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewPrivateLinkServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := privatelinkservices.NewPrivateLinkServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Get(ctx, id, privatelinkservices.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading %s: %+v", id, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("name", id.PrivateLinkServiceName)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))

if props := resp.PrivateLinkServiceProperties; props != nil {
d.Set("alias", props.Alias)
d.Set("enable_proxy_protocol", props.EnableProxyProtocol)
if props := model.Properties; props != nil {
d.Set("alias", props.Alias)
d.Set("enable_proxy_protocol", props.EnableProxyProtocol)

if autoApproval := props.AutoApproval; autoApproval != nil {
if err := d.Set("auto_approval_subscription_ids", utils.FlattenStringSlice(autoApproval.Subscriptions)); err != nil {
return fmt.Errorf("setting `auto_approval_subscription_ids`: %+v", err)
if autoApproval := props.AutoApproval; autoApproval != nil {
if err := d.Set("auto_approval_subscription_ids", utils.FlattenStringSlice(autoApproval.Subscriptions)); err != nil {
return fmt.Errorf("setting `auto_approval_subscription_ids`: %+v", err)
}
}
}
if visibility := props.Visibility; visibility != nil {
if err := d.Set("visibility_subscription_ids", utils.FlattenStringSlice(visibility.Subscriptions)); err != nil {
return fmt.Errorf("setting `visibility_subscription_ids`: %+v", err)
if visibility := props.Visibility; visibility != nil {
if err := d.Set("visibility_subscription_ids", utils.FlattenStringSlice(visibility.Subscriptions)); err != nil {
return fmt.Errorf("setting `visibility_subscription_ids`: %+v", err)
}
}
}

if props.IPConfigurations != nil {
if err := d.Set("nat_ip_configuration", flattenPrivateLinkServiceIPConfiguration(props.IPConfigurations)); err != nil {
return fmt.Errorf("setting `nat_ip_configuration`: %+v", err)
if props.IPConfigurations != nil {
if err := d.Set("nat_ip_configuration", flattenPrivateLinkServiceIPConfiguration(props.IPConfigurations)); err != nil {
return fmt.Errorf("setting `nat_ip_configuration`: %+v", err)
}
}
}
if props.LoadBalancerFrontendIPConfigurations != nil {
if err := d.Set("load_balancer_frontend_ip_configuration_ids", dataSourceFlattenPrivateLinkServiceFrontendIPConfiguration(props.LoadBalancerFrontendIPConfigurations)); err != nil {
return fmt.Errorf("setting `load_balancer_frontend_ip_configuration_ids`: %+v", err)
if props.LoadBalancerFrontendIPConfigurations != nil {
if err := d.Set("load_balancer_frontend_ip_configuration_ids", dataSourceFlattenPrivateLinkServiceFrontendIPConfiguration(props.LoadBalancerFrontendIPConfigurations)); err != nil {
return fmt.Errorf("setting `load_balancer_frontend_ip_configuration_ids`: %+v", err)
}
}
}
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return fmt.Errorf("setting `tags`: %+v", err)
}
}

d.SetId(id.ID())

return tags.FlattenAndSet(d, resp.Tags)
return nil
}

func dataSourceFlattenPrivateLinkServiceFrontendIPConfiguration(input *[]network.FrontendIPConfiguration) []string {
func dataSourceFlattenPrivateLinkServiceFrontendIPConfiguration(input *[]privatelinkservices.FrontendIPConfiguration) []string {
results := make([]string, 0)
if input == nil {
return results
}

for _, item := range *input {
if id := item.ID; id != nil {
if id := item.Id; id != nil {
results = append(results, *id)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/privateendpoints"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-11-01/privatelinkservices"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/network/2022-07-01/network"
)

func dataSourcePrivateLinkServiceEndpointConnections() *pluginsdk.Resource {
Expand All @@ -30,7 +28,7 @@ func dataSourcePrivateLinkServiceEndpointConnections() *pluginsdk.Resource {
"service_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
ValidateFunc: privatelinkservices.ValidatePrivateLinkServiceID,
},

"service_name": {
Expand Down Expand Up @@ -83,63 +81,64 @@ func dataSourcePrivateLinkServiceEndpointConnections() *pluginsdk.Resource {
}

func dataSourcePrivateLinkServiceEndpointConnectionsRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.PrivateLinkServiceClient
client := meta.(*clients.Client).Network.PrivateLinkServices
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

serviceId := d.Get("service_id").(string)

id, err := parse.PrivateLinkServiceID(serviceId)
id, err := privatelinkservices.ParsePrivateLinkServiceID(serviceId)
if err != nil {
return fmt.Errorf("parsing %q: %s", serviceId, err)
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Get(ctx, *id, privatelinkservices.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: %s was not found", *id)
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("reading %s: %+v", *id, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on %s: %+v", *id, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("service_id", serviceId)
d.Set("service_name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("service_id", id.ID())
d.Set("service_name", id.PrivateLinkServiceName)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))

if props := resp.PrivateLinkServiceProperties; props != nil {
if err := d.Set("private_endpoint_connections", dataSourceflattenPrivateLinkServicePrivateEndpointConnections(props.PrivateEndpointConnections)); err != nil {
return fmt.Errorf("setting `private_endpoint_connections`: %+v", err)
if props := model.Properties; props != nil {
if err := d.Set("private_endpoint_connections", dataSourceflattenPrivateLinkServicePrivateEndpointConnections(props.PrivateEndpointConnections)); err != nil {
return fmt.Errorf("setting `private_endpoint_connections`: %+v", err)
}
}
}

d.SetId(fmt.Sprintf("%s/privateLinkServiceEndpointConnections/%s", *resp.ID, id.Name))
privateEndpointId := privatelinkservices.NewPrivateEndpointConnectionID(id.SubscriptionId, id.ResourceGroupName, id.PrivateLinkServiceName, id.PrivateLinkServiceName)

d.SetId(privateEndpointId.ID())

return nil
}

func dataSourceflattenPrivateLinkServicePrivateEndpointConnections(input *[]network.PrivateEndpointConnection) []interface{} {
func dataSourceflattenPrivateLinkServicePrivateEndpointConnections(input *[]privatelinkservices.PrivateEndpointConnection) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, item := range *input {
v := make(map[string]interface{})
if id := item.ID; id != nil {
if id := item.Id; id != nil {
v["connection_id"] = *id
}
if name := item.Name; name != nil {
v["connection_name"] = *name
}

if props := item.PrivateEndpointConnectionProperties; props != nil {
if props := item.Properties; props != nil {
if p := props.PrivateEndpoint; p != nil {
if id := p.ID; id != nil {
if id := p.Id; id != nil {
v["private_endpoint_id"] = *id

id, _ := privateendpoints.ParsePrivateEndpointID(*id)
Expand Down
Loading
Loading