Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automanage: refactoring to use hashicorp/go-azure-sdk #25293

Merged
merged 9 commits into from
Mar 19, 2024
Prev Previous commit
Next Next commit
automanage: refactoring to use the Resource ID from `hashicorp/go-a…
…zure-sdk` incl. state migration
  • Loading branch information
tombuildsstuff committed Mar 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit a3d47e9cfb61f195ca0666894347d7e2e950fef3
36 changes: 23 additions & 13 deletions internal/services/automanage/automanage_configuration_resource.go
Original file line number Diff line number Diff line change
@@ -11,9 +11,9 @@ import (

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofiles"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automanage/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automanage/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automanage/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
@@ -92,6 +92,7 @@ type SchedulePolicyConfiguration struct {
type AutoManageConfigurationResource struct{}

var _ sdk.ResourceWithUpdate = AutoManageConfigurationResource{}
var _ sdk.ResourceWithStateMigration = AutoManageConfigurationResource{}

func (r AutoManageConfigurationResource) ResourceType() string {
return "azurerm_automanage_configuration"
@@ -102,7 +103,7 @@ func (r AutoManageConfigurationResource) ModelObject() interface{} {
}

func (r AutoManageConfigurationResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.AutomanageConfigurationID
return configurationprofiles.ValidateConfigurationProfileID
}

func (r AutoManageConfigurationResource) Arguments() map[string]*pluginsdk.Schema {
@@ -470,8 +471,8 @@ func (r AutoManageConfigurationResource) Create() sdk.ResourceFunc {

client := metadata.Client.Automanage.ConfigurationClient
subscriptionId := metadata.Client.Account.SubscriptionId
id := parse.NewAutomanageConfigurationID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id.ConfigurationProfileName, id.ResourceGroup)
id := configurationprofiles.NewConfigurationProfileID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id.ConfigurationProfileName, id.ResourceGroupName)
if err != nil && !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
@@ -488,7 +489,8 @@ func (r AutoManageConfigurationResource) Create() sdk.ResourceFunc {

properties.Properties.Configuration = expandAutomanageConfigurationProfile(model)

if _, err := client.CreateOrUpdate(ctx, id.ConfigurationProfileName, id.ResourceGroup, properties); err != nil {
// NOTE: ordering
if _, err := client.CreateOrUpdate(ctx, id.ConfigurationProfileName, id.ResourceGroupName, properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

@@ -504,7 +506,7 @@ func (r AutoManageConfigurationResource) Update() sdk.ResourceFunc {
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationClient

id, err := parse.AutomanageConfigurationID(metadata.ResourceData.Id())
id, err := configurationprofiles.ParseConfigurationProfileID(metadata.ResourceData.Id())
if err != nil {
return err
}
@@ -522,7 +524,7 @@ func (r AutoManageConfigurationResource) Update() sdk.ResourceFunc {
Tags: tags.Expand(metadata.ResourceData.Get("tags").(map[string]interface{})),
}

if _, err := client.CreateOrUpdate(ctx, id.ConfigurationProfileName, id.ResourceGroup, properties); err != nil {
if _, err := client.CreateOrUpdate(ctx, id.ConfigurationProfileName, id.ResourceGroupName, properties); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

@@ -537,12 +539,12 @@ func (r AutoManageConfigurationResource) Read() sdk.ResourceFunc {
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationClient

id, err := parse.AutomanageConfigurationID(metadata.ResourceData.Id())
id, err := configurationprofiles.ParseConfigurationProfileID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ConfigurationProfileName, id.ResourceGroup)
resp, err := client.Get(ctx, id.ConfigurationProfileName, id.ResourceGroupName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return metadata.MarkAsGone(id)
@@ -553,7 +555,7 @@ func (r AutoManageConfigurationResource) Read() sdk.ResourceFunc {

state := ConfigurationModel{
Name: id.ConfigurationProfileName,
ResourceGroupName: id.ResourceGroup,
ResourceGroupName: id.ResourceGroupName,
Location: location.NormalizeNilable(resp.Location),
}

@@ -606,12 +608,12 @@ func (r AutoManageConfigurationResource) Delete() sdk.ResourceFunc {
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationClient

id, err := parse.AutomanageConfigurationID(metadata.ResourceData.Id())
id, err := configurationprofiles.ParseConfigurationProfileID(metadata.ResourceData.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.ConfigurationProfileName); err != nil {
if _, err := client.Delete(ctx, id.ResourceGroupName, id.ConfigurationProfileName); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

@@ -620,6 +622,14 @@ func (r AutoManageConfigurationResource) Delete() sdk.ResourceFunc {
}
}

func (r AutoManageConfigurationResource) StateUpgraders() sdk.StateUpgradeData {
return sdk.StateUpgradeData{
SchemaVersion: 1,
Upgraders: map[int]pluginsdk.StateUpgrade{
0: migration.ConfigurationV0ToV1{},
},
}
}
func expandAutomanageConfigurationProfile(model ConfigurationModel) *map[string]interface{} {
// building configuration profile in json format
jsonConfig := make(map[string]interface{})
Original file line number Diff line number Diff line change
@@ -9,10 +9,10 @@ import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofiles"
"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/automanage/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
@@ -266,13 +266,13 @@ func TestAccAutoManageConfigurationProfile_update(t *testing.T) {
}

func (r AutoManageConfigurationProfileResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.AutomanageConfigurationID(state.ID)
id, err := configurationprofiles.ParseConfigurationProfileID(state.ID)
if err != nil {
return nil, err
}

client := clients.Automanage.ConfigurationClient
resp, err := client.Get(ctx, id.ConfigurationProfileName, id.ResourceGroup)
resp, err := client.Get(ctx, id.ConfigurationProfileName, id.ResourceGroupName)
if err != nil {
if response.WasNotFound(resp.Response.Response) {
return utils.Bool(false), nil
39 changes: 31 additions & 8 deletions internal/services/automanage/client/client.go
Original file line number Diff line number Diff line change
@@ -4,24 +4,47 @@
package client

import (
"fmt"

"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofilehciassignments"
"github.com/hashicorp/go-azure-sdk/resource-manager/automanage/2022-05-04/configurationprofiles"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/tombuildsstuff/kermit/sdk/automanage/2022-05-04/automanage"
)

type Client struct {
ConfigurationProfilesClient *configurationprofiles.ConfigurationProfilesClient
ConfigurationProfileHCIAssignmentsClient *configurationprofilehciassignments.ConfigurationProfileHCIAssignmentsClient

// NOTE: these clients use `tombuildsstuff/kermit` (a variant of `Azure/azure-sdk-for-go`) and shouldn't be used going forwards.
ConfigurationClient *automanage.ConfigurationProfilesClient
HCIAssignmentClient *automanage.ConfigurationProfileHCIAssignmentsClient
}

func NewClient(o *common.ClientOptions) *Client {
configurationProfileClient := automanage.NewConfigurationProfilesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&configurationProfileClient.Client, o.ResourceManagerAuthorizer)
func NewClient(o *common.ClientOptions) (*Client, error) {
configurationProfilesClient, err := configurationprofiles.NewConfigurationProfilesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ConfigurationProfiles client: %+v", err)
}
o.Configure(configurationProfilesClient.Client, o.Authorizers.ResourceManager)

hciAssignmentClient := automanage.NewConfigurationProfileHCIAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&hciAssignmentClient.Client, o.ResourceManagerAuthorizer)
configurationProfileHCIAssignmentsClient, err := configurationprofilehciassignments.NewConfigurationProfileHCIAssignmentsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ConfigurationProfilesHCIAssignments client: %+v", err)
}
o.Configure(configurationProfileHCIAssignmentsClient.Client, o.Authorizers.ResourceManager)

// NOTE: these clients use `tombuildsstuff/kermit` (a variant of `Azure/azure-sdk-for-go`) and shouldn't be used going forwards.
legacyConfigurationProfilesClient := automanage.NewConfigurationProfilesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&legacyConfigurationProfilesClient.Client, o.ResourceManagerAuthorizer)
legacyHCIAssignmentsClient := automanage.NewConfigurationProfileHCIAssignmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&legacyHCIAssignmentsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ConfigurationClient: &configurationProfileClient,
HCIAssignmentClient: &hciAssignmentClient,
}
ConfigurationProfilesClient: configurationProfilesClient,
ConfigurationProfileHCIAssignmentsClient: configurationProfileHCIAssignmentsClient,

ConfigurationClient: &legacyConfigurationProfilesClient,
HCIAssignmentClient: &legacyHCIAssignmentsClient,
}, nil
}
Loading