Skip to content

Commit c8faf9e

Browse files
authored
Merge pull request #497 from newrelic/chore/schema-updates
chore(install): make schema updates, fix bugs from previous validation PR
2 parents 4a985a7 + 2dfbf5f commit c8faf9e

22 files changed

+551
-220
lines changed

cmd/newrelic/command.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func initializeProfile() {
5252

5353
apiKey := os.Getenv("NEW_RELIC_API_KEY")
5454
envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")
55-
5655
region = os.Getenv("NEW_RELIC_REGION")
5756
licenseKey = os.Getenv("NEW_RELIC_LICENSE_KEY")
5857

internal/client/client.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package client
33
import (
44
"errors"
55
"fmt"
6-
"os"
7-
"strings"
86

97
"github.com/newrelic/newrelic-client-go/newrelic"
108

@@ -29,8 +27,6 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel
2927
// Create the New Relic Client
3028
defProfile := creds.Default()
3129

32-
defProfile = applyOverrides(defProfile)
33-
3430
if defProfile != nil {
3531
apiKey = defProfile.APIKey
3632
insightsInsertKey = defProfile.InsightsInsertKey
@@ -58,33 +54,3 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel
5854

5955
return nrClient, defProfile, nil
6056
}
61-
62-
// applyOverrides reads Profile info out of the Environment to override config
63-
func applyOverrides(p *credentials.Profile) *credentials.Profile {
64-
envAPIKey := os.Getenv("NEW_RELIC_API_KEY")
65-
envInsightsInsertKey := os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY")
66-
envRegion := os.Getenv("NEW_RELIC_REGION")
67-
68-
if envAPIKey == "" && envRegion == "" && envInsightsInsertKey == "" {
69-
return p
70-
}
71-
72-
out := credentials.Profile{}
73-
if p != nil {
74-
out = *p
75-
}
76-
77-
if envAPIKey != "" {
78-
out.APIKey = envAPIKey
79-
}
80-
81-
if envInsightsInsertKey != "" {
82-
out.InsightsInsertKey = envInsightsInsertKey
83-
}
84-
85-
if envRegion != "" {
86-
out.Region = strings.ToUpper(envRegion)
87-
}
88-
89-
return &out
90-
}

internal/config/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"reflect"
77
"strings"
8-
"time"
98

109
"github.com/newrelic/newrelic-cli/internal/utils"
1110

@@ -106,9 +105,9 @@ func LoadConfig(configDir string) (*Config, error) {
106105

107106
func (c *Config) setLogger() {
108107
log.SetFormatter(&log.TextFormatter{
109-
FullTimestamp: true,
110-
TimestampFormat: time.RFC3339,
111-
DisableLevelTruncation: true,
108+
DisableLevelTruncation: true,
109+
DisableTimestamp: true,
110+
EnvironmentOverrideColors: true,
112111
})
113112

114113
switch level := strings.ToUpper(c.LogLevel); level {

internal/client/client_integration_test.go renamed to internal/credentials/credentials_integration_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build integration
22

3-
package client
3+
package credentials
44

55
import (
66
"io/ioutil"
@@ -10,8 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111

1212
"github.com/newrelic/newrelic-client-go/pkg/region"
13-
14-
"github.com/newrelic/newrelic-cli/internal/credentials"
1513
)
1614

1715
var overrideEnvVars = []string{
@@ -27,11 +25,11 @@ func TestApplyOverrides(t *testing.T) {
2725
defer os.RemoveAll(f)
2826

2927
// Initialize the new configuration directory
30-
c, err := credentials.LoadCredentials(f)
28+
c, err := LoadCredentials(f)
3129
assert.NoError(t, err)
3230

3331
// Create an initial profile to work with
34-
testProfile := credentials.Profile{
32+
testProfile := Profile{
3533
Region: "us",
3634
APIKey: "apiKeyGoesHere",
3735
InsightsInsertKey: "insightsInsertKeyGoesHere",

internal/credentials/helpers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ func WithCredentialsFrom(configDir string, f func(c *Credentials)) {
2727
func DefaultProfile() *Profile {
2828
if defaultProfile == nil {
2929
WithCredentials(func(c *Credentials) {
30-
p := c.Profiles[c.DefaultProfile]
31-
defaultProfile = &p
30+
defaultProfile = c.Default()
3231
})
3332
}
3433

internal/credentials/profiles.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"reflect"
9+
"strconv"
910
"strings"
1011

1112
"github.com/mitchellh/mapstructure"
@@ -57,13 +58,56 @@ func LoadDefaultProfile(configDir string) (string, error) {
5758

5859
// Default returns the default profile
5960
func (c *Credentials) Default() *Profile {
61+
var p *Profile
6062
if c.DefaultProfile != "" {
6163
if val, ok := c.Profiles[c.DefaultProfile]; ok {
62-
return &val
64+
p = &val
6365
}
6466
}
6567

66-
return nil
68+
p = applyOverrides(p)
69+
return p
70+
}
71+
72+
// applyOverrides reads Profile info out of the Environment to override config
73+
func applyOverrides(p *Profile) *Profile {
74+
envAPIKey := os.Getenv("NEW_RELIC_API_KEY")
75+
envInsightsInsertKey := os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY")
76+
envRegion := os.Getenv("NEW_RELIC_REGION")
77+
envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")
78+
79+
if envAPIKey == "" && envRegion == "" && envInsightsInsertKey == "" && envAccountID == "" {
80+
return p
81+
}
82+
83+
out := Profile{}
84+
if p != nil {
85+
out = *p
86+
}
87+
88+
if envAPIKey != "" {
89+
out.APIKey = envAPIKey
90+
}
91+
92+
if envInsightsInsertKey != "" {
93+
out.InsightsInsertKey = envInsightsInsertKey
94+
}
95+
96+
if envRegion != "" {
97+
out.Region = strings.ToUpper(envRegion)
98+
}
99+
100+
if envAccountID != "" {
101+
accountID, err := strconv.Atoi(envAccountID)
102+
if err != nil {
103+
log.Warnf("Invalid account ID: %s", envAccountID)
104+
return &out
105+
}
106+
107+
out.AccountID = accountID
108+
}
109+
110+
return &out
67111
}
68112

69113
func readDefaultProfile(configDir string) (string, error) {

internal/install/command.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
)
1313

1414
var (
15-
interactiveMode bool
16-
installLogging bool
17-
autoDiscoveryMode bool
18-
recipeFriendlyNames []string
15+
specifyActions bool
16+
interactiveMode bool
17+
installLogging bool
18+
recipeNames []string
19+
recipeFilenames []string
1920
)
2021

2122
// Command represents the install command.
@@ -25,10 +26,11 @@ var Command = &cobra.Command{
2526
Hidden: true,
2627
Run: func(cmd *cobra.Command, args []string) {
2728
ic := installContext{
28-
interactiveMode: interactiveMode,
29-
installLogging: installLogging,
30-
autoDiscoveryMode: autoDiscoveryMode,
31-
recipeFriendlyNames: recipeFriendlyNames,
29+
interactiveMode: interactiveMode,
30+
installLogging: installLogging,
31+
recipeNames: recipeNames,
32+
recipeFilenames: recipeFilenames,
33+
specifyActions: specifyActions,
3234
}
3335

3436
client.WithClientAndProfile(func(nrClient *newrelic.NewRelic, profile *credentials.Profile) {
@@ -54,8 +56,9 @@ var Command = &cobra.Command{
5456
}
5557

5658
func init() {
57-
Command.Flags().BoolVarP(&interactiveMode, "interactive", "i", true, "enables interactive mode")
58-
Command.Flags().BoolVarP(&installLogging, "installLogging", "l", true, "installs New Relic logging")
59-
Command.Flags().BoolVarP(&autoDiscoveryMode, "autoDiscovery", "d", true, "enables auto-discovery mode")
60-
Command.Flags().StringSliceVarP(&recipeFriendlyNames, "recipe", "r", []string{}, "the name of a recipe to install")
59+
Command.Flags().BoolVarP(&interactiveMode, "interactive", "i", false, "enables interactive mode if specifyActions has been used")
60+
Command.Flags().BoolVarP(&installLogging, "installLogging", "l", false, "installs New Relic logging if specifyActions has been used")
61+
Command.Flags().BoolVarP(&specifyActions, "specifyActions", "s", false, "specify the actions to be run during install")
62+
Command.Flags().StringSliceVarP(&recipeNames, "recipe", "r", []string{}, "the name of a recipe to install")
63+
Command.Flags().StringSliceVarP(&recipeFilenames, "recipeFile", "c", []string{}, "a recipe file to install")
6164
}

internal/install/go_task_recipe_executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func newGoTaskRecipeExecutor() *goTaskRecipeExecutor {
2525
}
2626

2727
func (re *goTaskRecipeExecutor) execute(ctx context.Context, m discoveryManifest, r recipe) error {
28-
log.Debugf("Executing recipe %s", r.Metadata.Name)
28+
log.Debugf("Executing recipe %s", r.Name)
2929

3030
f, err := r.ToRecipeFile()
3131
if err != nil {
@@ -38,7 +38,7 @@ func (re *goTaskRecipeExecutor) execute(ctx context.Context, m discoveryManifest
3838
}
3939

4040
// Create a temporary task file.
41-
file, err := ioutil.TempFile("", r.Metadata.Name)
41+
file, err := ioutil.TempFile("", r.Name)
4242
defer os.Remove(file.Name())
4343
if err != nil {
4444
return err
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package install
2+
3+
type installContext struct {
4+
specifyActions bool
5+
interactiveMode bool
6+
installLogging bool
7+
installInfraAgent bool
8+
recipeNames []string
9+
recipeFilenames []string
10+
}
11+
12+
func (i *installContext) ShouldInstallInfraAgent() bool {
13+
return !i.RecipeFilenamesProvided() && (!i.specifyActions || i.installInfraAgent)
14+
}
15+
16+
func (i *installContext) ShouldInstallLogging() bool {
17+
return !i.RecipeFilenamesProvided() && (!i.specifyActions || i.installLogging)
18+
}
19+
20+
func (i *installContext) RecipeFilenamesProvided() bool {
21+
return len(i.recipeFilenames) > 0
22+
}
23+
24+
func (i *installContext) RecipeNamesProvided() bool {
25+
return len(i.recipeNames) > 0
26+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package install
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestShouldInstallInfraAgent_Default(t *testing.T) {
10+
ic := installContext{}
11+
require.True(t, ic.ShouldInstallInfraAgent())
12+
}
13+
14+
func TestShouldInstallInfraAgent_SpecifyActions(t *testing.T) {
15+
ic := installContext{
16+
specifyActions: true,
17+
}
18+
require.False(t, ic.ShouldInstallInfraAgent())
19+
20+
ic.installInfraAgent = true
21+
require.True(t, ic.ShouldInstallInfraAgent())
22+
}
23+
24+
func TestShouldInstallInfraAgent_RecipeFilenamesProvided(t *testing.T) {
25+
ic := installContext{
26+
recipeFilenames: []string{"testFilename"},
27+
}
28+
require.False(t, ic.ShouldInstallInfraAgent())
29+
30+
ic.installInfraAgent = true
31+
require.False(t, ic.ShouldInstallInfraAgent())
32+
}
33+
func TestShouldInstallLogging_Default(t *testing.T) {
34+
ic := installContext{}
35+
require.True(t, ic.ShouldInstallLogging())
36+
}
37+
38+
func TestShouldInstallLogging_SpecifyActions(t *testing.T) {
39+
ic := installContext{
40+
specifyActions: true,
41+
}
42+
require.False(t, ic.ShouldInstallLogging())
43+
44+
ic.installLogging = true
45+
require.True(t, ic.ShouldInstallLogging())
46+
}
47+
48+
func TestShouldInstallLogging_RecipeFilenamesProvided(t *testing.T) {
49+
ic := installContext{
50+
recipeFilenames: []string{"testFilename"},
51+
}
52+
require.False(t, ic.ShouldInstallLogging())
53+
54+
ic.installInfraAgent = true
55+
require.False(t, ic.ShouldInstallLogging())
56+
}
57+
58+
func TestRecipeNamesProvided(t *testing.T) {
59+
ic := installContext{}
60+
require.False(t, ic.RecipeNamesProvided())
61+
62+
ic.recipeNames = []string{"testName"}
63+
require.True(t, ic.RecipeNamesProvided())
64+
}
65+
66+
func TestRecipeFilenamesProvided(t *testing.T) {
67+
ic := installContext{}
68+
require.False(t, ic.RecipeFilenamesProvided())
69+
70+
ic.recipeFilenames = []string{"testFilename"}
71+
require.True(t, ic.RecipeFilenamesProvided())
72+
}

0 commit comments

Comments
 (0)