-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathproject.go
193 lines (154 loc) · 4.37 KB
/
project.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package lib
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/vmware/harbor/tests/apitests/api-testing/client"
"github.com/vmware/harbor/tests/apitests/api-testing/models"
)
//ProjectUtil : Util methods for project related
type ProjectUtil struct {
rootURI string
testingClient *client.APIClient
}
//NewProjectUtil : Constructor
func NewProjectUtil(rootURI string, httpClient *client.APIClient) *ProjectUtil {
if len(strings.TrimSpace(rootURI)) == 0 || httpClient == nil {
return nil
}
return &ProjectUtil{
rootURI: rootURI,
testingClient: httpClient,
}
}
//GetProjects : Get projects
//If name specified, then only get the specified project
func (pu *ProjectUtil) GetProjects(name string) ([]models.ExistingProject, error) {
url := pu.rootURI + "/api/projects"
if len(strings.TrimSpace(name)) > 0 {
url = url + "?name=" + name
}
data, err := pu.testingClient.Get(url)
if err != nil {
return nil, err
}
var pros []models.ExistingProject
if err = json.Unmarshal(data, &pros); err != nil {
return nil, err
}
return pros, nil
}
//GetProjectID : Get the project ID
//If no project existing with the name, then return -1
func (pu *ProjectUtil) GetProjectID(projectName string) int {
pros, err := pu.GetProjects(projectName)
if err != nil {
return -1
}
if len(pros) == 0 {
return -1
}
for _, pro := range pros {
if pro.Name == projectName {
return pro.ID
}
}
return -1
}
//CreateProject :Create project
func (pu *ProjectUtil) CreateProject(projectName string, accessLevel bool) error {
if len(strings.TrimSpace(projectName)) == 0 {
return errors.New("Empty project name for creating")
}
p := models.Project{
Name: projectName,
Metadata: &models.Metadata{
AccessLevel: fmt.Sprintf("%v", accessLevel),
},
}
body, err := json.Marshal(&p)
if err != nil {
return err
}
url := pu.rootURI + "/api/projects"
return pu.testingClient.Post(url, body)
}
//DeleteProject : Delete project
func (pu *ProjectUtil) DeleteProject(projectName string) error {
if len(strings.TrimSpace(projectName)) == 0 {
return errors.New("Empty project name for deleting")
}
pid := pu.GetProjectID(projectName)
if pid == -1 {
return errors.New("Failed to get project ID")
}
url := fmt.Sprintf("%s%s%d", pu.rootURI, "/api/projects/", pid)
return pu.testingClient.Delete(url)
}
//AssignRole : Assign role to user
func (pu *ProjectUtil) AssignRole(projectName, username string) error {
if len(strings.TrimSpace(projectName)) == 0 ||
len(strings.TrimSpace(username)) == 0 {
return errors.New("Project name and username are required for assigning role")
}
pid := pu.GetProjectID(projectName)
if pid == -1 {
return fmt.Errorf("Failed to get project ID with name %s", projectName)
}
m := models.Member{
RoleID: 2,
Member: &models.MemberUser{
Username: username,
},
}
body, err := json.Marshal(&m)
if err != nil {
return err
}
url := fmt.Sprintf("%s%s%d%s", pu.rootURI, "/api/projects/", pid, "/members")
return pu.testingClient.Post(url, body)
}
//RevokeRole : RevokeRole role from user
func (pu *ProjectUtil) RevokeRole(projectName string, username string) error {
if len(strings.TrimSpace(projectName)) == 0 {
return errors.New("Project name is required for revoking role")
}
if len(strings.TrimSpace(username)) == 0 {
return errors.New("User ID is required for revoking role")
}
pid := pu.GetProjectID(projectName)
if pid == -1 {
return fmt.Errorf("Failed to get project ID with name %s", projectName)
}
m, err := pu.GetProjectMember(pid, username)
if err != nil {
return err
}
url := fmt.Sprintf("%s%s%d%s%d", pu.rootURI, "/api/projects/", pid, "/members/", m.MID)
return pu.testingClient.Delete(url)
}
//GetProjectMember : Get the project member by name
func (pu *ProjectUtil) GetProjectMember(pid int, member string) (*models.ExistingMember, error) {
if pid == 0 {
return nil, errors.New("invalid project ID")
}
if len(strings.TrimSpace(member)) == 0 {
return nil, errors.New("empty member name")
}
url := fmt.Sprintf("%s/api/projects/%d/members", pu.rootURI, pid)
data, err := pu.testingClient.Get(url)
if err != nil {
return nil, err
}
members := []*models.ExistingMember{}
if err := json.Unmarshal(data, &members); err != nil {
return nil, err
}
for _, m := range members {
if m.Name == member {
return m, nil
}
}
return nil, fmt.Errorf("no member found by the name '%s'", member)
}