Skip to content

Commit

Permalink
Merge pull request #282 from cloudamqp/import/go-api
Browse files Browse the repository at this point in the history
Import Go-API client library with history (https://github.com/84codes/go-api)
  • Loading branch information
dentarg authored Jun 7, 2024
2 parents 31df7a7 + aa3ee1c commit ad86f8d
Show file tree
Hide file tree
Showing 25 changed files with 3,193 additions and 0 deletions.
95 changes: 95 additions & 0 deletions api/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package api

import (
"fmt"
"log"
"strconv"
)

func (api *API) ListInstances() ([]map[string]interface{}, error) {
var (
data []map[string]interface{}
failed map[string]interface{}
path = "api/instances"
)

response, err := api.sling.New().Path(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::account::list_instances data: %v", data)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("ListInstances failed, status: %v, message: %s", response.StatusCode, failed)
}
return data, nil
}

func (api *API) ListVpcs() ([]map[string]interface{}, error) {
var (
data []map[string]interface{}
failed map[string]interface{}
path = "/api/vpcs"
)

response, err := api.sling.New().Path(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::vpc::list data: %v", data)

if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("ListVpcs failed, status: %v, message: %v", response.StatusCode, failed)
}

for k := range data {
vpcID := strconv.FormatFloat(data[k]["id"].(float64), 'f', 0, 64)
data_temp, _ := api.readVpcName(vpcID)
data[k]["vpc_name"] = data_temp["name"]
}

return data, nil
}

func (api *API) RotatePassword(instanceID int) error {
var (
failed map[string]interface{}
path = fmt.Sprintf("api/instances/%d/account/rotate-password", instanceID)
)

response, err := api.sling.New().Post(path).Receive(nil, &failed)
if err != nil {
return err
}

switch response.StatusCode {
case 200:
return nil
case 204:
return nil
default:
return fmt.Errorf("failed to rotate api key, statusCode: %v, failed: %v",
response.StatusCode, failed)
}
}

func (api *API) RotateApiKey(instanceID int) error {
var (
failed map[string]interface{}
path = fmt.Sprintf("api/instances/%d/account/rotate-apikey", instanceID)
)

response, err := api.sling.New().Post(path).Receive(nil, &failed)
if err != nil {
return err
}

switch response.StatusCode {
case 200:
return nil
case 204:
return nil
default:
return fmt.Errorf("failed to rotate api key, statusCode: %v, failed: %v",
response.StatusCode, failed)
}
}
122 changes: 122 additions & 0 deletions api/alarms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package api

import (
"errors"
"fmt"
"log"
"strconv"
"time"
)

func (api *API) CreateAlarm(instanceID int, params map[string]interface{}) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
log.Printf("[DEBUG] go-api::alarm::create instance ID: %v, params: %v", instanceID, params)
path := fmt.Sprintf("/api/instances/%d/alarms", instanceID)
response, err := api.sling.New().Post(path).BodyJSON(params).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::alarm::create data: %v", data)

if err != nil {
return nil, err
}
if response.StatusCode != 201 {
return nil, fmt.Errorf("CreateAlarm failed, status: %v, message: %s", response.StatusCode, failed)
}

if id, ok := data["id"]; ok {
data["id"] = strconv.FormatFloat(id.(float64), 'f', 0, 64)
log.Printf("[DEBUG] go-api::alarm::create id set: %v", data["id"])
} else {
msg := fmt.Sprintf("go-api::instance::create Invalid alarm identifier: %v", data["id"])
log.Printf("[ERROR] %s", msg)
return nil, errors.New(msg)
}

return data, err
}

func (api *API) ReadAlarm(instanceID int, alarmID string) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
log.Printf("[DEBUG] go-api::alarm::read instance ID: %v, alarm ID: %v", instanceID, alarmID)
path := fmt.Sprintf("/api/instances/%v/alarms/%v", instanceID, alarmID)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::alarm::read data : %v", data)

if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("ReadAlarm failed, status: %v, message: %s", response.StatusCode, failed)
}

return data, err
}

func (api *API) ReadAlarms(instanceID int) ([]map[string]interface{}, error) {
var data []map[string]interface{}
failed := make(map[string]interface{})
log.Printf("[DEBUG] go-api::alarm::read instance ID: %v", instanceID)
path := fmt.Sprintf("/api/instances/%d/alarms", instanceID)
response, err := api.sling.New().Get(path).Receive(&data, &failed)
log.Printf("[DEBUG] go-api::alarm::read data: %v", data)

if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("Alarms::ReadAlarms failed, status: %v, message: %s", response.StatusCode, failed)
}

return data, err
}

func (api *API) UpdateAlarm(instanceID int, params map[string]interface{}) error {
failed := make(map[string]interface{})
log.Printf("[DEBUG] go-api::alarm::update instance ID: %v, params: %v", instanceID, params)
path := fmt.Sprintf("/api/instances/%v/alarms/%v", instanceID, params["id"])
response, err := api.sling.New().Put(path).BodyJSON(params).Receive(nil, &failed)

if err != nil {
return err
}
if response.StatusCode != 201 {
return fmt.Errorf("Alarms::UpdateAlarm failed, status: %v, message: %s", response.StatusCode, failed)
}

return err
}

func (api *API) DeleteAlarm(instanceID int, params map[string]interface{}) error {
failed := make(map[string]interface{})
log.Printf("[DEBUG] go-api::alarm::delete instance id: %v, params: %v", instanceID, params)
path := fmt.Sprintf("/api/instances/%v/alarms/%v", instanceID, params["id"])
response, _ := api.sling.New().Delete(path).BodyJSON(params).Receive(nil, &failed)

if response.StatusCode != 204 {
return fmt.Errorf("Alarm::DeleteAlarm failed, status: %v, message: %s", response.StatusCode, failed)
}

return api.waitUntilAlarmDeletion(instanceID, params["id"].(string))
}

func (api *API) waitUntilAlarmDeletion(instanceID int, id string) error {
log.Printf("[DEBUG] go-api::alarm::waitUntilAlarmDeletion waiting")
data := make(map[string]interface{})
failed := make(map[string]interface{})
for {
path := fmt.Sprintf("/api/instances/%v/alarms/%v", instanceID, id)
response, err := api.sling.New().Path(path).Receive(&data, &failed)

if err != nil {
log.Printf("[DEBUG] go-api::alarm::waitUntilAlarmDeletion error: %v", err)
return err
}
if response.StatusCode == 404 {
log.Print("[DEBUG] go-api::alarm::waitUntilAlarmDeletion deleted")
return nil
}

time.Sleep(10 * time.Second)
}
}
36 changes: 36 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package api

import (
"net/http"

"github.com/dghubble/sling"
)

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

func (api *API) DefaultRmqVersion() (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
_, err := api.sling.New().Get("/api/default_rabbitmq_version").Receive(&data, &failed)
if err != nil {
return nil, err
}
return data, nil
}

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(client).
Base(baseUrl).
SetBasicAuth("", apiKey).
Set("User-Agent", useragent),
client: client,
}
}
94 changes: 94 additions & 0 deletions api/aws_eventbridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package api

import (
"fmt"
"log"
"strconv"
)

func (api *API) CreateAwsEventBridge(instanceID int, params map[string]interface{}) (map[string]interface{}, error) {
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%d/eventbridges", instanceID)
)

log.Printf("[DEBUG] go-api::aws-eventbridge::create instance ID: %d, params: %v", instanceID, params)
response, err := api.sling.New().Post(path).BodyJSON(params).Receive(&data, &failed)
if err != nil {
return nil, err
}
if response.StatusCode != 201 {
return nil, fmt.Errorf("failed to create AWS EventBridge, status: %v, message: %s",
response.StatusCode, failed)
}
if id, ok := data["id"]; ok {
data["id"] = strconv.FormatFloat(id.(float64), 'f', 0, 64)
log.Printf("[DEBUG] go-api::aws-eventbridge::create EventBridge identifier: %v", data["id"])
} else {
return nil, fmt.Errorf("go-api::aws-eventbridge::create Invalid identifier: %v", data["id"])
}

return data, nil
}

func (api *API) ReadAwsEventBridge(instanceID int, eventbridgeID string) (map[string]interface{}, error) {
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%d/eventbridges/%s", instanceID, eventbridgeID)
)

response, err := api.sling.New().Get(path).Receive(&data, &failed)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("failed to read AWS EventBridge, status: %v, message: %s",
response.StatusCode, failed)
}

return data, nil
}

func (api *API) ReadAwsEventBridges(instanceID int) (map[string]interface{}, error) {
var (
data map[string]interface{}
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%d/eventbridges", instanceID)
)

response, err := api.sling.New().Get(path).Receive(&data, &failed)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("failed to read AWS EventBridges, status: %v, message: %s",
response.StatusCode, failed)
}

return data, nil
}

func (api *API) DeleteAwsEventBridge(instanceID int, eventbridgeID string) error {
var (
failed map[string]interface{}
path = fmt.Sprintf("/api/instances/%d/eventbridges/%s", instanceID, eventbridgeID)
)

log.Printf("[DEBUG] go-api::aws-eventbridge::delete instance id: %d, eventbridge id: %s", instanceID, eventbridgeID)
response, err := api.sling.New().Delete(path).Receive(nil, &failed)
if err != nil {
return err
}

switch response.StatusCode {
case 204:
return nil
case 404:
// AWS EventBridge not found in the backend. Silent let the resource be deleted.
return nil
}

return fmt.Errorf("failed to delete AWS EventBridge, status: %v, message: %s", response.StatusCode, failed)
}
41 changes: 41 additions & 0 deletions api/credentials.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package api

import (
"fmt"
"log"
"regexp"
"strconv"
)

func (api *API) ReadCredentials(id int) (map[string]interface{}, error) {
data := make(map[string]interface{})
failed := make(map[string]interface{})
instanceID := strconv.Itoa(id)
log.Printf("[DEBUG] go-api::credentials::read instance ID: %v", instanceID)
response, err := api.sling.New().Path("/api/instances/").Get(instanceID).Receive(&data, &failed)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("ReadCredentials failed, status: %v, message: %s", response.StatusCode, failed)
}

return extractInfo(data["url"].(string)), nil
}

func extractInfo(url string) map[string]interface{} {
paramsMap := make(map[string]interface{})
r := regexp.MustCompile(`^.*:\/\/(?P<username>(.*)):(?P<password>(.*))@`)
match := r.FindStringSubmatch(url)

for i, name := range r.SubexpNames() {
if name == "username" {
paramsMap["username"] = match[i]
}
if name == "password" {
paramsMap["password"] = match[i]
}
}

return paramsMap
}
Loading

0 comments on commit ad86f8d

Please sign in to comment.