|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +type TeamUser struct { |
| 8 | + ID string `json:"id,omitempty"` |
| 9 | + UserName string `json:"userName,omitempty"` |
| 10 | +} |
| 11 | + |
| 12 | +// Team spec |
| 13 | +type Team struct { |
| 14 | + ID string `json:"_id,omitempty"` |
| 15 | + Name string `json:"name,omitempty"` |
| 16 | + Type string `json:"type,omitempty"` |
| 17 | + Account string `json:"account,omitempty"` |
| 18 | + Tags []string `json:"tags,omitempty"` |
| 19 | + Users []TeamUser `json:"users,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +// NewTeam Codefresh API expects a list of users IDs to create a new team |
| 23 | +type NewTeam struct { |
| 24 | + ID string `json:"_id,omitempty"` |
| 25 | + Name string `json:"name,omitempty"` |
| 26 | + Type string `json:"type,omitempty"` |
| 27 | + Account string `json:"account,omitempty"` |
| 28 | + Tags []string `json:"tags,omitempty"` |
| 29 | + Users []string `json:"users,omitempty"` |
| 30 | +} |
| 31 | + |
| 32 | +// GetID implement CodefreshObject interface |
| 33 | +func (team *Team) GetID() string { |
| 34 | + return team.ID |
| 35 | +} |
| 36 | + |
| 37 | +func (client *Client) GetTeamList() ([]Team, error) { |
| 38 | + fullPath := "/team" |
| 39 | + opts := RequestOptions{ |
| 40 | + Path: fullPath, |
| 41 | + Method: "GET", |
| 42 | + } |
| 43 | + |
| 44 | + resp, err := client.RequestAPI(&opts) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + var teams []Team |
| 51 | + |
| 52 | + err = DecodeResponseInto(resp, &teams) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + return teams, nil |
| 58 | +} |
| 59 | + |
| 60 | +func (client *Client) GetTeamByName(name string) (*Team, error) { |
| 61 | + |
| 62 | + teams, err := client.GetTeamList() |
| 63 | + if err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + |
| 67 | + for _, team := range teams { |
| 68 | + if team.Name == name { |
| 69 | + return &team, nil |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return nil, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (client *Client) GetTeamByID(id string) (*Team, error) { |
| 77 | + |
| 78 | + teams, err := client.GetTeamList() |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + for _, team := range teams { |
| 84 | + if team.ID == id { |
| 85 | + return &team, nil |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return nil, nil |
| 90 | +} |
| 91 | + |
| 92 | +// |
| 93 | +func ConvertToNewTeam(team *Team) *NewTeam { |
| 94 | + var users []string |
| 95 | + |
| 96 | + for _, user := range team.Users { |
| 97 | + users = append(users, user.ID) |
| 98 | + } |
| 99 | + |
| 100 | + return &NewTeam{ |
| 101 | + ID: team.ID, |
| 102 | + Name: team.Name, |
| 103 | + Type: team.Type, |
| 104 | + Account: team.Account, |
| 105 | + Tags: team.Tags, |
| 106 | + Users: users, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// CreateTeam POST team |
| 111 | +func (client *Client) CreateTeam(team *Team) (*NewTeam, error) { |
| 112 | + |
| 113 | + newTeam := ConvertToNewTeam(team) |
| 114 | + body, err := EncodeToJSON(newTeam) |
| 115 | + |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + opts := RequestOptions{ |
| 120 | + Path: "/team", |
| 121 | + Method: "POST", |
| 122 | + Body: body, |
| 123 | + } |
| 124 | + |
| 125 | + resp, err := client.RequestAPI(&opts) |
| 126 | + |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + var respTeam NewTeam |
| 132 | + err = DecodeResponseInto(resp, &respTeam) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + return &respTeam, nil |
| 138 | +} |
| 139 | + |
| 140 | +// DeleteTeam |
| 141 | +func (client *Client) DeleteTeam(id string) error { |
| 142 | + fullPath := fmt.Sprintf("/team/%s", id) |
| 143 | + opts := RequestOptions{ |
| 144 | + Path: fullPath, |
| 145 | + Method: "DELETE", |
| 146 | + } |
| 147 | + |
| 148 | + _, err := client.RequestAPI(&opts) |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func (client *Client) SynchronizeClientWithGroup(name, ssoType string, notifications bool) error { |
| 158 | + |
| 159 | + fullPath := fmt.Sprintf("/team/group/synchronize/name/%s/type/%s?disableNotifications=%b", name, ssoType, notifications) |
| 160 | + opts := RequestOptions{ |
| 161 | + Path: fullPath, |
| 162 | + Method: "GET", |
| 163 | + } |
| 164 | + |
| 165 | + _, err := client.RequestAPI(&opts) |
| 166 | + |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func (client *Client) AddUserToTeam(teamID, userID string) (*Team, error) { |
| 175 | + |
| 176 | + fullPath := fmt.Sprintf("/team/%s/%s/assignUserToTeam", teamID, userID) |
| 177 | + opts := RequestOptions{ |
| 178 | + Path: fullPath, |
| 179 | + Method: "PUT", |
| 180 | + } |
| 181 | + |
| 182 | + resp, err := client.RequestAPI(&opts) |
| 183 | + |
| 184 | + if err != nil { |
| 185 | + return nil, err |
| 186 | + } |
| 187 | + |
| 188 | + var respTeam Team |
| 189 | + err = DecodeResponseInto(resp, &respTeam) |
| 190 | + if err != nil { |
| 191 | + return nil, err |
| 192 | + } |
| 193 | + |
| 194 | + return &respTeam, nil |
| 195 | +} |
| 196 | + |
| 197 | +func (client *Client) DeleteUserFromTeam(teamID, userID string) (*Team, error) { |
| 198 | + |
| 199 | + fullPath := fmt.Sprintf("/team/%s/%s/deleteUserFromTeam", teamID, userID) |
| 200 | + opts := RequestOptions{ |
| 201 | + Path: fullPath, |
| 202 | + Method: "PUT", |
| 203 | + } |
| 204 | + |
| 205 | + resp, err := client.RequestAPI(&opts) |
| 206 | + |
| 207 | + if err != nil { |
| 208 | + return nil, err |
| 209 | + } |
| 210 | + |
| 211 | + var respTeam Team |
| 212 | + err = DecodeResponseInto(resp, &respTeam) |
| 213 | + if err != nil { |
| 214 | + return nil, err |
| 215 | + } |
| 216 | + |
| 217 | + return &respTeam, nil |
| 218 | +} |
| 219 | + |
| 220 | +func (client *Client) RenameTeam(teamID, name string) (*Team, error) { |
| 221 | + |
| 222 | + fullPath := fmt.Sprintf("/team/%s/renameTeam", teamID) |
| 223 | + |
| 224 | + team := Team{Name: name} |
| 225 | + body, err := EncodeToJSON(team) |
| 226 | + |
| 227 | + if err != nil { |
| 228 | + return nil, err |
| 229 | + } |
| 230 | + |
| 231 | + opts := RequestOptions{ |
| 232 | + Path: fullPath, |
| 233 | + Method: "PUT", |
| 234 | + Body: body, |
| 235 | + } |
| 236 | + |
| 237 | + resp, err := client.RequestAPI(&opts) |
| 238 | + |
| 239 | + if err != nil { |
| 240 | + return nil, err |
| 241 | + } |
| 242 | + |
| 243 | + var respTeam Team |
| 244 | + err = DecodeResponseInto(resp, &respTeam) |
| 245 | + if err != nil { |
| 246 | + return nil, err |
| 247 | + } |
| 248 | + |
| 249 | + return &respTeam, nil |
| 250 | +} |
0 commit comments