Skip to content

Commit 684314b

Browse files
Prativa20markpeek
authored andcommitted
Add data source storage profile (#111)
Signed-off-by: Prativa Bawri <[email protected]>
1 parent a5769a0 commit 684314b

5 files changed

+277
-28
lines changed

vra/data_source_storage_profile.go

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package vra
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
"github.com/vmware/vra-sdk-go/pkg/client/storage_profile"
8+
"github.com/vmware/vra-sdk-go/pkg/models"
9+
10+
"log"
11+
)
12+
13+
func dataSourceStorageProfile() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceStorageProfileRead,
16+
17+
Schema: map[string]*schema.Schema{
18+
"default_item": &schema.Schema{
19+
Type: schema.TypeBool,
20+
Optional: true,
21+
},
22+
"filter": {
23+
Type: schema.TypeString,
24+
Optional: true,
25+
},
26+
"id": {
27+
Type: schema.TypeString,
28+
Optional: true,
29+
Computed: true,
30+
},
31+
"created_at": &schema.Schema{
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"description": &schema.Schema{
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"disk_properties": &schema.Schema{
40+
Type: schema.TypeMap,
41+
Computed: true,
42+
},
43+
"external_region_id": &schema.Schema{
44+
Type: schema.TypeString,
45+
Computed: true,
46+
},
47+
"links": linksSchema(),
48+
"name": &schema.Schema{
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"org_id": &schema.Schema{
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
"owner": &schema.Schema{
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
"supports_encryption": &schema.Schema{
61+
Type: schema.TypeBool,
62+
Computed: true,
63+
},
64+
"tags": tagsSchema(),
65+
"updated_at": &schema.Schema{
66+
Type: schema.TypeString,
67+
Computed: true,
68+
},
69+
},
70+
}
71+
}
72+
73+
func dataSourceStorageProfileRead(d *schema.ResourceData, meta interface{}) error {
74+
log.Printf("Reading the vra_storage_profile data source with filter %s", d.Get("filter"))
75+
apiClient := meta.(*Client).apiClient
76+
77+
var storageProfile *models.StorageProfile
78+
79+
id := d.Get("id").(string)
80+
filter := d.Get("filter").(string)
81+
82+
if id == "" && filter == "" {
83+
return fmt.Errorf("one of id or filter is required")
84+
}
85+
86+
if id != "" {
87+
getResp, err := apiClient.StorageProfile.GetStorageProfile(storage_profile.NewGetStorageProfileParams().WithID(id))
88+
89+
if err != nil {
90+
return err
91+
}
92+
storageProfile = getResp.GetPayload()
93+
} else {
94+
getResp, err := apiClient.StorageProfile.GetStorageProfiles(storage_profile.NewGetStorageProfilesParams().WithDollarFilter(withString(filter)))
95+
if err != nil {
96+
return err
97+
}
98+
99+
storageProfiles := *getResp.Payload
100+
if len(storageProfiles.Content) > 1 {
101+
return fmt.Errorf("vra_storage_profile must filter to a storage profile")
102+
}
103+
if len(storageProfiles.Content) == 0 {
104+
return fmt.Errorf("vra_storage_profile filter did not match any storage profile")
105+
}
106+
107+
storageProfile = storageProfiles.Content[0]
108+
}
109+
110+
d.SetId(*storageProfile.ID)
111+
d.Set("created_at", storageProfile.CreatedAt)
112+
d.Set("default_item", storageProfile.DefaultItem)
113+
d.Set("description", storageProfile.Description)
114+
d.Set("disk_properties", storageProfile.DiskProperties)
115+
d.Set("external_region_id", storageProfile.ExternalRegionID)
116+
d.Set("name", storageProfile.Name)
117+
d.Set("org_id", storageProfile.OrgID)
118+
d.Set("owner", storageProfile.Owner)
119+
d.Set("supports_encryption", storageProfile.SupportsEncryption)
120+
d.Set("updated_at", storageProfile.UpdatedAt)
121+
122+
if err := d.Set("tags", flattenTags(storageProfile.Tags)); err != nil {
123+
return fmt.Errorf("error setting storage profile tags - error: %v", err)
124+
}
125+
126+
if err := d.Set("links", flattenLinks(storageProfile.Links)); err != nil {
127+
return fmt.Errorf("error setting storage profile links - error: %#v", err)
128+
}
129+
130+
log.Printf("Finished reading the vra_storage_profile data source with filter %s", d.Get("filter"))
131+
return nil
132+
}
+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package vra
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
10+
"testing"
11+
)
12+
13+
func TestAccDataSourceVRAStorageProfile(t *testing.T) {
14+
rInt := acctest.RandInt()
15+
resourceName1 := "vra_storage_profile.this"
16+
dataSourceName1 := "data.vra_storage_profile.this"
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheckStorageProfile(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testAccCheckVRAStorageProfileDestroy,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourceVRAStorageProfileNotFound(rInt),
25+
ExpectError: regexp.MustCompile("vra_storage_profile filter did not match any storage profile"),
26+
},
27+
// TBD: Enable filter by name once this is fixed https://jira.eng.vmware.com/browse/VCOM-13947
28+
// {
29+
// Config: testAccDataSourceVRAStorageProfileNameFilter(rInt),
30+
// Check: resource.ComposeTestCheckFunc(
31+
// resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
32+
// resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
33+
// resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
34+
// resource.TestCheckResourceAttrPair(resourceName1, "default_item", dataSourceName1, "default_item"),
35+
// resource.TestCheckResourceAttrPair(resourceName1, "external_region_id", dataSourceName1, "region_id"),
36+
// ),
37+
// },
38+
{
39+
Config: testAccDataSourceVRAStorageProfileExternalRegionIDFilter(rInt),
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
42+
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
43+
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
44+
resource.TestCheckResourceAttrPair(resourceName1, "default_item", dataSourceName1, "default_item"),
45+
resource.TestCheckResourceAttrPair(resourceName1, "external_region_id", dataSourceName1, "external_region_id"),
46+
),
47+
},
48+
{
49+
Config: testAccDataSourceVRAStorageProfileByID(rInt),
50+
Check: resource.ComposeTestCheckFunc(
51+
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
52+
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
53+
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
54+
resource.TestCheckResourceAttrPair(resourceName1, "default_item", dataSourceName1, "default_item"),
55+
resource.TestCheckResourceAttrPair(resourceName1, "external_region_id", dataSourceName1, "external_region_id"),
56+
),
57+
},
58+
},
59+
})
60+
}
61+
62+
func testAccDataSourceVRAStorageProfileNotFound(rInt int) string {
63+
return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
64+
data "vra_storage_profile" "this" {
65+
filter = "externalRegionId eq 'foobar'"
66+
}`)
67+
}
68+
69+
// TBD: Enable filter by name once this is fixed https://jira.eng.vmware.com/browse/VCOM-13947
70+
// func testAccDataSourceVRAStorageProfileNameFilter(rInt int) string {
71+
// return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
72+
// data "vra_storage_profile" "this" {
73+
// filter = "name eq '${vra_storage_profile.my-storage-profile.name}'"
74+
// }`)
75+
// }
76+
77+
func testAccDataSourceVRAStorageProfileExternalRegionIDFilter(rInt int) string {
78+
return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
79+
data "vra_storage_profile" "this" {
80+
filter = "externalRegionId eq '${data.vra_region.this.id}'"
81+
}`)
82+
}
83+
84+
func testAccDataSourceVRAStorageProfileByID(rInt int) string {
85+
return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
86+
data "vra_storage_profile" "this" {
87+
id = vra_storage_profile.this.id
88+
}`)
89+
}

vra/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func Provider() *schema.Provider {
5858
"vra_region": dataSourceRegion(),
5959
"vra_region_enumeration": dataSourceRegionEnumeration(),
6060
"vra_security_group": dataSourceSecurityGroup(),
61+
"vra_storage_profile": dataSourceStorageProfile(),
6162
"vra_zone": dataSourceZone(),
6263
},
6364

vra/provider_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ func testAccPreCheckImageProfile(t *testing.T) {
108108
}
109109
}
110110

111+
func testAccPreCheckStorageProfile(t *testing.T) {
112+
if os.Getenv("VRA_REFRESH_TOKEN") == "" && os.Getenv("VRA_ACCESS_TOKEN") == "" {
113+
t.Fatal("VRA_REFRESH_TOKEN or VRA_ACCESS_TOKEN must be set for acceptance tests")
114+
}
115+
116+
envVars := [...]string{
117+
"VRA_URL",
118+
"VRA_REGION",
119+
}
120+
121+
for _, name := range envVars {
122+
if v := os.Getenv(name); v == "" {
123+
t.Fatalf("%s must be set for acceptance tests\n", name)
124+
}
125+
}
126+
}
127+
111128
func testAccPreCheckAWS(t *testing.T) {
112129
if v := os.Getenv("VRA_URL"); v == "" {
113130
t.Fatal("VRA_URL must be set for acceptance tests")

vra/resource_storage_profile_test.go

+38-28
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@ package vra
33
import (
44
"fmt"
55
"os"
6+
"regexp"
7+
"strconv"
68
"testing"
79

10+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
811
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
912
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1013
"github.com/vmware/vra-sdk-go/pkg/client/cloud_account"
1114
"github.com/vmware/vra-sdk-go/pkg/client/storage_profile"
1215
)
1316

1417
func TestAccVRAStorageProfileBasic(t *testing.T) {
18+
rInt := acctest.RandInt()
1519
resource.Test(t, resource.TestCase{
16-
PreCheck: func() { testAccPreCheckAWS(t) },
20+
PreCheck: func() { testAccPreCheckStorageProfile(t) },
1721
Providers: testAccProviders,
1822
CheckDestroy: testAccCheckVRAStorageProfileDestroy,
1923
Steps: []resource.TestStep{
2024
{
21-
Config: testAccCheckVRAStorageProfileConfig(),
25+
Config: testAccCheckVRAStorageProfileConfig(rInt),
2226
Check: resource.ComposeAggregateTestCheckFunc(
23-
testAccCheckVRAStorageProfileExists("vra_storage_profile.my-storage-profile"),
27+
testAccCheckVRAStorageProfileExists("vra_storage_profile.this"),
28+
resource.TestMatchResourceAttr(
29+
"vra_storage_profile.this", "name", regexp.MustCompile("^my-profile-"+strconv.Itoa(rInt))),
2430
resource.TestCheckResourceAttr(
25-
"vra_storage_profile.my-storage-profile", "name", "my-vra-storage-profile"),
31+
"vra_storage_profile.this", "description", "my storage profile"),
2632
resource.TestCheckResourceAttr(
27-
"vra_storage_profile.my-storage-profile", "description", "my storage profile"),
33+
"vra_storage_profile.this", "default_item", "true"),
2834
resource.TestCheckResourceAttr(
29-
"vra_storage_profile.my-storage-profile", "default_item", "true"),
35+
"vra_storage_profile.this", "external_region_id", os.Getenv("VRA_REGION")),
3036
),
3137
},
3238
},
@@ -69,34 +75,38 @@ func testAccCheckVRAStorageProfileDestroy(s *terraform.State) error {
6975
return nil
7076
}
7177

72-
func testAccCheckVRAStorageProfileConfig() string {
73-
// Need valid credentials since this is creating a real cloud account
78+
func testAccCheckVRAStorageProfileConfig(rInt int) string {
79+
// Need valid credentials since this is creating a real cloud account and network profile
7480
id := os.Getenv("VRA_AWS_ACCESS_KEY_ID")
7581
secret := os.Getenv("VRA_AWS_SECRET_ACCESS_KEY")
82+
region := os.Getenv("VRA_REGION")
7683
return fmt.Sprintf(`
77-
resource "vra_cloud_account_aws" "my-cloud-account" {
78-
name = "my-cloud-account"
79-
description = "test cloud account"
80-
access_key = "%s"
81-
secret_key = "%s"
82-
regions = ["us-east-1"]
83-
}
84+
resource "vra_cloud_account_aws" "this" {
85+
name = "my-cloud-account-%d"
86+
description = "test cloud account"
87+
access_key = "%s"
88+
secret_key = "%s"
89+
regions = ["%s"]
90+
}
8491
85-
data "vra_region" "us-east-1-region" {
86-
cloud_account_id = "${vra_cloud_account_aws.my-cloud-account.id}"
87-
region = "us-east-1"
88-
}
92+
data "vra_region" "this" {
93+
cloud_account_id = "${vra_cloud_account_aws.this.id}"
94+
region = "%s"
95+
}
8996
90-
resource "vra_zone" "my-zone" {
91-
name = "my-vra-zone"
92-
description = "description my-vra-zone"
93-
region_id = "${data.vra_region.us-east-1-region.id}"
94-
}
97+
resource "vra_zone" "this" {
98+
name = "my-vra-zone-%d"
99+
description = "description my-vra-zone"
100+
region_id = "${data.vra_region.this.id}"
101+
}
95102
96-
resource "vra_storage_profile" "my-storage-profile" {
97-
name = "my-vra-storage-profile"
103+
resource "vra_storage_profile" "this" {
104+
name = "my-profile-%d"
98105
description = "my storage profile"
99-
region_id = "${data.vra_region.us-east-1-region.id}"
106+
region_id = "${data.vra_region.this.id}"
100107
default_item = true
101-
}`, id, secret)
108+
disk_properties = {
109+
deviceType = "instance-store"
110+
}
111+
}`, rInt, id, secret, region, region, rInt, rInt)
102112
}

0 commit comments

Comments
 (0)