github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/dockerpromote.go (about) 1 package services 2 3 import ( 4 "encoding/json" 5 "errors" 6 "net/http" 7 "path" 8 9 rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient" 10 "github.com/cobalt77/jfrog-client-go/artifactory/services/utils" 11 "github.com/cobalt77/jfrog-client-go/auth" 12 clientutils "github.com/cobalt77/jfrog-client-go/utils" 13 "github.com/cobalt77/jfrog-client-go/utils/errorutils" 14 "github.com/cobalt77/jfrog-client-go/utils/log" 15 ) 16 17 type DockerPromoteService struct { 18 client *rthttpclient.ArtifactoryHttpClient 19 ArtDetails auth.ServiceDetails 20 } 21 22 func NewDockerPromoteService(client *rthttpclient.ArtifactoryHttpClient) *DockerPromoteService { 23 return &DockerPromoteService{client: client} 24 } 25 26 func (ps *DockerPromoteService) GetArtifactoryDetails() auth.ServiceDetails { 27 return ps.ArtDetails 28 } 29 30 func (ps *DockerPromoteService) SetArtifactoryDetails(rt auth.ServiceDetails) { 31 ps.ArtDetails = rt 32 } 33 34 func (ps *DockerPromoteService) GetJfrogHttpClient() (*rthttpclient.ArtifactoryHttpClient, error) { 35 return ps.client, nil 36 } 37 38 func (ps *DockerPromoteService) IsDryRun() bool { 39 return false 40 } 41 42 func (ps *DockerPromoteService) PromoteDocker(params DockerPromoteParams) error { 43 // Create URL 44 restApi := path.Join("api/docker", params.SourceRepo, "v2", "promote") 45 url, err := utils.BuildArtifactoryUrl(ps.ArtDetails.GetUrl(), restApi, nil) 46 if err != nil { 47 return err 48 } 49 50 // Create body 51 data := DockerPromoteBody{ 52 TargetRepo: params.TargetRepo, 53 DockerRepository: params.SourceDockerImage, 54 TargetDockerRepository: params.TargetDockerImage, 55 Tag: params.SourceTag, 56 TargetTag: params.TargetTag, 57 Copy: params.Copy, 58 } 59 requestContent, err := json.Marshal(data) 60 if err != nil { 61 return errorutils.CheckError(err) 62 } 63 64 // Send POST request 65 httpClientsDetails := ps.GetArtifactoryDetails().CreateHttpClientDetails() 66 utils.SetContentType("application/json", &httpClientsDetails.Headers) 67 resp, body, err := ps.client.SendPost(url, requestContent, &httpClientsDetails) 68 if err != nil { 69 return err 70 } 71 72 // Check results 73 if resp.StatusCode != http.StatusOK { 74 return errorutils.CheckError(errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 75 } 76 log.Debug("Artifactory response: ", resp.Status) 77 78 return nil 79 } 80 81 type DockerPromoteParams struct { 82 // Mandatory: 83 // The name of the source repository in Artifactory, e.g. "docker-local-1". Supported by local repositories only. 84 SourceRepo string 85 // The name of the target repository in Artifactory, e.g. "docker-local-2". Supported by local repositories only. 86 TargetRepo string 87 // The name of the source Docker image, e.g. "hello-world". 88 SourceDockerImage string 89 90 // Optional: 91 // The name of the target Docker image, e.g "hello-world2". If not specified - will use the same name as 'SourceDockerImage'. 92 TargetDockerImage string 93 // The name of the source image tag. If not specified - the entire docker repository will be promoted. 94 SourceTag string 95 // The name of the target image tag. If not specified - will use the same tag as 'SourceTag'. 96 TargetTag string 97 // If set to true, will do copy instead of move. 98 Copy bool 99 } 100 101 func (dp *DockerPromoteParams) GetTargetRepo() string { 102 return dp.TargetRepo 103 } 104 105 func (dp *DockerPromoteParams) GetSourceDockerImage() string { 106 return dp.SourceDockerImage 107 } 108 109 func (dp *DockerPromoteParams) GetTargetDockerRepository() string { 110 return dp.TargetDockerImage 111 } 112 113 func (dp *DockerPromoteParams) GetSourceTag() string { 114 return dp.SourceTag 115 } 116 117 func (dp *DockerPromoteParams) GetTargetTag() string { 118 return dp.TargetTag 119 } 120 121 func (dp *DockerPromoteParams) IsCopy() bool { 122 return dp.Copy 123 } 124 125 func NewDockerPromoteParams(sourceDockerImage, sourceRepo, targetRepo string) DockerPromoteParams { 126 return DockerPromoteParams{ 127 SourceDockerImage: sourceDockerImage, 128 SourceRepo: sourceRepo, 129 TargetRepo: targetRepo, 130 } 131 } 132 133 type DockerPromoteBody struct { 134 TargetRepo string `json:"targetRepo"` 135 DockerRepository string `json:"dockerRepository"` 136 TargetDockerRepository string `json:"targetDockerRepository"` 137 Tag string `json:"tag"` 138 TargetTag string `json:"targetTag"` 139 Copy bool `json:"copy"` 140 }