Skip to content

Commit 25bb1d4

Browse files
authored
Merge pull request #386 from vmware/fix-data-source-region
Several fixes for the data source region
2 parents 4250a3b + d1dba66 commit 25bb1d4

File tree

2 files changed

+70
-41
lines changed

2 files changed

+70
-41
lines changed

vra/data_source_region.go

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package vra
33
import (
44
"fmt"
55
"log"
6-
"strings"
76

87
"github.com/vmware/vra-sdk-go/pkg/client/location"
98
"github.com/vmware/vra-sdk-go/pkg/models"
@@ -19,46 +18,56 @@ func dataSourceRegion() *schema.Resource {
1918

2019
Schema: map[string]*schema.Schema{
2120
"cloud_account_id": {
22-
Type: schema.TypeString,
23-
Optional: true,
24-
Computed: true,
21+
Type: schema.TypeString,
22+
Optional: true,
23+
Computed: true,
24+
Description: "The id of the cloud account the region belongs to.",
2525
},
2626
"created_at": {
27-
Type: schema.TypeString,
28-
Computed: true,
27+
Type: schema.TypeString,
28+
Computed: true,
29+
Description: "Date when the entity was created. The date is in ISO 8601 and UTC.",
2930
},
3031
"external_region_id": {
31-
Type: schema.TypeString,
32-
Computed: true,
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "Unique identifier of region on the provider side.",
3335
},
3436
"filter": {
35-
Type: schema.TypeString,
36-
Optional: true,
37+
Type: schema.TypeString,
38+
Optional: true,
39+
Description: "Search criteria to narrow down Regions.",
3740
},
3841
"id": {
39-
Type: schema.TypeString,
40-
Optional: true,
41-
Computed: true,
42+
Type: schema.TypeString,
43+
Optional: true,
44+
Computed: true,
45+
Description: "The id of the region instance.",
4246
},
4347
"name": {
44-
Type: schema.TypeString,
45-
Computed: true,
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: "Name of region on the provider side. In vSphere, the name of the region is different from its id.",
4651
},
4752
"org_id": {
48-
Type: schema.TypeString,
49-
Computed: true,
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: "The id of the organization this entity belongs to.",
5056
},
5157
"owner": {
52-
Type: schema.TypeString,
53-
Computed: true,
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: "Email of the user that owns the entity.",
5461
},
5562
"region": {
56-
Type: schema.TypeString,
57-
Optional: true,
63+
Type: schema.TypeString,
64+
Optional: true,
65+
Description: "The specific region associated with the cloud account.",
5866
},
5967
"updated_at": {
60-
Type: schema.TypeString,
61-
Computed: true,
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: "Date when the entity was last updated. The date is ISO 8601 and UTC.",
6271
},
6372
},
6473
}
@@ -91,7 +100,12 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
91100
// config includes id, using id to get region details
92101
getResp, err := apiClient.Location.GetRegion(location.NewGetRegionParams().WithID(id.(string)))
93102
if err != nil {
94-
return err
103+
switch err.(type) {
104+
case *location.GetRegionNotFound:
105+
return fmt.Errorf("region %s not found", id.(string))
106+
default:
107+
return err
108+
}
95109
}
96110

97111
setFields(getResp.Payload)
@@ -146,20 +160,35 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
146160
if cloudAccountIDOk && regionOk {
147161
getResp, err := apiClient.CloudAccount.GetCloudAccount(cloud_account.NewGetCloudAccountParams().WithID(cloudAccountID.(string)))
148162
if err != nil {
149-
return err
163+
switch err.(type) {
164+
case *cloud_account.GetCloudAccountNotFound:
165+
return fmt.Errorf("cloud account %s not found", cloudAccountID.(string))
166+
default:
167+
return err
168+
}
150169
}
151170

171+
var id string
152172
cloudAccount := getResp.Payload
153-
for i, enabledRegion := range cloudAccount.EnabledRegionIds {
154-
if enabledRegion == region {
155-
d.SetId(strings.TrimPrefix(cloudAccount.Links["regions"].Hrefs[i], "/iaas/api/regions/"))
156-
return nil
173+
for _, enabledRegion := range cloudAccount.EnabledRegions {
174+
if enabledRegion.Name == region {
175+
id = *enabledRegion.ID
176+
break
157177
}
158178
}
159179

160-
resp, err := apiClient.Location.GetRegion(location.NewGetRegionParams().WithID(id.(string)))
180+
if id == "" {
181+
return fmt.Errorf("region %s not found", region)
182+
}
183+
184+
resp, err := apiClient.Location.GetRegion(location.NewGetRegionParams().WithID(id))
161185
if err != nil {
162-
return err
186+
switch err.(type) {
187+
case *location.GetRegionNotFound:
188+
return fmt.Errorf("region %s not found", id)
189+
default:
190+
return err
191+
}
163192
}
164193

165194
setFields(resp.Payload)

website/docs/d/vra_region.html.markdown

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,25 @@ data "vra_region" "this" {
3737
The region data source supports the following arguments:
3838

3939
## Argument Reference
40-
* `cloud_account_id` - (Optional) The Cloud Account ID.
40+
* `cloud_account_id` - (Optional) The id of the cloud account the region belongs to.
4141

42-
* `filter` - (Optional) Search criteria to narrow down Images.
42+
* `filter` - (Optional) Search criteria to narrow down Regions.
4343

44-
* `id` - (Optional) The ID of the region to find.
44+
* `id` - (Optional) The id of the region instance,
4545

4646
* `region` - (Optional) The specific region associated with the cloud account.
4747

48-
* `name` - (Optional) Name of the region from the associated vCenter.
48+
* `name` - (Optional) Name of region on the provider side. In vSphere, the name of the region is different from its id.
4949

5050
## Attribute Reference
51-
* `created_at` - Date when entity was created. Date and time format is ISO 8601 and UTC.
51+
* `created_at` - Date when the entity was created. The date is in ISO 8601 and UTC.
5252

53-
* `org_id` - ID of organization that entity belongs to.
53+
* `org_id` - The id of the organization this entity belongs to.
5454

55-
* `owner` - Email of entity owner.
55+
* `owner` - Email of the user that owns the entity.
5656

57-
* `external_region_id` - External regionId of region.
57+
* `external_region_id` - Unique identifier of region on the provider side.
5858

59-
* `id` - The ID of the given region within the cloud account.
59+
* `id` - The id of this resource instance.
6060

61-
* `updated_at` - Date when entity was updated. Date and time format is ISO 8601 and UTC.
61+
* `updated_at` - Date when the entity was last updated. The date is ISO 8601 and UTC.

0 commit comments

Comments
 (0)