Skip to content

Commit

Permalink
Adding minor feedback to cluster context command (#281)
Browse files Browse the repository at this point in the history
* Adding minor feedback to cluster context command

* Add usertoken auth
  • Loading branch information
dkeohane authored Nov 16, 2022
1 parent 09704fc commit b86232b
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions cmd/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ type contextOptions struct {
clusterID string
baseDomain string
days int
pages int
oauthtoken string
usertoken string
externalID string
infraID string
awsProfile string
}

const (
PagerDutyOauthTokenConfigKey = "pd_oauth_token"
PagerDutyUserTokenConfigKey = "pd_user_token"
)

// newCmdContext implements the context command to show the current context of a cluster
Expand All @@ -58,9 +61,11 @@ func newCmdContext() *cobra.Command {
contextCmd.Flags().StringVarP(&ops.clusterID, "cluster-id", "C", "", "Cluster ID")
contextCmd.Flags().StringVarP(&ops.awsProfile, "profile", "p", "", "AWS Profile")
contextCmd.Flags().BoolVarP(&ops.verbose, "verbose", "", false, "Verbose output")
contextCmd.Flags().BoolVarP(&ops.full, "full", "", false, "Run full suite of checks.")
contextCmd.Flags().IntVarP(&ops.days, "days", "z", 30, "Command will display X days of Error SLs sent to the cluster. Days is set to 30 by default")
contextCmd.Flags().StringVarP(&ops.oauthtoken, "oauthtoken", "t", "", "Pass in PD oauthtoken directly. If not passed in, by default will read token from ~/.config/pagerduty-cli/config.json")
contextCmd.Flags().BoolVar(&ops.full, "full", false, "Run full suite of checks.")
contextCmd.Flags().IntVarP(&ops.days, "days", "d", 30, "Command will display X days of Error SLs sent to the cluster. Days is set to 30 by default")
contextCmd.Flags().IntVar(&ops.pages, "pages", 40, "Command will display X pages of Cloud Trail logs for the cluster. Pages is set to 40 by default")
contextCmd.Flags().StringVar(&ops.oauthtoken, "oauthtoken", "", "Pass in PD oauthtoken directly. If not passed in, by default will read `pd_oauth_token` from ~/.config/osdctl")
contextCmd.Flags().StringVar(&ops.usertoken, "usertoken", "", "Pass in PD usertoken directly. If not passed in, by default will read `pd_user_token` from ~/.config/osdctl")

return contextCmd
}
Expand Down Expand Up @@ -133,7 +138,8 @@ func (o *contextOptions) run() error {
err = o.printPDAlerts()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't print pagerduty alerts: %v\n", err)
os.Exit(1)
// Here we don't actually want to error out, this is to ensure that even if we don't have the
// pd auth setup, we can still get the rest of the output.
}

// Print other helpful links
Expand Down Expand Up @@ -250,7 +256,17 @@ func (o *contextOptions) printServiceLogs() error {
return nil
}

func GetPagerdutyClient(oauthtoken string) (*pd.Client, error) {
func getPDUserClient(usertoken string) (*pd.Client, error) {
if usertoken == "" {
if !viper.IsSet(PagerDutyUserTokenConfigKey) {
return nil, fmt.Errorf("key %s is not set in config file", PagerDutyUserTokenConfigKey)
}
usertoken = viper.GetString(PagerDutyUserTokenConfigKey)
}
return pd.NewClient(usertoken), nil
}

func getPDOauthClient(oauthtoken string) (*pd.Client, error) {
if oauthtoken == "" {
if !viper.IsSet(PagerDutyOauthTokenConfigKey) {
return nil, fmt.Errorf("key %s is not set in config file", PagerDutyOauthTokenConfigKey)
Expand All @@ -260,6 +276,20 @@ func GetPagerdutyClient(oauthtoken string) (*pd.Client, error) {
return pd.NewOAuthClient(oauthtoken), nil
}

func GetPagerdutyClient(usertoken string, oauthtoken string) (*pd.Client, error) {
client, err := getPDUserClient(usertoken)
if client != nil {
fmt.Print("Usering User Client")
return client, err
}
client, err = getPDOauthClient(oauthtoken)
if err != nil {
return nil, fmt.Errorf("Failed to create both user and oauth clients for pd")
}
fmt.Print("Usering Oauth Client")
return client, err
}

func getPDSeviceID(pdClient *pd.Client, ctx context.Context, baseDomain string) (string, error) {
lsResponse, err := pdClient.ListServicesWithContext(ctx, pd.ListServiceOptions{Query: baseDomain})

Expand Down Expand Up @@ -426,7 +456,7 @@ func printHistoricalPDAlertSummary(pdClient *pd.Client, ctx context.Context, ser

func (o *contextOptions) printPDAlerts() error {

pdClient, err := GetPagerdutyClient(o.oauthtoken)
pdClient, err := GetPagerdutyClient(o.usertoken, o.oauthtoken)
if err != nil {
fmt.Println("error getting pd client: ", err.Error())
return err
Expand Down Expand Up @@ -475,8 +505,8 @@ func (o *contextOptions) printCloudTrailLogs() error {
foundEvents := []*cloudtrail.Event{}
var eventSearchInput = cloudtrail.LookupEventsInput{}

fmt.Println("Pulling and filtering the past 40 pages of Cloudtrail data")
for counter := 0; counter <= 40; counter++ {
fmt.Println("Pulling and filtering the past", o.pages, "pages of Cloudtrail data")
for counter := 0; counter <= o.pages; counter++ {
print(".")
cloudTrailEvents, err := awsJumpClient.LookupEvents(&eventSearchInput)
if err != nil {
Expand Down

0 comments on commit b86232b

Please sign in to comment.