Skip to content

Commit

Permalink
Dependencies: refactor azurerm_source_control_token to use `go-azur…
Browse files Browse the repository at this point in the history
…e-sdk` (#25016)

* switchout for go-azure-sdk

* switchout for go-azure-sdk for data source

* remove unused baseClient
  • Loading branch information
jackofallops authored Mar 1, 2024
1 parent 7f24631 commit ecd7208
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 62 deletions.
6 changes: 0 additions & 6 deletions internal/services/appservice/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/resourceproviders"
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/webapps"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/tombuildsstuff/kermit/sdk/web/2022-09-01/web"
)

type Client struct {
AppServiceEnvironmentClient *appserviceenvironments.AppServiceEnvironmentsClient
BaseClient *web.BaseClient
ResourceProvidersClient *resourceproviders.ResourceProvidersClient
ServicePlanClient *appserviceplans.AppServicePlansClient
WebAppsClient *webapps.WebAppsClient
Expand All @@ -29,9 +27,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(appServiceEnvironmentClient.Client, o.Authorizers.ResourceManager)

baseClient := web.NewWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&baseClient.Client, o.ResourceManagerAuthorizer)

webAppServiceClient, err := webapps.NewWebAppsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building WebApps client: %+v", err)
Expand All @@ -52,7 +47,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {

return &Client{
AppServiceEnvironmentClient: appServiceEnvironmentClient,
BaseClient: &baseClient,
ResourceProvidersClient: resourceProvidersClient,
ServicePlanClient: servicePlanClient,
WebAppsClient: webAppServiceClient,
Expand Down
21 changes: 11 additions & 10 deletions internal/services/appservice/source_control_token_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/resourceproviders"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/parse"
"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"
)

type AppServiceSourceControlTokenDataSource struct{}
Expand Down Expand Up @@ -62,24 +62,25 @@ func (d AppServiceSourceControlTokenDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.BaseClient
client := metadata.Client.AppService.ResourceProvidersClient

var sourceControlToken AppServiceSourceControlTokenModel
if err := metadata.Decode(&sourceControlToken); err != nil {
return fmt.Errorf("decoding %+v", err)
}
id := parse.NewAppServiceSourceControlTokenID(sourceControlToken.Type)
id := resourceproviders.NewSourceControlID(sourceControlToken.Type)

resp, err := client.GetSourceControl(ctx, id.Type)
resp, err := client.GetSourceControl(ctx, id)
if err != nil {
return fmt.Errorf("reading %s: %+v", id, err)
}
if resp.SourceControlProperties == nil {
return fmt.Errorf("reading properties of %s", id)
}

sourceControlToken.Token = utils.NormalizeNilableString(resp.Token)
sourceControlToken.TokenSecret = utils.NormalizeNilableString(resp.TokenSecret)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
sourceControlToken.Token = pointer.From(props.Token)
sourceControlToken.TokenSecret = pointer.From(props.TokenSecret)
}
}

metadata.SetID(id)

Expand Down
78 changes: 40 additions & 38 deletions internal/services/appservice/source_control_token_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/web/2023-01-01/resourceproviders"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appservice/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/web/2022-09-01/web"
)

type AppServiceSourceControlTokenResource struct{}
Expand Down Expand Up @@ -81,29 +81,29 @@ func (r AppServiceSourceControlTokenResource) Create() sdk.ResourceFunc {
return err
}

id := parse.NewAppServiceSourceControlTokenID(sourceControlToken.Type)
id := resourceproviders.NewSourceControlID(sourceControlToken.Type)

client := metadata.Client.AppService.BaseClient
client := metadata.Client.AppService.ResourceProvidersClient

existing, err := client.GetSourceControl(ctx, id.Type)
existing, err := client.GetSourceControl(ctx, id)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("%s not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}
if existing.SourceControlProperties != nil && existing.SourceControlProperties.Token != nil && *existing.SourceControlProperties.Token != "" {
if existing.Model.Properties != nil && pointer.From(existing.Model.Properties.Token) != "" {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}

sourceControlOAuth := web.SourceControl{
SourceControlProperties: &web.SourceControlProperties{
Token: utils.String(sourceControlToken.Token),
TokenSecret: utils.String(sourceControlToken.TokenSecret),
sourceControlOAuth := resourceproviders.SourceControl{
Properties: &resourceproviders.SourceControlProperties{
Token: pointer.To(sourceControlToken.Token),
TokenSecret: pointer.To(sourceControlToken.TokenSecret),
},
}

if _, err := client.UpdateSourceControl(ctx, id.Type, sourceControlOAuth); err != nil {
if _, err := client.UpdateSourceControl(ctx, id, sourceControlOAuth); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

Expand All @@ -118,28 +118,30 @@ func (r AppServiceSourceControlTokenResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.BaseClient
client := metadata.Client.AppService.ResourceProvidersClient

id, err := parse.AppServiceSourceControlTokenID(metadata.ResourceData.Id())
id, err := resourceproviders.ParseSourceControlID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.GetSourceControl(ctx, id.Type)
if err != nil || resp.SourceControlProperties == nil {
if utils.ResponseWasNotFound(resp.Response) {
resp, err := client.GetSourceControl(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("reading %s: %+v", id, err)
return fmt.Errorf("reading %s: %+v", *id, err)
}

state := AppServiceSourceControlTokenModel{}

state.Type = id.Type
state.Type = id.SourceControlName

if resp.SourceControlProperties != nil {
state.Token = utils.NormalizeNilableString(resp.Token)
state.TokenSecret = utils.NormalizeNilableString(resp.TokenSecret)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
state.Token = pointer.From(props.Token)
state.TokenSecret = pointer.From(props.TokenSecret)
}
}

return metadata.Encode(&state)
Expand All @@ -151,22 +153,22 @@ func (r AppServiceSourceControlTokenResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.BaseClient
client := metadata.Client.AppService.ResourceProvidersClient

id, err := parse.AppServiceSourceControlTokenID(metadata.ResourceData.Id())
id, err := resourceproviders.ParseSourceControlID(metadata.ResourceData.Id())
if err != nil {
return err
}

sourceControlOAuth := web.SourceControl{
SourceControlProperties: &web.SourceControlProperties{
Token: utils.String(""),
TokenSecret: utils.String(""),
sourceControlOAuth := resourceproviders.SourceControl{
Properties: &resourceproviders.SourceControlProperties{
Token: pointer.To(""),
TokenSecret: pointer.To(""),
},
}

if _, err := client.UpdateSourceControl(ctx, id.Type, sourceControlOAuth); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
if _, err := client.UpdateSourceControl(ctx, *id, sourceControlOAuth); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

return nil
Expand All @@ -184,7 +186,7 @@ func (r AppServiceSourceControlTokenResource) Update() sdk.ResourceFunc {
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var sourceControlToken AppServiceSourceControlTokenModel

id, err := parse.AppServiceSourceControlTokenID(metadata.ResourceData.Id())
id, err := resourceproviders.ParseSourceControlID(metadata.ResourceData.Id())
if err != nil {
return err
}
Expand All @@ -193,16 +195,16 @@ func (r AppServiceSourceControlTokenResource) Update() sdk.ResourceFunc {
return err
}

client := metadata.Client.AppService.BaseClient
client := metadata.Client.AppService.ResourceProvidersClient

sourceControlOAuth := web.SourceControl{
SourceControlProperties: &web.SourceControlProperties{
Token: utils.String(sourceControlToken.Token),
TokenSecret: utils.String(sourceControlToken.TokenSecret),
sourceControlOAuth := resourceproviders.SourceControl{
Properties: &resourceproviders.SourceControlProperties{
Token: pointer.To(sourceControlToken.Token),
TokenSecret: pointer.To(sourceControlToken.TokenSecret),
},
}

if _, err := client.UpdateSourceControl(ctx, id.Type, sourceControlOAuth); err != nil {
if _, err := client.UpdateSourceControl(ctx, *id, sourceControlOAuth); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

Expand Down
18 changes: 10 additions & 8 deletions internal/services/appservice/source_control_token_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"os"
"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/web/2023-01-01/resourceproviders"
"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/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

// (@jackofallops) Note: These tests require a valid GitHub token for ARM_GITHUB_ACCESS_TOKEN. This token needs the `repo` and `workflow` permissions on the referenced repositories.
Expand Down Expand Up @@ -65,17 +67,17 @@ func TestAccSourceControlGitHubToken_requiresImport(t *testing.T) {
}

func (r AppServiceGitHubTokenResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
resp, err := client.AppService.BaseClient.GetSourceControl(ctx, "GitHub")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return utils.Bool(false), err
resp, err := client.AppService.ResourceProvidersClient.GetSourceControl(ctx, resourceproviders.NewSourceControlID("GitHub"))
if err != nil || resp.Model == nil {
if response.WasNotFound(resp.HttpResponse) {
return pointer.To(false), err
}
return nil, fmt.Errorf("retrieving Source Control GitHub Token")
}
if resp.Token == nil || *resp.Token == "" {
return utils.Bool(false), nil
if resp.Model.Properties == nil || pointer.From(resp.Model.Properties.Token) == "" {
return pointer.To(false), nil
}
return utils.Bool(true), nil
return pointer.To(true), nil
}

func (r AppServiceGitHubTokenResource) basic(token string) string {
Expand Down

0 comments on commit ecd7208

Please sign in to comment.