Skip to content

Commit

Permalink
azurerm_traffic_manager_external_endpoint - split create/update (#2…
Browse files Browse the repository at this point in the history
…3746)

* azurerm_traffic_manager_external_endpoint - split create/update

* goimports
  • Loading branch information
mbfrahry authored Nov 1, 2023
1 parent 2699162 commit 2f0b375
Showing 1 changed file with 94 additions and 12 deletions.
106 changes: 94 additions & 12 deletions internal/services/trafficmanager/external_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2f0b375

Please sign in to comment.