@@ -3,7 +3,6 @@ package vra
3
3
import (
4
4
"fmt"
5
5
"log"
6
- "strings"
7
6
8
7
"github.com/vmware/vra-sdk-go/pkg/client/location"
9
8
"github.com/vmware/vra-sdk-go/pkg/models"
@@ -19,46 +18,56 @@ func dataSourceRegion() *schema.Resource {
19
18
20
19
Schema : map [string ]* schema.Schema {
21
20
"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." ,
25
25
},
26
26
"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." ,
29
30
},
30
31
"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." ,
33
35
},
34
36
"filter" : {
35
- Type : schema .TypeString ,
36
- Optional : true ,
37
+ Type : schema .TypeString ,
38
+ Optional : true ,
39
+ Description : "Search criteria to narrow down Regions." ,
37
40
},
38
41
"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." ,
42
46
},
43
47
"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." ,
46
51
},
47
52
"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." ,
50
56
},
51
57
"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." ,
54
61
},
55
62
"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." ,
58
66
},
59
67
"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." ,
62
71
},
63
72
},
64
73
}
@@ -91,7 +100,12 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
91
100
// config includes id, using id to get region details
92
101
getResp , err := apiClient .Location .GetRegion (location .NewGetRegionParams ().WithID (id .(string )))
93
102
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
+ }
95
109
}
96
110
97
111
setFields (getResp .Payload )
@@ -146,20 +160,35 @@ func dataSourceRegionRead(d *schema.ResourceData, meta interface{}) error {
146
160
if cloudAccountIDOk && regionOk {
147
161
getResp , err := apiClient .CloudAccount .GetCloudAccount (cloud_account .NewGetCloudAccountParams ().WithID (cloudAccountID .(string )))
148
162
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
+ }
150
169
}
151
170
171
+ var id string
152
172
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
157
177
}
158
178
}
159
179
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 ))
161
185
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
+ }
163
192
}
164
193
165
194
setFields (resp .Payload )
0 commit comments