diff --git a/internal/client/client.go b/internal/client/client.go index bc7898b8c..3a73cb0f5 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -14,7 +14,6 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel var ( err error personalAPIKey string - adminAPIKey string region string ) @@ -24,7 +23,6 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel defProfile = applyOverrides(defProfile) if defProfile != nil { - adminAPIKey = defProfile.AdminAPIKey personalAPIKey = defProfile.PersonalAPIKey region = defProfile.Region } else { @@ -32,7 +30,6 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel } nrClient, err := newrelic.New( - newrelic.ConfigAPIKey(adminAPIKey), newrelic.ConfigPersonalAPIKey(personalAPIKey), newrelic.ConfigLogLevel(cfg.LogLevel), newrelic.ConfigRegion(region), @@ -44,12 +41,12 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel return nrClient, nil } +// applyOverrides reads Profile info out of the Environment to override config func applyOverrides(p *credentials.Profile) *credentials.Profile { envAPIKey := os.Getenv("NEWRELIC_PERSONAL_API_KEY") - envAdminAPIKey := os.Getenv("NEWRELIC_ADMIN_API_KEY") envRegion := os.Getenv("NEWRELIC_REGION") - if envAPIKey == "" && envAdminAPIKey == "" && envRegion == "" { + if envAPIKey == "" && envRegion == "" { return p } @@ -65,10 +62,6 @@ func applyOverrides(p *credentials.Profile) *credentials.Profile { out.PersonalAPIKey = envAPIKey } - if envAdminAPIKey != "" { - out.AdminAPIKey = envAdminAPIKey - } - if envRegion != "" { out.Region = envRegion } diff --git a/internal/credentials/command.go b/internal/credentials/command.go index e2ad55839..42e17065d 100644 --- a/internal/credentials/command.go +++ b/internal/credentials/command.go @@ -12,7 +12,6 @@ var ( profileName string region string apiKey string - adminAPIKey string ) // Command is the base command for managing profiles @@ -29,10 +28,10 @@ var credentialsAdd = &cobra.Command{ The describe-deployments command performs a search for New Relic APM deployments. `, - Example: "newrelic credentials add -n -r --apiKey --adminAPIKey ", + Example: "newrelic credentials add -n -r --apiKey ", Run: func(cmd *cobra.Command, args []string) { WithCredentials(func(creds *Credentials) { - err := creds.AddProfile(profileName, region, apiKey, adminAPIKey) + err := creds.AddProfile(profileName, region, apiKey) if err != nil { log.Fatal(err) } @@ -117,11 +116,9 @@ func init() { credentialsAdd.Flags().StringVarP(&profileName, "profileName", "n", "", "The profile name to add") credentialsAdd.Flags().StringVarP(®ion, "region", "r", "", "us or eu region") credentialsAdd.Flags().StringVarP(&apiKey, "apiKey", "", "", "Personal API key") - credentialsAdd.Flags().StringVarP(&adminAPIKey, "adminAPIKey", "", "", "Admin API key for REST v2") credentialsAdd.MarkFlagRequired("profileName") credentialsAdd.MarkFlagRequired("region") - // credentialsAdd.MarkFlagRequired("apiKey") - // credentialsAdd.MarkFlagRequired("adminAPIKey") + credentialsAdd.MarkFlagRequired("apiKey") // Default Command.AddCommand(credentialsDefault) diff --git a/internal/credentials/credentials.go b/internal/credentials/credentials.go index 9df719e84..30c97bef6 100644 --- a/internal/credentials/credentials.go +++ b/internal/credentials/credentials.go @@ -69,7 +69,7 @@ func (c *Credentials) profileExists(profileName string) bool { } // AddProfile adds a new profile to the credentials file. -func (c *Credentials) AddProfile(profileName, region, apiKey, adminAPIKey string) error { +func (c *Credentials) AddProfile(profileName, region, apiKey string) error { if c.profileExists(profileName) { return fmt.Errorf("profile with name %s already exists", profileName) @@ -78,7 +78,6 @@ func (c *Credentials) AddProfile(profileName, region, apiKey, adminAPIKey string p := Profile{ Region: region, PersonalAPIKey: apiKey, - AdminAPIKey: adminAPIKey, } c.Profiles[profileName] = p @@ -150,10 +149,9 @@ func (c *Credentials) List() { defer color.Unset() colorMuted := color.New(color.FgHiBlack).SprintFunc() - nameLen := 4 // Name - keyLen := 8 // - adminKeyLen := 8 // - regionLen := 6 // Region + nameLen := 4 // Name + keyLen := 8 // + regionLen := 6 // Region // Find lengths for pretty printing for k, v := range c.Profiles { @@ -177,9 +175,9 @@ func (c *Credentials) List() { nameLen += len(defaultProfileString) - format := fmt.Sprintf("%%-%ds %%-%ds %%-%ds %%-%ds\n", nameLen, regionLen, keyLen, adminKeyLen) - fmt.Printf(format, "Name", "Region", "API key", "Admin API key") - fmt.Printf(format, strings.Repeat("-", nameLen), strings.Repeat("-", regionLen), strings.Repeat("-", keyLen), strings.Repeat("-", adminKeyLen)) + format := fmt.Sprintf("%%-%ds %%-%ds %%-%ds\n", nameLen, regionLen, keyLen) + fmt.Printf(format, "Name", "Region", "API key") + fmt.Printf(format, strings.Repeat("-", nameLen), strings.Repeat("-", regionLen), strings.Repeat("-", keyLen)) // Print them out for k, v := range c.Profiles { name := k @@ -191,11 +189,7 @@ func (c *Credentials) List() { key = v.PersonalAPIKey } - adminKey := colorMuted(hiddenKeyString) - if showKeys { - adminKey = v.AdminAPIKey - } - fmt.Printf(format, name, v.Region, key, adminKey) + fmt.Printf(format, name, v.Region, key) } fmt.Println("") } diff --git a/internal/credentials/profiles.go b/internal/credentials/profiles.go index aa6e4593b..3485abc2b 100644 --- a/internal/credentials/profiles.go +++ b/internal/credentials/profiles.go @@ -16,9 +16,8 @@ const DefaultProfileFile = "default-profile" // Profile contains data of a single profile type Profile struct { - AdminAPIKey string `mapstructure:"adminAPIKey" json:"adminAPIKey,omitempty"` // For accessing New Relic (Rest v2) - PersonalAPIKey string `mapstructure:"apiKey" json:"apiKey,omitempty"` // For accessing New Relic GraphQL resources - Region string `mapstructure:"region" json:"region,omitempty"` // Region to use for New Relic resources + PersonalAPIKey string `mapstructure:"apiKey" json:"apiKey,omitempty"` // For accessing New Relic GraphQL resources + Region string `mapstructure:"region" json:"region,omitempty"` // Region to use for New Relic resources } // LoadProfiles reads the credential profiles from the default path. diff --git a/internal/credentials/profiles_integration_test.go b/internal/credentials/profiles_integration_test.go index 0b3de9e47..279453d1e 100644 --- a/internal/credentials/profiles_integration_test.go +++ b/internal/credentials/profiles_integration_test.go @@ -44,12 +44,11 @@ func TestCredentials(t *testing.T) { assert.Equal(t, c.ConfigDirectory, f) // Create an initial profile to work with - err = c.AddProfile("testCase1", "us", "apiKeyGoesHere", "anotherApiKeyGoesHere") + err = c.AddProfile("testCase1", "us", "apiKeyGoesHere") assert.NoError(t, err) assert.Equal(t, len(c.Profiles), 1) assert.Equal(t, c.Profiles["testCase1"].Region, "us") assert.Equal(t, c.Profiles["testCase1"].PersonalAPIKey, "apiKeyGoesHere") - assert.Equal(t, c.Profiles["testCase1"].AdminAPIKey, "anotherApiKeyGoesHere") assert.Equal(t, c.DefaultProfile, "") // Set the default profile to the only one we've got @@ -58,18 +57,17 @@ func TestCredentials(t *testing.T) { assert.Equal(t, c.DefaultProfile, "testCase1") // Adding a profile with the same name should result in an error - err = c.AddProfile("testCase1", "us", "foot", "hand") + err = c.AddProfile("testCase1", "us", "foot") assert.Error(t, err) assert.Equal(t, len(c.Profiles), 1) assert.True(t, c.profileExists("testCase1")) // Create a second profile to work with - err = c.AddProfile("testCase2", "us", "apiKeyGoesHere", "anotherApiKeyGoesHere") + err = c.AddProfile("testCase2", "us", "apiKeyGoesHere") assert.NoError(t, err) assert.Equal(t, len(c.Profiles), 2) assert.Equal(t, c.Profiles["testCase2"].Region, "us") assert.Equal(t, c.Profiles["testCase2"].PersonalAPIKey, "apiKeyGoesHere") - assert.Equal(t, c.Profiles["testCase2"].AdminAPIKey, "anotherApiKeyGoesHere") // Set the default profile to the new one err = c.SetDefaultProfile("testCase2")