Skip to content

Commit

Permalink
Add data source storage profile (#111)
Browse files Browse the repository at this point in the history
Signed-off-by: Prativa Bawri <[email protected]>
  • Loading branch information
Prativa20 authored and markpeek committed Dec 6, 2019
1 parent a5769a0 commit 684314b
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 28 deletions.
132 changes: 132 additions & 0 deletions vra/data_source_storage_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package vra

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vmware/vra-sdk-go/pkg/client/storage_profile"
"github.com/vmware/vra-sdk-go/pkg/models"

"log"
)

func dataSourceStorageProfile() *schema.Resource {
return &schema.Resource{
Read: dataSourceStorageProfileRead,

Schema: map[string]*schema.Schema{
"default_item": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"filter": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"disk_properties": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"external_region_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"links": linksSchema(),
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"org_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"owner": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"supports_encryption": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"tags": tagsSchema(),
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceStorageProfileRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Reading the vra_storage_profile data source with filter %s", d.Get("filter"))
apiClient := meta.(*Client).apiClient

var storageProfile *models.StorageProfile

id := d.Get("id").(string)
filter := d.Get("filter").(string)

if id == "" && filter == "" {
return fmt.Errorf("one of id or filter is required")
}

if id != "" {
getResp, err := apiClient.StorageProfile.GetStorageProfile(storage_profile.NewGetStorageProfileParams().WithID(id))

if err != nil {
return err
}
storageProfile = getResp.GetPayload()
} else {
getResp, err := apiClient.StorageProfile.GetStorageProfiles(storage_profile.NewGetStorageProfilesParams().WithDollarFilter(withString(filter)))
if err != nil {
return err
}

storageProfiles := *getResp.Payload
if len(storageProfiles.Content) > 1 {
return fmt.Errorf("vra_storage_profile must filter to a storage profile")
}
if len(storageProfiles.Content) == 0 {
return fmt.Errorf("vra_storage_profile filter did not match any storage profile")
}

storageProfile = storageProfiles.Content[0]
}

d.SetId(*storageProfile.ID)
d.Set("created_at", storageProfile.CreatedAt)
d.Set("default_item", storageProfile.DefaultItem)
d.Set("description", storageProfile.Description)
d.Set("disk_properties", storageProfile.DiskProperties)
d.Set("external_region_id", storageProfile.ExternalRegionID)
d.Set("name", storageProfile.Name)
d.Set("org_id", storageProfile.OrgID)
d.Set("owner", storageProfile.Owner)
d.Set("supports_encryption", storageProfile.SupportsEncryption)
d.Set("updated_at", storageProfile.UpdatedAt)

if err := d.Set("tags", flattenTags(storageProfile.Tags)); err != nil {
return fmt.Errorf("error setting storage profile tags - error: %v", err)
}

if err := d.Set("links", flattenLinks(storageProfile.Links)); err != nil {
return fmt.Errorf("error setting storage profile links - error: %#v", err)
}

log.Printf("Finished reading the vra_storage_profile data source with filter %s", d.Get("filter"))
return nil
}
89 changes: 89 additions & 0 deletions vra/data_source_storage_profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package vra

import (
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

"testing"
)

func TestAccDataSourceVRAStorageProfile(t *testing.T) {
rInt := acctest.RandInt()
resourceName1 := "vra_storage_profile.this"
dataSourceName1 := "data.vra_storage_profile.this"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckStorageProfile(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVRAStorageProfileDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVRAStorageProfileNotFound(rInt),
ExpectError: regexp.MustCompile("vra_storage_profile filter did not match any storage profile"),
},
// TBD: Enable filter by name once this is fixed https://jira.eng.vmware.com/browse/VCOM-13947
// {
// Config: testAccDataSourceVRAStorageProfileNameFilter(rInt),
// Check: resource.ComposeTestCheckFunc(
// resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
// resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
// resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
// resource.TestCheckResourceAttrPair(resourceName1, "default_item", dataSourceName1, "default_item"),
// resource.TestCheckResourceAttrPair(resourceName1, "external_region_id", dataSourceName1, "region_id"),
// ),
// },
{
Config: testAccDataSourceVRAStorageProfileExternalRegionIDFilter(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
resource.TestCheckResourceAttrPair(resourceName1, "default_item", dataSourceName1, "default_item"),
resource.TestCheckResourceAttrPair(resourceName1, "external_region_id", dataSourceName1, "external_region_id"),
),
},
{
Config: testAccDataSourceVRAStorageProfileByID(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName1, "id", dataSourceName1, "id"),
resource.TestCheckResourceAttrPair(resourceName1, "description", dataSourceName1, "description"),
resource.TestCheckResourceAttrPair(resourceName1, "name", dataSourceName1, "name"),
resource.TestCheckResourceAttrPair(resourceName1, "default_item", dataSourceName1, "default_item"),
resource.TestCheckResourceAttrPair(resourceName1, "external_region_id", dataSourceName1, "external_region_id"),
),
},
},
})
}

func testAccDataSourceVRAStorageProfileNotFound(rInt int) string {
return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
data "vra_storage_profile" "this" {
filter = "externalRegionId eq 'foobar'"
}`)
}

// TBD: Enable filter by name once this is fixed https://jira.eng.vmware.com/browse/VCOM-13947
// func testAccDataSourceVRAStorageProfileNameFilter(rInt int) string {
// return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
// data "vra_storage_profile" "this" {
// filter = "name eq '${vra_storage_profile.my-storage-profile.name}'"
// }`)
// }

func testAccDataSourceVRAStorageProfileExternalRegionIDFilter(rInt int) string {
return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
data "vra_storage_profile" "this" {
filter = "externalRegionId eq '${data.vra_region.this.id}'"
}`)
}

func testAccDataSourceVRAStorageProfileByID(rInt int) string {
return testAccCheckVRAStorageProfileConfig(rInt) + fmt.Sprintf(`
data "vra_storage_profile" "this" {
id = vra_storage_profile.this.id
}`)
}
1 change: 1 addition & 0 deletions vra/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Provider() *schema.Provider {
"vra_region": dataSourceRegion(),
"vra_region_enumeration": dataSourceRegionEnumeration(),
"vra_security_group": dataSourceSecurityGroup(),
"vra_storage_profile": dataSourceStorageProfile(),
"vra_zone": dataSourceZone(),
},

Expand Down
17 changes: 17 additions & 0 deletions vra/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ func testAccPreCheckImageProfile(t *testing.T) {
}
}

func testAccPreCheckStorageProfile(t *testing.T) {
if os.Getenv("VRA_REFRESH_TOKEN") == "" && os.Getenv("VRA_ACCESS_TOKEN") == "" {
t.Fatal("VRA_REFRESH_TOKEN or VRA_ACCESS_TOKEN must be set for acceptance tests")
}

envVars := [...]string{
"VRA_URL",
"VRA_REGION",
}

for _, name := range envVars {
if v := os.Getenv(name); v == "" {
t.Fatalf("%s must be set for acceptance tests\n", name)
}
}
}

func testAccPreCheckAWS(t *testing.T) {
if v := os.Getenv("VRA_URL"); v == "" {
t.Fatal("VRA_URL must be set for acceptance tests")
Expand Down
66 changes: 38 additions & 28 deletions vra/resource_storage_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@ package vra
import (
"fmt"
"os"
"regexp"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/vmware/vra-sdk-go/pkg/client/cloud_account"
"github.com/vmware/vra-sdk-go/pkg/client/storage_profile"
)

func TestAccVRAStorageProfileBasic(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckAWS(t) },
PreCheck: func() { testAccPreCheckStorageProfile(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVRAStorageProfileDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckVRAStorageProfileConfig(),
Config: testAccCheckVRAStorageProfileConfig(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVRAStorageProfileExists("vra_storage_profile.my-storage-profile"),
testAccCheckVRAStorageProfileExists("vra_storage_profile.this"),
resource.TestMatchResourceAttr(
"vra_storage_profile.this", "name", regexp.MustCompile("^my-profile-"+strconv.Itoa(rInt))),
resource.TestCheckResourceAttr(
"vra_storage_profile.my-storage-profile", "name", "my-vra-storage-profile"),
"vra_storage_profile.this", "description", "my storage profile"),
resource.TestCheckResourceAttr(
"vra_storage_profile.my-storage-profile", "description", "my storage profile"),
"vra_storage_profile.this", "default_item", "true"),
resource.TestCheckResourceAttr(
"vra_storage_profile.my-storage-profile", "default_item", "true"),
"vra_storage_profile.this", "external_region_id", os.Getenv("VRA_REGION")),
),
},
},
Expand Down Expand Up @@ -69,34 +75,38 @@ func testAccCheckVRAStorageProfileDestroy(s *terraform.State) error {
return nil
}

func testAccCheckVRAStorageProfileConfig() string {
// Need valid credentials since this is creating a real cloud account
func testAccCheckVRAStorageProfileConfig(rInt int) string {
// Need valid credentials since this is creating a real cloud account and network profile
id := os.Getenv("VRA_AWS_ACCESS_KEY_ID")
secret := os.Getenv("VRA_AWS_SECRET_ACCESS_KEY")
region := os.Getenv("VRA_REGION")
return fmt.Sprintf(`
resource "vra_cloud_account_aws" "my-cloud-account" {
name = "my-cloud-account"
description = "test cloud account"
access_key = "%s"
secret_key = "%s"
regions = ["us-east-1"]
}
resource "vra_cloud_account_aws" "this" {
name = "my-cloud-account-%d"
description = "test cloud account"
access_key = "%s"
secret_key = "%s"
regions = ["%s"]
}
data "vra_region" "us-east-1-region" {
cloud_account_id = "${vra_cloud_account_aws.my-cloud-account.id}"
region = "us-east-1"
}
data "vra_region" "this" {
cloud_account_id = "${vra_cloud_account_aws.this.id}"
region = "%s"
}
resource "vra_zone" "my-zone" {
name = "my-vra-zone"
description = "description my-vra-zone"
region_id = "${data.vra_region.us-east-1-region.id}"
}
resource "vra_zone" "this" {
name = "my-vra-zone-%d"
description = "description my-vra-zone"
region_id = "${data.vra_region.this.id}"
}
resource "vra_storage_profile" "my-storage-profile" {
name = "my-vra-storage-profile"
resource "vra_storage_profile" "this" {
name = "my-profile-%d"
description = "my storage profile"
region_id = "${data.vra_region.us-east-1-region.id}"
region_id = "${data.vra_region.this.id}"
default_item = true
}`, id, secret)
disk_properties = {
deviceType = "instance-store"
}
}`, rInt, id, secret, region, region, rInt, rInt)
}

0 comments on commit 684314b

Please sign in to comment.