From f5655b3559109cfce32c49aea25c91a8ac2f7684 Mon Sep 17 00:00:00 2001 From: Ranjib Dey Date: Sat, 2 Apr 2016 23:48:31 -0700 Subject: [PATCH] implement create service cli --- addon.go | 7 +++- command/service_create.go | 42 +++++++++++++++++++++++- esacalation_policy.go | 8 ++--- service.go | 67 +++++++++++++++++++++++---------------- 4 files changed, 90 insertions(+), 34 deletions(-) diff --git a/addon.go b/addon.go index 4dbc8bc1..8de58d80 100644 --- a/addon.go +++ b/addon.go @@ -5,6 +5,7 @@ import ( log "github.com/Sirupsen/logrus" "github.com/google/go-querystring/query" "io/ioutil" + "net/http" ) type Addon struct { @@ -44,12 +45,16 @@ func (c *Client) InstallAddon(a Addon) error { resp, err := c.Post("/addons", data) defer resp.Body.Close() if err != nil { + return err + } + if resp.StatusCode != http.StatusCreated { ct, rErr := ioutil.ReadAll(resp.Body) if rErr == nil { log.Debug(string(ct)) } + return fmt.Errorf("Failed to create. HTTP Status code: %d", resp.StatusCode) } - return err + return nil } func (c *Client) DeleteAddon(id string) error { diff --git a/command/service_create.go b/command/service_create.go index a6dc5bb6..3cf00b03 100644 --- a/command/service_create.go +++ b/command/service_create.go @@ -1,11 +1,17 @@ package main import ( + "encoding/json" + "fmt" + "github.com/PagerDuty/go-pagerduty" + log "github.com/Sirupsen/logrus" "github.com/mitchellh/cli" + "os" "strings" ) type ServiceCreate struct { + Meta } func ServiceCreateCommand() (cli.Command, error) { @@ -14,7 +20,8 @@ func ServiceCreateCommand() (cli.Command, error) { func (c *ServiceCreate) Help() string { helpText := ` - ` + pd service create Create a new service from json file + ` + c.Meta.Help() return strings.TrimSpace(helpText) } @@ -23,5 +30,38 @@ func (c *ServiceCreate) Synopsis() string { } func (c *ServiceCreate) Run(args []string) int { + flags := c.Meta.FlagSet("service create") + flags.Usage = func() { fmt.Println(c.Help()) } + if err := flags.Parse(args); err != nil { + log.Error(err) + return -1 + } + if err := c.Meta.Setup(); err != nil { + log.Error(err) + return -1 + } + client := c.Meta.Client() + var s pagerduty.Service + if len(flags.Args()) != 1 { + log.Error("Please specify input json file") + return -1 + } + log.Info("Input file is:", flags.Arg(0)) + f, err := os.Open(flags.Arg(0)) + if err != nil { + log.Error(err) + return -1 + } + defer f.Close() + decoder := json.NewDecoder(f) + if err := decoder.Decode(&s); err != nil { + log.Errorln("Failed to decode json. Error:", err) + return -1 + } + log.Debugf("%#v", s) + if err := client.CreateService(s); err != nil { + log.Error(err) + return -1 + } return 0 } diff --git a/esacalation_policy.go b/esacalation_policy.go index d74fe0d2..0411398f 100644 --- a/esacalation_policy.go +++ b/esacalation_policy.go @@ -13,12 +13,12 @@ type EscalationRule struct { type EscalationPolicy struct { APIObject - Name string + Name string `json:"name,omitempty"` EscalationRules []APIObject `json:"escalation_rules,omitempty"` - Services []APIObject `json:"omitempty"` + Services []APIObject `json:"services,omitempty"` NumLoops uint `json:"num_loops,omitempty"` - Teams []APIObject `json:"omitempty"` - Description string `json:"omitempty"` + Teams []APIObject `json:"teams,omitempty"` + Description string `json:"description,omitempty"` } type ListEscalationPolicyResponse struct { diff --git a/service.go b/service.go index d970df94..338b6e1c 100644 --- a/service.go +++ b/service.go @@ -2,7 +2,10 @@ package pagerduty import ( "fmt" + log "github.com/Sirupsen/logrus" "github.com/google/go-querystring/query" + "io/ioutil" + "net/http" ) type EmailFilter struct { @@ -16,10 +19,10 @@ type EmailFilter struct { type Integration struct { APIObject - Name string - Service APIObject - CreatedAt string `json:"created_at"` - Vendor APIObject + Name string `json:"name,omitempty"` + Service APIObject `json:"service,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + Vendor APIObject `json:"vendor,omitempty"` IntegrationEmail string `json:"integration_email"` EmailIncidentCreation string `json:"email_incident_creation,omitempty"` EmailFilterMode string `json:"email_filter_mode"` @@ -27,23 +30,23 @@ type Integration struct { } type NamedTime struct { - Type string - Name string + Type string `json:"type,omitempty"` + Name string `json:"name,omitempty"` } type ScheduledAction struct { - Type string - At NamedTime - ToUrgency string `json:"to_urgency"` + Type string `json:"type,omitempty"` + At NamedTime `json:"at,omitempty"` + ToUrgency string `json:"to_urgency"` } type SupportHours struct { - Type string - Urgency string + Type string `json:"type,omitempty"` + Urgency string `json:"urgency,omitempty"` } type SupportHoursDetails struct { - Type string + Type string `json:"type,omitempty"` Timezone string `json:"time_zone"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` @@ -51,24 +54,24 @@ type SupportHoursDetails struct { } type IncidentUrgencyRule struct { - Type string - DuringSupportHours SupportHours `json:"during_support_hours"` - OutsideSupportHours SupportHours `json:"outside_support_hours"` + Type string `json:"type,omitempty"` + DuringSupportHours SupportHours `json:"during_support_hours,omitempty"` + OutsideSupportHours SupportHours `json:"outside_support_hours,omitempty"` } type Service struct { APIObject - Name string - Description string - AutoResolveTimeout uint `json:"auto_resolve_timeout"` - AcknowledgementTimeout uint `json:"acknowledgement_timeout"` - CreateAt string `json:"created_at"` - Status string `json:"status"` - LastIncidentTimestamp string `json:"last_incident_timestamp"` - Integrations []Integration `json:"integrations"` - EscalationPolicy EscalationPolicy `json:"escalation_policy"` - Teams []Team - IncidentUrgencyRule IncidentUrgencyRule `json:"incident_urgnecy_rule"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + AutoResolveTimeout uint `json:"auto_resolve_timeout,omitempty"` + AcknowledgementTimeout uint `json:"acknowledgement_timeout,omitempty"` + CreateAt string `json:"created_at,omitempty"` + Status string `json:"status,omitempty"` + LastIncidentTimestamp string `json:"last_incident_timestamp,omitempty"` + Integrations []Integration `json:"integrations,omitempty"` + EscalationPolicy EscalationPolicy `json:"escalation_policy,omitempty"` + Teams []Team `json:"teams,omitempty"` + IncidentUrgencyRule IncidentUrgencyRule `json:"incident_urgency_rule,omitempty"` SupportHours SupportHoursDetails `json:"support_hours,omitempty"` ScheduledActions []ScheduledAction `json:"scheduled_actions,omitempty"` } @@ -118,7 +121,7 @@ func (c *Client) GetService(id string, o GetServiceOptions) (*Service, error) { } s, ok := result["service"] if !ok { - return nil, fmt.Errorf("JSON responsde does not have service field") + return nil, fmt.Errorf("JSON response does not have service field") } return &s, nil } @@ -126,7 +129,15 @@ func (c *Client) GetService(id string, o GetServiceOptions) (*Service, error) { func (c *Client) CreateService(s Service) error { data := make(map[string]Service) data["service"] = s - _, err := c.Post("/services", data) + resp, err := c.Post("/services", data) + defer resp.Body.Close() + if resp.StatusCode != http.StatusCreated { + ct, rErr := ioutil.ReadAll(resp.Body) + if rErr == nil { + log.Debug(string(ct)) + } + return fmt.Errorf("Failed to create. HTTP Status code: %d", resp.StatusCode) + } return err }