From 078d874831b212dff958a3272ec42f639f84b244 Mon Sep 17 00:00:00 2001 From: Heng Lu <79895375+ms-henglu@users.noreply.github.com> Date: Fri, 9 Feb 2024 08:21:37 +0800 Subject: [PATCH] `azurerm_spring_cloud_configuration_service` - split the create and update methods (#24766) --- ...ng_cloud_configuration_service_resource.go | 67 ++++++++++++++++--- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/internal/services/springcloud/spring_cloud_configuration_service_resource.go b/internal/services/springcloud/spring_cloud_configuration_service_resource.go index 196a6dc706fa2..15aa5304f1bd8 100644 --- a/internal/services/springcloud/spring_cloud_configuration_service_resource.go +++ b/internal/services/springcloud/spring_cloud_configuration_service_resource.go @@ -22,9 +22,9 @@ import ( func resourceSpringCloudConfigurationService() *pluginsdk.Resource { return &pluginsdk.Resource{ - Create: resourceSpringCloudConfigurationServiceCreateUpdate, + Create: resourceSpringCloudConfigurationServiceCreate, Read: resourceSpringCloudConfigurationServiceRead, - Update: resourceSpringCloudConfigurationServiceCreateUpdate, + Update: resourceSpringCloudConfigurationServiceUpdate, Delete: resourceSpringCloudConfigurationServiceDelete, SchemaVersion: 1, @@ -159,7 +159,7 @@ func resourceSpringCloudConfigurationService() *pluginsdk.Resource { }, } } -func resourceSpringCloudConfigurationServiceCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { +func resourceSpringCloudConfigurationServiceCreate(d *pluginsdk.ResourceData, meta interface{}) error { subscriptionId := meta.(*clients.Client).Account.SubscriptionId client := meta.(*clients.Client).AppPlatform.ConfigurationServiceClient ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) @@ -171,17 +171,15 @@ func resourceSpringCloudConfigurationServiceCreateUpdate(d *pluginsdk.ResourceDa } id := parse.NewSpringCloudConfigurationServiceID(subscriptionId, springId.ResourceGroup, springId.SpringName, d.Get("name").(string)) - if d.IsNewResource() { - existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) - 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.ConfigurationServiceName) + if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return tf.ImportAsExistsError("azurerm_spring_cloud_configuration_service", id.ID()) + return fmt.Errorf("checking for existing %s: %+v", id, err) } } + if !utils.ResponseWasNotFound(existing.Response) { + return tf.ImportAsExistsError("azurerm_spring_cloud_configuration_service", id.ID()) + } configurationServiceResource := appplatform.ConfigurationServiceResource{ Properties: &appplatform.ConfigurationServiceProperties{ @@ -206,6 +204,53 @@ func resourceSpringCloudConfigurationServiceCreateUpdate(d *pluginsdk.ResourceDa return resourceSpringCloudConfigurationServiceRead(d, meta) } +func resourceSpringCloudConfigurationServiceUpdate(d *pluginsdk.ResourceData, meta interface{}) error { + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + client := meta.(*clients.Client).AppPlatform.ConfigurationServiceClient + 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.NewSpringCloudConfigurationServiceID(subscriptionId, springId.ResourceGroup, springId.SpringName, d.Get("name").(string)) + + existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("checking for existing %s: %+v", id, err) + } + } + if existing.Properties == nil { + return fmt.Errorf("retrieving %s: model was nil", id) + } + + properties := existing.Properties + if d.HasChange("generation") { + properties.Generation = appplatform.ConfigurationServiceGeneration(d.Get("generation").(string)) + } + + if d.HasChange("repository") { + properties.Settings.GitProperty.Repositories = expandConfigurationServiceConfigurationServiceGitRepositoryArray(d.Get("repository").([]interface{})) + } + + configurationServiceResource := appplatform.ConfigurationServiceResource{ + Properties: properties, + } + future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName, configurationServiceResource) + if err != nil { + return fmt.Errorf("creating/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) + } + + d.SetId(id.ID()) + return resourceSpringCloudConfigurationServiceRead(d, meta) +} + func resourceSpringCloudConfigurationServiceRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).AppPlatform.ConfigurationServiceClient ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)