Skip to content

Commit

Permalink
Internal API for the Visibility System
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Dec 20, 2024
1 parent 80abb8e commit c4b9ef9
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ type ReportUsageAttribute struct {
AttributeValue string
}

type ArtifactoryCallHome struct{}

func NewArtifactoryCallHome() *ArtifactoryCallHome {
return &ArtifactoryCallHome{}
}

func (rua *ReportUsageAttribute) isEmpty() bool {
return rua.AttributeName == ""
}

func validateAndGetUsageServerInfo(serviceManager artifactory.ArtifactoryServicesManager) (url string, clientDetails httputils.HttpClientDetails, err error) {
func (ach *ArtifactoryCallHome) validateAndGetUsageServerInfo(serviceManager artifactory.ArtifactoryServicesManager) (url string, clientDetails httputils.HttpClientDetails, err error) {
config := serviceManager.GetConfig()
if config == nil {
err = errorutils.CheckErrorf("expected full config, but no configuration exists.")
Expand Down Expand Up @@ -50,7 +56,7 @@ func validateAndGetUsageServerInfo(serviceManager artifactory.ArtifactoryService
return
}

func sendReport(url string, serviceManager artifactory.ArtifactoryServicesManager, clientDetails httputils.HttpClientDetails, bodyContent []byte) error {
func (ach *ArtifactoryCallHome) sendReport(url string, serviceManager artifactory.ArtifactoryServicesManager, clientDetails httputils.HttpClientDetails, bodyContent []byte) error {
clientDetails.SetContentTypeApplicationJson()
resp, body, err := serviceManager.Client().SendPost(url, bodyContent, &clientDetails)
if err != nil {
Expand All @@ -63,28 +69,28 @@ func sendReport(url string, serviceManager artifactory.ArtifactoryServicesManage
return nil
}

func ReportUsageToArtifactory(productId string, serviceManager artifactory.ArtifactoryServicesManager, features ...Feature) error {
url, clientDetails, err := validateAndGetUsageServerInfo(serviceManager)
func (ach *ArtifactoryCallHome) SendUsageToArtifactory(productId string, serviceManager artifactory.ArtifactoryServicesManager, features ...Feature) error {
url, clientDetails, err := ach.validateAndGetUsageServerInfo(serviceManager)
if err != nil || url == "" {
return err
}
bodyContent, err := usageFeaturesToJson(productId, features...)
if err != nil {
return err
}
return sendReport(url, serviceManager, clientDetails, bodyContent)
return ach.sendReport(url, serviceManager, clientDetails, bodyContent)
}

func SendReportUsage(productId, commandName string, serviceManager artifactory.ArtifactoryServicesManager, attributes ...ReportUsageAttribute) error {
url, clientDetails, err := validateAndGetUsageServerInfo(serviceManager)
func (ach *ArtifactoryCallHome) SendUsage(productId, commandName string, serviceManager artifactory.ArtifactoryServicesManager, attributes ...ReportUsageAttribute) error {
url, clientDetails, err := ach.validateAndGetUsageServerInfo(serviceManager)
if err != nil || url == "" {
return err
}
bodyContent, err := reportUsageToJson(productId, commandName, attributes...)
if err != nil {
return err
}
return sendReport(url, serviceManager, clientDetails, bodyContent)
return ach.sendReport(url, serviceManager, clientDetails, bodyContent)
}

func usageFeaturesToJson(productId string, features ...Feature) ([]byte, error) {
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions jfconnect/auth/jfconnectdetails.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import (
"github.com/jfrog/jfrog-client-go/auth"
)

func NewJfConnectDetails() auth.ServiceDetails {
return &jfConnectDetails{}
}

type jfConnectDetails struct {
auth.CommonConfigFields
}

func (jc *jfConnectDetails) GetVersion() (string, error) {
panic("Failed: Method is not implemented")
}
45 changes: 45 additions & 0 deletions jfconnect/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jfconnect

import (
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/jfconnect/services"
)

type Manager interface {
LogMetric([]byte) error
}

type jfConnectManager struct {
client *jfroghttpclient.JfrogHttpClient
config config.Config
}

func NewManager(config config.Config) (Manager, error) {
details := config.GetServiceDetails()
var err error
manager := &jfConnectManager{config: config}
manager.client, err = jfroghttpclient.JfrogClientBuilder().
SetCertificatesPath(config.GetCertificatesPath()).
SetInsecureTls(config.IsInsecureTls()).
SetClientCertPath(details.GetClientCertPath()).
SetClientCertKeyPath(details.GetClientCertKeyPath()).
AppendPreRequestInterceptor(details.RunPreRequestFunctions).
SetContext(config.GetContext()).
SetDialTimeout(config.GetDialTimeout()).
SetOverallRequestTimeout(config.GetOverallRequestTimeout()).
SetRetries(config.GetHttpRetries()).
SetRetryWaitMilliSecs(config.GetHttpRetryWaitMilliSecs()).
Build()

return manager, err
}

func (jm *jfConnectManager) Client() *jfroghttpclient.JfrogHttpClient {
return jm.client
}

func (jm *jfConnectManager) LogMetric(metric []byte) error {
jfConnectService := services.NewJfConnectService(jm.config.GetServiceDetails(), jm.client)
return jfConnectService.LogMetric(metric)
}
38 changes: 38 additions & 0 deletions jfconnect/services/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package services

import (
"net/http"

rtUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

type JfConnectService struct {
client *jfroghttpclient.JfrogHttpClient
serviceDetails *auth.ServiceDetails
}

func NewJfConnectService(serviceDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) *JfConnectService {
return &JfConnectService{serviceDetails: &serviceDetails, client: client}
}

func (js *JfConnectService) GetJfConnectDetails() auth.ServiceDetails {
return *js.serviceDetails
}

func (js *JfConnectService) LogMetric(metric []byte) error {
details := js.GetJfConnectDetails()
httpClientDetails := details.CreateHttpClientDetails()
rtUtils.SetContentType("application/json", &httpClientDetails.Headers)

url := clientutils.AddTrailingSlashIfNeeded(details.GetUrl())
url = url + "jfconnect/api/v1/backoffice/metrics/log"

Check failure on line 32 in jfconnect/services/metrics.go

View workflow job for this annotation

GitHub Actions / Static-Check

assignOp: replace `url = url + "jfconnect/api/v1/backoffice/metrics/log"` with `url += "jfconnect/api/v1/backoffice/metrics/log"` (gocritic)
resp, body, err := js.client.SendPost(url, metric, &httpClientDetails)
if err != nil {
return err
}
return errorutils.CheckResponseStatusWithBody(resp, body, http.StatusCreated, http.StatusOK)
}

0 comments on commit c4b9ef9

Please sign in to comment.