2018-06-11 13:13:19 +00:00
|
|
|
package registries
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type registryCreatePayload struct {
|
|
|
|
Name string
|
2019-11-12 03:28:31 +00:00
|
|
|
Type portainer.RegistryType
|
2018-06-11 13:13:19 +00:00
|
|
|
URL string
|
|
|
|
Authentication bool
|
|
|
|
Username string
|
|
|
|
Password string
|
2019-11-12 03:28:31 +00:00
|
|
|
Gitlab portainer.GitlabRegistryData
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *registryCreatePayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Name) {
|
|
|
|
return portainer.Error("Invalid registry name")
|
|
|
|
}
|
|
|
|
if govalidator.IsNull(payload.URL) {
|
|
|
|
return portainer.Error("Invalid registry URL")
|
|
|
|
}
|
|
|
|
if payload.Authentication && (govalidator.IsNull(payload.Username) || govalidator.IsNull(payload.Password)) {
|
|
|
|
return portainer.Error("Invalid credentials. Username and password must be specified when authentication is enabled")
|
|
|
|
}
|
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 {
|
|
|
|
return portainer.Error("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
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
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)
|
|
|
|
}
|