From b481131e996d44d4ef0ad33be649e94a86c10e0c Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Thu, 22 Aug 2024 15:06:18 -0500 Subject: [PATCH 1/5] fix: get the linked account if already linked --- ...esource_newrelic_cloud_gcp_link_account.go | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/newrelic/resource_newrelic_cloud_gcp_link_account.go b/newrelic/resource_newrelic_cloud_gcp_link_account.go index c3ea2fbfe..f9b72d7ca 100644 --- a/newrelic/resource_newrelic_cloud_gcp_link_account.go +++ b/newrelic/resource_newrelic_cloud_gcp_link_account.go @@ -47,6 +47,8 @@ func resourceNewRelicCloudGcpLinkAccountCreate(ctx context.Context, d *schema.Re providerConfig := meta.(*ProviderConfig) client := providerConfig.NewClient accountID := selectAccountID(providerConfig, d) + provider := d.Get("cloud_provider").(string) + name := d.Get("name").(string) linkAccountInput := expandGcpCloudLinkAccountInput(d) @@ -66,10 +68,37 @@ func resourceNewRelicCloudGcpLinkAccountCreate(ctx context.Context, d *schema.Re if len(cloudLinkAccountPayload.Errors) > 0 { for _, err := range cloudLinkAccountPayload.Errors { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: err.Type + " " + err.Message, - }) + if(string(err.Type) == "ERR_INVALID_DATA" && err.LinkedAccountId == 0) { + accounts, getLinkedAccountsErr := client.Cloud.GetLinkedAccounts(provider) + if(getLinkedAccountsErr != nil){ + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: err.Type + " " + err.Message + " " + getLinkedAccountsErr.Error(), + }) + } + + var account *cloud.CloudLinkedAccount + + for _, a := range *accounts { + if a.NrAccountId == accountID && strings.EqualFold(a.Name, name) { + account = &a + break + } + } + + if account == nil { + return diag.FromErr(fmt.Errorf("the name '%s' does not match any account for provider '%s", name, provider)) + } + + d.SetId(strconv.Itoa(account.ID)) + } else if(err.LinkedAccountId != 0) { + d.SetId(strconv.Itoa(err.LinkedAccountId)) + } else { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: err.Type + " " + err.Message, + }) + } } } From 93ea42fe54a2cfb8aad8a5063a8e5be526cbbc89 Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Thu, 22 Aug 2024 15:15:51 -0500 Subject: [PATCH 2/5] fix: style --- newrelic/resource_newrelic_cloud_gcp_link_account.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/newrelic/resource_newrelic_cloud_gcp_link_account.go b/newrelic/resource_newrelic_cloud_gcp_link_account.go index f9b72d7ca..40a2e8ed0 100644 --- a/newrelic/resource_newrelic_cloud_gcp_link_account.go +++ b/newrelic/resource_newrelic_cloud_gcp_link_account.go @@ -68,9 +68,9 @@ func resourceNewRelicCloudGcpLinkAccountCreate(ctx context.Context, d *schema.Re if len(cloudLinkAccountPayload.Errors) > 0 { for _, err := range cloudLinkAccountPayload.Errors { - if(string(err.Type) == "ERR_INVALID_DATA" && err.LinkedAccountId == 0) { + if string(err.Type) == "ERR_INVALID_DATA" && err.LinkedAccountId == 0 { accounts, getLinkedAccountsErr := client.Cloud.GetLinkedAccounts(provider) - if(getLinkedAccountsErr != nil){ + if getLinkedAccountsErr != nil { diags = append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: err.Type + " " + err.Message + " " + getLinkedAccountsErr.Error(), @@ -85,13 +85,13 @@ func resourceNewRelicCloudGcpLinkAccountCreate(ctx context.Context, d *schema.Re break } } - + if account == nil { return diag.FromErr(fmt.Errorf("the name '%s' does not match any account for provider '%s", name, provider)) } d.SetId(strconv.Itoa(account.ID)) - } else if(err.LinkedAccountId != 0) { + } else if err.LinkedAccountId != 0 { d.SetId(strconv.Itoa(err.LinkedAccountId)) } else { diags = append(diags, diag.Diagnostic{ From f8f511961e34671ef053adb91919b25f886c69e0 Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Thu, 22 Aug 2024 21:38:01 -0500 Subject: [PATCH 3/5] fix: get the linked account if already linked (aws) --- ...ewrelic_cloud_aws_govcloud_link_account.go | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go b/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go index 514204d45..9ec03ad9a 100755 --- a/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go +++ b/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go @@ -2,6 +2,7 @@ package newrelic import ( "context" + "fmt" "strconv" "strings" @@ -60,30 +61,65 @@ func resourceNewRelicAwsGovCloudLinkAccount() *schema.Resource { func resourceNewRelicAwsGovCloudLinkAccountCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { providerConfig := meta.(*ProviderConfig) client := providerConfig.NewClient - accountID := selectAccountID(providerConfig, d) + provider := d.Get("cloud_provider").(string) + name := d.Get("name").(string) linkAccountInput := expandAwsGovCloudLinkAccountInput(d) cloudLinkAccountPayload, err := client.Cloud.CloudLinkAccountWithContext(ctx, accountID, linkAccountInput) + + var diags diag.Diagnostics + if err != nil { return diag.FromErr(err) } - var diags diag.Diagnostics + + if cloudLinkAccountPayload == nil { + return diag.FromErr(fmt.Errorf("[ERROR] cloudLinkAccountPayload was nil")) + } + if len(cloudLinkAccountPayload.Errors) > 0 { for _, err := range cloudLinkAccountPayload.Errors { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: err.Type + " " + err.Message, - }) + if string(err.Type) == "ERR_INVALID_DATA" && err.LinkedAccountId == 0 { + accounts, getLinkedAccountsErr := client.Cloud.GetLinkedAccounts(provider) + if getLinkedAccountsErr != nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: err.Type + " " + err.Message + " " + getLinkedAccountsErr.Error(), + }) + } + + var account *cloud.CloudLinkedAccount + + for _, a := range *accounts { + if a.NrAccountId == accountID && strings.EqualFold(a.Name, name) { + account = &a + break + } + } + + if account == nil { + return diag.FromErr(fmt.Errorf("the name '%s' does not match any account for provider '%s", name, provider)) + } + + d.SetId(strconv.Itoa(account.ID)) + } else if err.LinkedAccountId != 0 { + d.SetId(strconv.Itoa(err.LinkedAccountId)) + } else { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: err.Type + " " + err.Message, + }) + } } - return diags } if len(cloudLinkAccountPayload.LinkedAccounts) > 0 { d.SetId(strconv.Itoa(cloudLinkAccountPayload.LinkedAccounts[0].ID)) } - return nil + + return diags } // Extracting the AWSGovCloud account credentials from Schema using expandAzureCloudLinkAccountInput From dc746edbeffd77d51780892d50dfb8a444997ceb Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Thu, 22 Aug 2024 21:38:09 -0500 Subject: [PATCH 4/5] fix: get the linked account if already linked (azure) --- ...ource_newrelic_cloud_azure_link_account.go | 45 ++++++++++++++++--- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/newrelic/resource_newrelic_cloud_azure_link_account.go b/newrelic/resource_newrelic_cloud_azure_link_account.go index ef1587312..010a4dc92 100755 --- a/newrelic/resource_newrelic_cloud_azure_link_account.go +++ b/newrelic/resource_newrelic_cloud_azure_link_account.go @@ -2,6 +2,7 @@ package newrelic import ( "context" + "fmt" "strconv" "strings" @@ -66,20 +67,54 @@ func resourceNewRelicCloudAzureLinkAccountCreate(ctx context.Context, d *schema. client := providerConfig.NewClient accountID := selectAccountID(providerConfig, d) linkAccountInput := expandAzureCloudLinkAccountInput(d) - var diags diag.Diagnostics + provider := d.Get("cloud_provider").(string) + name := d.Get("name").(string) cloudLinkAccountPayload, err := client.Cloud.CloudLinkAccountWithContext(ctx, accountID, linkAccountInput) + var diags diag.Diagnostics + if err != nil { return diag.FromErr(err) } + if cloudLinkAccountPayload == nil { + return diag.FromErr(fmt.Errorf("[ERROR] cloudLinkAccountPayload was nil")) + } + if len(cloudLinkAccountPayload.Errors) > 0 { for _, err := range cloudLinkAccountPayload.Errors { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: err.Type + " " + err.Message, - }) + if string(err.Type) == "ERR_INVALID_DATA" && err.LinkedAccountId == 0 { + accounts, getLinkedAccountsErr := client.Cloud.GetLinkedAccounts(provider) + if getLinkedAccountsErr != nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: err.Type + " " + err.Message + " " + getLinkedAccountsErr.Error(), + }) + } + + var account *cloud.CloudLinkedAccount + + for _, a := range *accounts { + if a.NrAccountId == accountID && strings.EqualFold(a.Name, name) { + account = &a + break + } + } + + if account == nil { + return diag.FromErr(fmt.Errorf("the name '%s' does not match any account for provider '%s", name, provider)) + } + + d.SetId(strconv.Itoa(account.ID)) + } else if err.LinkedAccountId != 0 { + d.SetId(strconv.Itoa(err.LinkedAccountId)) + } else { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: err.Type + " " + err.Message, + }) + } } } From d8d2e6d291c3fc1348082cf86d560e63d6a6ca5c Mon Sep 17 00:00:00 2001 From: Thomas Cross Date: Thu, 22 Aug 2024 21:52:04 -0500 Subject: [PATCH 5/5] fix: lint --- newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go b/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go index 9ec03ad9a..225901e4a 100755 --- a/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go +++ b/newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go @@ -74,7 +74,7 @@ func resourceNewRelicAwsGovCloudLinkAccountCreate(ctx context.Context, d *schema if err != nil { return diag.FromErr(err) } - + if cloudLinkAccountPayload == nil { return diag.FromErr(fmt.Errorf("[ERROR] cloudLinkAccountPayload was nil")) }