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

PLT-1326 added context to read cloud account #544

Merged
merged 6 commits into from
Nov 18, 2024
Merged
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
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ data "spectrocloud_cloudaccount_aws" "aws_account" {

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `depends` (String)
- `id` (String) ID of the AWS cloud account registered in Palette.
- `name` (String) Name of the AWS cloud account registered in Palette.
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ data "spectrocloud_cloudaccount_azure" "azure_account" {

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) ID of the Azure cloud account registered in Palette.
- `name` (String) Name of the Azure cloud account registered in Palette.

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ description: |-

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) The unique identifier of the cloud account. Either `id` or `name` must be provided.
- `name` (String) The name of the cloud account. Either `id` or `name` must be provided.
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_gcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ data "spectrocloud_cloudaccount_gcp" "gcp_account" {

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) ID of the GCP cloud account registered in Palette.
- `name` (String) Name of the GCP cloud account registered in Palette.
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_maas.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ description: |-

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) The unique ID of the cloud account. Either `id` or `name` must be provided, but not both.
- `name` (String) 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.

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_openstack.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ description: |-

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) The unique ID of the OpenStack cloud account. Either `id` or `name` must be provided, but not both.
- `name` (String) The name of the OpenStack cloud account. Either `id` or `name` must be provided, but not both.
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_tencent.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ description: |-

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) The unique ID of the Tencent cloud account. Either `id` or `name` must be provided, but not both.
- `name` (String) The name of the Tencent cloud account. Either `id` or `name` must be provided, but not both.

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/cloudaccount_vsphere.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ description: |-

### Optional

- `context` (String) The context of the cluster. Allowed values are `project` or `tenant` or ``.
- `id` (String) The unique ID of the vSphere cloud account. Either `id` or `name` must be provided, but not both.
- `name` (String) The name of the vSphere cloud account. Either `id` or `name` must be provided, but not both.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "spectrocloud_cloudaccount_aws" "aws_account" {
# id = <uid>
name = "ran-tf"
# context = "tenant"
}

output "aws_account" {
value = data.spectrocloud_cloudaccount_aws.aws_account
}
18 changes: 18 additions & 0 deletions examples/data-sources/spectrocloud_cloud_account/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
spectrocloud = {
version = ">= 0.13.2"
source = "spectrocloud/spectrocloud"
}
}
}

variable "sc_host" {}
variable "sc_api_key" {}
variable "sc_project_name" {}

provider "spectrocloud" {
host = var.sc_host
api_key = var.sc_api_key
project_name = var.sc_project_name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Spectro Cloud credentials
sc_host = "{Enter Spectro Cloud API Host}" #e.g: api.spectrocloud.com (for SaaS)
sc_api_key = "{Enter Spectro Cloud API Key}"
sc_project_name = "{Enter Spectro Cloud Project Name}" #e.g: Default
52 changes: 39 additions & 13 deletions spectrocloud/data_source_cloud_account_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +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 All @@ -28,6 +29,13 @@ func dataSourceCloudAccountAws() *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 ``. ",
},
"depends": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -48,20 +56,38 @@ func dataSourceCloudAccountAwsRead(_ context.Context, d *schema.ResourceData, m
if err != nil {
return diag.FromErr(err)
}

var account *models.V1AwsAccount
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
var fAccount *models.V1AwsAccount
filteredAccounts := make([]*models.V1AwsAccount, 0)
for _, acc := range accounts {
if v, ok := d.GetOk("id"); ok && v.(string) == acc.Metadata.UID {
fAccount = acc
break
}
if v, ok := d.GetOk("name"); ok && v.(string) == acc.Metadata.Name {
filteredAccounts = append(filteredAccounts, acc)
}
}
if len(filteredAccounts) > 1 {
if accContext, ok := d.GetOk("context"); ok && accContext != "" {
for _, ac := range filteredAccounts {
if ac.Metadata.Annotations["scope"] == accContext {
fAccount = ac
break
}
}
} else {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Found more than 1 AWS account",
Detail: fmt.Sprintf("more than 1 aws 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 {
fAccount = filteredAccounts[0]
}

if account == nil {
if fAccount == nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to find aws cloud account",
Expand All @@ -70,8 +96,8 @@ func dataSourceCloudAccountAwsRead(_ context.Context, d *schema.ResourceData, m
return diags
}

d.SetId(account.Metadata.UID)
err = d.Set("name", account.Metadata.Name)
d.SetId(fAccount.Metadata.UID)
err = d.Set("name", fAccount.Metadata.Name)
if err != nil {
return diag.FromErr(err)
}
Expand Down
35 changes: 35 additions & 0 deletions spectrocloud/data_source_cloud_account_aws_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package spectrocloud

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
"testing"
)

func prepareDataSourceCloudAccountAwsSchema() *schema.ResourceData {
d := dataSourceCloudAccountAws().TestResourceData()
return d
}

func TestDataSourceCloudAccountAwsRead(t *testing.T) {
d := prepareDataSourceCloudAccountAwsSchema()
var diags diag.Diagnostics

var ctx context.Context
_ = d.Set("name", "test-aws-account-1")
_ = d.Set("context", "project")
diags = dataSourceCloudAccountAwsRead(ctx, d, unitTestMockAPIClient)
assert.Equal(t, 0, len(diags))
}

func TestDataSourceCloudAccountAwsReadNegative(t *testing.T) {
d := prepareDataSourceCloudAccountAwsSchema()
var diags diag.Diagnostics

var ctx context.Context
_ = d.Set("name", "test-aws-account-1")
diags = dataSourceCloudAccountAwsRead(ctx, d, unitTestMockAPINegativeClient)
assertFirstDiagMessage(t, diags, "Unable to find aws cloud account")
}
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
Loading
Loading