Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat)implement create service cli #6

Merged
merged 1 commit into from
Apr 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/google/go-querystring/query"
"io/ioutil"
"net/http"
)

type Addon struct {
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 41 additions & 1 deletion command/service_create.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -14,7 +20,8 @@ func ServiceCreateCommand() (cli.Command, error) {

func (c *ServiceCreate) Help() string {
helpText := `
`
pd service create <FILE> Create a new service from json file
` + c.Meta.Help()
return strings.TrimSpace(helpText)
}

Expand All @@ -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
}
8 changes: 4 additions & 4 deletions esacalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
67 changes: 39 additions & 28 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,59 +19,59 @@ 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"`
EmailFilters []EmailFilter `json:"email_filters,omitempty"`
}

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"`
DaysOfWeek []uint `json:"days_of_week"`
}

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"`
}
Expand Down Expand Up @@ -118,15 +121,23 @@ 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
}

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
}

Expand Down