Skip to content

Commit

Permalink
Merge pull request #25293 from hashicorp/refactor/automanage-to-go-az…
Browse files Browse the repository at this point in the history
…ure-sdk

`automanage`: refactoring to use `hashicorp/go-azure-sdk`
  • Loading branch information
tombuildsstuff authored Mar 19, 2024
2 parents 0c80e68 + 3e77d2f commit 9fd2258
Show file tree
Hide file tree
Showing 56 changed files with 1,547 additions and 4,920 deletions.
4 changes: 3 additions & 1 deletion internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
if client.Authorization, err = authorization.NewClient(o); err != nil {
return fmt.Errorf("building clients for Authorization: %+v", err)
}
client.Automanage = automanage.NewClient(o)
if client.Automanage, err = automanage.NewClient(o); err != nil {
return fmt.Errorf("building clients for AutoManage: %+v", err)
}
if client.Automation, err = automation.NewClient(o); err != nil {
return fmt.Errorf("building clients for Automation: %+v", err)
}
Expand Down
139 changes: 75 additions & 64 deletions internal/services/automanage/automanage_configuration_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ import (
"regexp"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"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/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automanage/migration"
"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/automanage/2022-05-04/automanage"
)

type AzureSecurityBaselineConfiguration struct {
Expand Down Expand Up @@ -92,6 +91,7 @@ type SchedulePolicyConfiguration struct {
type AutoManageConfigurationResource struct{}

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

func (r AutoManageConfigurationResource) ResourceType() string {
return "azurerm_automanage_configuration"
Expand All @@ -102,7 +102,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 {
Expand Down Expand Up @@ -463,32 +463,34 @@ func (r AutoManageConfigurationResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationProfilesClient
subscriptionId := metadata.Client.Account.SubscriptionId

var model ConfigurationModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

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)
if err != nil && !utils.ResponseWasNotFound(existing.Response) {
id := configurationprofiles.NewConfigurationProfileID(subscriptionId, model.ResourceGroupName, model.Name)
existing, err := client.Get(ctx, id)
if err != nil && !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(r.ResourceType(), id)
}

properties := automanage.ConfigurationProfile{
Location: utils.String(location.Normalize(model.Location)),
Properties: &automanage.ConfigurationProfileProperties{},
Tags: tags.FromTypedObject(model.Tags),
properties := configurationprofiles.ConfigurationProfile{
Location: location.Normalize(model.Location),
Properties: &configurationprofiles.ConfigurationProfileProperties{},
Tags: pointer.To(model.Tags),
}

properties.Properties.Configuration = expandAutomanageConfigurationProfile(model)
properties.Properties.Configuration = expandConfigurationProfile(model)

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

Expand All @@ -502,9 +504,9 @@ func (r AutoManageConfigurationResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationClient
client := metadata.Client.Automanage.ConfigurationProfilesClient

id, err := parse.AutomanageConfigurationID(metadata.ResourceData.Id())
id, err := configurationprofiles.ParseConfigurationProfileID(metadata.ResourceData.Id())
if err != nil {
return err
}
Expand All @@ -514,15 +516,15 @@ func (r AutoManageConfigurationResource) Update() sdk.ResourceFunc {
return fmt.Errorf("decoding: %+v", err)
}

properties := automanage.ConfigurationProfile{
Location: utils.String(location.Normalize(metadata.ResourceData.Get("location").(string))),
Properties: &automanage.ConfigurationProfileProperties{
Configuration: expandAutomanageConfigurationProfile(model),
properties := configurationprofiles.ConfigurationProfile{
Location: location.Normalize(model.Location),
Properties: &configurationprofiles.ConfigurationProfileProperties{
Configuration: expandConfigurationProfile(model),
},
Tags: tags.Expand(metadata.ResourceData.Get("tags").(map[string]interface{})),
Tags: pointer.To(model.Tags),
}

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

Expand All @@ -535,16 +537,16 @@ func (r AutoManageConfigurationResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationClient
client := metadata.Client.Automanage.ConfigurationProfilesClient

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)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return metadata.MarkAsGone(id)
}

Expand All @@ -553,46 +555,45 @@ func (r AutoManageConfigurationResource) Read() sdk.ResourceFunc {

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

if resp.Properties != nil && resp.Properties.Configuration != nil {
configMap := resp.Properties.Configuration.(map[string]interface{})
if model := resp.Model; model != nil {
state.Location = location.Normalize(model.Location)
if props := model.Properties; props != nil && props.Configuration != nil {
configMap := (*props.Configuration).(map[string]interface{})

state.Antimalware = flattenAntimarewareConfig(configMap)
state.Antimalware = flattenAntiMalwareConfig(configMap)

state.AzureSecurityBaseline = flattenAzureSecurityBaselineConfig(configMap)
state.AzureSecurityBaseline = flattenAzureSecurityBaselineConfig(configMap)

state.Backup = flattenBackupConfig(configMap)
state.Backup = flattenBackupConfig(configMap)

if val, ok := configMap["AutomationAccount/Enable"]; ok {
state.AutomationAccountEnabled = val.(bool)
}
if val, ok := configMap["AutomationAccount/Enable"]; ok {
state.AutomationAccountEnabled = val.(bool)
}

if val, ok := configMap["BootDiagnostics/Enable"]; ok {
state.BootDiagnosticsEnabled = val.(bool)
}
if val, ok := configMap["BootDiagnostics/Enable"]; ok {
state.BootDiagnosticsEnabled = val.(bool)
}

if val, ok := configMap["DefenderForCloud/Enable"]; ok {
state.DefenderForCloudEnabled = val.(bool)
}
if val, ok := configMap["DefenderForCloud/Enable"]; ok {
state.DefenderForCloudEnabled = val.(bool)
}

if val, ok := configMap["GuestConfiguration/Enable"]; ok {
state.GuestConfigurationEnabled = val.(bool)
}
if val, ok := configMap["GuestConfiguration/Enable"]; ok {
state.GuestConfigurationEnabled = val.(bool)
}

if val, ok := configMap["LogAnalytics/Enable"]; ok {
state.LogAnalyticsEnabled = val.(bool)
}
if val, ok := configMap["LogAnalytics/Enable"]; ok {
state.LogAnalyticsEnabled = val.(bool)
}

if val, ok := configMap["Alerts/AutomanageStatusChanges/Enable"]; ok {
state.StatusChangeAlertEnabled = val.(bool)
if val, ok := configMap["Alerts/AutomanageStatusChanges/Enable"]; ok {
state.StatusChangeAlertEnabled = val.(bool)
}
}
}

if resp.Tags != nil {
state.Tags = tags.ToTypedObject(resp.Tags)
state.Tags = pointer.From(model.Tags)
}

return metadata.Encode(&state)
Expand All @@ -604,14 +605,14 @@ func (r AutoManageConfigurationResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automanage.ConfigurationClient
client := metadata.Client.Automanage.ConfigurationProfilesClient

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); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

Expand All @@ -620,7 +621,15 @@ func (r AutoManageConfigurationResource) Delete() sdk.ResourceFunc {
}
}

func expandAutomanageConfigurationProfile(model ConfigurationModel) *map[string]interface{} {
func (r AutoManageConfigurationResource) StateUpgraders() sdk.StateUpgradeData {
return sdk.StateUpgradeData{
SchemaVersion: 1,
Upgraders: map[int]pluginsdk.StateUpgrade{
0: migration.ConfigurationV0ToV1{},
},
}
}
func expandConfigurationProfile(model ConfigurationModel) *interface{} {
// building configuration profile in json format
jsonConfig := make(map[string]interface{})

Expand Down Expand Up @@ -717,10 +726,12 @@ func expandAutomanageConfigurationProfile(model ConfigurationModel) *map[string]
if model.StatusChangeAlertEnabled {
jsonConfig["Alerts/AutomanageStatusChanges/Enable"] = model.StatusChangeAlertEnabled
}
return &jsonConfig

var out interface{} = jsonConfig
return &out
}

func flattenAntimarewareConfig(configMap map[string]interface{}) []AntimalwareConfiguration {
func flattenAntiMalwareConfig(configMap map[string]interface{}) []AntimalwareConfiguration {
if val, ok := configMap["Antimalware/Enable"]; !ok || (val == nil) {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ 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/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"
)
Expand Down Expand Up @@ -266,20 +267,21 @@ 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)
client := clients.Automanage.ConfigurationProfilesClient

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)
if err != nil {
if response.WasNotFound(resp.Response.Response) {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return utils.Bool(resp.Response.Response != nil), nil
return pointer.To(resp.Model != nil), nil
}

func (r AutoManageConfigurationProfileResource) template(data acceptance.TestData) string {
Expand Down
31 changes: 20 additions & 11 deletions internal/services/automanage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@
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 {
ConfigurationClient *automanage.ConfigurationProfilesClient
HCIAssignmentClient *automanage.ConfigurationProfileHCIAssignmentsClient
ConfigurationProfilesClient *configurationprofiles.ConfigurationProfilesClient
ConfigurationProfileHCIAssignmentsClient *configurationprofilehciassignments.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)

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

0 comments on commit 9fd2258

Please sign in to comment.