Skip to content

Commit

Permalink
Adding vmware cloud account
Browse files Browse the repository at this point in the history
  • Loading branch information
saamalik committed Dec 31, 2020
1 parent 7b7f099 commit 925ac6c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/data-sources/cloudaccount_vsphere.md
Original file line number Diff line number Diff line change
@@ -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)


69 changes: 69 additions & 0 deletions spectrocloud/data_source_cloud_account_vsphere.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions spectrocloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 925ac6c

Please sign in to comment.