From 2f0b3750815c3a0a9de7f8ba7ee1cf5899054013 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Tue, 31 Oct 2023 23:59:28 -0700 Subject: [PATCH] `azurerm_traffic_manager_external_endpoint` - split create/update (#23746) * azurerm_traffic_manager_external_endpoint - split create/update * goimports --- .../external_endpoint_resource.go | 106 ++++++++++++++++-- 1 file changed, 94 insertions(+), 12 deletions(-) diff --git a/internal/services/trafficmanager/external_endpoint_resource.go b/internal/services/trafficmanager/external_endpoint_resource.go index 715fa5b19290..af3fc65889f4 100644 --- a/internal/services/trafficmanager/external_endpoint_resource.go +++ b/internal/services/trafficmanager/external_endpoint_resource.go @@ -7,6 +7,7 @@ import ( "fmt" "time" + "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/location" "github.com/hashicorp/go-azure-sdk/resource-manager/trafficmanager/2018-08-01/endpoints" @@ -23,9 +24,9 @@ import ( func resourceExternalEndpoint() *pluginsdk.Resource { return &pluginsdk.Resource{ - Create: resourceExternalEndpointCreateUpdate, + Create: resourceExternalEndpointCreate, Read: resourceExternalEndpointRead, - Update: resourceExternalEndpointCreateUpdate, + Update: resourceExternalEndpointUpdate, Delete: resourceExternalEndpointDelete, Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { endpointType, err := endpoints.ParseEndpointTypeID(id) @@ -149,9 +150,9 @@ func resourceExternalEndpoint() *pluginsdk.Resource { } } -func resourceExternalEndpointCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { +func resourceExternalEndpointCreate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).TrafficManager.EndpointsClient - ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d) defer cancel() profileId, err := profiles.ParseTrafficManagerProfileID(d.Get("profile_id").(string)) @@ -160,19 +161,18 @@ func resourceExternalEndpointCreateUpdate(d *pluginsdk.ResourceData, meta interf } id := endpoints.NewEndpointTypeID(profileId.SubscriptionId, profileId.ResourceGroupName, profileId.TrafficManagerProfileName, endpoints.EndpointTypeExternalEndpoints, d.Get("name").(string)) - if d.IsNewResource() { - existing, err := client.Get(ctx, id) - if err != nil { - if !response.WasNotFound(existing.HttpResponse) { - return fmt.Errorf("checking for presence of existing %s: %v", id, err) - } - } + existing, err := client.Get(ctx, id) + if err != nil { if !response.WasNotFound(existing.HttpResponse) { - return tf.ImportAsExistsError("azurerm_traffic_manager_external_endpoint", id.ID()) + return fmt.Errorf("checking for presence of existing %s: %v", id, err) } } + if !response.WasNotFound(existing.HttpResponse) { + return tf.ImportAsExistsError("azurerm_traffic_manager_external_endpoint", id.ID()) + } + status := endpoints.EndpointStatusEnabled if !d.Get("enabled").(bool) { status = endpoints.EndpointStatusDisabled @@ -265,6 +265,88 @@ func resourceExternalEndpointRead(d *pluginsdk.ResourceData, meta interface{}) e return nil } +func resourceExternalEndpointUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).TrafficManager.EndpointsClient + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + profileId, err := profiles.ParseTrafficManagerProfileID(d.Get("profile_id").(string)) + if err != nil { + return fmt.Errorf("parsing `profile_id`: %+v", err) + } + + id := endpoints.NewEndpointTypeID(profileId.SubscriptionId, profileId.ResourceGroupName, profileId.TrafficManagerProfileName, endpoints.EndpointTypeExternalEndpoints, d.Get("name").(string)) + + existing, err := client.Get(ctx, id) + if err != nil { + return fmt.Errorf("checking for presence of existing %s: %v", id, err) + } + + if existing.Model == nil || existing.Model.Properties == nil { + return fmt.Errorf("model/properties was nil for %s", id) + } + + params := *existing.Model + + if d.HasChange("enabled") { + status := endpoints.EndpointStatusEnabled + if !d.Get("enabled").(bool) { + status = endpoints.EndpointStatusDisabled + } + params.Properties.EndpointStatus = pointer.To(status) + } + + if d.HasChange("custom_header") { + params.Properties.CustomHeaders = expandEndpointCustomHeaderConfig(d.Get("custom_header").([]interface{})) + } + + if d.HasChange("target") { + params.Properties.Target = utils.String(d.Get("target").(string)) + } + + if d.HasChange("subnet") { + params.Properties.Subnets = expandEndpointSubnetConfig(d.Get("subnet").([]interface{})) + } + + if d.HasChange("priority") { + if priority := d.Get("priority").(int); priority != 0 { + params.Properties.Priority = utils.Int64(int64(priority)) + } + } + + if d.HasChange("weight") { + if weight := d.Get("weight").(int); weight != 0 { + params.Properties.Weight = utils.Int64(int64(weight)) + } + } + + if d.HasChange("endpoint_location") { + if endpointLocation := d.Get("endpoint_location").(string); endpointLocation != "" { + params.Properties.EndpointLocation = utils.String(endpointLocation) + } else { + params.Properties.EndpointLocation = nil + } + } + + if d.HasChange("geo_mappings") { + inputMappings := d.Get("geo_mappings").([]interface{}) + geoMappings := make([]string, 0) + for _, v := range inputMappings { + geoMappings = append(geoMappings, v.(string)) + } + if len(geoMappings) > 0 { + params.Properties.GeoMapping = &geoMappings + } else { + params.Properties.GeoMapping = nil + } + } + if _, err := client.CreateOrUpdate(ctx, id, params); err != nil { + return fmt.Errorf("creating/updating %s: %+v", id, err) + } + + return resourceExternalEndpointRead(d, meta) +} + func resourceExternalEndpointDelete(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).TrafficManager.EndpointsClient ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)