Skip to content

Commit 4092731

Browse files
authored
Allow region data source to lookup by filter and name (#301)
vra_region data source allows filter argument to lookup only by externalRegionId and cloudAccountId. If there are more than one results that match the filter criteria, users can use the 'name' argument to further filter the results to the first one in the list of results. Signed-off-by: Deepak Mettem <[email protected]>
1 parent 18185ef commit 4092731

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

examples/infrastructure_setup/vmc/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ resource "null_resource" "delay" {
5050
data "vra_region" "this" {
5151
// Important thing to note is the cloud_account_id in the filter criteria is ne (i.e. not equal) to VMC cloud account id
5252
filter = "externalRegionId eq '${var.external_region_id}' and cloudAccountId ne '${vra_cloud_account_vmc.this.id}'"
53+
name = var.region_name // Use 'name' argument in case there are other vCenter regions that have same external region id
5354

54-
// filter = "name eq 'SDDC-Datacenter' and externalRegionId eq '${var.external_region_id}' and cloud_account_id ne vra_cloud_account_vmc.vmc_cloud_account.id" // Important thing to note is the cloud_account_id in the filter criteria is ne (i.e. not equal) to VMC cloud account id.
55+
# filter by 'name' is not supported yet. Use the 'name' argument instead until it is supported
56+
# filter = "name eq 'SDDC-Datacenter' and externalRegionId eq '${var.external_region_id}' and cloudAccountId ne vra_cloud_account_vmc.vmc_cloud_account.id"
5557
}
5658

5759
resource "vra_zone" "this" {

examples/infrastructure_setup/vmc/terraform.tfvars.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ sddc_name = ""
88
vcenter_hostname = ""
99
vcenter_password = ""
1010
vcenter_username = ""
11+
region_name = ""
1112
external_region_id = ""
1213
user_email = ""
1314
fabric_network_name = ""

examples/infrastructure_setup/vmc/variables.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ variable "vcenter_password" {
2828
variable "vcenter_username" {
2929
}
3030

31+
variable "region_name" {
32+
}
33+
3134
variable "external_region_id" {
3235
}
3336

vra/data_source_region.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vra
22

33
import (
44
"fmt"
5+
"log"
56
"strings"
67

78
"github.com/vmware/vra-sdk-go/pkg/client/location"
@@ -40,7 +41,7 @@ func dataSourceRegion() *schema.Resource {
4041
},
4142
"name": {
4243
Type: schema.TypeString,
43-
Computed: true,
44+
Optional: true,
4445
},
4546
"org_id": {
4647
Type: schema.TypeString,
@@ -102,15 +103,40 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
102103
return err
103104
}
104105

106+
var region *models.Region
105107
regions := getResp.Payload
106108
if len(regions.Content) > 1 {
107-
return fmt.Errorf("vra_region must filter to a single region")
109+
log.Printf("received more than one result with the filter provided")
110+
name, nameOk := d.GetOk("name")
111+
112+
if !nameOk {
113+
return fmt.Errorf("vra_region must filter to a single region. Provide the 'name' argument to filter more")
114+
}
115+
116+
for _, reg := range regions.Content {
117+
if reg.Name == name.(string) {
118+
setFields(reg)
119+
return nil
120+
}
121+
}
122+
123+
return fmt.Errorf("more than one region found with the filter criteria, but the name provided did not match any regions")
108124
}
125+
109126
if len(regions.Content) == 0 {
110127
return fmt.Errorf("vra_region filter did not match any regions")
111128
}
112129

113-
region := regions.Content[0]
130+
if len(regions.Content) == 1 {
131+
region = regions.Content[0]
132+
133+
name, nameOk := d.GetOk("name")
134+
135+
if nameOk && region.Name != name.(string) {
136+
return fmt.Errorf("one region found with the filter criteria, but the name provided did not match")
137+
}
138+
}
139+
114140
setFields(region)
115141
return nil
116142
}

website/docs/d/vra_region.html.markdown

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,44 @@ data "vra_region" "this" {
1818
}
1919
```
2020

21+
**Region data source by filter:**
22+
```hcl
23+
data "vra_region" "this" {
24+
filter = "externalRegionId eq '${var.external_region_id}' and cloudAccountId eq '${vra_cloud_account.this.id}'"
25+
}
26+
```
27+
28+
**Region data source by filter and name:**
29+
For setting up vra_zone, vra_image_profile, vra_network_profile, etc for a VMC cloud account, use the following example.
30+
```hcl
31+
data "vra_region" "this" {
32+
filter = "externalRegionId eq '${var.external_region_id}' and cloudAccountId ne '${vra_cloud_account_vmc.this.id}'"
33+
name = var.region_name
34+
}
35+
```
36+
2137
The region data source supports the following arguments:
2238

2339
## Argument Reference
24-
* `cloud_account_id` - (Required) The Cloud Account ID.
40+
* `cloud_account_id` - (Optional) The Cloud Account ID.
41+
42+
* `filter` - (Optional) Search criteria to narrow down Images.
43+
44+
* `id` - (Optional) The ID of the region to find.
2545

26-
* `region` - (Required) The specific region associated with the cloud account.
46+
* `region` - (Optional) The specific region associated with the cloud account.
47+
48+
* `name` - (Optional) Name of the region from the associated vCenter.
2749

2850
## Attribute Reference
51+
* `created_at` - Date when entity was created. Date and time format is ISO 8601 and UTC.
52+
53+
* `org_id` - ID of organization that entity belongs to.
54+
55+
* `owner` - Email of entity owner.
56+
57+
* `external_region_id` - External regionId of region.
58+
2959
* `id` - The ID of the given region within the cloud account.
60+
61+
* `updated_at` - Date when entity was updated. Date and time format is ISO 8601 and UTC.

0 commit comments

Comments
 (0)