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