|
| 1 | +package bitbucket |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/mitchellh/mapstructure" |
| 8 | +) |
| 9 | + |
| 10 | +type SSHKeys struct { |
| 11 | + c *Client |
| 12 | +} |
| 13 | + |
| 14 | +type SSHKey struct { |
| 15 | + Uuid string `json:"uuid"` |
| 16 | + Label string `json:"label"` |
| 17 | + Key string `json:"key"` |
| 18 | + Comment string `json:"comment"` |
| 19 | + CreatedOm string `json:"created_on"` |
| 20 | +} |
| 21 | + |
| 22 | +type SSHKeyRes struct { |
| 23 | + Page int32 |
| 24 | + Pagelen int32 |
| 25 | + MaxDepth int32 |
| 26 | + Size int32 |
| 27 | + Items []SSHKey |
| 28 | +} |
| 29 | + |
| 30 | +func decodeSSHKey(response interface{}) (*SSHKey, error) { |
| 31 | + respMap := response.(map[string]interface{}) |
| 32 | + |
| 33 | + if respMap["type"] == "error" { |
| 34 | + return nil, DecodeError(respMap) |
| 35 | + } |
| 36 | + |
| 37 | + var sshKey = new(SSHKey) |
| 38 | + err := mapstructure.Decode(respMap, sshKey) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + return sshKey, nil |
| 44 | +} |
| 45 | + |
| 46 | +func buildSSHKeysBody(opt *SSHKeyOptions) (string, error) { |
| 47 | + body := map[string]interface{}{} |
| 48 | + body["label"] = opt.Label |
| 49 | + body["key"] = opt.Key |
| 50 | + |
| 51 | + data, err := json.Marshal(body) |
| 52 | + if err != nil { |
| 53 | + return "", err |
| 54 | + } |
| 55 | + |
| 56 | + return string(data), nil |
| 57 | +} |
| 58 | + |
| 59 | +func decodeSSHKeys(keysResponse interface{}) (*SSHKeyRes, error) { |
| 60 | + keysResponseMap, ok := keysResponse.(map[string]interface{}) |
| 61 | + if !ok { |
| 62 | + return nil, errors.New("Not a valid format") |
| 63 | + } |
| 64 | + |
| 65 | + keyArray := keysResponseMap["values"].([]interface{}) |
| 66 | + var keys []SSHKey |
| 67 | + for _, keyEntry := range keyArray { |
| 68 | + var key SSHKey |
| 69 | + err := mapstructure.Decode(keyEntry, &key) |
| 70 | + if err == nil { |
| 71 | + keys = append(keys, key) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + page, ok := keysResponseMap["page"].(float64) |
| 76 | + if !ok { |
| 77 | + page = 0 |
| 78 | + } |
| 79 | + |
| 80 | + pagelen, ok := keysResponseMap["pagelen"].(float64) |
| 81 | + if !ok { |
| 82 | + pagelen = 0 |
| 83 | + } |
| 84 | + max_depth, ok := keysResponseMap["max_width"].(float64) |
| 85 | + if !ok { |
| 86 | + max_depth = 0 |
| 87 | + } |
| 88 | + size, ok := keysResponseMap["size"].(float64) |
| 89 | + if !ok { |
| 90 | + size = 0 |
| 91 | + } |
| 92 | + |
| 93 | + keysResp := &SSHKeyRes{ |
| 94 | + Page: int32(page), |
| 95 | + Pagelen: int32(pagelen), |
| 96 | + MaxDepth: int32(max_depth), |
| 97 | + Size: int32(size), |
| 98 | + Items: keys, |
| 99 | + } |
| 100 | + return keysResp, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (sk *SSHKeys) Create(ro *SSHKeyOptions) (*SSHKey, error) { |
| 104 | + data, err := buildSSHKeysBody(ro) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + urlStr := sk.c.requestUrl("/users/%s/ssh-keys", ro.Owner) |
| 109 | + response, err := sk.c.execute("POST", urlStr, data) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + return decodeSSHKey(response) |
| 115 | +} |
| 116 | + |
| 117 | +func (sk *SSHKeys) Get(ro *SSHKeyOptions) (*SSHKey, error) { |
| 118 | + urlStr := sk.c.requestUrl("/users/%s/ssh-keys/%s", ro.Owner, ro.Uuid) |
| 119 | + response, err := sk.c.execute("GET", urlStr, "") |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return decodeSSHKey(response) |
| 125 | +} |
| 126 | + |
| 127 | +func (sk *SSHKeys) Delete(ro *SSHKeyOptions) (interface{}, error) { |
| 128 | + urlStr := sk.c.requestUrl("/users/%s/ssh-keys/%s", ro.Owner, ro.Uuid) |
| 129 | + return sk.c.execute("DELETE", urlStr, "") |
| 130 | +} |
0 commit comments