-
Notifications
You must be signed in to change notification settings - Fork 133
/
binMgr.go
74 lines (61 loc) · 2.21 KB
/
binMgr.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
package services
import (
"encoding/json"
"net/http"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
artUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/auth"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)
const (
binMgrAPIURL = "api/v1/binMgr"
)
// BinMgrService defines the http client and Xray details
type BinMgrService struct {
client *jfroghttpclient.JfrogHttpClient
XrayDetails auth.ServiceDetails
}
// NewBinMgrService creates a new Xray Binary Manager Service
func NewBinMgrService(client *jfroghttpclient.JfrogHttpClient) *BinMgrService {
return &BinMgrService{client: client}
}
// GetXrayDetails returns the Xray details
func (xbms *BinMgrService) GetXrayDetails() auth.ServiceDetails {
return xbms.XrayDetails
}
// GetJfrogHttpClient returns the http client
func (xbms *BinMgrService) GetJfrogHttpClient() *jfroghttpclient.JfrogHttpClient {
return xbms.client
}
// The getBinMgrURL does not end with a slash
// So, calling functions will need to add it
func (xbms *BinMgrService) getBinMgrURL() string {
return clientutils.AddTrailingSlashIfNeeded(xbms.XrayDetails.GetUrl()) + binMgrAPIURL
}
// AddBuildsToIndexing will add builds to indexing configuration
func (xbms *BinMgrService) AddBuildsToIndexing(buildNames []string) error {
payloadBody := addBuildsToIndexBody{BuildNames: buildNames}
content, err := json.Marshal(payloadBody)
if err != nil {
return errorutils.CheckError(err)
}
httpClientsDetails := xbms.XrayDetails.CreateHttpClientDetails()
artUtils.SetContentType("application/json", &httpClientsDetails.Headers)
var url = xbms.getBinMgrURL() + "/builds"
log.Info("Configuring Xray to index the build...")
resp, body, err := xbms.client.SendPost(url, content, &httpClientsDetails)
if err != nil {
return err
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated); err != nil {
return err
}
log.Debug("Xray response:", resp.Status)
log.Debug("Done adding builds to indexing configuration.")
return nil
}
type addBuildsToIndexBody struct {
BuildNames []string `json:"names"`
}