| 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 |
|
| 11 |
|
| 12 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 45 |
|
| 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 |
|
| 60 |
|
| 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 |
|
| 78 |
|
| 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 |
|
| 92 |
|
| 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 |
} |