From f1aea1db814560afc3370ee78a167c64d97c26c5 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Sun, 18 Feb 2024 09:57:53 +0800 Subject: [PATCH] `azurerm_spring_cloud_configuration_service` - switch to pandora sdk --- .../services/springcloud/client/client.go | 5 - ...ng_cloud_configuration_service_resource.go | 101 ++++++++---------- ...oud_configuration_service_resource_test.go | 15 +-- 3 files changed, 53 insertions(+), 68 deletions(-) diff --git a/internal/services/springcloud/client/client.go b/internal/services/springcloud/client/client.go index 8ca8030bf7fb..36e6375f9402 100644 --- a/internal/services/springcloud/client/client.go +++ b/internal/services/springcloud/client/client.go @@ -26,7 +26,6 @@ type Client struct { BuildServiceClient *appplatform.BuildServiceClient CertificatesClient *appplatform.CertificatesClient ConfigServersClient *appplatform.ConfigServersClient - ConfigurationServiceClient *appplatform.ConfigurationServicesClient ContainerRegistryClient *appplatform.ContainerRegistriesClient CustomDomainsClient *appplatform.CustomDomainsClient DevToolPortalClient *appplatform.DevToolPortalsClient @@ -79,9 +78,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) { configServersClient := appplatform.NewConfigServersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&configServersClient.Client, o.ResourceManagerAuthorizer) - configurationServiceClient := appplatform.NewConfigurationServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) - o.ConfigureClient(&configurationServiceClient.Client, o.ResourceManagerAuthorizer) - containerRegistryClient := appplatform.NewContainerRegistriesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&containerRegistryClient.Client, o.ResourceManagerAuthorizer) @@ -127,7 +123,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) { BuildServiceClient: &buildServiceClient, CertificatesClient: &certificatesClient, ConfigServersClient: &configServersClient, - ConfigurationServiceClient: &configurationServiceClient, ContainerRegistryClient: &containerRegistryClient, CustomDomainsClient: &customDomainsClient, DeploymentsClient: &deploymentsClient, diff --git a/internal/services/springcloud/spring_cloud_configuration_service_resource.go b/internal/services/springcloud/spring_cloud_configuration_service_resource.go index b982f99690fb..46445df7a830 100644 --- a/internal/services/springcloud/spring_cloud_configuration_service_resource.go +++ b/internal/services/springcloud/spring_cloud_configuration_service_resource.go @@ -9,16 +9,15 @@ import ( "time" "github.com/hashicorp/go-azure-helpers/lang/pointer" + "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/go-azure-sdk/resource-manager/appplatform/2023-11-01-preview/appplatform" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/migration" - "github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" - "github.com/hashicorp/terraform-provider-azurerm/utils" - "github.com/tombuildsstuff/kermit/sdk/appplatform/2023-05-01-preview/appplatform" ) type SpringCloudConfigurationServiceModel struct { @@ -74,8 +73,8 @@ func (s SpringCloudConfigurationServiceResource) Arguments() map[string]*schema. Type: pluginsdk.TypeString, Optional: true, ValidateFunc: validation.StringInSlice([]string{ - string(appplatform.ConfigurationServiceGenerationGen1), - string(appplatform.ConfigurationServiceGenerationGen2), + string(appplatform.ConfigurationServiceGenerationGenOne), + string(appplatform.ConfigurationServiceGenerationGenTwo), }, false), }, @@ -198,26 +197,26 @@ func (s SpringCloudConfigurationServiceResource) Create() sdk.ResourceFunc { return fmt.Errorf("decoding: %+v", err) } - client := metadata.Client.AppPlatform.ConfigurationServiceClient + client := metadata.Client.AppPlatform.AppPlatformClient springId, err := commonids.ParseSpringCloudServiceID(model.SpringCloudServiceId) if err != nil { return err } - id := parse.NewSpringCloudConfigurationServiceID(springId.SubscriptionId, springId.ResourceGroupName, springId.ServiceName, model.Name) + id := appplatform.NewConfigurationServiceID(springId.SubscriptionId, springId.ResourceGroupName, springId.ServiceName, model.Name) - existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) + existing, err := client.ConfigurationServicesGet(ctx, id) if err != nil { - if !utils.ResponseWasNotFound(existing.Response) { + if !response.WasNotFound(existing.HttpResponse) { return fmt.Errorf("checking for existing %s: %+v", id, err) } } - if !utils.ResponseWasNotFound(existing.Response) { + if !response.WasNotFound(existing.HttpResponse) { return metadata.ResourceRequiresImport(s.ResourceType(), id) } configurationServiceResource := appplatform.ConfigurationServiceResource{ Properties: &appplatform.ConfigurationServiceProperties{ - Generation: appplatform.ConfigurationServiceGeneration(model.Generation), + Generation: pointer.To(appplatform.ConfigurationServiceGeneration(model.Generation)), Settings: &appplatform.ConfigurationServiceSettings{ GitProperty: &appplatform.ConfigurationServiceGitProperty{ Repositories: expandConfigurationServiceConfigurationServiceGitRepositoryArray(model.Repository), @@ -225,15 +224,11 @@ func (s SpringCloudConfigurationServiceResource) Create() sdk.ResourceFunc { }, }, } - future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName, configurationServiceResource) + err = client.ConfigurationServicesCreateOrUpdateThenPoll(ctx, id, 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) - } - metadata.SetID(id) return nil }, @@ -253,22 +248,22 @@ func (s SpringCloudConfigurationServiceResource) Update() sdk.ResourceFunc { if err != nil { return err } - id := parse.NewSpringCloudConfigurationServiceID(springId.SubscriptionId, springId.ResourceGroupName, springId.ServiceName, model.Name) + id := appplatform.NewConfigurationServiceID(springId.SubscriptionId, springId.ResourceGroupName, springId.ServiceName, model.Name) - client := metadata.Client.AppPlatform.ConfigurationServiceClient - existing, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) + client := metadata.Client.AppPlatform.AppPlatformClient + existing, err := client.ConfigurationServicesGet(ctx, id) if err != nil { - if !utils.ResponseWasNotFound(existing.Response) { + if !response.WasNotFound(existing.HttpResponse) { return fmt.Errorf("checking for existing %s: %+v", id, err) } } - if existing.Properties == nil { + if existing.Model == nil || existing.Model.Properties == nil { return fmt.Errorf("retrieving %s: model was nil", id) } - properties := existing.Properties + properties := existing.Model.Properties if metadata.ResourceData.HasChange("generation") { - properties.Generation = appplatform.ConfigurationServiceGeneration(model.Generation) + properties.Generation = pointer.To(appplatform.ConfigurationServiceGeneration(model.Generation)) } if metadata.ResourceData.HasChange("repository") { @@ -278,15 +273,11 @@ func (s SpringCloudConfigurationServiceResource) Update() sdk.ResourceFunc { configurationServiceResource := appplatform.ConfigurationServiceResource{ Properties: properties, } - future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName, configurationServiceResource) + err = client.ConfigurationServicesCreateOrUpdateThenPoll(ctx, id, 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) - } - return nil }, } @@ -296,22 +287,22 @@ func (s SpringCloudConfigurationServiceResource) Read() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 5 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.AppPlatform.ConfigurationServiceClient + client := metadata.Client.AppPlatform.AppPlatformClient - id, err := parse.SpringCloudConfigurationServiceID(metadata.ResourceData.Id()) + id, err := appplatform.ParseConfigurationServiceID(metadata.ResourceData.Id()) if err != nil { return err } - resp, err := client.Get(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) + resp, err := client.ConfigurationServicesGet(ctx, *id) if err != nil { - if utils.ResponseWasNotFound(resp.Response) { + if response.WasNotFound(resp.HttpResponse) { return metadata.MarkAsGone(id) } return fmt.Errorf("retrieving %s: %+v", *id, err) } - springId := commonids.NewSpringCloudServiceID(id.SubscriptionId, id.ResourceGroup, id.SpringName) + springId := commonids.NewSpringCloudServiceID(id.SubscriptionId, id.ResourceGroupName, id.SpringName) var model SpringCloudConfigurationServiceModel if err := metadata.Decode(&model); err != nil { @@ -323,10 +314,12 @@ func (s SpringCloudConfigurationServiceResource) Read() sdk.ResourceFunc { SpringCloudServiceId: springId.ID(), } - if props := resp.Properties; props != nil { - state.Generation = string(props.Generation) - if props.Settings != nil && props.Settings.GitProperty != nil { - state.Repository = flattenConfigurationServiceConfigurationServiceGitRepositoryArray(props.Settings.GitProperty.Repositories, model.Repository) + if resp.Model != nil { + if props := resp.Model.Properties; props != nil { + state.Generation = string(pointer.From(props.Generation)) + if props.Settings != nil && props.Settings.GitProperty != nil { + state.Repository = flattenConfigurationServiceConfigurationServiceGitRepositoryArray(props.Settings.GitProperty.Repositories, model.Repository) + } } } @@ -339,22 +332,18 @@ func (s SpringCloudConfigurationServiceResource) Delete() sdk.ResourceFunc { return sdk.ResourceFunc{ Timeout: 30 * time.Minute, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { - client := metadata.Client.AppPlatform.ConfigurationServiceClient + client := metadata.Client.AppPlatform.AppPlatformClient - id, err := parse.SpringCloudConfigurationServiceID(metadata.ResourceData.Id()) + id, err := appplatform.ParseConfigurationServiceID(metadata.ResourceData.Id()) if err != nil { return err } - future, err := client.Delete(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) + err = client.ConfigurationServicesDeleteThenPoll(ctx, *id) if err != nil { return fmt.Errorf("deleting %s: %+v", id, err) } - if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting for deletion of %s: %+v", id, err) - } - return nil }, } @@ -366,10 +355,10 @@ func expandConfigurationServiceConfigurationServiceGitRepositoryArray(input []Sp results := make([]appplatform.ConfigurationServiceGitRepository, 0) for _, v := range input { repo := appplatform.ConfigurationServiceGitRepository{ - Name: pointer.To(v.Name), - Patterns: pointer.To(v.Patterns), - URI: pointer.To(v.Uri), - Label: pointer.To(v.Label), + Name: v.Name, + Patterns: v.Patterns, + Uri: v.Uri, + Label: v.Label, SearchPaths: pointer.To(v.SearchPaths), Username: pointer.To(v.Username), Password: pointer.To(v.Password), @@ -379,7 +368,7 @@ func expandConfigurationServiceConfigurationServiceGitRepositoryArray(input []Sp StrictHostKeyChecking: pointer.To(v.StrictHostKeyChecking), } if v.CaCertificateId != "" { - repo.CaCertResourceID = pointer.To(v.CaCertificateId) + repo.CaCertResourceId = pointer.To(v.CaCertificateId) } results = append(results, repo) } @@ -409,7 +398,7 @@ func flattenConfigurationServiceConfigurationServiceGitRepositoryArray(input *[] var privateKey string var username string var password string - if oldItem, ok := oldItems[pointer.From(item.Name)]; ok { + if oldItem, ok := oldItems[item.Name]; ok { hostKey = oldItem.HostKey hostKeyAlgorithm = oldItem.HostKeyAlgorithm privateKey = oldItem.PrivateKey @@ -418,17 +407,17 @@ func flattenConfigurationServiceConfigurationServiceGitRepositoryArray(input *[] } var caCertificateId string - if item.CaCertResourceID != nil { - certificatedId, err := parse.SpringCloudCertificateIDInsensitively(*item.CaCertResourceID) + if item.CaCertResourceId != nil { + certificatedId, err := appplatform.ParseCertificateIDInsensitively(*item.CaCertResourceId) if err == nil { caCertificateId = certificatedId.ID() } } results = append(results, SpringCloudRepositoryModel{ - Name: pointer.From(item.Name), - Label: pointer.From(item.Label), - Patterns: pointer.From(item.Patterns), - Uri: pointer.From(item.URI), + Name: item.Name, + Label: item.Label, + Patterns: item.Patterns, + Uri: item.Uri, CaCertificateId: caCertificateId, HostKey: hostKey, HostKeyAlgorithm: hostKeyAlgorithm, diff --git a/internal/services/springcloud/spring_cloud_configuration_service_resource_test.go b/internal/services/springcloud/spring_cloud_configuration_service_resource_test.go index e1df6e5e540c..4a6c9ef5dce6 100644 --- a/internal/services/springcloud/spring_cloud_configuration_service_resource_test.go +++ b/internal/services/springcloud/spring_cloud_configuration_service_resource_test.go @@ -8,12 +8,13 @@ import ( "fmt" "testing" + "github.com/hashicorp/go-azure-helpers/lang/pointer" + "github.com/hashicorp/go-azure-helpers/lang/response" + "github.com/hashicorp/go-azure-sdk/resource-manager/appplatform/2023-11-01-preview/appplatform" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" - "github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" - "github.com/hashicorp/terraform-provider-azurerm/utils" ) type SpringCloudConfigurationServiceResource struct{} @@ -138,18 +139,18 @@ func TestAccSpringCloudConfigurationService_caCertificateId(t *testing.T) { } func (r SpringCloudConfigurationServiceResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := parse.SpringCloudConfigurationServiceID(state.ID) + id, err := appplatform.ParseConfigurationServiceID(state.ID) if err != nil { return nil, err } - resp, err := client.AppPlatform.ConfigurationServiceClient.Get(ctx, id.ResourceGroup, id.SpringName, id.ConfigurationServiceName) + resp, err := client.AppPlatform.AppPlatformClient.ConfigurationServicesGet(ctx, *id) if err != nil { - if utils.ResponseWasNotFound(resp.Response) { - return utils.Bool(false), nil + if response.WasNotFound(resp.HttpResponse) { + return pointer.To(false), nil } return nil, fmt.Errorf("retrieving %q: %+v", id, err) } - return utils.Bool(true), nil + return pointer.To(resp.Model != nil), nil } func (r SpringCloudConfigurationServiceResource) template(data acceptance.TestData) string {