2020-09-23 20:19:43 +03:00
|
|
|
/*
|
2020-10-09 11:15:42 +03:00
|
|
|
Copyright 2020 Docker Hub Tool authors
|
2020-09-23 20:19:43 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
2020-10-22 19:11:21 +03:00
|
|
|
"context"
|
2020-09-23 20:19:43 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-10-20 12:32:04 +03:00
|
|
|
"sort"
|
2020-10-22 19:11:21 +03:00
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
2020-09-23 20:19:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
//GroupsURL path to the Hub API listing the groups in an organization
|
|
|
|
GroupsURL = "/v2/orgs/%s/groups/"
|
|
|
|
)
|
|
|
|
|
|
|
|
//Team represents a hub group in an organization
|
|
|
|
type Team struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
2020-09-23 20:43:34 +03:00
|
|
|
Members []Member
|
2020-09-23 20:19:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//GetTeams lists all the teams in an organization
|
2020-10-01 17:43:19 +03:00
|
|
|
func (c *Client) GetTeams(organization string) ([]Team, error) {
|
|
|
|
u, err := url.Parse(c.domain + fmt.Sprintf(GroupsURL, organization))
|
2020-09-23 20:19:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
q := url.Values{}
|
|
|
|
q.Add("page_size", fmt.Sprintf("%v", itemsPerPage))
|
|
|
|
q.Add("page", "1")
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
|
2020-10-01 17:43:19 +03:00
|
|
|
teams, next, err := c.getTeamsPage(u.String(), organization)
|
2020-09-23 20:19:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for next != "" {
|
2020-10-01 17:43:19 +03:00
|
|
|
pageTeams, n, err := c.getTeamsPage(next, organization)
|
2020-09-23 20:19:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
next = n
|
|
|
|
teams = append(teams, pageTeams...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:43:19 +03:00
|
|
|
func (c *Client) getTeamsPage(url, organization string) ([]Team, string, error) {
|
2020-09-23 20:19:43 +03:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2020-10-09 16:50:37 +03:00
|
|
|
response, err := c.doRequest(req, WithHubToken(c.token))
|
2020-09-23 20:19:43 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
var hubResponse hubGroupResponse
|
|
|
|
if err := json.Unmarshal(response, &hubResponse); err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
var teams []Team
|
2020-10-22 19:11:21 +03:00
|
|
|
eg, _ := errgroup.WithContext(context.Background())
|
2020-09-23 20:19:43 +03:00
|
|
|
for _, result := range hubResponse.Results {
|
2020-10-22 19:11:21 +03:00
|
|
|
result := result
|
|
|
|
eg.Go(func() error {
|
2020-10-20 12:32:04 +03:00
|
|
|
members, err := c.GetMembersPerTeam(organization, result.Name)
|
|
|
|
if err != nil {
|
2020-10-22 19:11:21 +03:00
|
|
|
return err
|
2020-10-20 12:32:04 +03:00
|
|
|
}
|
|
|
|
team := Team{
|
|
|
|
Name: result.Name,
|
|
|
|
Description: result.Description,
|
|
|
|
Members: members,
|
|
|
|
}
|
|
|
|
teams = append(teams, team)
|
2020-10-22 19:11:21 +03:00
|
|
|
return nil
|
|
|
|
})
|
2020-09-23 20:19:43 +03:00
|
|
|
}
|
2020-10-22 19:11:21 +03:00
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return []Team{}, "", err
|
|
|
|
}
|
|
|
|
|
2020-10-20 12:32:04 +03:00
|
|
|
sort.Slice(teams, func(i, j int) bool {
|
|
|
|
return teams[i].Name < teams[j].Name
|
|
|
|
})
|
2020-10-22 19:11:21 +03:00
|
|
|
|
2020-09-23 20:19:43 +03:00
|
|
|
return teams, hubResponse.Next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type hubGroupResponse struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
Next string `json:"next,omitempty"`
|
|
|
|
Previous string `json:"previous,omitempty"`
|
|
|
|
Results []hubGroupResult `json:"results,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type hubGroupResult struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
ID int `json:"id"`
|
|
|
|
}
|