-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add data source storage profile (#111)
Signed-off-by: Prativa Bawri <[email protected]>
- Loading branch information
Showing
5 changed files
with
277 additions
and
28 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,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 | ||
} |
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,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 | ||
}`) | ||
} |
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
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
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