1
1
package vra
2
2
3
3
import (
4
+ "context"
5
+ "fmt"
6
+ "strings"
7
+ "time"
8
+
9
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
4
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
+ "github.com/vmware/vra-sdk-go/pkg/client"
5
13
"github.com/vmware/vra-sdk-go/pkg/client/cloud_account"
14
+ "github.com/vmware/vra-sdk-go/pkg/client/request"
6
15
"github.com/vmware/vra-sdk-go/pkg/models"
7
16
)
8
17
9
18
func dataSourceRegionEnumeration () * schema.Resource {
10
19
return & schema.Resource {
11
- Read : dataSourceRegionEnumerationRead ,
20
+ ReadContext : dataSourceRegionEnumerationRead ,
12
21
DeprecationMessage : "'region_enumeration' is deprecated. Use 'region_enumeration_vsphere' instead." ,
13
22
14
23
Schema : map [string ]* schema.Schema {
15
24
"accept_self_signed_cert" : {
16
- Type : schema .TypeBool ,
17
- Optional : true ,
18
- Default : false ,
25
+ Type : schema .TypeBool ,
26
+ Optional : true ,
27
+ Default : false ,
28
+ Description : "Whether to accept self signed certificate when connecting to the vCenter Server." ,
19
29
},
20
30
"dcid" : {
21
- Type : schema .TypeString ,
22
- Optional : true ,
31
+ Type : schema .TypeString ,
32
+ Optional : true ,
33
+ Description : "Identifier of a data collector vm deployed in the on premise infrastructure." ,
23
34
},
24
35
"hostname" : {
25
- Type : schema .TypeString ,
26
- Required : true ,
36
+ Type : schema .TypeString ,
37
+ Required : true ,
38
+ Description : "IP address or FQDN of the vCenter Server." ,
27
39
},
28
40
"password" : {
29
- Type : schema .TypeString ,
30
- Required : true ,
31
- Sensitive : true ,
41
+ Type : schema .TypeString ,
42
+ Required : true ,
43
+ Sensitive : true ,
44
+ Description : "Password of the vCenter Server." ,
32
45
},
33
46
"username" : {
34
- Type : schema .TypeString ,
35
- Required : true ,
47
+ Type : schema .TypeString ,
48
+ Required : true ,
49
+ Description : "Username of the vCenter Server." ,
36
50
},
37
51
"regions" : {
38
- Type : schema .TypeSet ,
39
- Computed : true ,
52
+ Type : schema .TypeSet ,
53
+ Computed : true ,
54
+ Description : "A set of region ids that can be enabled for this cloud account." ,
40
55
Elem : & schema.Schema {
41
56
Type : schema .TypeString ,
42
57
},
@@ -45,23 +60,75 @@ func dataSourceRegionEnumeration() *schema.Resource {
45
60
}
46
61
}
47
62
48
- func dataSourceRegionEnumerationRead (d * schema.ResourceData , meta interface {}) error {
63
+ func dataSourceRegionEnumerationRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
49
64
apiClient := meta .(* Client ).apiClient
50
65
51
- getResp , err := apiClient .CloudAccount .EnumerateVSphereRegions (cloud_account .NewEnumerateVSphereRegionsParams ().WithBody (& models.CloudAccountVsphereSpecification {
52
- AcceptSelfSignedCertificate : d .Get ("accept_self_signed_cert" ).(bool ),
53
- Dcid : d .Get ("dcid" ).(string ),
54
- HostName : withString (d .Get ("hostname" ).(string )),
55
- Password : withString (d .Get ("password" ).(string )),
56
- Username : withString (d .Get ("username" ).(string )),
57
- }))
66
+ enumResp , err := apiClient .CloudAccount .EnumerateVSphereRegionsAsync (
67
+ cloud_account .NewEnumerateVSphereRegionsAsyncParams ().
68
+ WithAPIVersion (withString (IaaSAPIVersion )).
69
+ WithTimeout (IncreasedTimeOut ).
70
+ WithBody (& models.CloudAccountVsphereRegionEnumerationSpecification {
71
+ AcceptSelfSignedCertificate : d .Get ("accept_self_signed_cert" ).(bool ),
72
+ Dcid : d .Get ("dcid" ).(string ),
73
+ HostName : d .Get ("hostname" ).(string ),
74
+ Password : d .Get ("password" ).(string ),
75
+ Username : d .Get ("username" ).(string ),
76
+ }))
77
+ if err != nil {
78
+ return diag .FromErr (err )
79
+ }
80
+
81
+ stateChangeFunc := resource.StateChangeConf {
82
+ Delay : 5 * time .Second ,
83
+ Pending : []string {models .RequestTrackerStatusINPROGRESS },
84
+ Refresh : dataSourceRegionEnumerationReadRefreshFunc (* apiClient , * enumResp .Payload .ID ),
85
+ Target : []string {models .RequestTrackerStatusFINISHED },
86
+ Timeout : d .Timeout (schema .TimeoutRead ),
87
+ MinTimeout : 5 * time .Second ,
88
+ }
89
+
90
+ resourceIds , err := stateChangeFunc .WaitForStateContext (ctx )
91
+ if err != nil {
92
+ return diag .FromErr (err )
93
+ }
94
+ enumID := (resourceIds .([]string ))[0 ]
58
95
96
+ getResp , err := apiClient .CloudAccount .GetRegionEnumerationResult (
97
+ cloud_account .NewGetRegionEnumerationResultParams ().
98
+ WithAPIVersion (withString (IaaSAPIVersion )).
99
+ WithTimeout (IncreasedTimeOut ).
100
+ WithID (enumID ))
59
101
if err != nil {
60
- return err
102
+ return diag . FromErr ( err )
61
103
}
62
104
63
- d .Set ("regions" , getResp .Payload .ExternalRegionIds )
105
+ d .Set ("regions" , extractIdsFromRegionSpecification ( getResp .Payload .ExternalRegions ) )
64
106
d .SetId (d .Get ("hostname" ).(string ))
65
107
66
108
return nil
67
109
}
110
+
111
+ func dataSourceRegionEnumerationReadRefreshFunc (apiClient client.MulticloudIaaS , id string ) resource.StateRefreshFunc {
112
+ return func () (interface {}, string , error ) {
113
+ reqResp , err := apiClient .Request .GetRequestTracker (request .NewGetRequestTrackerParams ().WithID (id ))
114
+ if err != nil {
115
+ return "" , models .RequestTrackerStatusFAILED , err
116
+ }
117
+
118
+ status := reqResp .Payload .Status
119
+ switch * status {
120
+ case models .RequestTrackerStatusFAILED :
121
+ return []string {"" }, * status , fmt .Errorf (reqResp .Payload .Message )
122
+ case models .RequestTrackerStatusINPROGRESS :
123
+ return [... ]string {id }, * status , nil
124
+ case models .RequestTrackerStatusFINISHED :
125
+ regionEnumerationIds := make ([]string , len (reqResp .Payload .Resources ))
126
+ for i , r := range reqResp .Payload .Resources {
127
+ regionEnumerationIds [i ] = strings .TrimPrefix (r , "/iaas/api/cloud-accounts/region-enumeration/" )
128
+ }
129
+ return regionEnumerationIds , * status , nil
130
+ default :
131
+ return [... ]string {id }, reqResp .Payload .Message , fmt .Errorf ("dataSourceRegionEnumerationReadRefreshFunc: unknown status %v" , * status )
132
+ }
133
+ }
134
+ }
0 commit comments