forked from jfrog/jfrog-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reportusage.go
81 lines (72 loc) · 2.62 KB
/
reportusage.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
package usage
import (
"encoding/json"
"fmt"
"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
versionutil "github.com/jfrog/jfrog-client-go/utils/version"
"github.com/pkg/errors"
"net/http"
)
const minArtifactoryVersion = "6.9.0"
func SendReportUsage(productId, commandName string, serviceManager *artifactory.ArtifactoryServicesManager) error {
config := serviceManager.GetConfig()
if config == nil {
return errorutils.CheckError(errors.New("Expected full config, but no configuration exists."))
}
rtDetails := config.GetArtDetails()
if rtDetails == nil {
return errorutils.CheckError(errors.New("Artifactory details not configured."))
}
url, err := utils.BuildArtifactoryUrl(rtDetails.GetUrl(), "api/system/usage", make(map[string]string))
if err != nil {
return err
}
clientDetails := rtDetails.CreateHttpClientDetails()
// Check Artifactory version
artifactoryVersion, err := rtDetails.GetVersion()
if err != nil {
return err
}
if !isVersionCompatible(artifactoryVersion) {
log.Debug(fmt.Sprintf("Expected Artifactory version %s or above, got %s", minArtifactoryVersion, artifactoryVersion))
return nil
}
bodyContent, err := reportUsageToJson(productId, commandName)
if err != nil {
return err
}
utils.AddHeader("Content-Type", "application/json", &clientDetails.Headers)
resp, body, err := serviceManager.Client().SendPost(url, bodyContent, &clientDetails)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("Artifactory response: " + resp.Status + "\n" + clientutils.IndentJson(body))
}
log.Debug("Artifactory response:", resp.Status)
log.Debug("Usage info sent successfully.")
return nil
}
// Returns an error if the Artifactory version is not compatible
func isVersionCompatible(artifactoryVersion string) bool {
// API exists from Artifactory version 6.9.0 and above:
version := versionutil.NewVersion(artifactoryVersion)
return version.AtLeast(minArtifactoryVersion)
}
func reportUsageToJson(productId, commandName string) ([]byte, error) {
featureInfo := feature{FeatureId: commandName}
params := reportUsageParams{ProductId: productId, Features: []feature{featureInfo}}
bodyContent, err := json.Marshal(params)
return bodyContent, errorutils.CheckError(err)
}
type reportUsageParams struct {
ProductId string `json:"productId"`
Features []feature `json:"features,omitempty"`
}
type feature struct {
FeatureId string `json:"featureId,omitempty"`
}