Skip to content
This repository was archived by the owner on Jan 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type API struct {
sling *sling.Sling
sling *sling.Sling
client *http.Client
}

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

func New(baseUrl, apiKey string, useragent string) *API {
func New(baseUrl, apiKey string, useragent string, client *http.Client) *API {
if len(useragent) == 0 {
useragent = "84codes go-api"
}
return &API{
sling: sling.New().
Client(http.DefaultClient).
Client(client).
Base(baseUrl).
SetBasicAuth("", apiKey).
Set("User-Agent", useragent),
client: client,
}
}
6 changes: 3 additions & 3 deletions api/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func (api *API) waitUntilReady(instanceID string) (map[string]interface{}, error
log.Printf("[DEBUG] go-api::instance::waitUntilReady waiting")

for {
time.Sleep(10 * time.Second)
response, err := api.sling.New().Path(path).Receive(&data, &failed)
if err != nil {
return nil, err
Expand All @@ -34,6 +33,7 @@ func (api *API) waitUntilReady(instanceID string) (map[string]interface{}, error
return nil, fmt.Errorf("waitUntilReady failed, status: %v, message: %s",
response.StatusCode, failed)
}
time.Sleep(10 * time.Second)
}
}

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

for {
time.Sleep(15 * time.Second)
_, err := api.sling.New().Path(path).Receive(&data, &failed)
if err != nil {
return err
Expand All @@ -61,6 +60,7 @@ func (api *API) waitUntilAllNodesReady(instanceID string) error {
if ready {
return nil
}
time.Sleep(15 * time.Second)
}
}

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

log.Printf("[DEBUG] go-api::instance::waitUntilDeletion waiting")
for {
time.Sleep(10 * time.Second)
response, err := api.sling.New().Path(path).Receive(&data, &failed)
if err != nil {
log.Printf("[DEBUG] go-api::instance::waitUntilDeletion error: %v", err)
Expand All @@ -121,6 +120,7 @@ func (api *API) waitUntilDeletion(instanceID string) error {
log.Print("[DEBUG] go-api::instance::waitUntilDeletion deleted")
return nil
}
time.Sleep(10 * time.Second)
}
}

Expand Down
4 changes: 2 additions & 2 deletions api/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (api *API) ValidatePlan(name string) error {
path = "api/plans"
)

response, err := api.sling.New().Get(path).Receive(&data, &failed)
response, err := api.sling.New().Client(api.client).Get(path).Receive(&data, &failed)
if err != nil {
return err
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func (api *API) ValidateRegion(region string) error {
platform string
)

response, err := api.sling.New().Get(path).Receive(&data, &failed)
response, err := api.sling.New().Client(api.client).Get(path).Receive(&data, &failed)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion api/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func (api *API) PostAction(instanceID int, nodeName string, action string) (map[
func (api *API) waitOnNodeAction(instanceID int, nodeName string, action string) (map[string]interface{}, error) {
log.Printf("[DEBUG] go-api::nodes::waitOnNodeAction waiting")
for {
time.Sleep(20 * time.Second)
data, err := api.ReadNode(instanceID, nodeName)

if err != nil {
Expand All @@ -84,5 +83,6 @@ func (api *API) waitOnNodeAction(instanceID int, nodeName string, action string)
return data, nil
}
}
time.Sleep(20 * time.Second)
}
}
2 changes: 1 addition & 1 deletion api/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (api *API) waitUntilPluginChanged(instanceID int, pluginName string, enable
attempt, sleep, timeout int) (map[string]interface{}, error) {

for {
time.Sleep(time.Duration(sleep) * time.Second)
if attempt*sleep > timeout {
return nil, fmt.Errorf("wait until plugin changed reached timeout of %d seconds", timeout)
}
Expand All @@ -193,5 +192,6 @@ func (api *API) waitUntilPluginChanged(instanceID int, pluginName string, enable
return response, nil
}
attempt++
time.Sleep(time.Duration(sleep) * time.Second)
}
}
2 changes: 1 addition & 1 deletion api/plugins_community.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func (api *API) waitUntilPluginUninstalled(instanceID int, pluginName string,
log.Printf("[DEBUG] go-api::plugin_community::waitUntilPluginUninstalled instance id: %v, name: %v",
instanceID, pluginName)
for {
time.Sleep(time.Duration(sleep) * time.Second)
if attempt*sleep > timeout {
return nil, fmt.Errorf("wait until plugin uninstalled reached timeout of %d seconds", timeout)
}
Expand All @@ -164,5 +163,6 @@ func (api *API) waitUntilPluginUninstalled(instanceID int, pluginName string,
return response, nil
}
attempt++
time.Sleep(time.Duration(sleep) * time.Second)
}
}
2 changes: 1 addition & 1 deletion api/upgrade_rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (api *API) waitUntilUpgraded(instanceID int) (string, error) {
failed := make(map[string]interface{})

for {
time.Sleep(30 * time.Second)
path := fmt.Sprintf("api/instances/%v/nodes", instanceID)
_, err := api.sling.New().Path(path).Receive(&data, &failed)
if err != nil {
Expand All @@ -65,5 +64,6 @@ func (api *API) waitUntilUpgraded(instanceID int) (string, error) {
if ready {
return "", nil
}
time.Sleep(30 * time.Second)
}
}
2 changes: 1 addition & 1 deletion api/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func (api *API) waitUntilVpcReady(vpcID string) error {

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

Expand Down
6 changes: 3 additions & 3 deletions api/vpc_gcp_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func (api *API) waitForGcpPeeringStatus(path, peerID string,
)

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

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

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

Expand Down
1 change: 0 additions & 1 deletion api/vpc_gcp_peering_withvpcid.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (api *API) RequestVpcGcpPeeringWithVpcId(vpcID string, params map[string]in
return data, nil
}

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

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ func main() {
}

func New(baseUrl, apiKey string) *api.API {
return api.New(baseUrl, apiKey, "84codes go-api")
return api.New(baseUrl, apiKey, "84codes go-api", nil)
}