Skip to content

Commit

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

automation: moving `azurerm_automation_account`'s registration info to using `go-azure-sdk`
  • Loading branch information
tombuildsstuff authored Oct 13, 2023
2 parents de21fce + d7dc9bc commit f3e4628
Show file tree
Hide file tree
Showing 67 changed files with 530 additions and 28,064 deletions.
73 changes: 43 additions & 30 deletions internal/services/automation/automation_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
identity2 "github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2019-06-01/agentregistrationinformation"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/automationaccount"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceAutomationAccount() *pluginsdk.Resource {
Expand Down Expand Up @@ -84,7 +86,6 @@ func dataSourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{}
defer cancel()

id := automationaccount.NewAutomationAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
Expand All @@ -94,43 +95,55 @@ func dataSourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{}
}
d.SetId(id.ID())

iresp, err := iclient.Get(ctx, id.ResourceGroupName, id.AutomationAccountName)
infoId := agentregistrationinformation.NewAutomationAccountID(id.SubscriptionId, id.ResourceGroupName, id.AutomationAccountName)
infoResp, err := iclient.Get(ctx, infoId)
if err != nil {
if utils.ResponseWasNotFound(iresp.Response) {
return fmt.Errorf("%q Account Registration Information was not found", id)
}
return fmt.Errorf("retreiving Automation Account Registration Information %s: %+v", id, err)
}
if iresp.Keys != nil {
d.Set("primary_key", iresp.Keys.Primary)
d.Set("secondary_key", iresp.Keys.Secondary)
return fmt.Errorf("retreiving Agent Registration Information for %s: %+v", id, err)
}

identity, err := identity2.FlattenSystemAndUserAssignedMap(resp.Model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
if model := resp.Model; model != nil {
flattenedIdentity, err := identity.FlattenSystemAndUserAssignedMap(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
if err := d.Set("identity", flattenedIdentity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if props := model.Properties; props != nil {
d.Set("private_endpoint_connection", flattenPrivateEndpointConnectionsDataSource(props.PrivateEndpointConnections))
d.Set("hybrid_service_url", props.AutomationHybridServiceUrl)
}
}

d.Set("endpoint", iresp.Endpoint)
if resp.Model != nil && resp.Model.Properties != nil {
d.Set("private_endpoint_connection", flattenPrivateEndpointConnections(resp.Model.Properties.PrivateEndpointConnections))
d.Set("hybrid_service_url", resp.Model.Properties.AutomationHybridServiceUrl)
endpoint := ""
primaryKey := ""
secondaryKey := ""
if model := infoResp.Model; model != nil {
endpoint = pointer.From(model.Endpoint)
if keys := model.Keys; keys != nil {
primaryKey = pointer.From(keys.Primary)
secondaryKey = pointer.From(keys.Secondary)
}
}
d.Set("endpoint", endpoint)
d.Set("primary_key", primaryKey)
d.Set("secondary_key", secondaryKey)

return nil
}

func flattenPrivateEndpointConnections(conns *[]automationaccount.PrivateEndpointConnection) (res []interface{}) {
if conns == nil || len(*conns) == 0 {
return
func flattenPrivateEndpointConnectionsDataSource(input *[]automationaccount.PrivateEndpointConnection) []interface{} {
if input == nil {
return []interface{}{}
}
for _, con := range *conns {
res = append(res, map[string]interface{}{
"id": con.Id,
"name": con.Name,

output := make([]interface{}, 0)
for _, item := range *input {
output = append(output, map[string]interface{}{
"id": pointer.From(item.Id),
"name": pointer.From(item.Name),
})
}
return res
return output
}
100 changes: 59 additions & 41 deletions internal/services/automation/automation_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2019-06-01/agentregistrationinformation"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/automationaccount"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down Expand Up @@ -282,7 +283,8 @@ func resourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

keysResp, err := registrationClient.Get(ctx, id.ResourceGroupName, id.AutomationAccountName)
infoId := agentregistrationinformation.NewAutomationAccountID(id.SubscriptionId, id.ResourceGroupName, id.AutomationAccountName)
keysResp, err := registrationClient.Get(ctx, infoId)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[DEBUG] Agent Registration Info for %s was not found - removing from state!", *id)
Expand All @@ -296,55 +298,56 @@ func resourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{})
d.Set("name", id.AutomationAccountName)
d.Set("resource_group_name", id.ResourceGroupName)

d.Set("location", location.NormalizeNilable(resp.Model.Location))
publicNetworkAccessEnabled := true
if resp.Model == nil || resp.Model.Properties == nil {
return fmt.Errorf("retrieving Automation Account got empty Model")
}
prop := resp.Model.Properties
if prop.PublicNetworkAccess != nil {
publicNetworkAccessEnabled = *prop.PublicNetworkAccess
}
d.Set("public_network_access_enabled", publicNetworkAccessEnabled)
skuName := ""
if sku := prop.Sku; sku != nil {
skuName = string(prop.Sku.Name)
}
d.Set("sku_name", skuName)
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
if props := model.Properties; props != nil {
publicNetworkAccessEnabled := true
if props.PublicNetworkAccess != nil {
publicNetworkAccessEnabled = *props.PublicNetworkAccess
}
d.Set("public_network_access_enabled", publicNetworkAccessEnabled)

localAuthEnabled := true
if val := prop.DisableLocalAuth; val != nil && *val {
localAuthEnabled = false
}
d.Set("local_authentication_enabled", localAuthEnabled)
skuName := ""
if sku := props.Sku; sku != nil {
skuName = string(sku.Name)
}
d.Set("sku_name", skuName)

if err := d.Set("encryption", flattenEncryption(prop.Encryption)); err != nil {
return fmt.Errorf("setting `encryption`: %+v", err)
}
localAuthEnabled := true
if val := props.DisableLocalAuth; val != nil && *val {
localAuthEnabled = false
}
d.Set("local_authentication_enabled", localAuthEnabled)

d.Set("dsc_server_endpoint", keysResp.Endpoint)
if keys := keysResp.Keys; keys != nil {
d.Set("dsc_primary_access_key", keys.Primary)
d.Set("dsc_secondary_access_key", keys.Secondary)
}
if err := d.Set("encryption", flattenEncryption(props.Encryption)); err != nil {
return fmt.Errorf("setting `encryption`: %+v", err)
}
d.Set("hybrid_service_url", props.AutomationHybridServiceUrl)

d.Set("hybrid_service_url", prop.AutomationHybridServiceUrl)
identity, err := identity.FlattenSystemAndUserAssignedMap(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

identity, err := identity.FlattenSystemAndUserAssignedMap(resp.Model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}
d.Set("private_endpoint_connection", flattenPrivateEndpointConnections(props.PrivateEndpointConnections))
}

if resp.Model != nil && resp.Model.Properties != nil {
d.Set("private_endpoint_connection", flattenPrivateEndpointConnections(resp.Model.Properties.PrivateEndpointConnections))
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}
}

if resp.Model.Tags != nil {
return flattenAndSetTags(d, *resp.Model.Tags)
if model := keysResp.Model; model != nil {
d.Set("dsc_server_endpoint", model.Endpoint)
if keys := model.Keys; keys != nil {
d.Set("dsc_primary_access_key", keys.Primary)
d.Set("dsc_secondary_access_key", keys.Secondary)
}
}

return nil
}

Expand Down Expand Up @@ -438,3 +441,18 @@ func flattenEncryption(encryption *automationaccount.EncryptionProperties) []int
},
}
}

func flattenPrivateEndpointConnections(input *[]automationaccount.PrivateEndpointConnection) []interface{} {
if input == nil {
return []interface{}{}
}

output := make([]interface{}, 0)
for _, item := range *input {
output = append(output, map[string]interface{}{
"id": pointer.From(item.Id),
"name": pointer.From(item.Name),
})
}
return output
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (t AutomationAccountResource) Exists(ctx context.Context, clients *clients.
return nil, fmt.Errorf("retrieving Automation Account %q (resource group: %q): %+v", id.AutomationAccountName, id.ResourceGroupName, err)
}

return utils.Bool(resp.Model.Properties != nil), nil
return utils.Bool(resp.Model != nil), nil
}

func (AutomationAccountResource) basic(data acceptance.TestData) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"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/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/dscconfiguration"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/validate"
"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"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -88,7 +88,7 @@ func resourceAutomationDscConfiguration() *pluginsdk.Resource {
Computed: true,
},

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand Down Expand Up @@ -178,13 +178,9 @@ func resourceAutomationDscConfigurationRead(d *pluginsdk.ResourceData, meta inte
d.Set("state", string(pointer.From(props.State)))
}

if model.Tags != nil {
err := flattenAndSetTags(d, *model.Tags)
if err != nil {
return err
}
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}

}

contentResp, err := client.GetContent(ctx, *id)
Expand Down
8 changes: 4 additions & 4 deletions internal/services/automation/automation_runbook_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/jobschedule"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/runbook"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/runbookdraft"
Expand All @@ -21,7 +22,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/helper"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/validate"
"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"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -239,7 +239,7 @@ func resourceAutomationRunbook() *pluginsdk.Resource {
ValidateFunc: validation.IntAtLeast(0),
},

"tags": tags.Schema(),
"tags": commonschema.Tags(),
},
}
}
Expand Down Expand Up @@ -438,8 +438,8 @@ func resourceAutomationRunbookRead(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("setting `job_schedule`: %+v", err)
}

if t := model.Tags; t != nil {
return flattenAndSetTags(d, *t)
if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return err
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/services/automation/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package client
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/preview/automation/mgmt/2020-01-13-preview/automation" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2015-10-31/webhook"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2019-06-01/agentregistrationinformation"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2019-06-01/softwareupdateconfiguration"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2020-01-13-preview/watcher"
automation_2022_08_08 "github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08"
Expand All @@ -18,7 +18,7 @@ import (
type Client struct {
*automation_2022_08_08.Client

AgentRegistrationInfoClient *automation.AgentRegistrationInformationClient
AgentRegistrationInfoClient *agentregistrationinformation.AgentRegistrationInformationClient
SoftwareUpdateConfigClient *softwareupdateconfiguration.SoftwareUpdateConfigurationClient
WebhookClient *webhook.WebhookClient
WatcherClient *watcher.WatcherClient
Expand All @@ -32,7 +32,7 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
return nil, fmt.Errorf("building Automation client: %+v", err)
}

agentRegistrationInfoClient := automation.NewAgentRegistrationInformationClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
agentRegistrationInfoClient := agentregistrationinformation.NewAgentRegistrationInformationClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&agentRegistrationInfoClient.Client, o.ResourceManagerAuthorizer)

softUpClient := softwareupdateconfiguration.NewSoftwareUpdateConfigurationClientWithBaseURI(o.ResourceManagerEndpoint)
Expand Down
11 changes: 0 additions & 11 deletions internal/services/automation/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package automation

import (
"fmt"

"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

func interfaceValueToString(v interface{}) (string, error) {
Expand Down Expand Up @@ -42,12 +40,3 @@ func flattenMap(strStrMap map[string]string) map[string]interface{} {

return output
}

func flattenAndSetTags(d *pluginsdk.ResourceData, tagMap map[string]string) error {
flattened := flattenMap(tagMap)
if err := d.Set("tags", flattened); err != nil {
return fmt.Errorf("setting `tags`: %s", err)
}

return nil
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f3e4628

Please sign in to comment.