-
Notifications
You must be signed in to change notification settings - Fork 70
/
client.go
116 lines (93 loc) · 3.31 KB
/
client.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
package client
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/cli"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/install/types"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/v2/newrelic"
"github.com/newrelic/newrelic-client-go/v2/pkg/apiaccess"
clientLogging "github.com/newrelic/newrelic-client-go/v2/pkg/logging"
log "github.com/sirupsen/logrus"
)
var (
NRClient *newrelic.NewRelic
serviceName = "newrelic-cli"
)
// NewClient initializes the New Relic client.
func NewClient(profileName string) (*newrelic.NewRelic, error) {
apiKey := configAPI.GetProfileString(profileName, config.APIKey)
licenseKey := configAPI.GetProfileString(profileName, config.LicenseKey)
if apiKey == "" && licenseKey == "" {
return nil, errors.New("a User API key or License key is required, set a default profile or use the NEW_RELIC_API_KEY or NEW_RELIC_LICENSE_KEY environment variables")
}
region := configAPI.GetProfileString(profileName, config.Region)
userAgent := fmt.Sprintf("newrelic-cli/%s (https://github.com/newrelic/newrelic-cli)", cli.Version())
// Feed our logrus instance to the client's logrus adapter
logger := clientLogging.NewLogrusLogger(clientLogging.ConfigLoggerInstance(config.Logger))
cfgOpts := []newrelic.ConfigOption{
newrelic.ConfigPersonalAPIKey(apiKey),
newrelic.ConfigInsightsInsertKey(licenseKey),
newrelic.ConfigLogger(logger),
newrelic.ConfigRegion(region),
newrelic.ConfigUserAgent(userAgent),
newrelic.ConfigServiceName(serviceName),
}
nrClient, err := newrelic.New(cfgOpts...)
if err != nil {
return nil, fmt.Errorf("unable to create New Relic client with error: %s", err)
}
return nrClient, nil
}
func RequireClient(cmd *cobra.Command, args []string) {
if NRClient == nil {
log.Fatalf("could not initialize New Relic client, make sure your profile is configured with `newrelic profile configure`")
}
}
func FetchLicenseKey(accountID int, profileName string) (string, error) {
var client *newrelic.NewRelic
var err error
if profileName == "" {
client = NRClient
} else {
client, err = NewClient(profileName)
if err != nil {
return "", err
}
}
var key string
retryFunc := func() error {
key, err = execLicenseKeyRequest(utils.SignalCtx, client, accountID)
if err != nil {
return err
}
return nil
}
r := utils.NewRetry(config.DefaultPostMaxRetries, (config.DefaultPostRetryDelaySec * 1000), retryFunc)
retryCtx := r.ExecWithRetries(utils.SignalCtx)
if !retryCtx.Success {
return "", retryCtx.MostRecentError()
}
return key, nil
}
func execLicenseKeyRequest(ctx context.Context, client *newrelic.NewRelic, accountID int) (string, error) {
params := apiaccess.APIAccessKeySearchQuery{
Scope: apiaccess.APIAccessKeySearchScope{
AccountIDs: []int{accountID},
IngestTypes: []apiaccess.APIAccessIngestKeyType{apiaccess.APIAccessIngestKeyTypeTypes.LICENSE},
},
Types: []apiaccess.APIAccessKeyType{apiaccess.APIAccessKeyTypeTypes.INGEST},
}
keys, err := client.APIAccess.SearchAPIAccessKeysWithContext(ctx, params)
if err != nil {
return "", err
}
if len(keys) > 0 {
return keys[0].Key, nil
}
return "", types.ErrorFetchingLicenseKey
}