2016-05-17 12:49:02 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-25 09:25:16 +02:00
|
|
|
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2016-05-17 12:49:02 +02:00
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
"github.com/vmware/harbor/utils/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RepPolicyAPI struct {
|
|
|
|
BaseAPI
|
|
|
|
policyID int64
|
2016-05-27 09:04:20 +02:00
|
|
|
policy *models.RepPolicy
|
2016-05-17 12:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *RepPolicyAPI) Prepare() {
|
|
|
|
uid := pa.ValidateUser()
|
|
|
|
var err error
|
|
|
|
isAdmin, err := dao.IsAdminRole(uid)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to Check if the user is admin, error: %v, uid: %d", err, uid)
|
|
|
|
}
|
|
|
|
if !isAdmin {
|
|
|
|
pa.CustomAbort(http.StatusForbidden, "")
|
|
|
|
}
|
|
|
|
idStr := pa.Ctx.Input.Param(":id")
|
|
|
|
if len(idStr) > 0 {
|
|
|
|
pa.policyID, err = strconv.ParseInt(idStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error parsing policy id: %s, error: %v", idStr, err)
|
|
|
|
pa.CustomAbort(http.StatusBadRequest, "invalid policy id")
|
|
|
|
}
|
|
|
|
p, err := dao.GetRepPolicy(pa.policyID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error occurred in GetRepPolicy, error: %v", err)
|
|
|
|
pa.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
pa.CustomAbort(http.StatusNotFound, fmt.Sprintf("policy does not exist, id: %v", pa.policyID))
|
|
|
|
}
|
2016-05-27 09:04:20 +02:00
|
|
|
pa.policy = p
|
2016-05-17 12:49:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get ...
|
|
|
|
func (pa *RepPolicyAPI) Get() {
|
|
|
|
projectID, err := pa.GetInt64("project_id")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to get project id, error: %v", err)
|
|
|
|
pa.RenderError(http.StatusBadRequest, "Invalid project id")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
policies, err := dao.GetRepPolicyByProject(projectID)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to query policies from db, error: %v", err)
|
|
|
|
pa.RenderError(http.StatusInternalServerError, "Failed to query policies")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pa.Data["json"] = policies
|
|
|
|
pa.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post ...
|
|
|
|
func (pa *RepPolicyAPI) Post() {
|
|
|
|
policy := models.RepPolicy{}
|
|
|
|
pa.DecodeJSONReq(&policy)
|
|
|
|
pid, err := dao.AddRepPolicy(policy)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to add policy to DB, error: %v", err)
|
|
|
|
pa.RenderError(http.StatusInternalServerError, "Internal Error")
|
|
|
|
return
|
|
|
|
}
|
2016-05-25 09:25:16 +02:00
|
|
|
|
2016-05-27 09:04:20 +02:00
|
|
|
if policy.Enabled == 1 {
|
|
|
|
go func() {
|
|
|
|
if err := TriggerReplication(pid, "", nil, models.RepOpTransfer); err != nil {
|
|
|
|
log.Errorf("failed to trigger replication of %d: %v", pid, err)
|
|
|
|
} else {
|
|
|
|
log.Infof("replication of %d triggered", pid)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-05-25 09:25:16 +02:00
|
|
|
|
2016-05-17 12:49:02 +02:00
|
|
|
pa.Redirect(http.StatusCreated, strconv.FormatInt(pid, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
type enablementReq struct {
|
|
|
|
Enabled int `json:"enabled"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pa *RepPolicyAPI) UpdateEnablement() {
|
|
|
|
e := enablementReq{}
|
|
|
|
pa.DecodeJSONReq(&e)
|
2016-05-27 09:04:20 +02:00
|
|
|
if e.Enabled != 0 && e.Enabled != 1 {
|
2016-05-17 12:49:02 +02:00
|
|
|
pa.RenderError(http.StatusBadRequest, "invalid enabled value")
|
|
|
|
return
|
|
|
|
}
|
2016-05-27 09:04:20 +02:00
|
|
|
|
|
|
|
if pa.policy.Enabled == e.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dao.UpdateRepPolicyEnablement(pa.policyID, e.Enabled); err != nil {
|
2016-05-17 12:49:02 +02:00
|
|
|
log.Errorf("Failed to update policy enablement in DB, error: %v", err)
|
|
|
|
pa.RenderError(http.StatusInternalServerError, "Internal Error")
|
|
|
|
return
|
|
|
|
}
|
2016-05-25 09:25:16 +02:00
|
|
|
|
|
|
|
if e.Enabled == 1 {
|
|
|
|
go func() {
|
2016-05-27 04:45:21 +02:00
|
|
|
if err := TriggerReplication(pa.policyID, "", nil, models.RepOpTransfer); err != nil {
|
2016-05-25 09:25:16 +02:00
|
|
|
log.Errorf("failed to trigger replication of %d: %v", pa.policyID, err)
|
|
|
|
} else {
|
|
|
|
log.Infof("replication of %d triggered", pa.policyID)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-05-17 12:49:02 +02:00
|
|
|
}
|