2018-06-11 13:13:19 +00:00
|
|
|
package registries
|
|
|
|
|
|
|
|
import (
|
2020-07-07 21:57:52 +00:00
|
|
|
"errors"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type registryCreatePayload struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// Name that will be used to identify this registry
|
|
|
|
Name string `example:"my-registry" validate:"required"`
|
|
|
|
// Registry Type. Valid values are: 1 (Quay.io), 2 (Azure container registry), 3 (custom registry) or 4 (Gitlab registry)
|
|
|
|
Type portainer.RegistryType `example:"1" validate:"required" enums:"1,2,3,4"`
|
|
|
|
// URL or IP address of the Docker registry
|
|
|
|
URL string `example:"registry.mydomain.tld:2375" validate:"required"`
|
|
|
|
// Is authentication against this registry enabled
|
|
|
|
Authentication bool `example:"false" validate:"required"`
|
|
|
|
// Username used to authenticate against this registry. Required when Authentication is true
|
|
|
|
Username string `example:"registry_user"`
|
|
|
|
// Password used to authenticate against this registry. required when Authentication is true
|
|
|
|
Password string `example:"registry_password"`
|
|
|
|
// Gitlab specific details, required when type = 4
|
|
|
|
Gitlab portainer.GitlabRegistryData
|
2021-03-12 23:47:35 +00:00
|
|
|
// Quay specific details, required when type = 1
|
|
|
|
Quay portainer.QuayRegistryData
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *registryCreatePayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Name) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid registry name")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.URL) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid registry URL")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
if payload.Authentication && (govalidator.IsNull(payload.Username) || govalidator.IsNull(payload.Password)) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid credentials. Username and password must be specified when authentication is enabled")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2019-11-12 03:28:31 +00:00
|
|
|
if payload.Type != portainer.QuayRegistry && payload.Type != portainer.AzureRegistry && payload.Type != portainer.CustomRegistry && payload.Type != portainer.GitlabRegistry {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid registry type. Valid values are: 1 (Quay.io), 2 (Azure container registry), 3 (custom registry) or 4 (Gitlab registry)")
|
2018-12-09 03:49:27 +00:00
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id RegistryCreate
|
|
|
|
// @summary Create a new registry
|
|
|
|
// @description Create a new registry.
|
|
|
|
// @description **Access policy**: administrator
|
|
|
|
// @tags registries
|
|
|
|
// @security jwt
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
|
|
|
// @param body body registryCreatePayload true "Registry details"
|
|
|
|
// @success 200 {object} portainer.Registry "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /registries [post]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) registryCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload registryCreatePayload
|
|
|
|
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
registry := &portainer.Registry{
|
2019-05-24 06:04:58 +00:00
|
|
|
Type: portainer.RegistryType(payload.Type),
|
|
|
|
Name: payload.Name,
|
|
|
|
URL: payload.URL,
|
|
|
|
Authentication: payload.Authentication,
|
|
|
|
Username: payload.Username,
|
|
|
|
Password: payload.Password,
|
|
|
|
UserAccessPolicies: portainer.UserAccessPolicies{},
|
|
|
|
TeamAccessPolicies: portainer.TeamAccessPolicies{},
|
2019-11-12 03:28:31 +00:00
|
|
|
Gitlab: payload.Gitlab,
|
2021-03-12 23:47:35 +00:00
|
|
|
Quay: payload.Quay,
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.Registry().CreateRegistry(registry)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the registry inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
hideFields(registry)
|
|
|
|
return response.JSON(w, registry)
|
|
|
|
}
|