Skip to content

Commit

Permalink
azurerm_spring_cloud_gateway - split to create/update functions (#2…
Browse files Browse the repository at this point in the history
…4116)

* `azurerm_spring_cloud_gateway` - split to create/update functions

* update log message

* fix misspelling
  • Loading branch information
ms-henglu authored Dec 6, 2023
1 parent 61c14a4 commit fbf55e9
Showing 1 changed file with 107 additions and 17 deletions.
124 changes: 107 additions & 17 deletions internal/services/springcloud/spring_cloud_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/migration"
Expand All @@ -22,9 +23,9 @@ import (

func resourceSpringCloudGateway() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSpringCloudGatewayCreateUpdate,
Create: resourceSpringCloudGatewayCreate,
Read: resourceSpringCloudGatewayRead,
Update: resourceSpringCloudGatewayCreateUpdate,
Update: resourceSpringCloudGatewayUpdate,
Delete: resourceSpringCloudGatewayDelete,

SchemaVersion: 1,
Expand Down Expand Up @@ -315,7 +316,8 @@ func resourceSpringCloudGateway() *pluginsdk.Resource {
},
}
}
func resourceSpringCloudGatewayCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {

func resourceSpringCloudGatewayCreate(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).AppPlatform.GatewayClient
servicesClient := meta.(*clients.Client).AppPlatform.ServicesClient
Expand All @@ -328,24 +330,22 @@ func resourceSpringCloudGatewayCreateUpdate(d *pluginsdk.ResourceData, meta inte
}
id := parse.NewSpringCloudGatewayID(subscriptionId, springId.ResourceGroup, springId.SpringName, d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.GatewayName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}
existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.GatewayName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_spring_cloud_gateway", id.ID())
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_spring_cloud_gateway", id.ID())
}

service, err := servicesClient.Get(ctx, springId.ResourceGroup, springId.SpringName)
if err != nil {
return fmt.Errorf("checking for presence of existing Spring Cloud Service %q (Resource Group %q): %+v", springId.SpringName, springId.ResourceGroup, err)
return fmt.Errorf("checking for presence of existing %s: %+v", springId, err)
}
if service.Sku == nil || service.Sku.Name == nil || service.Sku.Tier == nil {
return fmt.Errorf("invalid `sku` for Spring Cloud Service %q (Resource Group %q)", springId.SpringName, springId.ResourceGroup)
return fmt.Errorf("invalid `sku` for %s", springId)
}

gatewayResource := appplatform.GatewayResource{
Expand All @@ -355,8 +355,8 @@ func resourceSpringCloudGatewayCreateUpdate(d *pluginsdk.ResourceData, meta inte
ApmTypes: expandGatewayGatewayApmTypes(d.Get("application_performance_monitoring_types").([]interface{})),
CorsProperties: expandGatewayGatewayCorsProperties(d.Get("cors").([]interface{})),
EnvironmentVariables: expandGatewayGatewayEnvironmentVariables(d.Get("environment_variables").(map[string]interface{}), d.Get("sensitive_environment_variables").(map[string]interface{})),
HTTPSOnly: utils.Bool(d.Get("https_only").(bool)),
Public: utils.Bool(d.Get("public_network_access_enabled").(bool)),
HTTPSOnly: pointer.To(d.Get("https_only").(bool)),
Public: pointer.To(d.Get("public_network_access_enabled").(bool)),
ResourceRequests: expandGatewayGatewayResourceRequests(d.Get("quota").([]interface{})),
SsoProperties: expandGatewaySsoProperties(d.Get("sso").([]interface{})),
},
Expand All @@ -369,11 +369,101 @@ func resourceSpringCloudGatewayCreateUpdate(d *pluginsdk.ResourceData, meta inte

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, id.GatewayName, gatewayResource)
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
return fmt.Errorf("creating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceSpringCloudGatewayRead(d, meta)
}

func resourceSpringCloudGatewayUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).AppPlatform.GatewayClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

springId, err := parse.SpringCloudServiceID(d.Get("spring_cloud_service_id").(string))
if err != nil {
return err
}
id := parse.NewSpringCloudGatewayID(subscriptionId, springId.ResourceGroup, springId.SpringName, d.Get("name").(string))

existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.GatewayName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("retrieving %s: %+v", id, err)
}
}
if utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("retrieving %s: resource was not found", id)
}

if existing.Properties == nil {
return fmt.Errorf("retrieving %s: properties was nil", id)
}
properties := existing.Properties

if existing.Sku == nil {
return fmt.Errorf("retrieving %s: sku was nil", id)
}
sku := existing.Sku

if d.HasChange("client_authorization") {
properties.ClientAuth = expandGatewayClientAuth(d.Get("client_authorization").([]interface{}))
}

if d.HasChange("api_metadata") {
properties.APIMetadataProperties = expandGatewayGatewayAPIMetadataProperties(d.Get("api_metadata").([]interface{}))
}

if d.HasChange("application_performance_monitoring_types") {
properties.ApmTypes = expandGatewayGatewayApmTypes(d.Get("application_performance_monitoring_types").([]interface{}))
}

if d.HasChange("cors") {
properties.CorsProperties = expandGatewayGatewayCorsProperties(d.Get("cors").([]interface{}))
}

if d.HasChange("environment_variables") || d.HasChange("sensitive_environment_variables") {
properties.EnvironmentVariables = expandGatewayGatewayEnvironmentVariables(d.Get("environment_variables").(map[string]interface{}), d.Get("sensitive_environment_variables").(map[string]interface{}))
}

if d.HasChange("https_only") {
properties.HTTPSOnly = pointer.To(d.Get("https_only").(bool))
}

if d.HasChange("public_network_access_enabled") {
properties.Public = pointer.To(d.Get("public_network_access_enabled").(bool))
}

if d.HasChange("quota") {
properties.ResourceRequests = expandGatewayGatewayResourceRequests(d.Get("quota").([]interface{}))
}

if d.HasChange("sso") {
properties.SsoProperties = expandGatewaySsoProperties(d.Get("sso").([]interface{}))
}

if d.HasChange("instance_count") {
sku.Capacity = pointer.To(int32(d.Get("instance_count").(int)))
}

gatewayResource := appplatform.GatewayResource{
Properties: properties,
Sku: sku,
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, id.GatewayName, gatewayResource)
if err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %s: %+v", id, err)
return fmt.Errorf("waiting for update of %s: %+v", id, err)
}

d.SetId(id.ID())
Expand Down

0 comments on commit fbf55e9

Please sign in to comment.