From 925ac6c5d9856bc32d22fe2bd21846bc422afacf Mon Sep 17 00:00:00 2001 From: Saad Malik Date: Thu, 31 Dec 2020 08:53:54 -0800 Subject: [PATCH] Adding vmware cloud account --- docs/data-sources/cloudaccount_vsphere.md | 21 ++++++ .../data_source_cloud_account_vsphere.go | 69 +++++++++++++++++++ spectrocloud/provider.go | 1 + 3 files changed, 91 insertions(+) create mode 100644 docs/data-sources/cloudaccount_vsphere.md create mode 100644 spectrocloud/data_source_cloud_account_vsphere.go diff --git a/docs/data-sources/cloudaccount_vsphere.md b/docs/data-sources/cloudaccount_vsphere.md new file mode 100644 index 00000000..685b5945 --- /dev/null +++ b/docs/data-sources/cloudaccount_vsphere.md @@ -0,0 +1,21 @@ +--- +page_title: "spectrocloud_cloudaccount_vsphere Data Source - terraform-provider-spectrocloud" +subcategory: "" +description: |- + +--- + +# Data Source `spectrocloud_cloudaccount_vsphere` + + + + + +## Schema + +### Optional + +- **id** (String) The ID of this resource. +- **name** (String) + + diff --git a/spectrocloud/data_source_cloud_account_vsphere.go b/spectrocloud/data_source_cloud_account_vsphere.go new file mode 100644 index 00000000..e88759e0 --- /dev/null +++ b/spectrocloud/data_source_cloud_account_vsphere.go @@ -0,0 +1,69 @@ +package spectrocloud + +import ( + "context" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/spectrocloud/hapi/models" + "github.com/spectrocloud/terraform-provider-spectrocloud/pkg/client" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceCloudAccountVsphere() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCloudAccountVsphereRead, + + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ExactlyOneOf: []string{"id", "name"}, + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ExactlyOneOf: []string{"id", "name"}, + }, + }, + } +} + +func dataSourceCloudAccountVsphereRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + c := m.(*client.V1alpha1Client) + + // Warning or errors can be collected in a slice type + var diags diag.Diagnostics + + accounts, err := c.GetCloudAccountsVsphere() + if err != nil { + return diag.FromErr(err) + } + + var account *models.V1alpha1VsphereAccount + 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 + } + } + + if account == nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "Unable to find vsphere cloud account", + Detail: "Unable to find the specified vsphere cloud account", + }) + return diags + } + + d.SetId(account.Metadata.UID) + d.Set("name", account.Metadata.Name) + + return diags +} \ No newline at end of file diff --git a/spectrocloud/provider.go b/spectrocloud/provider.go index 483371ca..bb430f2f 100644 --- a/spectrocloud/provider.go +++ b/spectrocloud/provider.go @@ -53,6 +53,7 @@ func New(_ string) func() *schema.Provider { "spectrocloud_cloudaccount_aws": dataSourceCloudAccountAws(), "spectrocloud_cloudaccount_azure": dataSourceCloudAccountAzure(), "spectrocloud_cloudaccount_gcp": dataSourceCloudAccountGcp(), + "spectrocloud_cloudaccount_vsphere": dataSourceCloudAccountVsphere(), }, ConfigureContextFunc: providerConfigure, }