-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters