From 8217b267597bdfdad97a7c7c5544cb6d949e4ccd Mon Sep 17 00:00:00 2001 From: dheeruk12 <96110782+dheeruk12@users.noreply.github.com> Date: Mon, 18 Jul 2022 17:52:26 +0530 Subject: [PATCH] added maintenace_window,tags,priority,incident_tags --- client/client.go | 39 ++++++++-------- client/error.go | 2 +- client/maintenance_window.go | 90 ++++++++++++++++++++++++++++++++++++ client/priority.go | 17 +++++-- client/schedules.go | 12 +++-- client/tags.go | 32 +++++++++++-- client/users.go | 2 +- 7 files changed, 160 insertions(+), 34 deletions(-) create mode 100644 client/maintenance_window.go diff --git a/client/client.go b/client/client.go index a9b3360..cc0ce81 100644 --- a/client/client.go +++ b/client/client.go @@ -24,22 +24,23 @@ type Config struct { } type Client struct { - baseURL *url.URL - client *http.Client - Config *Config - Teams *TeamService - Services *Service - Schedules *ScheduleService - Roles *RoleService - Integrations *IntegrationServerice - Incidents *IncidentService - Esp *EspService - Members *MemberService - Invite *InviteService - Users *UserService - AlertRules *AlertRuleService - Priority *PriorityService - Tags *TagsService + baseURL *url.URL + client *http.Client + Config *Config + Teams *TeamService + Services *Service + Schedules *ScheduleService + Roles *RoleService + Integrations *IntegrationServerice + Incidents *IncidentService + Esp *EspService + Members *MemberService + Invite *InviteService + Users *UserService + AlertRules *AlertRuleService + Priority *PriorityService + Tags *TagsService + MaintenanceWindow *MaintenanceWindowService } type Response struct { @@ -79,6 +80,7 @@ func NewClient(config *Config) (*Client, error) { c.AlertRules = &AlertRuleService{c} c.Priority = &PriorityService{c} c.Tags = &TagsService{c} + c.MaintenanceWindow = &MaintenanceWindowService{c} return c, nil @@ -154,9 +156,10 @@ func (c *Client) checkResponse(res *Response) error { func (c *Client) decodeErrorResponse(res *Response) error { - v := &errorResponse{Error: &Error{ErrorResponse: res}} + v := &errorResponse{Error: &Error{ErrorResponse: res, Code: res.Response.StatusCode}} if err := c.DecodeJSON(res, v); err != nil { - return fmt.Errorf("%s API call to %s failed: %v", res.Response.Request.Method, res.Response.Request.URL.String(), res.Response.Status) + + return fmt.Errorf("%s APIs call to %s failed: %v", res.Response.Request.Method, res.Response.Request.URL.String(), res.Response.Status) } return v.Error diff --git a/client/error.go b/client/error.go index 072f0a5..d7013d7 100644 --- a/client/error.go +++ b/client/error.go @@ -23,5 +23,5 @@ type Error struct { } func (e *Error) Error() string { - return fmt.Sprintf("%s API call to %s failed %v. Code: %d, Errors: %v, Message: %s", e.ErrorResponse.Response.Request.Method, e.ErrorResponse.Response.Request.URL.String(), e.ErrorResponse.Response.Status, e.Code, e.Errors, e.Message) + return fmt.Sprintf("%s API call to %s failed %v. Errors: %s, Message: %s", e.ErrorResponse.Response.Request.Method, e.ErrorResponse.Response.Request.URL.String(), e.ErrorResponse.Response.Status, e.ErrorResponse.Response.Request.Body, e.Message) } diff --git a/client/maintenance_window.go b/client/maintenance_window.go new file mode 100644 index 0000000..cc692ef --- /dev/null +++ b/client/maintenance_window.go @@ -0,0 +1,90 @@ +package client + +import ( + "encoding/json" + "fmt" +) + +type MaintenanceWindowService service + +type ServiceMaintenance struct { + UniqueID string `json:"unique_id,omitempty"` + Service string `json:"service"` +} + +type MaintenanceWindow struct { + UniqueID string `json:"unique_id,omitempty"` + Name string `json:"name"` + RepeatInterval int `json:"repeat_interval,omitempty"` + RepeatUntil string `json:"repeat_until,omitempty"` + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` + TimeZone string `json:"time_zone"` + Services []ServiceMaintenance `json:"services"` + Creation_Date string `json:"creation_date"` +} + +func (c *MaintenanceWindowService) CreateMaintenanceWindow(team string, maintenance *MaintenanceWindow) (*MaintenanceWindow, error) { + path := fmt.Sprintf("/api/account/teams/%s/maintenance/", team) + body, err := c.client.newRequestDo("POST", path, maintenance) + if err != nil { + return nil, err + } + var s MaintenanceWindow + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *MaintenanceWindowService) UpdateMaintenanceWindow(team string, maintenance_id string, maintenance *MaintenanceWindow) (*MaintenanceWindow, error) { + path := fmt.Sprintf("/api/account/teams/%s/maintenance/%s/", team, maintenance_id) + body, err := c.client.newRequestDo("PUT", path, maintenance) + if err != nil { + return nil, err + } + var s MaintenanceWindow + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *MaintenanceWindowService) GetMaintenanceWindows(team string) ([]MaintenanceWindow, error) { + path := fmt.Sprintf("/api/account/teams/%s/maintenance/", team) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []MaintenanceWindow + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *MaintenanceWindowService) DeleteMaintenanceWindow(team string, maintenance_id string) error { + path := fmt.Sprintf("/api/account/teams/%s/maintenance/%s/", team, maintenance_id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} + +func (c *MaintenanceWindowService) GetMaintenanceWindowById(team string, maintenance_id string) (*MaintenanceWindow, error) { + path := fmt.Sprintf("/api/account/teams/%s/maintenance/%s/", team, maintenance_id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s MaintenanceWindow + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} diff --git a/client/priority.go b/client/priority.go index 7898099..38a0e80 100644 --- a/client/priority.go +++ b/client/priority.go @@ -8,6 +8,15 @@ import ( type PriorityService service type Priority struct { + Unique_Id string `json:"unique_id,omitempty"` + Name string `json:"name"` + Description string `json:"description"` + Color string `json:"color"` + Team int `json:"team,omitempty"` + Creation_Date string `json:"creation_date,omitempty"` +} + +type ReadPriority struct { Unique_Id string `json:"unique_id,omitempty"` Name string `json:"name"` Description string `json:"description"` @@ -30,13 +39,13 @@ func (c *PriorityService) CreatePriority(team string, priority *Priority) (*Prio return &s, nil } -func (c *PriorityService) GetPriority(team string) ([]Priority, error) { +func (c *PriorityService) GetPriority(team string) ([]ReadPriority, error) { path := fmt.Sprintf("/api/account/teams/%s/priority/", team) body, err := c.client.newRequestDo("GET", path, nil) if err != nil { return nil, err } - var s []Priority + var s []ReadPriority err = json.Unmarshal(body.BodyBytes, &s) if err != nil { return nil, err @@ -44,13 +53,13 @@ func (c *PriorityService) GetPriority(team string) ([]Priority, error) { return s, nil } -func (c *PriorityService) GetPriorityById(team, id string) (*Priority, error) { +func (c *PriorityService) GetPriorityById(team, id string) (*ReadPriority, error) { path := fmt.Sprintf("/api/account/teams/%s/priority/%s/", team, id) body, err := c.client.newRequestDo("GET", path, nil) if err != nil { return nil, err } - var s Priority + var s ReadPriority err = json.Unmarshal(body.BodyBytes, &s) if err != nil { return nil, err diff --git a/client/schedules.go b/client/schedules.go index d35866a..d6e8683 100644 --- a/client/schedules.go +++ b/client/schedules.go @@ -11,12 +11,12 @@ type Restrictions struct { Duration int `json:"duration"` StartDayOfWeek int `json:"start_day_of_week"` StartTimeOfDay string `json:"start_time_of_day"` - Unique_Id string `json:"unique_id",omitempty` + Unique_Id string `json:"unique_id,omitempty"` } type Users struct { User string `json:"user"` Position int `json:"position"` - Unique_Id string `json:"unique_id",omitempty` + Unique_Id string `json:"unique_id,omitempty"` } type Overrides struct { @@ -24,7 +24,7 @@ type Overrides struct { User string `json:"user"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` - Unique_Id string `json:"unique_id",omitempty` + Unique_Id string `json:"unique_id,omitempty"` } type Layers struct { ShiftLength int `json:"shift_length"` @@ -42,6 +42,7 @@ type Layers struct { type CreateUserLayer struct { User string `json:"user"` } + type CreateLayers struct { ShiftLength int `json:"shift_length"` Name string `json:"name"` @@ -49,6 +50,7 @@ type CreateLayers struct { RotationEndTime string `json:"rotation_end_time"` RestrictionType int `json:"restriction_type",omitempty` Users []CreateUserLayer `json:"users"` + Restrictions []Restrictions `json:"restrictions"` } type CreateSchedule struct { @@ -59,7 +61,7 @@ type CreateSchedule struct { Team string `json:"team"` Layers []CreateLayers `json:"layers"` Overrides []Overrides `json:"overrides"` - Unique_Id string `json:"unique_id",omitempty` + Unique_Id string `json:"unique_id,omitempty"` } type Schedules struct { Name string `json:"name"` @@ -67,7 +69,7 @@ type Schedules struct { Summary string `json:"summary"` Time_zone string `json:"time_zone"` Team string `json:"team"` - Unique_Id string `json:"unique_id",omitempty` + Unique_Id string `json:"unique_id,omitempty"` Layers []Layers `json:"layers"` Overrides []Overrides `json:"overrides"` } diff --git a/client/tags.go b/client/tags.go index 0bf903f..860b6b5 100644 --- a/client/tags.go +++ b/client/tags.go @@ -8,6 +8,14 @@ import ( type TagsService service type Tag struct { + Unique_Id string `json:"unique_id,omitempty"` + Name string `json:"name"` + Color string `json:"color"` + Team int `json:"team,omitempty"` + Creation_Date string `json:"creation_date,omitempty"` +} + +type ReadTag struct { Unique_Id string `json:"unique_id,omitempty"` Name string `json:"name"` Color string `json:"color"` @@ -29,13 +37,27 @@ func (c *TagsService) CreateTag(team string, tags *Tag) (*Tag, error) { return &s, nil } -func (c *TagsService) GetPriority(team string) ([]Tag, error) { +func (c *TagsService) UpdateTag(team string, tag_id string, tags *Tag) (*Tag, error) { + path := fmt.Sprintf("/api/account/teams/%s/tags/%s/", team, tag_id) + body, err := c.client.newRequestDo("PUT", path, tags) + if err != nil { + return nil, err + } + var s Tag + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *TagsService) GetTags(team string) ([]ReadTag, error) { path := fmt.Sprintf("/api/account/teams/%s/tags/", team) body, err := c.client.newRequestDo("GET", path, nil) if err != nil { return nil, err } - var s []Tag + var s []ReadTag err = json.Unmarshal(body.BodyBytes, &s) if err != nil { return nil, err @@ -43,13 +65,13 @@ func (c *TagsService) GetPriority(team string) ([]Tag, error) { return s, nil } -func (c *TagsService) GetPriorityById(team, id string) (*Tag, error) { +func (c *TagsService) GetTagId(team, id string) (*ReadTag, error) { path := fmt.Sprintf("/api/account/teams/%s/tags/%s/", team, id) body, err := c.client.newRequestDo("GET", path, nil) if err != nil { return nil, err } - var s Tag + var s ReadTag err = json.Unmarshal(body.BodyBytes, &s) if err != nil { return nil, err @@ -57,7 +79,7 @@ func (c *TagsService) GetPriorityById(team, id string) (*Tag, error) { return &s, nil } -func (c *TagsService) DeletePriority(team, id string) error { +func (c *TagsService) DeleteTag(team, id string) error { path := fmt.Sprintf("/api/account/teams/%s/tags/%s/", team, id) _, err := c.client.newRequestDo("DELETE", path, nil) if err != nil { diff --git a/client/users.go b/client/users.go index 13280a5..2868c1c 100644 --- a/client/users.go +++ b/client/users.go @@ -19,7 +19,7 @@ type GetUsers struct { } func (c *UserService) GetUsers(email string) ([]GetUsers, error) { - path := "/api/account/users" + path := "/api/account/users/" body, err := c.client.newRequestDo("GET", path, nil) if err != nil {