docker-hub-description-updater/pkg/hub/hub.go

170 lines
4.3 KiB
Go
Raw Normal View History

2019-09-16 19:36:27 +00:00
package hub
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
2019-09-16 19:36:27 +00:00
2019-09-24 12:42:46 +00:00
"github.com/volker-raschek/docker-hub-description-updater/pkg/types"
2019-09-16 19:36:27 +00:00
)
var (
dockerHubAPI = "https://hub.docker.com/v2"
)
type Hub struct {
client *http.Client
credentials *types.LoginCredentials
token *types.Token
}
// GetRepository returns a repository struct
func (h *Hub) GetRepository(namespace string, name string) (*types.Repository, error) {
2019-09-16 19:36:27 +00:00
if len(namespace) <= 0 {
return nil, errorNoNamespaceDefined
}
if len(name) <= 0 {
return nil, errorNoRepositoryDefined
}
rawURL := fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, namespace, name)
url, err := url.Parse(rawURL)
2019-09-16 19:36:27 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToParseURL, err)
2019-09-16 19:36:27 +00:00
}
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
2019-09-16 19:36:27 +00:00
}
if h.token == nil {
token, err := h.getToken()
if err != nil {
return nil, err
}
h.token = token
2019-09-16 19:36:27 +00:00
}
req.Header.Add("Authorization", fmt.Sprintf("JWT %v", h.token.Token))
2019-09-16 19:36:27 +00:00
resp, err := h.client.Do(req)
2019-09-16 19:36:27 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToSendRequest, err)
2019-09-16 19:36:27 +00:00
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v: expect %v, received %v", errorUnexpectedHTTPStatuscode, http.StatusOK, resp.StatusCode)
2019-09-16 19:36:27 +00:00
}
repository := new(types.Repository)
jsonDecoder := json.NewDecoder(resp.Body)
if err := jsonDecoder.Decode(repository); err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
2019-09-16 19:36:27 +00:00
}
return repository, nil
}
func (h *Hub) getToken() (*types.Token, error) {
2019-09-16 19:36:27 +00:00
loginBuffer := new(bytes.Buffer)
jsonEncoder := json.NewEncoder(loginBuffer)
if err := jsonEncoder.Encode(h.credentials); err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
2019-09-16 19:36:27 +00:00
}
rawURL := fmt.Sprintf("%v/users/login/", dockerHubAPI)
req, err := http.NewRequest(http.MethodPost, rawURL, loginBuffer)
2019-09-16 19:36:27 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
2019-09-16 19:36:27 +00:00
}
req.Header.Add("Content-Type", "application/json")
resp, err := h.client.Do(req)
2019-09-16 19:36:27 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
2019-09-16 19:36:27 +00:00
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v: expect %v, received %v", errorUnexpectedHTTPStatuscode, http.StatusOK, resp.StatusCode)
2019-09-16 19:36:27 +00:00
}
token := new(types.Token)
jsonDecoder := json.NewDecoder(resp.Body)
if err := jsonDecoder.Decode(token); err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
2019-09-16 19:36:27 +00:00
}
return token, nil
}
// PatchRepository updates the docker hub repository
func (h *Hub) PatchRepository(repository *types.Repository) (*types.Repository, error) {
2019-09-16 19:36:27 +00:00
if len(repository.Namespcace) <= 0 {
return nil, errorNoNamespaceDefined
}
if len(repository.Name) <= 0 {
return nil, errorNoRepositoryDefined
}
if h.token == nil {
token, err := h.getToken()
if err != nil {
return nil, err
}
h.token = token
2019-09-16 19:36:27 +00:00
}
2019-09-16 19:36:27 +00:00
rawURL := fmt.Sprintf("%v/repositories/%v/%v", dockerHubAPI, repository.Namespcace, repository.Name)
patchURL, err := url.Parse(rawURL)
2019-11-12 19:11:40 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToParseURL, err)
2019-11-12 19:11:40 +00:00
}
2019-09-16 19:36:27 +00:00
data := &url.Values{}
2019-09-16 19:36:27 +00:00
data.Set("full_description", repository.FullDescription)
patchURL.RawQuery = data.Encode()
2019-09-16 19:36:27 +00:00
req, err := http.NewRequest(http.MethodPatch, patchURL.String(), nil)
2019-09-16 19:36:27 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
2019-09-16 19:36:27 +00:00
}
req.Header.Set("Authorization", fmt.Sprintf("JWT %v", h.token.Token))
resp, err := h.client.Do(req)
2019-09-16 19:36:27 +00:00
if err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToCreateRequest, err)
2019-09-16 19:36:27 +00:00
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%v: expect %v, received %v", errorUnexpectedHTTPStatuscode, http.StatusOK, resp.StatusCode)
2019-09-16 19:36:27 +00:00
}
patchedRepository := new(types.Repository)
2019-11-12 19:11:40 +00:00
jsonDecoder := json.NewDecoder(resp.Body)
if err := jsonDecoder.Decode(h.token); err != nil {
return nil, fmt.Errorf("%v: %v", errorFailedToParseJSON, err)
2019-09-16 19:36:27 +00:00
}
return patchedRepository, nil
}
func New(credentials *types.LoginCredentials) *Hub {
return &Hub{
client: &http.Client{
Timeout: time.Second * 15,
},
credentials: credentials,
}
}