Skip to content

Commit

Permalink
update for issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 committed Jun 27, 2022
1 parent f70774a commit 6f2ff80
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func dataSourceAutomationAccount() *pluginsdk.Resource {

func dataSourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{}) error {
iclient := meta.(*clients.Client).Automation.AgentRegistrationInfoClient
client := meta.(*clients.Client).Automation.AccountPandoraClient
client := meta.(*clients.Client).Automation.AccountClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId

ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
Expand Down
17 changes: 10 additions & 7 deletions internal/services/automation/automation_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func resourceAutomationAccount() *pluginsdk.Resource {
}

func resourceAutomationAccountCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.AccountPandoraClient
client := meta.(*clients.Client).Automation.AccountClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -118,7 +118,7 @@ func resourceAutomationAccountCreate(d *pluginsdk.ResourceData, meta interface{}
if identityVal.Type != identity.TypeNone {
parameters.Identity = identityVal
}
if tagsVal := tags.ExpandPandora(d.Get("tags").(map[string]interface{})); tagsVal != nil {
if tagsVal := expandTags(d.Get("tags").(map[string]interface{})); tagsVal != nil {
parameters.Tags = &tagsVal
}

Expand All @@ -131,7 +131,7 @@ func resourceAutomationAccountCreate(d *pluginsdk.ResourceData, meta interface{}
}

func resourceAutomationAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.AccountPandoraClient
client := meta.(*clients.Client).Automation.AccountClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -154,7 +154,7 @@ func resourceAutomationAccountUpdate(d *pluginsdk.ResourceData, meta interface{}
Identity: identity,
}

if tagsVal := tags.ExpandPandora(d.Get("tags").(map[string]interface{})); tagsVal != nil {
if tagsVal := expandTags(d.Get("tags").(map[string]interface{})); tagsVal != nil {
parameters.Tags = &tagsVal
}

Expand All @@ -166,7 +166,7 @@ func resourceAutomationAccountUpdate(d *pluginsdk.ResourceData, meta interface{}
}

func resourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.AccountPandoraClient
client := meta.(*clients.Client).Automation.AccountClient
registrationClient := meta.(*clients.Client).Automation.AgentRegistrationInfoClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -203,6 +203,9 @@ func resourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{})

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
Expand All @@ -229,13 +232,13 @@ func resourceAutomationAccountRead(d *pluginsdk.ResourceData, meta interface{})
}

if resp.Model.Tags != nil {
return tags.FlattenAndSetPandora(d, *resp.Model.Tags)
return flattenAndSetTags(d, *resp.Model.Tags)
}
return nil
}

func resourceAutomationAccountDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Automation.AccountPandoraClient
client := meta.(*clients.Client).Automation.AccountClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down
11 changes: 6 additions & 5 deletions internal/services/automation/automation_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2021-06-22/automationaccount"

"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/automation/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -160,17 +161,17 @@ func TestAccAutomationAccount_userAssignedIdentity(t *testing.T) {
}

func (t AutomationAccountResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.AutomationAccountID(state.ID)
id, err := automationaccount.ParseAutomationAccountID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Automation.AccountClient.Get(ctx, id.ResourceGroup, id.Name)
resp, err := clients.Automation.AccountClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Automation Account %q (resource group: %q): %+v", id.Name, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving Automation Account %q (resource group: %q): %+v", id.AutomationAccountName, id.ResourceGroupName, err)
}

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

func (AutomationAccountResource) basic(data acceptance.TestData) string {
Expand Down
10 changes: 3 additions & 7 deletions internal/services/automation/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

type Client struct {
AccountClient *automation.AccountClient
AccountPandoraClient *automationaccount.AutomationAccountClient
//AccountClient *automation.AccountClient
AccountClient *automationaccount.AutomationAccountClient
AgentRegistrationInfoClient *automation.AgentRegistrationInformationClient
CertificateClient *automation.CertificateClient
ConnectionClient *automation.ConnectionClient
Expand All @@ -26,12 +26,9 @@ type Client struct {
}

func NewClient(o *common.ClientOptions) *Client {
accountClient := automation.NewAccountClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
accountClient := automationaccount.NewAutomationAccountClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer)

accountPandoraClietn := automationaccount.NewAutomationAccountClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&accountPandoraClietn.Client, o.ResourceManagerAuthorizer)

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

Expand Down Expand Up @@ -76,7 +73,6 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
AccountClient: &accountClient,
AccountPandoraClient: &accountPandoraClietn,
AgentRegistrationInfoClient: &agentRegistrationInfoClient,
CertificateClient: &certificateClient,
ConnectionClient: &connectionClient,
Expand Down
50 changes: 50 additions & 0 deletions internal/services/automation/transition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package automation

import (
"fmt"

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

func tagValueToString(v interface{}) (string, error) {
switch value := v.(type) {
case string:
return value, nil
case int:
return fmt.Sprintf("%d", value), nil
default:
return "", fmt.Errorf("unknown tag type %T in tag value", value)
}
}

func expandTags(tagsMap map[string]interface{}) map[string]string {
output := make(map[string]string, len(tagsMap))

for i, v := range tagsMap {
// Validate should have ignored this error already
value, _ := tagValueToString(v)
output[i] = value
}

return output
}

func flattenTags(tagMap map[string]string) map[string]interface{} {
// If tagsMap is nil, len(tagsMap) will be 0.
output := make(map[string]interface{}, len(tagMap))

for i, v := range tagMap {
output[i] = v
}

return output
}

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

return nil
}
12 changes: 0 additions & 12 deletions internal/tags/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,3 @@ func Expand(tagsMap map[string]interface{}) map[string]*string {

return output
}

func ExpandPandora(tagsMap map[string]interface{}) map[string]string {
output := make(map[string]string, len(tagsMap))

for i, v := range tagsMap {
// Validate should have ignored this error already
value, _ := TagValueToString(v)
output[i] = value
}

return output
}
20 changes: 0 additions & 20 deletions internal/tags/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,3 @@ func FlattenAndSet(d *pluginsdk.ResourceData, tagMap map[string]*string) error {

return nil
}

func FlattenPandora(tagMap map[string]string) map[string]interface{} {
// If tagsMap is nil, len(tagsMap) will be 0.
output := make(map[string]interface{}, len(tagMap))

for i, v := range tagMap {
output[i] = v
}

return output
}

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

return nil
}

0 comments on commit 6f2ff80

Please sign in to comment.