From 1b1841ef1332b2c96f59bfbe815d64a3009b7591 Mon Sep 17 00:00:00 2001 From: Sivaanand Murugesan Date: Tue, 19 Nov 2024 01:01:52 +0530 Subject: [PATCH] Added context support for cloud account data sourcs --- spectrocloud/data_source_cloud_account_aws.go | 3 +- .../data_source_cloud_account_azure.go | 33 +++++++++++++++++-- .../data_source_cloud_account_custom.go | 33 +++++++++++++++++-- spectrocloud/data_source_cloud_account_gcp.go | 33 +++++++++++++++++-- .../data_source_cloud_account_maas.go | 30 +++++++++++++++++ .../data_source_cloud_account_openstack.go | 30 +++++++++++++++++ .../data_source_cloud_account_tencent.go | 30 +++++++++++++++++ .../data_source_cloud_account_vsphere.go | 30 +++++++++++++++++ 8 files changed, 214 insertions(+), 8 deletions(-) diff --git a/spectrocloud/data_source_cloud_account_aws.go b/spectrocloud/data_source_cloud_account_aws.go index 3cbd7342..90ee96f1 100644 --- a/spectrocloud/data_source_cloud_account_aws.go +++ b/spectrocloud/data_source_cloud_account_aws.go @@ -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, diff --git a/spectrocloud/data_source_cloud_account_azure.go b/spectrocloud/data_source_cloud_account_azure.go index 65fd5211..d74fac70 100644 --- a/spectrocloud/data_source_cloud_account_azure.go +++ b/spectrocloud/data_source_cloud_account_azure.go @@ -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" @@ -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 ``. ", + }, }, } } @@ -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 { diff --git a/spectrocloud/data_source_cloud_account_custom.go b/spectrocloud/data_source_cloud_account_custom.go index a7efbdef..f700719e 100644 --- a/spectrocloud/data_source_cloud_account_custom.go +++ b/spectrocloud/data_source_cloud_account_custom.go @@ -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" ) @@ -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 ``. ", + }, }, } } @@ -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 { diff --git a/spectrocloud/data_source_cloud_account_gcp.go b/spectrocloud/data_source_cloud_account_gcp.go index ebd2b19c..a6b5a03b 100644 --- a/spectrocloud/data_source_cloud_account_gcp.go +++ b/spectrocloud/data_source_cloud_account_gcp.go @@ -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" @@ -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 ``. ", + }, }, } } @@ -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 { diff --git a/spectrocloud/data_source_cloud_account_maas.go b/spectrocloud/data_source_cloud_account_maas.go index cc35c0b1..14dd5f41 100644 --- a/spectrocloud/data_source_cloud_account_maas.go +++ b/spectrocloud/data_source_cloud_account_maas.go @@ -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" @@ -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 ``. ", + }, }, } } @@ -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 { @@ -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, diff --git a/spectrocloud/data_source_cloud_account_openstack.go b/spectrocloud/data_source_cloud_account_openstack.go index 7140c334..ee411eb1 100644 --- a/spectrocloud/data_source_cloud_account_openstack.go +++ b/spectrocloud/data_source_cloud_account_openstack.go @@ -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" @@ -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 ``. ", + }, }, } } @@ -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 { @@ -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, diff --git a/spectrocloud/data_source_cloud_account_tencent.go b/spectrocloud/data_source_cloud_account_tencent.go index 0e04ddb0..ef753345 100644 --- a/spectrocloud/data_source_cloud_account_tencent.go +++ b/spectrocloud/data_source_cloud_account_tencent.go @@ -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" @@ -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 ``. ", + }, }, } } @@ -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 { @@ -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, diff --git a/spectrocloud/data_source_cloud_account_vsphere.go b/spectrocloud/data_source_cloud_account_vsphere.go index 577079a7..e60ad581 100644 --- a/spectrocloud/data_source_cloud_account_vsphere.go +++ b/spectrocloud/data_source_cloud_account_vsphere.go @@ -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" @@ -27,6 +29,13 @@ func dataSourceCloudAccountVsphere() *schema.Resource { ExactlyOneOf: []string{"id", "name"}, Description: "The name of the vSphere 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 ``. ", + }, }, } } @@ -43,6 +52,7 @@ func dataSourceCloudAccountVsphereRead(_ context.Context, d *schema.ResourceData } var account *models.V1VsphereAccount + filteredAccounts := make([]*models.V1VsphereAccount, 0) for _, a := range accounts { if v, ok := d.GetOk("id"); ok && v.(string) == a.Metadata.UID { @@ -54,6 +64,26 @@ func dataSourceCloudAccountVsphereRead(_ 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,