Skip to content

Commit

Permalink
Fix Content Library resource and Region Storage Policy data source (#…
Browse files Browse the repository at this point in the history
…1349)

Signed-off-by: abarreiro <[email protected]>
  • Loading branch information
adambarreiro authored Nov 11, 2024
1 parent 9df0deb commit fe24686
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 97 deletions.
6 changes: 3 additions & 3 deletions .changes/v4.0.0/1339-features.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
* **New Data Source:** `vcd_tm_region_storage_policy` to read Region Storage Policies [GH-1339]
* **New Resource:** `vcd_tm_content_library` to manage Content Libraries [GH-1339]
* **New Data Source:** `vcd_tm_content_library` to read Content Libraries [GH-1339]
* **New Data Source:** `vcd_tm_region_storage_policy` to read Region Storage Policies [GH-1339, GH-1349]
* **New Resource:** `vcd_tm_content_library` to manage Content Libraries [GH-1339, GH-1349]
* **New Data Source:** `vcd_tm_content_library` to read Content Libraries [GH-1339, GH-1349]
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/kr/pretty v0.3.1
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.5
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.6
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.5 h1:loocPkclRMoVpiCSBWjs7BSbxj453NzqV+qoZwZvedQ=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.5/go.mod h1:68KHsVns52dsq/w5JQYzauaU/+NAi1FmCxhBrFc/VoQ=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.6 h1:gCv8L2S9kIbgIiwmiX3Pip+BChuyMAB/XqSKUsFF3Og=
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.6/go.mod h1:68KHsVns52dsq/w5JQYzauaU/+NAi1FmCxhBrFc/VoQ=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
3 changes: 2 additions & 1 deletion vcd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ type TestConfig struct {
MaxRetryTimeout int `json:"maxRetryTimeout"`
} `json:"provider"`
Tm struct {
Org string `json:"org"` // temporary field to make skipIfNotTm work
Org string `json:"org"` // temporary field to make skipIfNotTm work
CreateRegion bool `json:"createRegion"`
Region string `json:"region"` //
RegionStoragePolicy string `json:"regionStoragePolicy"` //
Vdc string `json:"vdc"` //
Expand Down
4 changes: 2 additions & 2 deletions vcd/datasource_vcd_tm_content_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func datasourceVcdTmContentLibrary() *schema.Resource {
Required: true,
Description: "The name of the Content Library",
},
"storage_policy_ids": {
"storage_class_ids": {
Type: schema.TypeSet,
Computed: true,
Description: "A set of Region Storage Policy or VDC Storage Policy IDs used by this Content Library",
Description: "A set of storage class IDs used by this Content Library",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand Down
60 changes: 49 additions & 11 deletions vcd/datasource_vcd_tm_region_storage_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,87 @@ package vcd

import (
"context"
"fmt"
"github.com/vmware/go-vcloud-director/v3/types/v56"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const labelTmRegionStoragePolicy = "Region Storage Policy"

func datasourceVcdTmRegionStoragePolicy() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdTmRegionStoragePolicyRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Region Storage Policy name",
Description: fmt.Sprintf("%s name", labelTmRegionStoragePolicy),
},
"description": {
"region_id": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the Region Storage Policy",
Required: true,
Description: fmt.Sprintf("The Region that this %s belongs to", labelTmRegionStoragePolicy),
},
"region_id": {
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The Region that this Region Storage Policy belongs to",
Description: fmt.Sprintf("Description of the %s", labelTmRegionStoragePolicy),
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The creation status of the Region Storage Policy. Can be [NOT_READY, READY]",
Description: fmt.Sprintf("The creation status of the %s. Can be [NOT_READY, READY]", labelTmRegionStoragePolicy),
},
"storage_capacity_mb": {
Type: schema.TypeInt,
Computed: true,
Description: "Storage capacity in megabytes for this Region Storage Policy",
Description: fmt.Sprintf("Storage capacity in megabytes for this %s", labelTmRegionStoragePolicy),
},
"storage_consumed_mb": {
Type: schema.TypeInt,
Computed: true,
Description: "Consumed storage in megabytes for this Region Storage Policy",
Description: fmt.Sprintf("Consumed storage in megabytes for this %s", labelTmRegionStoragePolicy),
},
},
}
}

func datasourceVcdTmRegionStoragePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return genericVcdTmRegionStoragePolicyRead(ctx, d, meta, "datasource")
func datasourceVcdTmRegionStoragePolicyRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
regionId := d.Get("region_id").(string)
region, err := vcdClient.GetRegionById(regionId)
if err != nil {
return diag.Errorf("error retrieving Region with ID '%s': %s", regionId, err)
}

rspName := d.Get("name").(string)
rsp, err := region.GetStoragePolicyByName(rspName)
if err != nil {
return diag.Errorf("error retrieving Region Storage Policy '%s': %s", rspName, err)
}

err = setRegionStoragePolicyData(d, rsp.RegionStoragePolicy)
if err != nil {
return diag.Errorf("error saving Region Storage Policy data into state: %s", err)
}

d.SetId(rsp.RegionStoragePolicy.ID)
return nil
}

func setRegionStoragePolicyData(d *schema.ResourceData, rsp *types.RegionStoragePolicy) error {
dSet(d, "name", rsp.Name)
dSet(d, "description", rsp.Description)
regionId := ""
if rsp.Region != nil {
regionId = rsp.Region.ID
}
dSet(d, "region_id", regionId)
dSet(d, "storage_capacity_mb", rsp.StorageCapacityMB)
dSet(d, "storage_consumed_mb", rsp.StorageConsumedMB)
dSet(d, "status", rsp.Status)

return nil
}
2 changes: 1 addition & 1 deletion vcd/datasource_vcd_vcenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func TestAccVcdVcenterDS(t *testing.T) {
const datasourceTestVcenter = `
data "vcd_vcenter" "vc" {
name = "{{.Vcenter}}"
}
}
`
28 changes: 15 additions & 13 deletions vcd/resource_vcd_tm_content_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vcd
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -26,11 +27,11 @@ func resourceVcdTmContentLibrary() *schema.Resource {
ForceNew: true, // TODO: TM: Update not supported
Description: "The name of the Content Library",
},
"storage_policy_ids": {
"storage_class_ids": {
Type: schema.TypeSet,
Required: true,
ForceNew: true, // TODO: TM: Update not supported
Description: "A set of Region Storage Policy IDs or VDC Storage Policy IDs used by this Content Library",
Description: "A set of storage class IDs used by this Content Library",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Expand Down Expand Up @@ -130,7 +131,7 @@ func resourceVcdTmContentLibraryCreate(ctx context.Context, d *schema.ResourceDa
return diag.Errorf("error creating Content Library: %s", err)
}

d.SetId(cl.ContentLibrary.Id)
d.SetId(cl.ContentLibrary.ID)

return resourceVcdTmContentLibraryRead(ctx, d, meta)
}
Expand Down Expand Up @@ -181,7 +182,7 @@ func genericVcdTmContentLibraryRead(_ context.Context, d *schema.ResourceData, m
return diag.Errorf("error saving Content Library data into state: %s", err)
}

d.SetId(cl.ContentLibrary.Id)
d.SetId(cl.ContentLibrary.ID)
return nil
}

Expand All @@ -207,17 +208,17 @@ func resourceVcdTmContentLibraryImport(_ context.Context, d *schema.ResourceData
return nil, fmt.Errorf("error retrieving Content Library with name '%s': %s", d.Id(), err)
}

d.SetId(rsp.ContentLibrary.Id)
d.SetId(rsp.ContentLibrary.ID)
dSet(d, "name", rsp.ContentLibrary.Name)
return []*schema.ResourceData{d}, nil
}

func getContentLibraryType(d *schema.ResourceData) (*types.ContentLibrary, error) {
t := &types.ContentLibrary{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
AutoAttach: d.Get("auto_attach").(bool),
StoragePolicies: convertSliceOfStringsToOpenApiReferenceIds(convertTypeListToSliceOfStrings(d.Get("storage_policy_ids").(*schema.Set).List())),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
AutoAttach: d.Get("auto_attach").(bool),
StorageClasses: convertSliceOfStringsToOpenApiReferenceIds(convertTypeListToSliceOfStrings(d.Get("storage_class_ids").(*schema.Set).List())),
}
if v, ok := d.GetOk("subscription_config"); ok {
subsConfig := v.([]interface{})[0].(map[string]interface{})
Expand All @@ -243,11 +244,12 @@ func setTmContentLibraryData(d *schema.ResourceData, cl *types.ContentLibrary) e
dSet(d, "owner_org_id", cl.Org.ID)
}

sps := make([]string, len(cl.StoragePolicies))
for i, sp := range cl.StoragePolicies {
sps[i] = sp.ID
scs := make([]string, len(cl.StorageClasses))
for i, sc := range cl.StorageClasses {
// TODO: TM: When vcd_region_storage_policy data source starts using :storageClass: UUID, we can get rid of this
scs[i] = strings.ReplaceAll(sc.ID, "storageClass", "regionStoragePolicy")
}
err := d.Set("storage_policy_ids", sps)
err := d.Set("storage_class_ids", scs)
if err != nil {
return err
}
Expand Down
19 changes: 14 additions & 5 deletions vcd/resource_vcd_tm_content_library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package vcd

import (
"fmt"
"regexp"
"testing"

Expand All @@ -15,16 +16,23 @@ func TestAccVcdTmContentLibrary(t *testing.T) {
skipIfNotSysAdmin(t)
skipIfNotTm(t)

vCenterHcl, vCenterHclRef := getVCenterHcl(t)
nsxManagerHcl, nsxManagerHclRef := getNsxManagerHcl(t)
regionHcl, regionHclRef := getRegionHcl(t, vCenterHclRef, nsxManagerHclRef)

var params = StringMap{
"Name": t.Name(),
"RegionId": fmt.Sprintf("%s.id", regionHclRef),
"RegionStoragePolicy": testConfig.Tm.RegionStoragePolicy,
"Tags": "tm",
}
testParamsNotEmpty(t, params)

configText1 := templateFill(testAccVcdTmContentLibraryStep1, params)
preRequisites := vCenterHcl + nsxManagerHcl + regionHcl

configText1 := templateFill(preRequisites+testAccVcdTmContentLibraryStep1, params)
params["FuncName"] = t.Name() + "-step2"
configText2 := templateFill(testAccVcdTmContentLibraryStep2, params)
configText2 := templateFill(preRequisites+testAccVcdTmContentLibraryStep2, params)

debugPrintf("#[DEBUG] CONFIGURATION step1: %s\n", configText1)
debugPrintf("#[DEBUG] CONFIGURATION step2: %s\n", configText2)
Expand All @@ -43,7 +51,7 @@ func TestAccVcdTmContentLibrary(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", t.Name()),
resource.TestCheckResourceAttr(resourceName, "description", t.Name()),
resource.TestCheckResourceAttr(resourceName, "storage_policy_ids.#", "1"),
resource.TestCheckResourceAttr(resourceName, "storage_class_ids.#", "1"),
resource.TestCheckResourceAttr(resourceName, "auto_attach", "true"), // TODO: TM: Test with false
resource.TestCheckResourceAttrSet(resourceName, "creation_date"),
resource.TestCheckResourceAttr(resourceName, "is_shared", "true"), // TODO: TM: Test with false
Expand Down Expand Up @@ -74,13 +82,14 @@ func TestAccVcdTmContentLibrary(t *testing.T) {

const testAccVcdTmContentLibraryStep1 = `
data "vcd_tm_region_storage_policy" "sp" {
name = "{{.RegionStoragePolicy}}"
region_id = {{.RegionId}}
name = "{{.RegionStoragePolicy}}"
}
resource "vcd_tm_content_library" "cl" {
name = "{{.Name}}"
description = "{{.Name}}"
storage_policy_ids = [
storage_class_ids = [
data.vcd_tm_region_storage_policy.sp.id
]
}
Expand Down
51 changes: 0 additions & 51 deletions vcd/resource_vcd_tm_region_storage_policy.go

This file was deleted.

18 changes: 18 additions & 0 deletions vcd/sample_vcd_test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,23 @@
"edgeGateway": "tenant_edgegateway",
"ovaCatalog": "tkgm_catalog",
"ovaName": ""
},
"tm": {
"org": "tf-test",
"createRegion": true,
"region": "three-zone-region",
"regionStoragePolicy": "vSAN Default Storage Policy",
"vdc": "three-zone-vdc",
"createVcenter": true,
"vcenterUsername": "[email protected]",
"vcenterPassword": "",
"vcenterUrl": "https://vcenter.my-company.com",
"vcenterStorageProfile": "vSAN Default Storage Policy",
"vcenterSupervisor": "supervisor1",
"vcenterSupervisorZone": "zone1",
"createNsxtManager": true,
"nsxtManagerUsername": "admin",
"nsxtManagerPassword": "",
"nsxtManagerUrl": "https://nsxmanager.my-company.com"
}
}
Loading

0 comments on commit fe24686

Please sign in to comment.