Skip to content

Commit f5cf3ba

Browse files
author
Caige Nichols
committed
Merge branch 'storm-server-api' into 'master'
Storm server api See merge request masre/liquidweb-go!10
2 parents 1350501 + c664d8e commit f5cf3ba

21 files changed

+449
-264
lines changed

.gitlab-ci.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
stages:
22
- build
33

4-
variables:
5-
DOCKER_HOST: tcp://docker:2375/
6-
DOCKER_DRIVER: overlay2
7-
8-
services:
9-
- docker:dind
10-
114
build:
125
tags:
136
- docker-with-registry

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ build:
33

44
vet:
55
go vet
6+
7+
test:
8+
go test ./...

client/client.go

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"crypto/tls"
66
"encoding/json"
7-
"errors"
87
"fmt"
98
"io/ioutil"
109
"net/http"
@@ -93,78 +92,32 @@ func NewClient(config *Config) *Client {
9392
// if gotErr != nil {
9493
// panic(gotErr)
9594
// }
96-
func (client *Client) Call(method string, params interface{}) (interface{}, error) {
95+
func (client *Client) Call(method string, params interface{}, into interface{}) error {
9796
bsRb, err := client.CallRaw(method, params)
9897
if err != nil {
99-
return nil, err
98+
return err
10099
}
101100

102-
// json decode into interface
103-
var decodedResp interface{}
104-
if jsonDecodeErr := json.Unmarshal(bsRb, &decodedResp); jsonDecodeErr != nil {
105-
return nil, jsonDecodeErr
106-
}
107-
mapDecodedResp, ok := decodedResp.(map[string]interface{})
108-
if !ok {
109-
return nil, errors.New("endpoint did not return the expected JSON structure")
101+
var raw map[string]interface{}
102+
if err = json.Unmarshal(bsRb, &raw); err != nil {
103+
return err
110104
}
111-
errorClass, ok := mapDecodedResp["error_class"]
105+
errorClass, ok := raw["error_class"]
112106
if ok {
113107
errorClassStr := errorClass.(string)
114108
if errorClassStr != "" {
115-
return nil, liquidweb.LWAPIError{
109+
return liquidweb.LWAPIError{
116110
ErrorClass: errorClassStr,
117-
ErrorFullMsg: mapDecodedResp["full_message"].(string),
118-
ErrorMsg: mapDecodedResp["error"].(string),
111+
ErrorFullMsg: raw["full_message"].(string),
112+
ErrorMsg: raw["error"].(string),
119113
}
120114
}
121115
}
122-
// no LW errors so return the decoded response
123-
return decodedResp, nil
124-
}
125116

126-
// CallInto is like call, but instead of returning an interface you pass it a
127-
// struct which is filled, much like the json.Unmarshal function. The struct
128-
// you pass must satisfy the LWAPIRes interface. If you embed the LWAPIError
129-
// struct from this package into your struct, this will be taken care of for you.
130-
//
131-
// Example:
132-
// type ZoneDetails struct {
133-
// lwApi.LWAPIError
134-
// AvlZone string `json:"availability_zone"`
135-
// Desc string `json:"description"`
136-
// GatewayDevs []string `json:"gateway_devices"`
137-
// HvType string `json:"hv_type"`
138-
// ID int `json:"id"`
139-
// Legacy int `json:"legacy"`
140-
// Name string `json:"name"`
141-
// Status string `json:"status"`
142-
// SourceHVs []string `json:"valid_source_hvs"`
143-
// }
144-
// var zone ZoneDetails
145-
// err = apiClient.CallInto("network/zone/details", paramers, &zone)
146-
// if err != nil {
147-
// log.Fatal(err)
148-
// }
149-
// fmt.Printf("Got struct %#v\n", zone)
150-
//
151-
func (client *Client) CallInto(method string, params interface{}, into liquidweb.LWAPIRes) error {
152-
bsRb, err := client.CallRaw(method, params)
153-
if err != nil {
117+
// Response should be valid, decode it.
118+
if err = json.Unmarshal(bsRb, &into); err != nil {
154119
return err
155120
}
156-
157-
err = json.Unmarshal(bsRb, into)
158-
if err != nil {
159-
return err
160-
}
161-
162-
if into.HasError() {
163-
// the LWAPIRes satisfies the Error interface, so we can just return it on
164-
// error.
165-
return into
166-
}
167-
168121
return nil
169122
}
170123

liquidweb.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88

99
// Backend is an interface for calls against Liquid Web's API.
1010
type Backend interface {
11-
Call(string, interface{}) (interface{}, error)
12-
CallInto(string, interface{}, LWAPIRes) error
11+
Call(string, interface{}, interface{}) error
1312
CallRaw(string, interface{}) ([]byte, error)
1413
}
1514

16-
// LWAPIRes is a convenient interface used (for example) by CallInto to ensure a passed
15+
// LWAPIRes is a convenient interface used (for example) by Call to ensure a passed
1716
// struct knows how to indicate whether or not it had an error.
1817
type LWAPIRes interface {
1918
Error() string

network/dns.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,11 @@ type DNSRecord struct {
6868

6969
// DNSRecordList is an envelope for the API result containing either a list of DNS Records or an error.
7070
type DNSRecordList struct {
71-
liquidweb.LWAPIError
7271
liquidweb.ListMeta
7372
Items []DNSRecord `json:"items,omitempty"`
7473
}
7574

76-
// DNSRecordItem is an envelope for the API result containing either a DNS Record or an error.
77-
type DNSRecordItem struct {
78-
liquidweb.LWAPIError
79-
DNSRecord
80-
}
81-
8275
// DNSRecordDeletion represents the API result when deleting a DNS Record.
8376
type DNSRecordDeletion struct {
84-
liquidweb.LWAPIError
85-
Deleted int `json:"deleted"`
77+
Deleted types.FlexInt `json:"deleted"`
8678
}

network/dns_client.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66

77
// DNSBackend describes the interface for interactions with the API.
88
type DNSBackend interface {
9-
Create(*DNSRecordParams) (*DNSRecordItem, error)
10-
Details(int) *DNSRecordItem
11-
List(*DNSRecordParams) *DNSRecordList
12-
Update(*DNSRecordParams) *DNSRecordItem
13-
Delete(*DNSRecordParams) *DNSRecordDeletion
9+
Create(*DNSRecordParams) (*DNSRecord, error)
10+
Details(int) (*DNSRecord, error)
11+
List(*DNSRecordParams) (*DNSRecordList, error)
12+
Update(*DNSRecordParams) (*DNSRecord, error)
13+
Delete(*DNSRecordParams) (*DNSRecordDeletion, error)
1414
}
1515

1616
// DNSClient is the backend implementation for interacting with DNS Records.
@@ -19,9 +19,9 @@ type DNSClient struct {
1919
}
2020

2121
// Create creates a new DNS Record.
22-
func (c *DNSClient) Create(params *DNSRecordParams) (*DNSRecordItem, error) {
23-
var result DNSRecordItem
24-
err := c.Backend.CallInto("v1/Network/DNS/Record/create", params, &result)
22+
func (c *DNSClient) Create(params *DNSRecordParams) (*DNSRecord, error) {
23+
var result DNSRecord
24+
err := c.Backend.Call("v1/Network/DNS/Record/create", params, &result)
2525
if err != nil {
2626
return nil, err
2727
}
@@ -30,35 +30,44 @@ func (c *DNSClient) Create(params *DNSRecordParams) (*DNSRecordItem, error) {
3030
}
3131

3232
// Details returns details about a DNS Record.
33-
func (c *DNSClient) Details(id int) *DNSRecordItem {
34-
var result DNSRecordItem
33+
func (c *DNSClient) Details(id int) (*DNSRecord, error) {
34+
var result DNSRecord
3535
params := DNSRecordParams{ID: id}
3636

37-
c.Backend.CallInto("v1/Network/DNS/Record/details", params, &result)
38-
39-
return &result
37+
err := c.Backend.Call("v1/Network/DNS/Record/details", params, &result)
38+
if err != nil {
39+
return nil, err
40+
}
41+
return &result, nil
4042
}
4143

4244
// List returns a list of DNS Records.
43-
func (c *DNSClient) List(params *DNSRecordParams) *DNSRecordList {
45+
func (c *DNSClient) List(params *DNSRecordParams) (*DNSRecordList, error) {
4446
list := &DNSRecordList{}
4547

46-
c.Backend.CallInto("v1/Network/DNS/Record/list", params, list)
47-
return list
48+
err := c.Backend.Call("v1/Network/DNS/Record/list", params, list)
49+
if err != nil {
50+
return nil, err
51+
}
52+
return list, nil
4853
}
4954

5055
// Update will update a DNS Record.
51-
func (c *DNSClient) Update(params *DNSRecordParams) *DNSRecordItem {
52-
var result DNSRecordItem
53-
c.Backend.CallInto("v1/Network/DNS/Record/update", params, &result)
54-
55-
return &result
56+
func (c *DNSClient) Update(params *DNSRecordParams) (*DNSRecord, error) {
57+
var result DNSRecord
58+
err := c.Backend.Call("v1/Network/DNS/Record/update", params, &result)
59+
if err != nil {
60+
return nil, err
61+
}
62+
return &result, nil
5663
}
5764

5865
// Delete will delete a DNS Record.
59-
func (c *DNSClient) Delete(params *DNSRecordParams) *DNSRecordDeletion {
66+
func (c *DNSClient) Delete(params *DNSRecordParams) (*DNSRecordDeletion, error) {
6067
var result DNSRecordDeletion
61-
c.Backend.CallInto("v1/Network/DNS/Record/delete", params, &result)
62-
63-
return &result
68+
err := c.Backend.Call("v1/Network/DNS/Record/delete", params, &result)
69+
if err != nil {
70+
return nil, err
71+
}
72+
return &result, nil
6473
}

network/load_balancer.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package network
33
import (
44
"net"
55

6-
liquidweb "git.liquidweb.com/masre/liquidweb-go"
76
"git.liquidweb.com/masre/liquidweb-go/types"
87
)
98

@@ -66,14 +65,7 @@ type LoadBalancer struct {
6665
VIP types.IPAddr `json:"vip,omitempty"`
6766
}
6867

69-
// LoadBalancerItem is an envelope for the API result containing either a load balancer or an error.
70-
type LoadBalancerItem struct {
71-
liquidweb.LWAPIError
72-
LoadBalancer
73-
}
74-
7568
// LoadBalancerDeletion represents the API result when deleting a load balancer.
7669
type LoadBalancerDeletion struct {
77-
liquidweb.LWAPIError
7870
Destroyed string `json:"destroyed"`
7971
}

network/load_balancer_client.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66

77
// LoadBalancerBackend describes the interface for interactions with the API.
88
type LoadBalancerBackend interface {
9-
Create(LoadBalancerParams) (*LoadBalancerItem, error)
10-
Details(string) *LoadBalancerItem
11-
Update(LoadBalancerParams) *LoadBalancerItem
12-
Delete(string) *LoadBalancerDeletion
9+
Create(LoadBalancerParams) (*LoadBalancer, error)
10+
Details(string) (*LoadBalancer, error)
11+
Update(LoadBalancerParams) (*LoadBalancer, error)
12+
Delete(string) (*LoadBalancerDeletion, error)
1313
}
1414

1515
// LoadBalancerClient is the backend implementation for interacting with the API.
@@ -18,9 +18,9 @@ type LoadBalancerClient struct {
1818
}
1919

2020
// Create creates a new load balancer.
21-
func (c *LoadBalancerClient) Create(params LoadBalancerParams) (*LoadBalancerItem, error) {
22-
var result LoadBalancerItem
23-
err := c.Backend.CallInto("v1/Network/LoadBalancer/create", params, &result)
21+
func (c *LoadBalancerClient) Create(params LoadBalancerParams) (*LoadBalancer, error) {
22+
var result LoadBalancer
23+
err := c.Backend.Call("v1/Network/LoadBalancer/create", params, &result)
2424
if err != nil {
2525
return nil, err
2626
}
@@ -29,29 +29,36 @@ func (c *LoadBalancerClient) Create(params LoadBalancerParams) (*LoadBalancerIte
2929
}
3030

3131
// Details returns details about a load balancer.
32-
func (c *LoadBalancerClient) Details(uniqID string) *LoadBalancerItem {
33-
var result LoadBalancerItem
32+
func (c *LoadBalancerClient) Details(uniqID string) (*LoadBalancer, error) {
33+
var result LoadBalancer
3434
params := LoadBalancerParams{UniqID: uniqID}
3535

36-
c.Backend.CallInto("v1/Network/LoadBalancer/details", params, &result)
36+
err := c.Backend.Call("v1/Network/LoadBalancer/details", params, &result)
37+
if err != nil {
38+
return nil, err
39+
}
3740

38-
return &result
41+
return &result, nil
3942
}
4043

4144
// Update will update a load balancer.
42-
func (c *LoadBalancerClient) Update(params LoadBalancerParams) *LoadBalancerItem {
43-
var result LoadBalancerItem
44-
c.Backend.CallInto("v1/Network/LoadBalancer/update", params, &result)
45-
46-
return &result
45+
func (c *LoadBalancerClient) Update(params LoadBalancerParams) (*LoadBalancer, error) {
46+
var result LoadBalancer
47+
err := c.Backend.Call("v1/Network/LoadBalancer/update", params, &result)
48+
if err != nil {
49+
return nil, err
50+
}
51+
return &result, nil
4752
}
4853

4954
// Delete will delete a load balancer.
50-
func (c *LoadBalancerClient) Delete(uniqID string) *LoadBalancerDeletion {
55+
func (c *LoadBalancerClient) Delete(uniqID string) (*LoadBalancerDeletion, error) {
5156
var result LoadBalancerDeletion
5257
params := LoadBalancerParams{UniqID: uniqID}
5358

54-
c.Backend.CallInto("v1/Network/LoadBalancer/delete", params, &result)
55-
56-
return &result
59+
err := c.Backend.Call("v1/Network/LoadBalancer/delete", params, &result)
60+
if err != nil {
61+
return nil, err
62+
}
63+
return &result, nil
5764
}

network/vip.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package network
22

33
import (
4-
liquidweb "git.liquidweb.com/masre/liquidweb-go"
54
"git.liquidweb.com/masre/liquidweb-go/types"
65
)
76

@@ -23,15 +22,8 @@ type VIP struct {
2322
IP string `json:"ip,omitempty"`
2423
}
2524

26-
// VIPItem is an envelope for the API result containing either a VIP or an error.
27-
type VIPItem struct {
28-
liquidweb.LWAPIError
29-
VIP
30-
}
31-
3225
// VIPDeletion represents the API result when deleting a VIP.
3326
type VIPDeletion struct {
34-
liquidweb.LWAPIError
3527
Destroyed string `json:"destroyed"`
3628
}
3729

0 commit comments

Comments
 (0)