diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index c2540db99..b4247c2f9 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -84,8 +84,11 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { if imageRepo.ID != 0 { return constant.ErrRecordExist } + if req.Protocol == "http" { - _ = u.handleRegistries(req.DownloadUrl, "", "create") + if err := u.handleRegistries(req.DownloadUrl, "", "create"); err != nil { + return fmt.Errorf("create registry %s failed, err: %v", req.DownloadUrl, err) + } if err := validateDockerConfig(); err != nil { return err } @@ -114,23 +117,18 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { return err } } + if req.Auth { + if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { + return err + } + } if err := copier.Copy(&imageRepo, &req); err != nil { return errors.WithMessage(constant.ErrStructTransform, err.Error()) } imageRepo.Status = constant.StatusSuccess - if req.Auth { - if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { - imageRepo.Status = constant.StatusFailed - imageRepo.Message = err.Error() - } - } - if err := imageRepoRepo.Create(&imageRepo); err != nil { - return err - } - - return nil + return imageRepoRepo.Create(&imageRepo) } func (u *ImageRepoService) BatchDelete(req dto.ImageRepoDelete) error { @@ -156,33 +154,47 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if err != nil { return err } - if repo.DownloadUrl != req.DownloadUrl || (!repo.Auth && req.Auth) { - _ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update") + if repo.Protocol == "http" && req.Protocol == "https" { + if err := u.handleRegistries("", repo.DownloadUrl, "delete"); err != nil { + return fmt.Errorf("delete registry %s failed, err: %v", repo.DownloadUrl, err) + } + } + if repo.Protocol == "http" && req.Protocol == "http" { + if err := u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update"); err != nil { + return fmt.Errorf("update registry %s => %s failed, err: %v", repo.DownloadUrl, req.DownloadUrl, err) + } + } + if repo.Protocol == "https" && req.Protocol == "http" { + if err := u.handleRegistries(req.DownloadUrl, "", "create"); err != nil { + return fmt.Errorf("create registry %s failed, err: %v", req.DownloadUrl, err) + } + } + if repo.Auth != req.Auth || repo.DownloadUrl != req.DownloadUrl { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } - if err := validateDockerConfig(); err != nil { - return err - } - if err := restartDocker(); err != nil { - return err + if req.Auth { + if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { + return err + } } } + + if err := validateDockerConfig(); err != nil { + return err + } + if err := restartDocker(); err != nil { + return err + } + upMap := make(map[string]interface{}) upMap["download_url"] = req.DownloadUrl upMap["protocol"] = req.Protocol upMap["username"] = req.Username upMap["password"] = req.Password upMap["auth"] = req.Auth - upMap["status"] = constant.StatusSuccess upMap["message"] = "" - if req.Auth { - if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil { - upMap["status"] = constant.StatusFailed - upMap["message"] = err.Error() - } - } return imageRepoRepo.Update(req.ID, upMap) }