Debian Codegraph @

cloudapi/machine_tags.go

raw symbols SCIP
1 package cloudapi
2
3 import (
4 "net/http"
5
6 "github.com/joyent/gocommon/client"
7 "github.com/joyent/gocommon/errors"
8 )
9
10 // AddMachineTags adds additional tags to the specified machine.
11 // This API lets you append new tags, not overwrite existing tags.
12 // See API docs: http://apidocs.joyent.com/cloudapi/#AddMachineTags
13 func (c *Client) AddMachineTags(machineID string, tags map[string]string) (map[string]string, error) {
14 var resp map[string]string
15 req := request{
16 method: client.POST,
17 url: makeURL(apiMachines, machineID, apiTags),
18 reqValue: tags,
19 resp: &resp,
20 }
21 if _, err := c.sendRequest(req); err != nil {
22 return nil, errors.Newf(err, "failed to add tags for machine with id %s", machineID)
23 }
24 return resp, nil
25 }
26
27 // ReplaceMachineTags replaces existing tags for the specified machine.
28 // This API lets you overwrite existing tags, not append to existing tags.
29 // See API docs: http://apidocs.joyent.com/cloudapi/#ReplaceMachineTags
30 func (c *Client) ReplaceMachineTags(machineID string, tags map[string]string) (map[string]string, error) {
31 var resp map[string]string
32 req := request{
33 method: client.PUT,
34 url: makeURL(apiMachines, machineID, apiTags),
35 reqValue: tags,
36 resp: &resp,
37 }
38 if _, err := c.sendRequest(req); err != nil {
39 return nil, errors.Newf(err, "failed to replace tags for machine with id %s", machineID)
40 }
41 return resp, nil
42 }
43
44 // ListMachineTags returns the complete set of tags associated with the specified machine.
45 // See API docs: http://apidocs.joyent.com/cloudapi/#ListMachineTags
46 func (c *Client) ListMachineTags(machineID string) (map[string]string, error) {
47 var resp map[string]string
48 req := request{
49 method: client.GET,
50 url: makeURL(apiMachines, machineID, apiTags),
51 resp: &resp,
52 }
53 if _, err := c.sendRequest(req); err != nil {
54 return nil, errors.Newf(err, "failed to get list of tags for machine with id %s", machineID)
55 }
56 return resp, nil
57 }
58
59 // GetMachineTag returns the value for a single tag on the specified machine.
60 // See API docs: http://apidocs.joyent.com/cloudapi/#GetMachineTag
61 func (c *Client) GetMachineTag(machineID, tagKey string) (string, error) {
62 var resp []byte
63 requestHeaders := make(http.Header)
64 requestHeaders.Set("Accept", "text/plain")
65 req := request{
66 method: client.GET,
67 url: makeURL(apiMachines, machineID, apiTags, tagKey),
68 resp: &resp,
69 reqHeader: requestHeaders,
70 }
71 if _, err := c.sendRequest(req); err != nil {
72 return "", errors.Newf(err, "failed to get tag %s for machine with id %s", tagKey, machineID)
73 }
74 return string(resp), nil
75 }
76
77 // DeleteMachineTag deletes a single tag from the specified machine.
78 // See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineTag
79 func (c *Client) DeleteMachineTag(machineID, tagKey string) error {
80 req := request{
81 method: client.DELETE,
82 url: makeURL(apiMachines, machineID, apiTags, tagKey),
83 expectedStatus: http.StatusNoContent,
84 }
85 if _, err := c.sendRequest(req); err != nil {
86 return errors.Newf(err, "failed to delete tag with key %s for machine with id %s", tagKey, machineID)
87 }
88 return nil
89 }
90
91 // DeleteMachineTags deletes all tags from the specified machine.
92 // See API docs: http://apidocs.joyent.com/cloudapi/#DeleteMachineTags
93 func (c *Client) DeleteMachineTags(machineID string) error {
94 req := request{
95 method: client.DELETE,
96 url: makeURL(apiMachines, machineID, apiTags),
97 expectedStatus: http.StatusNoContent,
98 }
99 if _, err := c.sendRequest(req); err != nil {
100 return errors.Newf(err, "failed to delete tags for machine with id %s", machineID)
101 }
102 return nil
103 }