-
Notifications
You must be signed in to change notification settings - Fork 69
/
client.go
136 lines (108 loc) · 3.81 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package client
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"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/newrelic"
"github.com/newrelic/newrelic-client-go/pkg/apiaccess"
clientLogging "github.com/newrelic/newrelic-client-go/pkg/logging"
log "github.com/sirupsen/logrus"
)
var (
NRClient *newrelic.NewRelic
serviceName = "newrelic-cli"
version = "dev"
)
// NewClient initializes the New Relic client.
func NewClient(profileName string) (*newrelic.NewRelic, error) {
apiKey := configAPI.GetProfileString(profileName, config.APIKey)
insightsInsertKey := configAPI.GetProfileString(profileName, config.InsightsInsertKey)
if apiKey == "" && insightsInsertKey == "" {
return nil, errors.New("a User API key or Ingest API key is required, set a default profile or use the NEW_RELIC_API_KEY or NEW_RELIC_INSIGHTS_INSERT_KEY environment variables")
}
region := configAPI.GetProfileString(profileName, config.Region)
userAgent := fmt.Sprintf("newrelic-cli/%s (https://github.com/newrelic/newrelic-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(insightsInsertKey),
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) {
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
}
func FetchInsightsInsertKey(accountID int, profileName string) (string, error) {
client, err := NewClient(profileName)
if err != nil {
return "", err
}
// Check for an existing key first
keys, err := client.APIAccess.ListInsightsInsertKeys(accountID)
if err != nil {
return "", types.ErrorFetchingInsightsInsertKey
}
// We already have a key, return it
if len(keys) > 0 {
return keys[0].Key, nil
}
// Create a new key if one doesn't exist
key, err := client.APIAccess.CreateInsightsInsertKey(accountID)
if err != nil {
return "", types.ErrorFetchingInsightsInsertKey
}
return key.Key, nil
}