Skip to content

Commit 7adb616

Browse files
Preparation for Go VCR testing (#48)
### WHY are these changes introduced? To use Go-VCR testing package for the provider, the HTTP client transport need to be changeable. ### WHAT is this pull request doing? - Make HTTP client accessable and to be changed. - When polling, change to sleep after an attempt have been triggered. --------- Co-authored-by: Magnus Hörberg <magnus.hoerberg@gmail.com>
1 parent fca0b01 commit 7adb616

File tree

10 files changed

+18
-17
lines changed

10 files changed

+18
-17
lines changed

api/api.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
type API struct {
10-
sling *sling.Sling
10+
sling *sling.Sling
11+
client *http.Client
1112
}
1213

1314
func (api *API) DefaultRmqVersion() (map[string]interface{}, error) {
@@ -20,15 +21,16 @@ func (api *API) DefaultRmqVersion() (map[string]interface{}, error) {
2021
return data, nil
2122
}
2223

23-
func New(baseUrl, apiKey string, useragent string) *API {
24+
func New(baseUrl, apiKey string, useragent string, client *http.Client) *API {
2425
if len(useragent) == 0 {
2526
useragent = "84codes go-api"
2627
}
2728
return &API{
2829
sling: sling.New().
29-
Client(http.DefaultClient).
30+
Client(client).
3031
Base(baseUrl).
3132
SetBasicAuth("", apiKey).
3233
Set("User-Agent", useragent),
34+
client: client,
3335
}
3436
}

api/instance.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func (api *API) waitUntilReady(instanceID string) (map[string]interface{}, error
1818
log.Printf("[DEBUG] go-api::instance::waitUntilReady waiting")
1919

2020
for {
21-
time.Sleep(10 * time.Second)
2221
response, err := api.sling.New().Path(path).Receive(&data, &failed)
2322
if err != nil {
2423
return nil, err
@@ -34,6 +33,7 @@ func (api *API) waitUntilReady(instanceID string) (map[string]interface{}, error
3433
return nil, fmt.Errorf("waitUntilReady failed, status: %v, message: %s",
3534
response.StatusCode, failed)
3635
}
36+
time.Sleep(10 * time.Second)
3737
}
3838
}
3939

@@ -45,7 +45,6 @@ func (api *API) waitUntilAllNodesReady(instanceID string) error {
4545
)
4646

4747
for {
48-
time.Sleep(15 * time.Second)
4948
_, err := api.sling.New().Path(path).Receive(&data, &failed)
5049
if err != nil {
5150
return err
@@ -61,6 +60,7 @@ func (api *API) waitUntilAllNodesReady(instanceID string) error {
6160
if ready {
6261
return nil
6362
}
63+
time.Sleep(15 * time.Second)
6464
}
6565
}
6666

@@ -106,7 +106,6 @@ func (api *API) waitUntilDeletion(instanceID string) error {
106106

107107
log.Printf("[DEBUG] go-api::instance::waitUntilDeletion waiting")
108108
for {
109-
time.Sleep(10 * time.Second)
110109
response, err := api.sling.New().Path(path).Receive(&data, &failed)
111110
if err != nil {
112111
log.Printf("[DEBUG] go-api::instance::waitUntilDeletion error: %v", err)
@@ -121,6 +120,7 @@ func (api *API) waitUntilDeletion(instanceID string) error {
121120
log.Print("[DEBUG] go-api::instance::waitUntilDeletion deleted")
122121
return nil
123122
}
123+
time.Sleep(10 * time.Second)
124124
}
125125
}
126126

api/metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (api *API) ValidatePlan(name string) error {
2323
path = "api/plans"
2424
)
2525

26-
response, err := api.sling.New().Get(path).Receive(&data, &failed)
26+
response, err := api.sling.New().Client(api.client).Get(path).Receive(&data, &failed)
2727
if err != nil {
2828
return err
2929
}
@@ -88,7 +88,7 @@ func (api *API) ValidateRegion(region string) error {
8888
platform string
8989
)
9090

91-
response, err := api.sling.New().Get(path).Receive(&data, &failed)
91+
response, err := api.sling.New().Client(api.client).Get(path).Receive(&data, &failed)
9292
if err != nil {
9393
return err
9494
}

api/nodes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func (api *API) PostAction(instanceID int, nodeName string, action string) (map[
6767
func (api *API) waitOnNodeAction(instanceID int, nodeName string, action string) (map[string]interface{}, error) {
6868
log.Printf("[DEBUG] go-api::nodes::waitOnNodeAction waiting")
6969
for {
70-
time.Sleep(20 * time.Second)
7170
data, err := api.ReadNode(instanceID, nodeName)
7271

7372
if err != nil {
@@ -84,5 +83,6 @@ func (api *API) waitOnNodeAction(instanceID int, nodeName string, action string)
8483
return data, nil
8584
}
8685
}
86+
time.Sleep(20 * time.Second)
8787
}
8888
}

api/plugins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ func (api *API) waitUntilPluginChanged(instanceID int, pluginName string, enable
176176
attempt, sleep, timeout int) (map[string]interface{}, error) {
177177

178178
for {
179-
time.Sleep(time.Duration(sleep) * time.Second)
180179
if attempt*sleep > timeout {
181180
return nil, fmt.Errorf("wait until plugin changed reached timeout of %d seconds", timeout)
182181
}
@@ -193,5 +192,6 @@ func (api *API) waitUntilPluginChanged(instanceID int, pluginName string, enable
193192
return response, nil
194193
}
195194
attempt++
195+
time.Sleep(time.Duration(sleep) * time.Second)
196196
}
197197
}

api/plugins_community.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ func (api *API) waitUntilPluginUninstalled(instanceID int, pluginName string,
151151
log.Printf("[DEBUG] go-api::plugin_community::waitUntilPluginUninstalled instance id: %v, name: %v",
152152
instanceID, pluginName)
153153
for {
154-
time.Sleep(time.Duration(sleep) * time.Second)
155154
if attempt*sleep > timeout {
156155
return nil, fmt.Errorf("wait until plugin uninstalled reached timeout of %d seconds", timeout)
157156
}
@@ -164,5 +163,6 @@ func (api *API) waitUntilPluginUninstalled(instanceID int, pluginName string,
164163
return response, nil
165164
}
166165
attempt++
166+
time.Sleep(time.Duration(sleep) * time.Second)
167167
}
168168
}

api/upgrade_rabbitmq.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (api *API) waitUntilUpgraded(instanceID int) (string, error) {
4646
failed := make(map[string]interface{})
4747

4848
for {
49-
time.Sleep(30 * time.Second)
5049
path := fmt.Sprintf("api/instances/%v/nodes", instanceID)
5150
_, err := api.sling.New().Path(path).Receive(&data, &failed)
5251
if err != nil {
@@ -65,5 +64,6 @@ func (api *API) waitUntilUpgraded(instanceID int) (string, error) {
6564
if ready {
6665
return "", nil
6766
}
67+
time.Sleep(30 * time.Second)
6868
}
6969
}

api/vpc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func (api *API) waitUntilVpcReady(vpcID string) error {
1616

1717
log.Printf("[DEBUG] go-api::vpc::waitUntilVpcReady waiting")
1818
for {
19-
time.Sleep(10 * time.Second)
2019
response, err := api.sling.New().Get(path).Receive(&data, &failed)
2120
if err != nil {
2221
return err
@@ -32,6 +31,7 @@ func (api *API) waitUntilVpcReady(vpcID string) error {
3231
return fmt.Errorf("waitUntilReady failed, status: %v, message: %s",
3332
response.StatusCode, failed)
3433
}
34+
time.Sleep(10 * time.Second)
3535
}
3636
}
3737

api/vpc_gcp_peering.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ func (api *API) waitForGcpPeeringStatus(path, peerID string,
1717
)
1818

1919
for {
20-
time.Sleep(time.Duration(sleep) * time.Second)
2120
if attempt*sleep > timeout {
2221
return fmt.Errorf("wait until GCP VPC peering status reached timeout of %d seconds", timeout)
2322
}
@@ -42,6 +41,7 @@ func (api *API) waitForGcpPeeringStatus(path, peerID string,
4241
log.Printf("[INFO] go-api::vpc_gcp_peering::waitForGcpPeeringStatus Waiting for state = ACTIVE "+
4342
"attempt %d until timeout: %d", attempt, (timeout - (attempt * sleep)))
4443
attempt++
44+
time.Sleep(time.Duration(sleep) * time.Second)
4545
}
4646
}
4747

@@ -122,7 +122,7 @@ func (api *API) readVpcGcpPeeringWithRetry(path string, attempt, sleep, timeout
122122
if err != nil {
123123
return attempt, nil, err
124124
} else if attempt*sleep > timeout {
125-
return attempt, nil, fmt.Errorf("read plugins reached timeout of %d seconds", timeout)
125+
return attempt, nil, fmt.Errorf("read VPC peering reached timeout of %d seconds", timeout)
126126
}
127127

128128
switch response.StatusCode {
@@ -137,7 +137,7 @@ func (api *API) readVpcGcpPeeringWithRetry(path string, attempt, sleep, timeout
137137
return api.readVpcGcpPeeringWithRetry(path, attempt, sleep, timeout)
138138
}
139139
}
140-
return attempt, nil, fmt.Errorf("read plugin with retry failed, status: %v, message: %s",
140+
return attempt, nil, fmt.Errorf("read VPC peering with retry failed, status: %v, message: %s",
141141
response.StatusCode, failed)
142142
}
143143

api/vpc_gcp_peering_withvpcid.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func (api *API) RequestVpcGcpPeeringWithVpcId(vpcID string, params map[string]in
2828
return data, nil
2929
}
3030

31-
// func (api *API) ReadVpcGcpPeering(instanceID, sleep, timeout int) (
3231
func (api *API) ReadVpcGcpPeeringWithVpcId(vpcID string, sleep, timeout int) (
3332
map[string]interface{}, error) {
3433

0 commit comments

Comments
 (0)