Skip to content

Commit e95e9ce

Browse files
committed
Add fabric_datastore_vsphere resource
Add a new vra_fabric_datastore_vsphere resource Signed-off-by: Ferran Rodenas <[email protected]>
1 parent c03603f commit e95e9ce

File tree

9 files changed

+388
-0
lines changed

9 files changed

+388
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# fabric datastore vSphere example
2+
3+
**__NOTE: THIS RESOURCE TYPE MUST BE IMPORTED. ATTEMPTING TO CREATE IT WILL ERROR__ **
4+
5+
These resources (fabric datasource vSphere) are discovered in vRA(C) as part of creating a Cloud Account and can not therefore be "created" or "destroyed" in a traditional sense.
6+
7+
This is an example on how to import a fabric datastore vSphere resource into terraform and then manage settings on it.
8+
9+
## Getting Started
10+
11+
There are variables which need to be added to a `terraform.tfvars` file:
12+
13+
* `url` - The base url for API operations
14+
* `refresh_token` - The refresh token for API operations
15+
* `insecure` - Specify whether to validate TLS certificates
16+
17+
To facilitate adding these variables, a sample tfvars file can be copied first:
18+
19+
```shell
20+
cp terraform.tfvars.sample terraform.tfvars
21+
```
22+
23+
This examples assumes a cloud account is already set up.
24+
25+
To import the resource you must find the ID of the fabric datastore vSphere. There are a couple of way this ID can be aquired:
26+
27+
1. Via API calls
28+
2. Viewing the object in a browser and pulling the ID from the url (ex value: `6c78facd-2ba0-4e06-b6f6-5682dec55056`)
29+
30+
Once the information is added to `terraform.tfvars` the resource can be imported and the manage via:
31+
32+
```shell
33+
terraform import vra_fabric_datastore_vsphere.this <fabricDatastorevSphereID>
34+
```
35+
36+
If the import is successful output from the command should resemble
37+
38+
```shell
39+
vra_fabric_datastore_vsphere.this: Importing from ID "<fabricDatastorevSphereID>"...
40+
vra_fabric_datastore_vsphere.this: Import prepared!
41+
Prepared vra_fabric_datastore_vsphere for import
42+
vra_fabric_datastore_vsphere.this: Refreshing state... [id=<fabricDatastorevSphereID>]
43+
44+
Import successful!
45+
46+
The resources that were imported are shown above. These resources are now in
47+
your Terraform state and will henceforth be managed by Terraform
48+
```
49+
50+
To apply your settings you can now perform an apply comand:
51+
52+
```shell
53+
terraform apply
54+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
provider "vra" {
2+
url = var.url
3+
refresh_token = var.refresh_token
4+
insecure = var.insecure
5+
}
6+
7+
resource "vra_fabric_datastore_vsphere" "this" {
8+
tags {
9+
key = "foo"
10+
value = "bar"
11+
}
12+
}
13+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
url = ""
2+
refresh_token = ""
3+
insecure = ""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "url" {
2+
description = "The base url for API operations"
3+
}
4+
5+
variable "refresh_token" {
6+
description = "The refresh token for API operations"
7+
}
8+
9+
variable "insecure" {
10+
description = "Specify whether to validate TLS certificates"
11+
}

vra/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func Provider() *schema.Provider {
105105
"vra_content_source": resourceContentSource(),
106106
"vra_deployment": resourceDeployment(),
107107
"vra_fabric_compute": resourceFabricCompute(),
108+
"vra_fabric_datastore_vsphere": resourceFabricDatastoreVsphere(),
108109
"vra_fabric_network_vsphere": resourceFabricNetworkVsphere(),
109110
"vra_flavor_profile": resourceFlavorProfile(),
110111
"vra_image_profile": resourceImageProfile(),

vra/provider_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,20 @@ func testAccPreCheckFabricCompute(t *testing.T) {
545545
}
546546
}
547547
}
548+
549+
func testAccPreCheckFabricDatastoreVsphere(t *testing.T) {
550+
if os.Getenv("VRA_REFRESH_TOKEN") == "" && os.Getenv("VRA_ACCESS_TOKEN") == "" {
551+
t.Fatal("VRA_REFRESH_TOKEN or VRA_ACCESS_TOKEN must be set for acceptance tests")
552+
}
553+
554+
envVars := [...]string{
555+
"VRA_URL",
556+
"VRA_FABRIC_DATASTORE_VSPHERE_NAME",
557+
}
558+
559+
for _, name := range envVars {
560+
if v := os.Getenv(name); v == "" {
561+
t.Fatalf("%s must be set for acceptance tests\n", name)
562+
}
563+
}
564+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package vra
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/vmware/vra-sdk-go/pkg/client/fabric_vsphere_datastore"
8+
"github.com/vmware/vra-sdk-go/pkg/models"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func resourceFabricDatastoreVsphere() *schema.Resource {
15+
return &schema.Resource{
16+
CreateContext: resourceFabricDatastoreVsphereCreate,
17+
ReadContext: resourceFabricDatastoreVsphereRead,
18+
UpdateContext: resourceFabricDatastoreVsphereUpdate,
19+
DeleteContext: resourceFabricDatastoreVsphereDelete,
20+
Importer: &schema.ResourceImporter{
21+
StateContext: schema.ImportStatePassthroughContext,
22+
},
23+
24+
Schema: map[string]*schema.Schema{
25+
"cloud_account_ids": {
26+
Type: schema.TypeSet,
27+
Computed: true,
28+
Description: "Set of ids of the cloud accounts this entity belongs to.",
29+
Elem: &schema.Schema{
30+
Type: schema.TypeString,
31+
},
32+
},
33+
"created_at": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "Date when the entity was created. The date is in ISO 8601 and UTC.",
37+
},
38+
"description": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "A human-friendly description.",
42+
},
43+
"external_id": {
44+
Type: schema.TypeString,
45+
Computed: true,
46+
Description: "External entity Id on the provider side.",
47+
},
48+
"external_region_id": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
Description: "Id of datacenter in which the datastore is present.",
52+
},
53+
"free_size_gb": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
Description: "Indicates free size available in datastore.",
57+
},
58+
"links": linksSchema(),
59+
"name": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "A human-friendly name used as an identifier for the vSphere fabric datastore resource instance.",
63+
},
64+
"org_id": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
Description: "The id of the organization this entity belongs to.",
68+
},
69+
"owner": {
70+
Type: schema.TypeString,
71+
Computed: true,
72+
Description: "Email of the user that owns the entity.",
73+
},
74+
"tags": tagsSchema(),
75+
"type": {
76+
Type: schema.TypeString,
77+
Computed: true,
78+
Description: "Type of datastore.",
79+
},
80+
"updated_at": {
81+
Type: schema.TypeString,
82+
Computed: true,
83+
Description: "Date when the entity was last updated. The date is ISO 8601 and UTC.",
84+
},
85+
},
86+
}
87+
}
88+
89+
func resourceFabricDatastoreVsphereCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
90+
return diag.FromErr(errors.New("vra_fabric_datastore_vsphere resources are only importable"))
91+
}
92+
93+
func resourceFabricDatastoreVsphereRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
94+
apiClient := m.(*Client).apiClient
95+
96+
id := d.Id()
97+
98+
resp, err := apiClient.FabricvSphereDatastore.GetFabricVSphereDatastore(fabric_vsphere_datastore.NewGetFabricVSphereDatastoreParams().WithID(id))
99+
if err != nil {
100+
switch err.(type) {
101+
case *fabric_vsphere_datastore.GetFabricVSphereDatastoreNotFound:
102+
return diag.Errorf("vSphere fabric datastore '%s' not found", id)
103+
default:
104+
// nop
105+
}
106+
return diag.FromErr(err)
107+
}
108+
109+
fabricVsphereDatastore := resp.GetPayload()
110+
d.SetId(*fabricVsphereDatastore.ID)
111+
d.Set("cloud_account_ids", fabricVsphereDatastore.CloudAccountIds)
112+
d.Set("created_at", fabricVsphereDatastore.CreatedAt)
113+
d.Set("description", fabricVsphereDatastore.Description)
114+
d.Set("external_id", fabricVsphereDatastore.ExternalID)
115+
d.Set("external_region_id", fabricVsphereDatastore.ExternalRegionID)
116+
d.Set("free_size_gb", fabricVsphereDatastore.FreeSizeGB)
117+
d.Set("name", fabricVsphereDatastore.Name)
118+
d.Set("org_id", fabricVsphereDatastore.OrgID)
119+
d.Set("owner", fabricVsphereDatastore.Owner)
120+
d.Set("type", fabricVsphereDatastore.Type)
121+
d.Set("updated_at", fabricVsphereDatastore.UpdatedAt)
122+
123+
if err := d.Set("links", flattenLinks(fabricVsphereDatastore.Links)); err != nil {
124+
return diag.Errorf("error setting vSphere fabric datastore links - error: %#v", err)
125+
}
126+
127+
if err := d.Set("tags", flattenTags(fabricVsphereDatastore.Tags)); err != nil {
128+
return diag.Errorf("error setting vSphere fabric datastore tags - error: %v", err)
129+
}
130+
131+
return nil
132+
}
133+
134+
func resourceFabricDatastoreVsphereUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
135+
apiClient := m.(*Client).apiClient
136+
137+
id := d.Id()
138+
139+
fabricVsphereDatastoreSpecification := &models.FabricVsphereDatastoreSpecification{
140+
Tags: expandTags(d.Get("tags").(*schema.Set).List()),
141+
}
142+
143+
_, err := apiClient.FabricvSphereDatastore.UpdateFabricVsphereDatastore(fabric_vsphere_datastore.NewUpdateFabricVsphereDatastoreParams().WithID(id).WithBody(fabricVsphereDatastoreSpecification))
144+
if err != nil {
145+
return diag.FromErr(err)
146+
}
147+
148+
return resourceFabricDatastoreVsphereRead(ctx, d, m)
149+
}
150+
151+
func resourceFabricDatastoreVsphereDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
152+
return nil
153+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package vra
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"regexp"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
"github.com/vmware/vra-sdk-go/pkg/client/fabric_vsphere_datastore"
12+
)
13+
14+
func TestAccVRAFabricDatastoreVsphere_importBasic(t *testing.T) {
15+
resourceName := "vra_fabric_datastore_vsphere"
16+
fabricDatastoreVsphereID := os.Getenv("VRA_FABRIC_DATASTORE_VSPHERE_ID")
17+
18+
checkFn := func(s []*terraform.InstanceState) error {
19+
if len(s) != 1 {
20+
return fmt.Errorf("expected 1 state %#v", s)
21+
}
22+
23+
fabricDatastoreVsphereState := s[0]
24+
if fabricDatastoreVsphereID != fabricDatastoreVsphereState.ID {
25+
return fmt.Errorf("expected fabric datastore vSphere ID of %s,%s received instead", fabricDatastoreVsphereID, fabricDatastoreVsphereState.ID)
26+
}
27+
return nil
28+
}
29+
resource.Test(t, resource.TestCase{
30+
PreCheck: func() { testAccPreCheckFabricDatastoreVsphere(t) },
31+
Providers: testAccProviders,
32+
CheckDestroy: testAccCheckVRAFabricDatastoreVsphereDestroy,
33+
Steps: []resource.TestStep{
34+
{
35+
Config: testAccCheckVRAFabricDatastoreVsphereConfig(),
36+
ExpectError: regexp.MustCompile("vra_fabric_datastore_vsphere resources are only importable"),
37+
},
38+
{
39+
Config: testAccCheckVRAFabricDatastoreVsphereConfig(),
40+
ResourceName: resourceName,
41+
ImportState: true,
42+
ImportStateId: fabricDatastoreVsphereID,
43+
ImportStateCheck: checkFn,
44+
},
45+
},
46+
})
47+
}
48+
49+
func testAccCheckVRAFabricDatastoreVsphereDestroy(s *terraform.State) error {
50+
apiClient := testAccProviderVRA.Meta().(*Client).apiClient
51+
52+
for _, rs := range s.RootModule().Resources {
53+
if rs.Type == "vra_fabric_datastore_vsphere" {
54+
if _, err := apiClient.FabricvSphereDatastore.GetFabricVSphereDatastore(fabric_vsphere_datastore.NewGetFabricVSphereDatastoreParams().WithID(rs.Primary.ID)); err == nil {
55+
return fmt.Errorf("Resource 'vra_fabric_datastore_vsphere' with id `%s` does not exist", rs.Primary.ID)
56+
}
57+
}
58+
59+
}
60+
61+
return nil
62+
}
63+
64+
func testAccCheckVRAFabricDatastoreVsphereConfig() string {
65+
fabricDatastoreVsphereName := os.Getenv("VRA_FABRIC_DATASTORE_VSPHERE_NAME")
66+
return fmt.Sprintf(`
67+
resource "vra_fabric_datastore_vsphere" "this" {
68+
name = "%s"
69+
}`, fabricDatastoreVsphereName)
70+
}

0 commit comments

Comments
 (0)