|
| 1 | +// This file was automatically generated. DO NOT EDIT. |
| 2 | +// If you have any remark or suggestion do not hesitate to open an issue. |
| 3 | + |
| 4 | +package account |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "net" |
| 11 | + "net/http" |
| 12 | + "net/url" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/scaleway/scaleway-sdk-go/internal/errors" |
| 16 | + "github.com/scaleway/scaleway-sdk-go/internal/marshaler" |
| 17 | + "github.com/scaleway/scaleway-sdk-go/internal/parameter" |
| 18 | + "github.com/scaleway/scaleway-sdk-go/namegenerator" |
| 19 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 20 | +) |
| 21 | + |
| 22 | +// always import dependencies |
| 23 | +var ( |
| 24 | + _ fmt.Stringer |
| 25 | + _ json.Unmarshaler |
| 26 | + _ url.URL |
| 27 | + _ net.IP |
| 28 | + _ http.Header |
| 29 | + _ bytes.Reader |
| 30 | + _ time.Time |
| 31 | + |
| 32 | + _ scw.ScalewayRequest |
| 33 | + _ marshaler.Duration |
| 34 | + _ scw.File |
| 35 | + _ = parameter.AddToQuery |
| 36 | + _ = namegenerator.GetRandomName |
| 37 | +) |
| 38 | + |
| 39 | +// API this API allows to manage your scaleway account |
| 40 | +type API struct { |
| 41 | + client *scw.Client |
| 42 | +} |
| 43 | + |
| 44 | +// NewAPI returns a API object from a Scaleway client. |
| 45 | +func NewAPI(client *scw.Client) *API { |
| 46 | + return &API{ |
| 47 | + client: client, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +type ListSSHKeysRequestOrderBy string |
| 52 | + |
| 53 | +const ( |
| 54 | + // ListSSHKeysRequestOrderByCreatedAtAsc is [insert doc]. |
| 55 | + ListSSHKeysRequestOrderByCreatedAtAsc = ListSSHKeysRequestOrderBy("created_at_asc") |
| 56 | + // ListSSHKeysRequestOrderByCreatedAtDesc is [insert doc]. |
| 57 | + ListSSHKeysRequestOrderByCreatedAtDesc = ListSSHKeysRequestOrderBy("created_at_desc") |
| 58 | + // ListSSHKeysRequestOrderByUpdatedAtAsc is [insert doc]. |
| 59 | + ListSSHKeysRequestOrderByUpdatedAtAsc = ListSSHKeysRequestOrderBy("updated_at_asc") |
| 60 | + // ListSSHKeysRequestOrderByUpdatedAtDesc is [insert doc]. |
| 61 | + ListSSHKeysRequestOrderByUpdatedAtDesc = ListSSHKeysRequestOrderBy("updated_at_desc") |
| 62 | + // ListSSHKeysRequestOrderByNameAsc is [insert doc]. |
| 63 | + ListSSHKeysRequestOrderByNameAsc = ListSSHKeysRequestOrderBy("name_asc") |
| 64 | + // ListSSHKeysRequestOrderByNameDesc is [insert doc]. |
| 65 | + ListSSHKeysRequestOrderByNameDesc = ListSSHKeysRequestOrderBy("name_desc") |
| 66 | +) |
| 67 | + |
| 68 | +func (enum ListSSHKeysRequestOrderBy) String() string { |
| 69 | + if enum == "" { |
| 70 | + // return default value if empty |
| 71 | + return "created_at_asc" |
| 72 | + } |
| 73 | + return string(enum) |
| 74 | +} |
| 75 | + |
| 76 | +func (enum ListSSHKeysRequestOrderBy) MarshalJSON() ([]byte, error) { |
| 77 | + return []byte(fmt.Sprintf(`"%s"`, enum)), nil |
| 78 | +} |
| 79 | + |
| 80 | +func (enum *ListSSHKeysRequestOrderBy) UnmarshalJSON(data []byte) error { |
| 81 | + tmp := "" |
| 82 | + |
| 83 | + if err := json.Unmarshal(data, &tmp); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + *enum = ListSSHKeysRequestOrderBy(ListSSHKeysRequestOrderBy(tmp).String()) |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +type ListSSHKeysResponse struct { |
| 92 | + SSHKeys []*SSHKey `json:"ssh_keys"` |
| 93 | + |
| 94 | + TotalCount uint32 `json:"total_count"` |
| 95 | +} |
| 96 | + |
| 97 | +type SSHKey struct { |
| 98 | + ID string `json:"id"` |
| 99 | + |
| 100 | + Name string `json:"name"` |
| 101 | + |
| 102 | + PublicKey string `json:"public_key"` |
| 103 | + |
| 104 | + Fingerprint string `json:"fingerprint"` |
| 105 | + |
| 106 | + CreatedAt time.Time `json:"created_at"` |
| 107 | + |
| 108 | + UpdatedAt time.Time `json:"updated_at"` |
| 109 | + |
| 110 | + CreationInfo *SSHKeyCreationInfo `json:"creation_info"` |
| 111 | +} |
| 112 | + |
| 113 | +type SSHKeyCreationInfo struct { |
| 114 | + Address string `json:"address"` |
| 115 | + |
| 116 | + UserAgent string `json:"user_agent"` |
| 117 | + |
| 118 | + CountryCode string `json:"country_code"` |
| 119 | +} |
| 120 | + |
| 121 | +// Service API |
| 122 | + |
| 123 | +type ListSSHKeysRequest struct { |
| 124 | + // OrderBy |
| 125 | + // |
| 126 | + // Default value: created_at_asc |
| 127 | + OrderBy ListSSHKeysRequestOrderBy `json:"-"` |
| 128 | + |
| 129 | + Page *int32 `json:"-"` |
| 130 | + |
| 131 | + PageSize *int32 `json:"-"` |
| 132 | + |
| 133 | + Name *string `json:"-"` |
| 134 | + |
| 135 | + OrganizationID *string `json:"-"` |
| 136 | +} |
| 137 | + |
| 138 | +// ListSSHKeys list all SSH keys |
| 139 | +func (s *API) ListSSHKeys(req *ListSSHKeysRequest, opts ...scw.RequestOption) (*ListSSHKeysResponse, error) { |
| 140 | + var err error |
| 141 | + |
| 142 | + defaultOrganizationID, exist := s.client.GetDefaultOrganizationID() |
| 143 | + if (req.OrganizationID == nil || *req.OrganizationID == "") && exist { |
| 144 | + req.OrganizationID = &defaultOrganizationID |
| 145 | + } |
| 146 | + |
| 147 | + defaultPageSize, exist := s.client.GetDefaultPageSize() |
| 148 | + if (req.PageSize == nil || *req.PageSize == 0) && exist { |
| 149 | + req.PageSize = &defaultPageSize |
| 150 | + } |
| 151 | + |
| 152 | + query := url.Values{} |
| 153 | + parameter.AddToQuery(query, "order_by", req.OrderBy) |
| 154 | + parameter.AddToQuery(query, "page", req.Page) |
| 155 | + parameter.AddToQuery(query, "page_size", req.PageSize) |
| 156 | + parameter.AddToQuery(query, "name", req.Name) |
| 157 | + parameter.AddToQuery(query, "organization_id", req.OrganizationID) |
| 158 | + |
| 159 | + scwReq := &scw.ScalewayRequest{ |
| 160 | + Method: "GET", |
| 161 | + Path: "/account/v2alpha1/ssh-keys", |
| 162 | + Query: query, |
| 163 | + Headers: http.Header{}, |
| 164 | + } |
| 165 | + |
| 166 | + var resp ListSSHKeysResponse |
| 167 | + |
| 168 | + err = s.client.Do(scwReq, &resp, opts...) |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + return &resp, nil |
| 173 | +} |
| 174 | + |
| 175 | +// UnsafeGetTotalCount should not be used |
| 176 | +// Internal usage only |
| 177 | +func (r *ListSSHKeysResponse) UnsafeGetTotalCount() int { |
| 178 | + return int(r.TotalCount) |
| 179 | +} |
| 180 | + |
| 181 | +// UnsafeAppend should not be used |
| 182 | +// Internal usage only |
| 183 | +func (r *ListSSHKeysResponse) UnsafeAppend(res interface{}) (int, scw.SdkError) { |
| 184 | + results, ok := res.(*ListSSHKeysResponse) |
| 185 | + if !ok { |
| 186 | + return 0, errors.New("%T type cannot be appended to type %T", res, r) |
| 187 | + } |
| 188 | + |
| 189 | + r.SSHKeys = append(r.SSHKeys, results.SSHKeys...) |
| 190 | + r.TotalCount += uint32(len(results.SSHKeys)) |
| 191 | + return len(results.SSHKeys), nil |
| 192 | +} |
| 193 | + |
| 194 | +type CreateSSHKeyRequest struct { |
| 195 | + Name string `json:"name"` |
| 196 | + |
| 197 | + PublicKey string `json:"public_key"` |
| 198 | + |
| 199 | + OrganizationID string `json:"organization_id"` |
| 200 | +} |
| 201 | + |
| 202 | +// CreateSSHKey create an SSH key |
| 203 | +func (s *API) CreateSSHKey(req *CreateSSHKeyRequest, opts ...scw.RequestOption) (*SSHKey, error) { |
| 204 | + var err error |
| 205 | + |
| 206 | + if req.OrganizationID == "" { |
| 207 | + defaultOrganizationID, _ := s.client.GetDefaultOrganizationID() |
| 208 | + req.OrganizationID = defaultOrganizationID |
| 209 | + } |
| 210 | + |
| 211 | + scwReq := &scw.ScalewayRequest{ |
| 212 | + Method: "POST", |
| 213 | + Path: "/account/v2alpha1/ssh-keys", |
| 214 | + Headers: http.Header{}, |
| 215 | + } |
| 216 | + |
| 217 | + err = scwReq.SetBody(req) |
| 218 | + if err != nil { |
| 219 | + return nil, err |
| 220 | + } |
| 221 | + |
| 222 | + var resp SSHKey |
| 223 | + |
| 224 | + err = s.client.Do(scwReq, &resp, opts...) |
| 225 | + if err != nil { |
| 226 | + return nil, err |
| 227 | + } |
| 228 | + return &resp, nil |
| 229 | +} |
| 230 | + |
| 231 | +type GetSSHKeyRequest struct { |
| 232 | + SSHKeyID string `json:"-"` |
| 233 | +} |
| 234 | + |
| 235 | +// GetSSHKey get SSH key details |
| 236 | +func (s *API) GetSSHKey(req *GetSSHKeyRequest, opts ...scw.RequestOption) (*SSHKey, error) { |
| 237 | + var err error |
| 238 | + |
| 239 | + if fmt.Sprint(req.SSHKeyID) == "" { |
| 240 | + return nil, errors.New("field SSHKeyID cannot be empty in request") |
| 241 | + } |
| 242 | + |
| 243 | + scwReq := &scw.ScalewayRequest{ |
| 244 | + Method: "GET", |
| 245 | + Path: "/account/v2alpha1/ssh-key/" + fmt.Sprint(req.SSHKeyID) + "", |
| 246 | + Headers: http.Header{}, |
| 247 | + } |
| 248 | + |
| 249 | + var resp SSHKey |
| 250 | + |
| 251 | + err = s.client.Do(scwReq, &resp, opts...) |
| 252 | + if err != nil { |
| 253 | + return nil, err |
| 254 | + } |
| 255 | + return &resp, nil |
| 256 | +} |
| 257 | + |
| 258 | +type UpdateSSHKeyRequest struct { |
| 259 | + SSHKeyID string `json:"-"` |
| 260 | + |
| 261 | + Name *string `json:"name"` |
| 262 | +} |
| 263 | + |
| 264 | +// UpdateSSHKey update an SSH key |
| 265 | +func (s *API) UpdateSSHKey(req *UpdateSSHKeyRequest, opts ...scw.RequestOption) (*SSHKey, error) { |
| 266 | + var err error |
| 267 | + |
| 268 | + if fmt.Sprint(req.SSHKeyID) == "" { |
| 269 | + return nil, errors.New("field SSHKeyID cannot be empty in request") |
| 270 | + } |
| 271 | + |
| 272 | + scwReq := &scw.ScalewayRequest{ |
| 273 | + Method: "PATCH", |
| 274 | + Path: "/account/v2alpha1/ssh-key/" + fmt.Sprint(req.SSHKeyID) + "", |
| 275 | + Headers: http.Header{}, |
| 276 | + } |
| 277 | + |
| 278 | + err = scwReq.SetBody(req) |
| 279 | + if err != nil { |
| 280 | + return nil, err |
| 281 | + } |
| 282 | + |
| 283 | + var resp SSHKey |
| 284 | + |
| 285 | + err = s.client.Do(scwReq, &resp, opts...) |
| 286 | + if err != nil { |
| 287 | + return nil, err |
| 288 | + } |
| 289 | + return &resp, nil |
| 290 | +} |
| 291 | + |
| 292 | +type DeleteSSHKeyRequest struct { |
| 293 | + SSHKeyID string `json:"-"` |
| 294 | +} |
| 295 | + |
| 296 | +// DeleteSSHKey delete an SSH key |
| 297 | +func (s *API) DeleteSSHKey(req *DeleteSSHKeyRequest, opts ...scw.RequestOption) error { |
| 298 | + var err error |
| 299 | + |
| 300 | + if fmt.Sprint(req.SSHKeyID) == "" { |
| 301 | + return errors.New("field SSHKeyID cannot be empty in request") |
| 302 | + } |
| 303 | + |
| 304 | + scwReq := &scw.ScalewayRequest{ |
| 305 | + Method: "DELETE", |
| 306 | + Path: "/account/v2alpha1/ssh-key/" + fmt.Sprint(req.SSHKeyID) + "", |
| 307 | + Headers: http.Header{}, |
| 308 | + } |
| 309 | + |
| 310 | + err = s.client.Do(scwReq, nil, opts...) |
| 311 | + if err != nil { |
| 312 | + return err |
| 313 | + } |
| 314 | + return nil |
| 315 | +} |
0 commit comments