| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package cloudapi |
| 9 |
|
| 10 |
import ( |
| 11 |
"net/http" |
| 12 |
"net/url" |
| 13 |
"path" |
| 14 |
|
| 15 |
"github.com/joyent/gocommon/client" |
| 16 |
jh "github.com/joyent/gocommon/http" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
|
| 21 |
DefaultAPIVersion = "~7.3" |
| 22 |
|
| 23 |
|
| 24 |
apiKeys = "keys" |
| 25 |
apiPackages = "packages" |
| 26 |
apiImages = "images" |
| 27 |
apiDatacenters = "datacenters" |
| 28 |
apiMachines = "machines" |
| 29 |
apiMetadata = "metadata" |
| 30 |
apiSnapshots = "snapshots" |
| 31 |
apiTags = "tags" |
| 32 |
apiAnalytics = "analytics" |
| 33 |
apiInstrumentations = "instrumentations" |
| 34 |
apiInstrumentationsValue = "value" |
| 35 |
apiInstrumentationsRaw = "raw" |
| 36 |
apiInstrumentationsHeatmap = "heatmap" |
| 37 |
apiInstrumentationsImage = "image" |
| 38 |
apiInstrumentationsDetails = "details" |
| 39 |
apiUsage = "usage" |
| 40 |
apiAudit = "audit" |
| 41 |
apiFirewallRules = "fwrules" |
| 42 |
apiFirewallRulesEnable = "enable" |
| 43 |
apiFirewallRulesDisable = "disable" |
| 44 |
apiNetworks = "networks" |
| 45 |
apiFabricVLANs = "fabrics/default/vlans" |
| 46 |
apiFabricNetworks = "networks" |
| 47 |
apiNICs = "nics" |
| 48 |
apiServices = "services" |
| 49 |
|
| 50 |
|
| 51 |
actionExport = "export" |
| 52 |
actionStop = "stop" |
| 53 |
actionStart = "start" |
| 54 |
actionReboot = "reboot" |
| 55 |
actionResize = "resize" |
| 56 |
actionRename = "rename" |
| 57 |
actionEnableFw = "enable_firewall" |
| 58 |
actionDisableFw = "disable_firewall" |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
type Client struct { |
| 63 |
client client.Client |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func New(client client.Client) *Client { |
| 68 |
return &Client{client} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
type Filter struct { |
| 73 |
v url.Values |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
func NewFilter() *Filter { |
| 78 |
return &Filter{make(url.Values)} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
func (f *Filter) Set(filter, value string) { |
| 83 |
f.v.Set(filter, value) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (f *Filter) Add(filter, value string) { |
| 88 |
f.v.Add(filter, value) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
type request struct { |
| 93 |
method string |
| 94 |
url string |
| 95 |
filter *Filter |
| 96 |
reqValue interface{} |
| 97 |
reqHeader http.Header |
| 98 |
resp interface{} |
| 99 |
respHeader *http.Header |
| 100 |
expectedStatus int |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func (c *Client) sendRequest(req request) (*jh.ResponseData, error) { |
| 105 |
request := jh.RequestData{ |
| 106 |
ReqValue: req.reqValue, |
| 107 |
ReqHeaders: req.reqHeader, |
| 108 |
} |
| 109 |
if req.filter != nil { |
| 110 |
request.Params = &req.filter.v |
| 111 |
} |
| 112 |
if req.expectedStatus == 0 { |
| 113 |
req.expectedStatus = http.StatusOK |
| 114 |
} |
| 115 |
respData := jh.ResponseData{ |
| 116 |
RespValue: req.resp, |
| 117 |
RespHeaders: req.respHeader, |
| 118 |
ExpectedStatus: []int{req.expectedStatus}, |
| 119 |
} |
| 120 |
err := c.client.SendRequest(req.method, req.url, "", &request, &respData) |
| 121 |
return &respData, err |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func makeURL(parts ...string) string { |
| 126 |
return path.Join(parts...) |
| 127 |
} |