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

fix: get the linked account if already linked #2735

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -60,30 +61,65 @@
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 {

Check failure on line 84 in newrelic/resource_newrelic_cloud_aws_govcloud_link_account.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
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
Expand Down
45 changes: 40 additions & 5 deletions newrelic/resource_newrelic_cloud_azure_link_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -66,20 +67,54 @@
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 {

Check failure on line 87 in newrelic/resource_newrelic_cloud_azure_link_account.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
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,
})
}
}
}

Expand Down
37 changes: 33 additions & 4 deletions newrelic/resource_newrelic_cloud_gcp_link_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
providerConfig := meta.(*ProviderConfig)
client := providerConfig.NewClient
accountID := selectAccountID(providerConfig, d)
provider := d.Get("cloud_provider").(string)
name := d.Get("name").(string)

linkAccountInput := expandGcpCloudLinkAccountInput(d)

Expand All @@ -66,10 +68,37 @@

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 {

Check failure on line 71 in newrelic/resource_newrelic_cloud_gcp_link_account.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)
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,
})
}
}
}

Expand Down
Loading