Skip to content

Commit

Permalink
azurerm_spring_cloud_configuration_service - switch to pandora sdk (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu authored and rizkybiz committed Feb 21, 2024
1 parent d673b25 commit 0c3459d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 68 deletions.
5 changes: 0 additions & 5 deletions internal/services/springcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
},

Expand Down Expand Up @@ -198,42 +197,38 @@ 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),
},
},
},
}
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
},
Expand All @@ -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") {
Expand All @@ -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
},
}
Expand All @@ -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 {
Expand All @@ -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)
}
}
}

Expand All @@ -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
},
}
Expand All @@ -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),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0c3459d

Please sign in to comment.