Skip to content

Commit

Permalink
Merge pull request #497 from newrelic/chore/schema-updates
Browse files Browse the repository at this point in the history
chore(install): make schema updates, fix bugs from previous validation PR
  • Loading branch information
ctrombley authored Dec 8, 2020
2 parents 4a985a7 + 2dfbf5f commit c8faf9e
Show file tree
Hide file tree
Showing 22 changed files with 551 additions and 220 deletions.
1 change: 0 additions & 1 deletion cmd/newrelic/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func initializeProfile() {

apiKey := os.Getenv("NEW_RELIC_API_KEY")
envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")

region = os.Getenv("NEW_RELIC_REGION")
licenseKey = os.Getenv("NEW_RELIC_LICENSE_KEY")

Expand Down
34 changes: 0 additions & 34 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package client
import (
"errors"
"fmt"
"os"
"strings"

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

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

defProfile = applyOverrides(defProfile)

if defProfile != nil {
apiKey = defProfile.APIKey
insightsInsertKey = defProfile.InsightsInsertKey
Expand Down Expand Up @@ -58,33 +54,3 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel

return nrClient, defProfile, nil
}

// applyOverrides reads Profile info out of the Environment to override config
func applyOverrides(p *credentials.Profile) *credentials.Profile {
envAPIKey := os.Getenv("NEW_RELIC_API_KEY")
envInsightsInsertKey := os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY")
envRegion := os.Getenv("NEW_RELIC_REGION")

if envAPIKey == "" && envRegion == "" && envInsightsInsertKey == "" {
return p
}

out := credentials.Profile{}
if p != nil {
out = *p
}

if envAPIKey != "" {
out.APIKey = envAPIKey
}

if envInsightsInsertKey != "" {
out.InsightsInsertKey = envInsightsInsertKey
}

if envRegion != "" {
out.Region = strings.ToUpper(envRegion)
}

return &out
}
7 changes: 3 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"reflect"
"strings"
"time"

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

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

func (c *Config) setLogger() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339,
DisableLevelTruncation: true,
DisableLevelTruncation: true,
DisableTimestamp: true,
EnvironmentOverrideColors: true,
})

switch level := strings.ToUpper(c.LogLevel); level {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build integration

package client
package credentials

import (
"io/ioutil"
Expand All @@ -10,8 +10,6 @@ import (
"github.com/stretchr/testify/assert"

"github.com/newrelic/newrelic-client-go/pkg/region"

"github.com/newrelic/newrelic-cli/internal/credentials"
)

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

// Initialize the new configuration directory
c, err := credentials.LoadCredentials(f)
c, err := LoadCredentials(f)
assert.NoError(t, err)

// Create an initial profile to work with
testProfile := credentials.Profile{
testProfile := Profile{
Region: "us",
APIKey: "apiKeyGoesHere",
InsightsInsertKey: "insightsInsertKeyGoesHere",
Expand Down
3 changes: 1 addition & 2 deletions internal/credentials/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ func WithCredentialsFrom(configDir string, f func(c *Credentials)) {
func DefaultProfile() *Profile {
if defaultProfile == nil {
WithCredentials(func(c *Credentials) {
p := c.Profiles[c.DefaultProfile]
defaultProfile = &p
defaultProfile = c.Default()
})
}

Expand Down
48 changes: 46 additions & 2 deletions internal/credentials/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"reflect"
"strconv"
"strings"

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

// Default returns the default profile
func (c *Credentials) Default() *Profile {
var p *Profile
if c.DefaultProfile != "" {
if val, ok := c.Profiles[c.DefaultProfile]; ok {
return &val
p = &val
}
}

return nil
p = applyOverrides(p)
return p
}

// applyOverrides reads Profile info out of the Environment to override config
func applyOverrides(p *Profile) *Profile {
envAPIKey := os.Getenv("NEW_RELIC_API_KEY")
envInsightsInsertKey := os.Getenv("NEW_RELIC_INSIGHTS_INSERT_KEY")
envRegion := os.Getenv("NEW_RELIC_REGION")
envAccountID := os.Getenv("NEW_RELIC_ACCOUNT_ID")

if envAPIKey == "" && envRegion == "" && envInsightsInsertKey == "" && envAccountID == "" {
return p
}

out := Profile{}
if p != nil {
out = *p
}

if envAPIKey != "" {
out.APIKey = envAPIKey
}

if envInsightsInsertKey != "" {
out.InsightsInsertKey = envInsightsInsertKey
}

if envRegion != "" {
out.Region = strings.ToUpper(envRegion)
}

if envAccountID != "" {
accountID, err := strconv.Atoi(envAccountID)
if err != nil {
log.Warnf("Invalid account ID: %s", envAccountID)
return &out
}

out.AccountID = accountID
}

return &out
}

func readDefaultProfile(configDir string) (string, error) {
Expand Down
27 changes: 15 additions & 12 deletions internal/install/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
)

var (
interactiveMode bool
installLogging bool
autoDiscoveryMode bool
recipeFriendlyNames []string
specifyActions bool
interactiveMode bool
installLogging bool
recipeNames []string
recipeFilenames []string
)

// Command represents the install command.
Expand All @@ -25,10 +26,11 @@ var Command = &cobra.Command{
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
ic := installContext{
interactiveMode: interactiveMode,
installLogging: installLogging,
autoDiscoveryMode: autoDiscoveryMode,
recipeFriendlyNames: recipeFriendlyNames,
interactiveMode: interactiveMode,
installLogging: installLogging,
recipeNames: recipeNames,
recipeFilenames: recipeFilenames,
specifyActions: specifyActions,
}

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

func init() {
Command.Flags().BoolVarP(&interactiveMode, "interactive", "i", true, "enables interactive mode")
Command.Flags().BoolVarP(&installLogging, "installLogging", "l", true, "installs New Relic logging")
Command.Flags().BoolVarP(&autoDiscoveryMode, "autoDiscovery", "d", true, "enables auto-discovery mode")
Command.Flags().StringSliceVarP(&recipeFriendlyNames, "recipe", "r", []string{}, "the name of a recipe to install")
Command.Flags().BoolVarP(&interactiveMode, "interactive", "i", false, "enables interactive mode if specifyActions has been used")
Command.Flags().BoolVarP(&installLogging, "installLogging", "l", false, "installs New Relic logging if specifyActions has been used")
Command.Flags().BoolVarP(&specifyActions, "specifyActions", "s", false, "specify the actions to be run during install")
Command.Flags().StringSliceVarP(&recipeNames, "recipe", "r", []string{}, "the name of a recipe to install")
Command.Flags().StringSliceVarP(&recipeFilenames, "recipeFile", "c", []string{}, "a recipe file to install")
}
4 changes: 2 additions & 2 deletions internal/install/go_task_recipe_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newGoTaskRecipeExecutor() *goTaskRecipeExecutor {
}

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

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

// Create a temporary task file.
file, err := ioutil.TempFile("", r.Metadata.Name)
file, err := ioutil.TempFile("", r.Name)
defer os.Remove(file.Name())
if err != nil {
return err
Expand Down
26 changes: 26 additions & 0 deletions internal/install/install_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package install

type installContext struct {
specifyActions bool
interactiveMode bool
installLogging bool
installInfraAgent bool
recipeNames []string
recipeFilenames []string
}

func (i *installContext) ShouldInstallInfraAgent() bool {
return !i.RecipeFilenamesProvided() && (!i.specifyActions || i.installInfraAgent)
}

func (i *installContext) ShouldInstallLogging() bool {
return !i.RecipeFilenamesProvided() && (!i.specifyActions || i.installLogging)
}

func (i *installContext) RecipeFilenamesProvided() bool {
return len(i.recipeFilenames) > 0
}

func (i *installContext) RecipeNamesProvided() bool {
return len(i.recipeNames) > 0
}
72 changes: 72 additions & 0 deletions internal/install/install_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package install

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestShouldInstallInfraAgent_Default(t *testing.T) {
ic := installContext{}
require.True(t, ic.ShouldInstallInfraAgent())
}

func TestShouldInstallInfraAgent_SpecifyActions(t *testing.T) {
ic := installContext{
specifyActions: true,
}
require.False(t, ic.ShouldInstallInfraAgent())

ic.installInfraAgent = true
require.True(t, ic.ShouldInstallInfraAgent())
}

func TestShouldInstallInfraAgent_RecipeFilenamesProvided(t *testing.T) {
ic := installContext{
recipeFilenames: []string{"testFilename"},
}
require.False(t, ic.ShouldInstallInfraAgent())

ic.installInfraAgent = true
require.False(t, ic.ShouldInstallInfraAgent())
}
func TestShouldInstallLogging_Default(t *testing.T) {
ic := installContext{}
require.True(t, ic.ShouldInstallLogging())
}

func TestShouldInstallLogging_SpecifyActions(t *testing.T) {
ic := installContext{
specifyActions: true,
}
require.False(t, ic.ShouldInstallLogging())

ic.installLogging = true
require.True(t, ic.ShouldInstallLogging())
}

func TestShouldInstallLogging_RecipeFilenamesProvided(t *testing.T) {
ic := installContext{
recipeFilenames: []string{"testFilename"},
}
require.False(t, ic.ShouldInstallLogging())

ic.installInfraAgent = true
require.False(t, ic.ShouldInstallLogging())
}

func TestRecipeNamesProvided(t *testing.T) {
ic := installContext{}
require.False(t, ic.RecipeNamesProvided())

ic.recipeNames = []string{"testName"}
require.True(t, ic.RecipeNamesProvided())
}

func TestRecipeFilenamesProvided(t *testing.T) {
ic := installContext{}
require.False(t, ic.RecipeFilenamesProvided())

ic.recipeFilenames = []string{"testFilename"}
require.True(t, ic.RecipeFilenamesProvided())
}
6 changes: 3 additions & 3 deletions internal/install/mock_nrdb_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (c *mockNrdbClient) ThrowError(message string) {
c.error = message
}

func (c *mockNrdbClient) ReturnResultsAfterNAttempts(results []nrdb.NrdbResult, attempts int) {
func (c *mockNrdbClient) ReturnResultsAfterNAttempts(before []nrdb.NrdbResult, after []nrdb.NrdbResult, attempts int) {
c.results = func() []nrdb.NrdbResult {
if c.attempts < attempts {
return []nrdb.NrdbResult{}
return before
}

return results
return after
}
}

Expand Down
Loading

0 comments on commit c8faf9e

Please sign in to comment.