Skip to content

Commit

Permalink
Added context support for cloud account data sourcs
Browse files Browse the repository at this point in the history
  • Loading branch information
SivaanandM committed Nov 18, 2024
1 parent 9f00739 commit 1b1841e
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 8 deletions.
3 changes: 1 addition & 2 deletions spectrocloud/data_source_cloud_account_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func dataSourceCloudAccountAws() *schema.Resource {
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant`. " +
"Defaults to `project`." + PROJECT_NAME_NUANCE,
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
"depends": {
Type: schema.TypeString,
Expand Down
33 changes: 31 additions & 2 deletions spectrocloud/data_source_cloud_account_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -48,6 +50,13 @@ func dataSourceCloudAccountAzure() *schema.Resource {
Description: "The status of the disable properties option.",
Computed: true,
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
},
}
}
Expand All @@ -64,15 +73,35 @@ func dataSourceCloudAccountAzureRead(_ context.Context, d *schema.ResourceData,
}

var account *models.V1AzureAccount
filteredAccounts := make([]*models.V1AzureAccount, 0)
for _, a := range accounts {

if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
account = a
break
} else if v, ok := d.GetOk("name"); ok && v.(string) == a.Metadata.Name {
account = a
break
filteredAccounts = append(filteredAccounts, a)
}
}

if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
account = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found multiple cloud accounts",
Detail: fmt.Sprintf("more than 1 account found for name - '%s'. Kindly re-try with `context` set, Allowed value `project` or `tenant`", d.Get("name").(string)),
})
return diags
}
} else if len(filteredAccounts) == 1 {
account = filteredAccounts[0]
}

if account == nil {
Expand Down
33 changes: 31 additions & 2 deletions spectrocloud/data_source_cloud_account_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/spectrocloud/palette-sdk-go/api/models"
)

Expand Down Expand Up @@ -31,6 +33,13 @@ func dataSourceCloudAccountCustom() *schema.Resource {
Required: true,
Description: "The custom cloud provider name (e.g., `nutanix`).",
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
},
}
}
Expand All @@ -46,14 +55,34 @@ func dataSourceCloudAccountCustomRead(_ context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}
var account *models.V1CustomAccount
filteredAccounts := make([]*models.V1CustomAccount, 0)
for _, a := range accounts {
if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
account = a
break
} else if v, ok := d.GetOk("name"); ok && v.(string) == a.Metadata.Name {
account = a
break
filteredAccounts = append(filteredAccounts, a)
}
}

if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
account = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found multiple cloud accounts",
Detail: fmt.Sprintf("more than 1 account found for name - '%s'. Kindly re-try with `context` set, Allowed value `project` or `tenant`", d.Get("name").(string)),
})
return diags
}
} else if len(filteredAccounts) == 1 {
account = filteredAccounts[0]
}

if account == nil {
Expand Down
33 changes: 31 additions & 2 deletions spectrocloud/data_source_cloud_account_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -28,6 +30,13 @@ func dataSourceCloudAccountGcp() *schema.Resource {
Computed: true,
ExactlyOneOf: []string{"id", "name"},
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
},
}
}
Expand All @@ -44,15 +53,35 @@ func dataSourceCloudAccountGcpRead(_ context.Context, d *schema.ResourceData, m
}

var account *models.V1GcpAccount
filteredAccounts := make([]*models.V1GcpAccount, 0)
for _, a := range accounts {

if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
account = a
break
} else if v, ok := d.GetOk("name"); ok && v.(string) == a.Metadata.Name {
account = a
break
filteredAccounts = append(filteredAccounts, a)
}
}

if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
account = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found multiple cloud accounts",
Detail: fmt.Sprintf("more than 1 account found for name - '%s'. Kindly re-try with `context` set, Allowed value `project` or `tenant`", d.Get("name").(string)),
})
return diags
}
} else if len(filteredAccounts) == 1 {
account = filteredAccounts[0]
}

if account == nil {
Expand Down
30 changes: 30 additions & 0 deletions spectrocloud/data_source_cloud_account_maas.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -37,6 +39,13 @@ func dataSourceCloudAccountMaas() *schema.Resource {
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the cloud account. This can be used instead of `id` to retrieve the account details. Only one of `id` or `name` can be specified.",
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
},
}
}
Expand All @@ -53,6 +62,7 @@ func dataSourceCloudAccountMaasRead(_ context.Context, d *schema.ResourceData, m
}

var account *models.V1MaasAccount
filteredAccounts := make([]*models.V1MaasAccount, 0)
for _, a := range accounts {

if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
Expand All @@ -64,6 +74,26 @@ func dataSourceCloudAccountMaasRead(_ context.Context, d *schema.ResourceData, m
}
}

if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
account = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found multiple cloud accounts",
Detail: fmt.Sprintf("more than 1 account found for name - '%s'. Kindly re-try with `context` set, Allowed value `project` or `tenant`", d.Get("name").(string)),
})
return diags
}
} else if len(filteredAccounts) == 1 {
account = filteredAccounts[0]
}

if account == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand Down
30 changes: 30 additions & 0 deletions spectrocloud/data_source_cloud_account_openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -27,6 +29,13 @@ func dataSourceCloudAccountOpenStack() *schema.Resource {
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the OpenStack cloud account. Either `id` or `name` must be provided, but not both.",
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
},
}
}
Expand All @@ -43,6 +52,7 @@ func dataSourceCloudAccountOpenStackRead(_ context.Context, d *schema.ResourceDa
}

var account *models.V1OpenStackAccount
filteredAccounts := make([]*models.V1OpenStackAccount, 0)
for _, a := range accounts {

if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
Expand All @@ -54,6 +64,26 @@ func dataSourceCloudAccountOpenStackRead(_ context.Context, d *schema.ResourceDa
}
}

if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
account = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found multiple cloud accounts",
Detail: fmt.Sprintf("more than 1 account found for name - '%s'. Kindly re-try with `context` set, Allowed value `project` or `tenant`", d.Get("name").(string)),
})
return diags
}
} else if len(filteredAccounts) == 1 {
account = filteredAccounts[0]
}

if account == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand Down
30 changes: 30 additions & 0 deletions spectrocloud/data_source_cloud_account_tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package spectrocloud

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -32,6 +34,13 @@ func dataSourceCloudAccountTencent() *schema.Resource {
ExactlyOneOf: []string{"id", "name"},
Description: "The name of the Tencent cloud account. Either `id` or `name` must be provided, but not both.",
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "",
ValidateFunc: validation.StringInSlice([]string{"", "project", "tenant"}, false),
Description: "The context of the cluster. Allowed values are `project` or `tenant` or ``. ",
},
},
}
}
Expand All @@ -48,6 +57,7 @@ func dataSourceCloudAccountTencentRead(_ context.Context, d *schema.ResourceData
}

var account *models.V1TencentAccount
filteredAccounts := make([]*models.V1TencentAccount, 0)
for _, a := range accounts {

if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID {
Expand All @@ -59,6 +69,26 @@ func dataSourceCloudAccountTencentRead(_ context.Context, d *schema.ResourceData
}
}

if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
account = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found multiple cloud accounts",
Detail: fmt.Sprintf("more than 1 account found for name - '%s'. Kindly re-try with `context` set, Allowed value `project` or `tenant`", d.Get("name").(string)),
})
return diags
}
} else if len(filteredAccounts) == 1 {
account = filteredAccounts[0]
}

if account == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Expand Down
Loading

0 comments on commit 1b1841e

Please sign in to comment.