Skip to content

Commit fbc5d8a

Browse files
authored
Add vcfa_org_vdc resource and data source + vcfa_region_zone data source (#9)
Signed-off-by: abarreiro <abarreiro@vmware.com>
1 parent ab82547 commit fbc5d8a

19 files changed

+914
-76
lines changed

.changes/v1.0.0/9-features.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- **New Resource:** `vcfa_org_vdc` to manage Organization VDCs (Region Quotas) [GH-9]
2+
- **New Data Source:** `vcfa_org_vdc` to read Organization VDCs (Region Quotas) [GH-9]
3+
- **New Data Source:** `vcfa_region_zone` to read Region Zones [GH-9]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.3
55
require (
66
github.com/hashicorp/go-version v1.7.0
77
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
8-
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.17
8+
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.18
99
)
1010

1111
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
149149
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
150150
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
151151
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
152-
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.17 h1:fvM3DH708LrdTsfrSwAmq5lAssCcBhM6Zb1nfVCi5YU=
153-
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.17/go.mod h1:68KHsVns52dsq/w5JQYzauaU/+NAi1FmCxhBrFc/VoQ=
152+
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.18 h1:0mqWqVNRXdAW3zsgP4rYQ1CfIIHIE58+dsdVISXblVY=
153+
github.com/vmware/go-vcloud-director/v3 v3.0.0-alpha.18/go.mod h1:68KHsVns52dsq/w5JQYzauaU/+NAi1FmCxhBrFc/VoQ=
154154
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
155155
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
156156
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

scripts/staticcheck-config.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export STATICCHECK_URL=https://github.com/dominikh/go-tools/releases/download
2+
export STATICCHECK_VERSION=2023.1.7
3+
export STATICCHECK_FILE=staticcheck_linux_amd64.tar.gz

vcfa/datasource_not_found_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func testSpecificDataSourceNotFound(dataSourceName string, vcdClient *VCDClient)
4545
reason: "Data source vcfa_tm_version always returns data, it is not possible to get ENF",
4646
},
4747
{
48-
// TODO: TM: Retrieving non-existent Supervisor by ID returns 404 and not ENF
48+
// TODO: TM: Retrieving non-existent Supervisor by ID returns 400 and not ENF
4949
dataSourceName: "vcfa_supervisor_zone",
50-
reason: "TODO: TM: Retrieving non-existent Supervisor by ID returns 404 and not ENF",
50+
reason: "TODO: TM: Retrieving non-existent Supervisor by ID returns 400 and not ENF",
5151
},
5252
}
5353
for _, skip := range skipAlwaysSlice {
@@ -78,6 +78,8 @@ func testSpecificDataSourceNotFound(dataSourceName string, vcdClient *VCDClient)
7878
"vcfa_supervisor_zone",
7979
"vcfa_vcenter",
8080
"vcfa_ip_space",
81+
"vcfa_region_zone",
82+
"vcfa_org_vdc",
8183
}
8284

8385
if contains(dataSourcesRequiringSysAdmin, dataSourceName) && !usingSysAdmin() {
@@ -142,6 +144,8 @@ func addMandatoryParams(dataSourceName string, mandatoryFields []string, t *test
142144
templateFields = templateFields + `vcenter_id = "urn:vcloud:vimserver:12345678-1234-1234-1234-123456789012"` + "\n"
143145
case "region_id":
144146
templateFields = templateFields + `region_id = "urn:vcloud:region:12345678-1234-1234-1234-123456789012"` + "\n"
147+
case "org_id":
148+
templateFields = templateFields + `org_id = "urn:vcloud:org:12345678-1234-1234-1234-123456789012"` + "\n"
145149
}
146150
}
147151

vcfa/datasource_vcfa_org_vdc.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package vcfa
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/vmware/go-vcloud-director/v3/govcd"
10+
"github.com/vmware/go-vcloud-director/v3/types/v56"
11+
)
12+
13+
func datasourceVcfaOrgVdc() *schema.Resource {
14+
return &schema.Resource{
15+
ReadContext: datasourceVcfaOrgVdcRead,
16+
17+
Schema: map[string]*schema.Schema{
18+
"org_id": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
Description: "Parent Organization ID",
22+
},
23+
"region_id": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
Description: "Parent Region ID",
27+
},
28+
"description": {
29+
Type: schema.TypeString,
30+
Computed: true,
31+
Description: fmt.Sprintf("Description of the %s", labelVcfaOrgVdc),
32+
},
33+
"name": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: fmt.Sprintf("Name of the %s", labelVcfaOrgVdc),
37+
},
38+
"supervisor_ids": {
39+
Type: schema.TypeSet,
40+
Computed: true,
41+
Elem: &schema.Schema{Type: schema.TypeString},
42+
Description: fmt.Sprintf("A set of Supervisor IDs that back this %s", labelVcfaOrgVdc),
43+
},
44+
"zone_resource_allocations": {
45+
Type: schema.TypeSet,
46+
Computed: true,
47+
Elem: orgVdcDsZoneResourceAllocation,
48+
Description: "A set of Region Zones and their resource allocations",
49+
},
50+
"status": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
Description: fmt.Sprintf("%s status", labelVcfaOrgVdc),
54+
},
55+
},
56+
}
57+
}
58+
59+
var orgVdcDsZoneResourceAllocation = &schema.Resource{
60+
Schema: map[string]*schema.Schema{
61+
"region_zone_name": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: fmt.Sprintf("%s Name", labelVcfaRegionZone),
65+
},
66+
"region_zone_id": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: fmt.Sprintf("%s ID", labelVcfaRegionZone),
70+
},
71+
"memory_limit_mib": {
72+
Type: schema.TypeInt,
73+
Computed: true,
74+
Description: "Memory limit in MiB",
75+
},
76+
"memory_reservation_mib": {
77+
Type: schema.TypeInt,
78+
Computed: true,
79+
Description: "Memory reservation in MiB",
80+
},
81+
"cpu_limit_mhz": {
82+
Type: schema.TypeInt,
83+
Computed: true,
84+
Description: "CPU limit in MHz",
85+
},
86+
"cpu_reservation_mhz": {
87+
Type: schema.TypeInt,
88+
Computed: true,
89+
Description: "CPU reservation in MHz",
90+
},
91+
},
92+
}
93+
94+
func datasourceVcfaOrgVdcRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
95+
vcdClient := meta.(*VCDClient)
96+
getByNameAndOrgId := func(_ string) (*govcd.TmVdc, error) {
97+
region, err := vcdClient.GetRegionById(d.Get("region_id").(string))
98+
if err != nil {
99+
return nil, err
100+
}
101+
org, err := vcdClient.GetOrgById(d.Get("org_id").(string))
102+
if err != nil {
103+
return nil, err
104+
}
105+
return vcdClient.GetTmVdcByName(fmt.Sprintf("%s_%s", org.Org.Name, region.Region.Name))
106+
}
107+
108+
c := dsReadConfig[*govcd.TmVdc, types.TmVdc]{
109+
entityLabel: labelVcfaOrgVdc,
110+
getEntityFunc: getByNameAndOrgId,
111+
stateStoreFunc: setTmVdcData,
112+
}
113+
return readDatasource(ctx, d, meta, c)
114+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package vcfa
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/vmware/go-vcloud-director/v3/govcd"
10+
"github.com/vmware/go-vcloud-director/v3/types/v56"
11+
)
12+
13+
const labelVcfaRegionZone = "Region Zone"
14+
15+
func datasourceVcfaRegionZone() *schema.Resource {
16+
return &schema.Resource{
17+
ReadContext: resourceVcfaRegionZoneRead,
18+
19+
Schema: map[string]*schema.Schema{
20+
"region_id": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
Description: fmt.Sprintf("Parent Region ID for %s", labelVcfaRegionZone),
24+
},
25+
"name": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
Description: fmt.Sprintf("Name of %s", labelVcfaRegionZone),
29+
},
30+
"memory_limit_mib": {
31+
Type: schema.TypeInt,
32+
Computed: true,
33+
Description: "Memory limit in MiB",
34+
},
35+
"memory_reservation_used_mib": {
36+
Type: schema.TypeInt,
37+
Computed: true,
38+
Description: "Memory reservation in MiB",
39+
},
40+
"memory_reservation_mib": {
41+
Type: schema.TypeInt,
42+
Computed: true,
43+
Description: "Memory reservation in MiB",
44+
},
45+
"cpu_limit_mhz": {
46+
Type: schema.TypeInt,
47+
Computed: true,
48+
Description: "CPU limit in MHz",
49+
},
50+
"cpu_reservation_mhz": {
51+
Type: schema.TypeInt,
52+
Computed: true,
53+
Description: "CPU reservation in MHz",
54+
},
55+
"cpu_reservation_used_mhz": {
56+
Type: schema.TypeInt,
57+
Computed: true,
58+
Description: "CPU reservation in MHz",
59+
},
60+
},
61+
}
62+
}
63+
64+
func resourceVcfaRegionZoneRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
65+
vcdClient := meta.(*VCDClient)
66+
region, err := vcdClient.GetRegionById(d.Get("region_id").(string))
67+
if err != nil {
68+
return diag.Errorf("error retrieving %s: %s", labelVcfaRegion, err)
69+
}
70+
getRegionZone := func(name string) (*govcd.Zone, error) {
71+
return region.GetZoneByName(name)
72+
}
73+
74+
c := dsReadConfig[*govcd.Zone, types.Zone]{
75+
entityLabel: labelVcfaRegionZone,
76+
getEntityFunc: getRegionZone,
77+
stateStoreFunc: setZoneData,
78+
}
79+
return readDatasource(ctx, d, meta, c)
80+
}
81+
82+
func setZoneData(_ *VCDClient, d *schema.ResourceData, z *govcd.Zone) error {
83+
if z == nil || z.Zone == nil {
84+
return fmt.Errorf("nil %s", labelVcfaRegionZone)
85+
}
86+
d.SetId(z.Zone.ID)
87+
dSet(d, "memory_limit_mib", z.Zone.MemoryLimitMiB)
88+
dSet(d, "memory_reservation_used_mib", z.Zone.MemoryReservationUsedMiB)
89+
dSet(d, "memory_reservation_mib", z.Zone.MemoryReservationMiB)
90+
dSet(d, "cpu_limit_mhz", z.Zone.CPULimitMhz)
91+
dSet(d, "cpu_reservation_mhz", z.Zone.CPUReservationMhz)
92+
dSet(d, "cpu_reservation_used_mhz", z.Zone.CPUReservationUsedMhz)
93+
94+
return nil
95+
}

vcfa/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var globalDataSourceMap = map[string]*schema.Resource{
4040
"vcfa_supervisor_zone": datasourceVcfaSupervisorZone(), // 1.0
4141
"vcfa_region": datasourceVcfaRegion(), // 1.0
4242
"vcfa_ip_space": datasourceVcfaIpSpace(), // 1.0
43+
"vcfa_region_zone": datasourceVcfaRegionZone(), // 1.0
44+
"vcfa_org_vdc": datasourceVcfaOrgVdc(), // 1.0
4345
}
4446

4547
var globalResourceMap = map[string]*schema.Resource{
@@ -48,6 +50,7 @@ var globalResourceMap = map[string]*schema.Resource{
4850
"vcfa_nsx_manager": resourceVcfaNsxManager(), // 1.0
4951
"vcfa_region": resourceVcfaRegion(), // 1.0
5052
"vcfa_ip_space": resourceVcfaIpSpace(), // 1.0
53+
"vcfa_org_vdc": resourceVcfaOrgVdc(), // 1.0
5154
}
5255

5356
// Provider returns a terraform.ResourceProvider.

vcfa/remove_leftovers_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ func removeLeftovers(govcdClient *govcd.VCDClient, verbose bool) error {
7070
}
7171
}
7272

73+
// --------------------------------------------------------------
74+
// VDCs
75+
// --------------------------------------------------------------
76+
if govcdClient.Client.IsSysAdmin {
77+
vdcs, err := govcdClient.GetAllTmVdcs(nil)
78+
if err != nil {
79+
return fmt.Errorf("error retrieving VDCs: %s", err)
80+
}
81+
for _, vdc := range vdcs {
82+
toBeDeleted := shouldDeleteEntity(alsoDelete, doNotDelete, vdc.TmVdc.Name, "vcfa_org_vdc", 2, verbose)
83+
if toBeDeleted {
84+
fmt.Printf("\t REMOVING VDC %s\n", vdc.TmVdc.Name)
85+
err := vdc.Delete()
86+
if err != nil {
87+
return fmt.Errorf("error deleting %s '%s': %s", labelVcfaOrgVdc, vdc.TmVdc.Name, err)
88+
}
89+
}
90+
}
91+
}
92+
7393
// --------------------------------------------------------------
7494
// Regions
7595
// --------------------------------------------------------------

vcfa/resource_vcfa_ip_space.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,22 @@ func resourceVcfaIpSpace() *schema.Resource {
6969
Description: "External scope in CIDR format",
7070
},
7171
"default_quota_max_subnet_size": {
72-
Type: schema.TypeString, // Values are 'ints', TypeString + validation is used to handle 0
73-
Required: true,
74-
Description: fmt.Sprintf("Maximum subnet size represented as a prefix length (e.g. 24, 28) in %s", labelVcfaIpSpace),
75-
ValidateFunc: IsIntAndAtLeast(-1),
72+
Type: schema.TypeString, // Values are 'ints', TypeString + validation is used to handle 0
73+
Required: true,
74+
Description: fmt.Sprintf("Maximum subnet size represented as a prefix length (e.g. 24, 28) in %s", labelVcfaIpSpace),
75+
ValidateDiagFunc: IsIntAndAtLeast(-1),
7676
},
7777
"default_quota_max_cidr_count": {
78-
Type: schema.TypeString, // Values are 'ints', TypeString + validation is used to handle 0
79-
Required: true,
80-
Description: fmt.Sprintf("Maximum number of subnets that can be allocated from internal scope in this %s. ('-1' for unlimited)", labelVcfaIpSpace),
81-
ValidateFunc: IsIntAndAtLeast(-1),
78+
Type: schema.TypeString, // Values are 'ints', TypeString + validation is used to handle 0
79+
Required: true,
80+
Description: fmt.Sprintf("Maximum number of subnets that can be allocated from internal scope in this %s. ('-1' for unlimited)", labelVcfaIpSpace),
81+
ValidateDiagFunc: IsIntAndAtLeast(-1),
8282
},
8383
"default_quota_max_ip_count": {
84-
Type: schema.TypeString, // Values are 'ints', TypeString + validation is used to handle 0
85-
Required: true,
86-
Description: fmt.Sprintf("Maximum number of single floating IP addresses that can be allocated from internal scope in this %s. ('-1' for unlimited)", labelVcfaIpSpace),
87-
ValidateFunc: IsIntAndAtLeast(-1),
84+
Type: schema.TypeString, // Values are 'ints', TypeString + validation is used to handle 0
85+
Required: true,
86+
Description: fmt.Sprintf("Maximum number of single floating IP addresses that can be allocated from internal scope in this %s. ('-1' for unlimited)", labelVcfaIpSpace),
87+
ValidateDiagFunc: IsIntAndAtLeast(-1),
8888
},
8989
"internal_scope": {
9090
Type: schema.TypeSet,

0 commit comments

Comments
 (0)