1package gsclient 2 3import ( 4 "context" 5 "errors" 6 "net/http" 7 "path" 8) 9 10//SshkeyList JSON struct of a list of SSH-keys 11type SshkeyList struct { 12 //Array of SSH-keys 13 List map[string]SshkeyProperties `json:"sshkeys"` 14} 15 16//Sshkey JSON struct of a single SSH-key 17type Sshkey struct { 18 //Properties of a SSH-key 19 Properties SshkeyProperties `json:"sshkey"` 20} 21 22//SshkeyProperties JSON struct of properties of a single SSH-key 23type SshkeyProperties struct { 24 //The human-readable name of the object. It supports the full UTF-8 charset, with a maximum of 64 characters. 25 Name string `json:"name"` 26 27 //The UUID of an object is always unique, and refers to a specific object. 28 ObjectUUID string `json:"object_uuid"` 29 30 //Status indicates the status of the object. 31 Status string `json:"status"` 32 33 //Defines the date and time the object was initially created. 34 CreateTime GSTime `json:"create_time"` 35 36 //Defines the date and time of the last object change. 37 ChangeTime GSTime `json:"change_time"` 38 39 //The OpenSSH public key string (all key types are supported => ed25519, ecdsa, dsa, rsa, rsa1). 40 Sshkey string `json:"sshkey"` 41 42 //List of labels. 43 Labels []string `json:"labels"` 44 45 //The User-UUID of the account which created this SSH Key. 46 UserUUID string `json:"user_uuid"` 47} 48 49//SshkeyCreateRequest JSON struct of a request for creating a SSH-key 50type SshkeyCreateRequest struct { 51 //The human-readable name of the object. It supports the full UTF-8 charset, with a maximum of 64 characters. 52 Name string `json:"name"` 53 54 //The OpenSSH public key string (all key types are supported => ed25519, ecdsa, dsa, rsa, rsa1). 55 Sshkey string `json:"sshkey"` 56 57 //List of labels. Optional. 58 Labels []string `json:"labels,omitempty"` 59} 60 61//SshkeyUpdateRequest JSON struct of a request for updating a SSH-key 62type SshkeyUpdateRequest struct { 63 //The human-readable name of the object. It supports the full UTF-8 charset, with a maximum of 64 characters. 64 //Optional. 65 Name string `json:"name,omitempty"` 66 67 //The OpenSSH public key string (all key types are supported => ed25519, ecdsa, dsa, rsa, rsa1). Optional. 68 Sshkey string `json:"sshkey,omitempty"` 69 70 //List of labels. Optional. 71 Labels *[]string `json:"labels,omitempty"` 72} 73 74//GetSshkey gets a ssh key 75// 76//See: https://gridscale.io/en//api-documentation/index.html#operation/getSshKey 77func (c *Client) GetSshkey(ctx context.Context, id string) (Sshkey, error) { 78 if !isValidUUID(id) { 79 return Sshkey{}, errors.New("'id' is invalid") 80 } 81 r := gsRequest{ 82 uri: path.Join(apiSshkeyBase, id), 83 method: http.MethodGet, 84 skipCheckingRequest: true, 85 } 86 var response Sshkey 87 err := r.execute(ctx, *c, &response) 88 return response, err 89} 90 91//GetSshkeyList gets a list of ssh keys 92// 93//See: https://gridscale.io/en//api-documentation/index.html#operation/getSshKeys 94func (c *Client) GetSshkeyList(ctx context.Context) ([]Sshkey, error) { 95 r := gsRequest{ 96 uri: apiSshkeyBase, 97 method: http.MethodGet, 98 skipCheckingRequest: true, 99 } 100 101 var response SshkeyList 102 var sshKeys []Sshkey 103 err := r.execute(ctx, *c, &response) 104 for _, properties := range response.List { 105 sshKeys = append(sshKeys, Sshkey{Properties: properties}) 106 } 107 return sshKeys, err 108} 109 110//CreateSshkey creates a ssh key 111// 112//See: https://gridscale.io/en//api-documentation/index.html#operation/createSshKey 113func (c *Client) CreateSshkey(ctx context.Context, body SshkeyCreateRequest) (CreateResponse, error) { 114 r := gsRequest{ 115 uri: apiSshkeyBase, 116 method: "POST", 117 body: body, 118 } 119 var response CreateResponse 120 err := r.execute(ctx, *c, &response) 121 return response, err 122} 123 124//DeleteSshkey deletes a ssh key 125// 126//See: https://gridscale.io/en//api-documentation/index.html#operation/deleteSshKey 127func (c *Client) DeleteSshkey(ctx context.Context, id string) error { 128 if !isValidUUID(id) { 129 return errors.New("'id' is invalid") 130 } 131 r := gsRequest{ 132 uri: path.Join(apiSshkeyBase, id), 133 method: http.MethodDelete, 134 } 135 return r.execute(ctx, *c, nil) 136} 137 138//UpdateSshkey updates a ssh key 139// 140//See: https://gridscale.io/en//api-documentation/index.html#operation/updateSshKey 141func (c *Client) UpdateSshkey(ctx context.Context, id string, body SshkeyUpdateRequest) error { 142 if !isValidUUID(id) { 143 return errors.New("'id' is invalid") 144 } 145 r := gsRequest{ 146 uri: path.Join(apiSshkeyBase, id), 147 method: http.MethodPatch, 148 body: body, 149 } 150 return r.execute(ctx, *c, nil) 151} 152 153//GetSshkeyEventList gets a ssh key's events 154// 155//See: https://gridscale.io/en//api-documentation/index.html#operation/getSshKeyEvents 156func (c *Client) GetSshkeyEventList(ctx context.Context, id string) ([]Event, error) { 157 if !isValidUUID(id) { 158 return nil, errors.New("'id' is invalid") 159 } 160 r := gsRequest{ 161 uri: path.Join(apiSshkeyBase, id, "events"), 162 method: http.MethodGet, 163 skipCheckingRequest: true, 164 } 165 var response EventList 166 var sshEvents []Event 167 err := r.execute(ctx, *c, &response) 168 for _, properties := range response.List { 169 sshEvents = append(sshEvents, Event{Properties: properties}) 170 } 171 return sshEvents, err 172} 173