This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuser.go
101 lines (86 loc) · 2.38 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// User represents a User
type User struct {
Email string `json:"email"`
Firstname string `json:"firstname"`
Fullname string `json:"fullname"`
ID string `json:"id"`
Lastname string `json:"lastname"`
Organizations []Organization `json:"organizations"`
Roles []Role `json:"roles"`
SSHPublicKeys []KeyDefinition `json:"ssh_public_keys"`
}
// KeyDefinition represents a key
type KeyDefinition struct {
Key string `json:"key"`
Fingerprint string `json:"fingerprint,omitempty"`
}
// UsersDefinition represents the response of a GET /user
type UsersDefinition struct {
User User `json:"user"`
}
// UserPatchSSHKeyDefinition represents a User Patch
type UserPatchSSHKeyDefinition struct {
SSHPublicKeys []KeyDefinition `json:"ssh_public_keys"`
}
// PatchUserSSHKey updates a user
func (s *API) PatchUserSSHKey(UserID string, definition UserPatchSSHKeyDefinition) (*User, error) {
resp, err := s.PatchResponse(AccountAPI, fmt.Sprintf("users/%s", UserID), definition)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var user UsersDefinition
if err = json.Unmarshal(body, &user); err != nil {
return nil, err
}
return &user.User, nil
}
// GetUserID returns the userID
func (s *API) GetUserID() (string, error) {
resp, err := s.GetResponsePaginate(AccountAPI, fmt.Sprintf("tokens/%s", s.Token), url.Values{})
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return "", err
}
var token getTokenResponse
if err = json.Unmarshal(body, &token); err != nil {
return "", err
}
return token.Token.UserID, nil
}
// GetUser returns the user
func (s *API) GetUser() (*User, error) {
userID, err := s.GetUserID()
if err != nil {
return nil, err
}
resp, err := s.GetResponsePaginate(AccountAPI, fmt.Sprintf("users/%s", userID), url.Values{})
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := s.handleHTTPError([]int{http.StatusOK}, resp)
if err != nil {
return nil, err
}
var user UsersDefinition
if err = json.Unmarshal(body, &user); err != nil {
return nil, err
}
return &user.User, nil
}