diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b0f894 --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# zenduty-go-sdk + Zenduty API client in Go, primarily used by [Zenduty](https://github.com/Zenduty/zenduty-go-sdk) provider in Terraform. + + ## Installation +```bash +go get github.com/Zenduty/zenduty-go-sdk +``` + + +## Getting started +Before you begin making use of the SDK, make sure you have your Zenduty Access Token. + +``` +import "github.com/Zenduty/zenduty-go-sdk" +``` +Configure the Token and Url + +``` +config := &client.Config{ + Token: "", // enter token for authentication + BaseURL: "", // your url + } +``` +Based on the service you want to communicate with,Create object for required class,For example, to create Team + + + +## Example usage +```go +package main + +import ( + "fmt" + + "github.com/Zenduty/zenduty-go-sdk" +) + + +func main() { + config := &client.Config{ + Token: "", // enter token for authentication + } + c, err := client.NewClient(config) + if err != nil { + panic(err) + } + + newteam := &client.Team{} + newteam.Name = "test" + + resp, err := c.Teams.CreateTeam(newteam) + if err != nil { + panic(err) + } + fmt.Printf("%+v\n", resp) + +} + + +``` + diff --git a/alert_rules.go b/alert_rules.go new file mode 100644 index 0000000..5986025 --- /dev/null +++ b/alert_rules.go @@ -0,0 +1,97 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type AlertRuleService service + +type AlertAction struct { + UniqueId string `json:"unique_id,omitempty"` + ActionType int `json:"action_type"` + Key string `json:"key"` + Value string `json:"value"` + Assigned_To string `json:"assign_to"` + EscalationPolicy string `json:"escalation_policy"` + Schedule string `json:"schedule"` + TeamPriority string `json:"team_priority"` + SLA string `json:"sla"` +} + +type AlertRule struct { + Unique_Id string `json:"unique_id"` + Description string `json:"description"` + Position int `json:"position,omitempty"` + Stop bool `json:"stop,omitempty"` + RuleType int `json:"ruleType,omitempty"` + RuleJson string `json:"rule_json"` + Conditions []string `json:"conditions,omitempty"` + Actions []AlertAction `json:"actions,omitempty"` +} + +func (c *AlertRuleService) CreateAlertRule(team_id, service_id, integration_id string, rule *AlertRule) (*AlertRule, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/transformers/", team_id, service_id, integration_id) + body, err := c.client.newRequestDo("POST", path, rule) + if err != nil { + return nil, err + } + var s AlertRule + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *AlertRuleService) GetAlertRules(team_id, service_id, integration_id string) ([]AlertRule, error) { + + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/transformers/", team_id, service_id, integration_id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []AlertRule + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *AlertRuleService) GetAlertRule(team_id, service_id, integration_id, id string) (*AlertRule, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/transformers/%s/", team_id, service_id, integration_id, id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s AlertRule + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *AlertRuleService) UpdateAlertRule(team_id, service_id, integration_id, id string, rule *AlertRule) (*AlertRule, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/transformers/%s/", team_id, service_id, integration_id, id) + body, err := c.client.newRequestDo("PUT", path, rule) + if err != nil { + return nil, err + } + var s AlertRule + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *AlertRuleService) DeleteAlertRule(team_id, service_id, integration_id, id string) error { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/transformers/%s/", team_id, service_id, integration_id, id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} diff --git a/client.go b/client.go new file mode 100644 index 0000000..07017fe --- /dev/null +++ b/client.go @@ -0,0 +1,170 @@ +package zenduty + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/url" +) + +const ( + defaultBaseURL = "https://www.zenduty.com" +) + +type service struct { + client *Client +} + +type Config struct { + BaseURL string + HTTPClient *http.Client + Token string +} + +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 +} + +type Response struct { + Response *http.Response + BodyBytes []byte +} + +func NewClient(config *Config) (*Client, error) { + if config.HTTPClient == nil { + config.HTTPClient = http.DefaultClient + } + + if config.BaseURL == "" { + config.BaseURL = defaultBaseURL + } + + baseURL, err := url.Parse(config.BaseURL) + if err != nil { + return nil, err + } + + c := &Client{ + baseURL: baseURL, + client: config.HTTPClient, + Config: config, + } + c.Teams = &TeamService{c} + c.Services = &Service{c} + c.Schedules = &ScheduleService{c} + c.Roles = &RoleService{c} + c.Integrations = &IntegrationServerice{c} + c.Incidents = &IncidentService{c} + c.Esp = &EspService{c} + c.Members = &MemberService{c} + c.Invite = &InviteService{c} + c.Users = &UserService{c} + c.AlertRules = &AlertRuleService{c} + c.Priority = &PriorityService{c} + c.Tags = &TagsService{c} + + return c, nil + +} + +func (c *Client) newRequest(method, path string, body interface{}) (*http.Request, error) { + rel := &url.URL{Path: path} + u := c.baseURL.ResolveReference(rel) + + var buf []byte + if body != nil { + buf, _ = json.Marshal(body) + } + + req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(buf)) + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", "application/json") + req.Header.Set("Authorization", fmt.Sprintf("token %s", c.Config.Token)) + + return req, nil +} + +func (c *Client) newRequestDo(method, path string, body interface{}) (*Response, error) { + req, err := c.newRequest(method, path, body) + if err != nil { + return nil, err + } + return c.doRequest(req) +} + +func (c *Client) doRequest(req *http.Request) (*Response, error) { + res, err := c.client.Do(req) + if err != nil { + return nil, err + } + body, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + defer res.Body.Close() + response := &Response{ + Response: res, + BodyBytes: body, + } + if err := c.checkResponse(response); err != nil { + return response, err + } + + // if v != nil { + // if err := c.DecodeJSON(response, v); err != nil { + // return response, err + // } + // } + + return response, nil + +} + +func (c *Client) DecodeJSON(res *Response, v interface{}) error { + return json.Unmarshal(res.BodyBytes, v) +} + +func (c *Client) checkResponse(res *Response) error { + if res.Response.StatusCode >= 200 && res.Response.StatusCode <= 299 { + return nil + } + + return c.decodeErrorResponse(res) +} + +func (c *Client) decodeErrorResponse(res *Response) error { + + v := &errorResponse{Error: &Error{ErrorResponse: res}} + 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 v.Error +} + +func CheckError(err error) error { + if err != nil { + return err + } + return nil +} diff --git a/error.go b/error.go new file mode 100644 index 0000000..2e4f8ec --- /dev/null +++ b/error.go @@ -0,0 +1,27 @@ +package zenduty + +import ( + "errors" + "fmt" +) + +var ( + ErrNoToken = errors.New("an empty token was provided") + + ErrAuthFailure = errors.New("failed to authenticate using the provided token") +) + +type errorResponse struct { + Error *Error `json:"error"` +} + +type Error struct { + ErrorResponse *Response + Code int `json:"code,omitempty"` + Errors interface{} `json:"error,omitempty"` + Message string `json:"message,omitempty"` +} + +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) +} diff --git a/esp.go b/esp.go new file mode 100644 index 0000000..062f873 --- /dev/null +++ b/esp.go @@ -0,0 +1,95 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type EspService service +type Targets struct { + Target_type int `json:"target_type"` + Target_id string `json:"target_id"` +} +type Rules struct { + Delay int `json:"delay"` + Targets []Targets `json:"targets"` + Position int `json:"position"` + Unique_Id string `json:"unique_id"` +} + +type EscalationPolicy struct { + Name string `json:"name"` + Description string `json:"description"` + Summary string `json:"summary"` + Team string `json:"team"` + Unique_Id string `json:"unique_id"` + Repeat_Policy int `json:"repeat_policy"` + Move_To_Next bool `json:"move_to_next"` + Global_Ep bool `json:"global_ep"` + Rules []Rules `json:"rules"` +} + +func (c *EspService) CreateEscalationPolicy(team string, policy *EscalationPolicy) (*EscalationPolicy, error) { + path := fmt.Sprintf("/api/account/teams/%s/escalation_policies/", team) + body, err := c.client.newRequestDo("POST", path, policy) + if err != nil { + return nil, err + } + var s EscalationPolicy + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *EspService) GetEscalationPolicy(team string) ([]EscalationPolicy, error) { + path := fmt.Sprintf("/api/account/teams/%s/escalation_policies/", team) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []EscalationPolicy + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *EspService) GetEscalationPolicyById(team, id string) (*EscalationPolicy, error) { + path := fmt.Sprintf("/api/account/teams/%s/escalation_policies/%s/", team, id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s EscalationPolicy + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *EspService) DeleteEscalationPolicy(team, id string) error { + path := fmt.Sprintf("/api/account/teams/%s/escalation_policies/%s/", team, id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} + +func (c *EspService) UpdateEscalationPolicy(team, id string, policy *EscalationPolicy) (*EscalationPolicy, error) { + path := fmt.Sprintf("/api/account/teams/%s/escalation_policies/%s/", team, id) + body, err := c.client.newRequestDo("PUT", path, policy) + if err != nil { + return nil, err + } + var s EscalationPolicy + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4c22463 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/Zenduty/zenduty-go-sdk + +go 1.17 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/incidents.go b/incidents.go new file mode 100644 index 0000000..31cf8d2 --- /dev/null +++ b/incidents.go @@ -0,0 +1,134 @@ +package zenduty + +import ( + "encoding/json" +) + +type IncidentService service + +type Incident struct { + Service string `json:"service"` + EscalationPolicy string `json:"escalation_policy"` + User string `json:"user"` + Title string `json:"title"` + Summary string `json:"summary"` +} + +type service_object struct { + Name string `json:"name"` + Creation_Date string `json:"creation_date"` + Summary string `json:"summary"` + Description string `json:"description"` + Unique_Id string `json:"unique_id"` + Auto_Resolve_Timeouts int `json:"auto_resolve_timeout"` + Created_By string `json:"created_by"` + Team_Priority string `json:"team_priority"` + Task_Template string `json:"task_template"` + Acknowledgment_Timeout int `json:"acknowledge_timeout"` + Status int `json:"status"` + EscalationPolicy string `json:"escalation_policy"` + Team string `json:"team"` + Sla string `json:"sla"` + Collation_Time int `json:"collation_time"` + Collation int `json:"collation"` +} + +type escalation_policy_object struct { + Unique_Id string `json:"unique_id"` + Name string `json:"name"` +} + +type Incidents struct { + Summary string `json:"summary"` + Incident_Number int `json:"incident_number"` + Creation_Date string `json:"creation_date"` + Status int `json:"status"` + Unique_Id string `json:"unique_id"` + Service_Object service_object + Title string `json:"title"` + Incident_Key string `json:"incident_key"` + Service string `json:"service"` + Urgency int `json:"urgency"` + Merged_With string `json:"merged_with"` + Assigned_To string `json:"assigned_to"` + Escalation_Policy string `json:"escalation_policy"` + Escalation_Policy_Object escalation_policy_object `json:"escalation_policy_object"` + Assigned_to_name string `json:"assigned_to_name"` + Resolved_Date string `json:"resolved_date"` + Acknowledged_Date string `json:"acknowledged_date"` + Context_Window_start string `json:"context_window_start"` + Context_Window_end string `json:"context_window_end"` + Tags []string `json:"tags"` + Sla string `json:"sla"` + Team_Priority string `json:"team_priority"` + Team_Priority_Object string `json:"team_priority_object"` +} + +type IncidentPagination struct { + Results []Incidents `json:"results"` + Next string `json:"next"` + Previous string `json:"previous"` + Count int `json:"count"` +} + +type IncidentStatus struct { + Status int `json:"status"` +} + +func (c *IncidentService) CreateIncident(incident *Incident) (*Incidents, error) { + path := "/api/incidents/" + + body, err := c.client.newRequestDo("POST", path, incident) + if err != nil { + return nil, err + } + var i Incidents + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *IncidentService) UpdateIncident(id string, incident *IncidentStatus) (*Incidents, error) { + path := "/api/incidents/" + id + "/" + + body, err := c.client.newRequestDo("PATCH", path, incident) + if err != nil { + return nil, err + } + var i Incidents + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *IncidentService) GetIncidents() (*IncidentPagination, error) { + path := "/api/incidents/" + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var i IncidentPagination + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *IncidentService) GetIncidentByNumber(id string) (*Incidents, error) { + path := "/api/incidents/" + id + "/" + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var i Incidents + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} diff --git a/integrations.go b/integrations.go new file mode 100644 index 0000000..594209e --- /dev/null +++ b/integrations.go @@ -0,0 +1,97 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type IntegrationServerice service + +type ApplicationReference struct { + Name string `json:"name"` + Icon_Url string `json:"icon_url"` + Summary string `json:"summary"` + Description string `json:"description"` + Unique_Id string `json:"unique_id"` + Avalability_Plan_id int `json:"availability_plan_id"` + Setup_Instructions string `json:"setup_instructions"` + Extension string `json:"extension"` + Application_Type int `json:"application_type"` + Categories string `json:"categories"` + Documentation_Link string `json:"documentation_link"` +} + +type IntegrationCreate struct { + Name string `json:"name"` + Summary string `json:"summary"` + Application string `json:"application"` +} + +type Integration struct { + Name string `json:"name"` + Creation_Date string `json:"creation_date"` + Summary string `json:"summary"` + Description string `json:"description"` + Unique_Id string `json:"unique_id"` + Service string `json:"service"` + Application string `json:"application"` + Application_Reference ApplicationReference `json:"application_reference"` + Integration_key string `json:"integration_key"` + Created_By string `json:"created_by"` + Is_Enabled bool `json:"is_enabled"` + Create_Incident_For int `json:"create_incident_for"` + Integration_Type int `json:"integration_type"` + Default_Urgency int `json:"default_urggency"` +} + +func (c *IntegrationServerice) CreateIntegration(team string, service_id string, integration *IntegrationCreate) (*Integration, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/", team, service_id) + + body, err := c.client.newRequestDo("POST", path, integration) + if err != nil { + return nil, err + } + var i Integration + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *IntegrationServerice) GetIntegrations(team, service_id string) ([]Integration, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/", team, service_id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var i []Integration + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return i, nil +} + +func (c *IntegrationServerice) GetIntegrationByID(team, service_id, id string) (*Integration, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/", team, service_id, id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var i Integration + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *IntegrationServerice) DeleteIntegration(team, service_id, id string) error { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/", team, service_id, id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} diff --git a/invites.go b/invites.go new file mode 100644 index 0000000..f2460b4 --- /dev/null +++ b/invites.go @@ -0,0 +1,41 @@ +package zenduty + +import ( + "encoding/json" +) + +type InviteService service + +type EmailAccounts struct { + First_Name string `json:"first_name"` + Last_Name string `json:"last_name"` + Email string `json:"email"` + Role int `json:"role"` +} + +type Invite struct { + EmailAccounts []EmailAccounts `json:"email_accounts"` + Team string `json:"team"` +} + +type InviteResponse struct { + Unique_Id string `json:"unique_id"` + Team string `json:"team"` + User User `json:"user"` + Joining_Date string `json:"joining_date"` + Role int `json:"role"` +} + +func (c *InviteService) CreateInvite(invite *Invite) ([]InviteResponse, error) { + path := "/api/account/invite/" + body, err := c.client.newRequestDo("POST", path, invite) + if err != nil { + return nil, err + } + var s []InviteResponse + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} diff --git a/members.go b/members.go new file mode 100644 index 0000000..7f5223e --- /dev/null +++ b/members.go @@ -0,0 +1,90 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type MemberService service + +type Member struct { + Unique_Id string `json:"unique_id",omitempty` + Team string `json:"team",omitempty` + User string `json:"user",omitempty` + Role int `json:"role",omitempty` +} + +type MemberResponse struct { + Unique_Id string `json:"unique_id"` + Team string `json:"team"` + User User `json:"user"` + Joining_Date string `json:"joining_date"` + Role int `json:"role"` +} + +func (c *MemberService) CreateTeamMember(team string, member *Member) (*Member, error) { + path := fmt.Sprintf("/api/account/teams/%s/members/", team) + + body, err := c.client.newRequestDo("POST", path, member) + if err != nil { + return nil, err + } + var s Member + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *MemberService) UpdateTeamMember(member *Member) (*Member, error) { + path := fmt.Sprintf("/api/account/teams/%s/members/%s/", member.Team, member.Unique_Id) + body, err := c.client.newRequestDo("PATCH", path, member) + if err != nil { + return nil, err + } + var s Member + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *MemberService) DeleteTeamMember(team string, member string) error { + path := fmt.Sprintf("/api/account/teams/%s/members/%s/", team, member) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + + return nil +} + +func (c *MemberService) GetTeamMembers(team string) ([]MemberResponse, error) { + path := fmt.Sprintf("/api/account/teams/%s/members/", team) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []MemberResponse + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *MemberService) GetTeamMembersByID(team, id string) (*MemberResponse, error) { + path := fmt.Sprintf("/api/account/teams/%s/members/%s/", team, id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s MemberResponse + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} diff --git a/priority.go b/priority.go new file mode 100644 index 0000000..ba73fbd --- /dev/null +++ b/priority.go @@ -0,0 +1,82 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +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 string `json:"team,omitempty"` + Creation_Date string `json:"creation_date,omitempty"` +} + +func (c *PriorityService) CreatePriority(team string, priority *Priority) (*Priority, error) { + path := fmt.Sprintf("/api/account/teams/%s/priority/", team) + body, err := c.client.newRequestDo("POST", path, priority) + if err != nil { + return nil, err + } + var s Priority + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *PriorityService) GetPriority(team string) ([]Priority, 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 + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *PriorityService) GetPriorityById(team, id string) (*Priority, 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 + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *PriorityService) UpdatePriority(team, id string, priority *Priority) (*Priority, error) { + path := fmt.Sprintf("/api/account/teams/%s/priority/%s/", team, id) + body, err := c.client.newRequestDo("PUT", path, priority) + if err != nil { + return nil, err + } + var s Priority + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *PriorityService) DeletePriority(team, id string) error { + path := fmt.Sprintf("/api/account/teams/%s/priority/%s/", team, id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} diff --git a/roles.go b/roles.go new file mode 100644 index 0000000..9321ccc --- /dev/null +++ b/roles.go @@ -0,0 +1,70 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type RoleService service + +type Roles struct { + Team string `json:"team",omitempty` + Unique_Id string `json:"unique_id",omitempty` + Title string `json:"title"` + Description string `json:"description"` + Creation_Date string `json:"creation_date",omitempty` + Rank int `json:"rank",omitempty` +} + +func (c *RoleService) CreateRole(team string, role *Roles) (*Roles, error) { + path := fmt.Sprintf("/api/account/teams/%s/roles/", team) + + body, err := c.client.newRequestDo("POST", path, role) + if err != nil { + return nil, err + } + var r Roles + err = json.Unmarshal(body.BodyBytes, &r) + if err != nil { + return nil, err + } + return &r, nil +} + +func (c *RoleService) GetRoles(team string) ([]Roles, error) { + path := fmt.Sprintf("/api/account/teams/%s/roles/", team) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var r []Roles + err = json.Unmarshal(body.BodyBytes, &r) + if err != nil { + return nil, err + } + return r, nil +} + +func (c *RoleService) UpdateRoles(team string, role *Roles) (*Roles, error) { + path := fmt.Sprintf("/api/account/teams/%s/roles/%s/", team, role.Unique_Id) + + body, err := c.client.newRequestDo("PATCH", path, role) + if err != nil { + return nil, err + } + var r Roles + err = json.Unmarshal(body.BodyBytes, &r) + if err != nil { + return nil, err + } + return &r, nil +} + +func (c *RoleService) DeleteRole(team string, role string) error { + path := fmt.Sprintf("/api/account/teams/%s/roles/%s/", team, role) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} diff --git a/schedules.go b/schedules.go new file mode 100644 index 0000000..a3687ea --- /dev/null +++ b/schedules.go @@ -0,0 +1,140 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type ScheduleService service + +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` +} +type Users struct { + User string `json:"user"` + Position int `json:"position"` + Unique_Id string `json:"unique_id",omitempty` +} + +type Overrides struct { + Name string `json:"name"` + User string `json:"user"` + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` + Unique_Id string `json:"unique_id",omitempty` +} +type Layers struct { + ShiftLength int `json:"shift_length"` + Name string `json:"name"` + RotationStartTime string `json:"rotation_start_time"` + RotationEndTime string `json:"rotation_end_time"` + UniqueId string `json:"unique_id"` + LastEdited string `json:"last_edited"` + RestrictionType int `json:"restriction_type",omitempty` + IsActive bool `json:"is_active",omitempty` + Restrictions []Restrictions `json:"restrictions"` + Users []Users `json:"users"` +} + +type CreateUserLayer struct { + User string `json:"user"` +} +type CreateLayers struct { + ShiftLength int `json:"shift_length"` + Name string `json:"name"` + RotationStartTime string `json:"rotation_start_time"` + RotationEndTime string `json:"rotation_end_time"` + RestrictionType int `json:"restriction_type",omitempty` + Users []CreateUserLayer `json:"users"` +} + +type CreateSchedule struct { + Name string `json:"name"` + Description string `json:"description"` + Summary string `json:"summary"` + Time_zone string `json:"time_zone"` + Team string `json:"team"` + Layers []CreateLayers `json:"layers"` + Overrides []Overrides `json:"overrides"` + Unique_Id string `json:"unique_id",omitempty` +} +type Schedules struct { + Name string `json:"name"` + Description string `json:"description"` + Summary string `json:"summary"` + Time_zone string `json:"time_zone"` + Team string `json:"team"` + Unique_Id string `json:"unique_id",omitempty` + Layers []Layers `json:"layers"` + Overrides []Overrides `json:"overrides"` +} + +func (c *ScheduleService) CreateSchedule(team string, schedule *CreateSchedule) (*CreateSchedule, error) { + + path := fmt.Sprintf("/api/account/teams/%s/schedules/", team) + body, err := c.client.newRequestDo("POST", path, schedule) + if err != nil { + return nil, err + } + var s CreateSchedule + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *ScheduleService) GetSchedules(team string) ([]Schedules, error) { + path := fmt.Sprintf("/api/account/teams/%s/schedules/", team) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []Schedules + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *ScheduleService) GetScheduleByID(team, id string) (*Schedules, error) { + path := fmt.Sprintf("/api/account/teams/%s/schedules/%s/", team, id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s Schedules + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *ScheduleService) DeleteScheduleByID(team, id string) error { + path := fmt.Sprintf("/api/account/teams/%s/schedules/%s/", team, id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} + +func (c *ScheduleService) UpdateScheduleByID(team, id string, schedule *CreateSchedule) (*CreateSchedule, error) { + + path := fmt.Sprintf("/api/account/teams/%s/schedules/%s/", team, id) + body, err := c.client.newRequestDo("PATCH", path, schedule) + if err != nil { + return nil, err + } + var s CreateSchedule + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} diff --git a/services.go b/services.go new file mode 100644 index 0000000..2232521 --- /dev/null +++ b/services.go @@ -0,0 +1,97 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type Service service + +type Services struct { + Name string `json:"name"` + Creation_Date string `json:"creation_date"` + Summary string `json:"summary"` + Description string `json:"description"` + Unique_Id string `json:"unique_id",omitempty` + Auto_Resolve_Timeout int `json:"auto_resolve_timeout",omitempty` + Created_By string `json:"created_by",omitempty` + Team_Priority string `json:"team_priority"` + Task_Template string `json:"task_template"` + Acknowledgment_Timeout int `json:"acknowledge_timeout",omitempty` + Status int `json:"status",omitempty` + Escalation_Policy string `json:"escalation_policy"` + Team string `json:"team"` + Sla string `json:"sla"` + Collation_Time int `json:"collation_time"` + Collation int `json:"collation"` + Under_Maintenance bool `json:"under_maintenance",omitempty` +} + +func (c *Service) CreateService(team string, service *Services) (*Services, error) { + + path := fmt.Sprintf("/api/account/teams/%s/services/", team) + body, err := c.client.newRequestDo("POST", path, service) + if err != nil { + return nil, err + } + var i Services + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *Service) GetServices(team string) ([]Services, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/", team) + + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + + var i []Services + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return i, nil +} + +func (c *Service) GetServicesById(team, id string) (*Services, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/", team, id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var i Services + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *Service) UpdateService(team, id string, service *Services) (*Services, error) { + + path := fmt.Sprintf("/api/account/teams/%s/services/%s/", team, id) + body, err := c.client.newRequestDo("PATCH", path, service) + if err != nil { + return nil, err + } + var i Services + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + return &i, nil +} + +func (c *Service) DeleteService(team string, id string) error { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/", team, id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} diff --git a/services_test.go b/services_test.go new file mode 100644 index 0000000..10413fd --- /dev/null +++ b/services_test.go @@ -0,0 +1,25 @@ +package zenduty + +import "testing" + +func TestClient_DeleteService(t *testing.T) { + type args struct { + team string + id string + } + tests := []struct { + name string + c *Client + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.c.Services.DeleteService(tt.args.team, tt.args.id); (err != nil) != tt.wantErr { + t.Errorf("Client.DeleteService() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/tags.go b/tags.go new file mode 100644 index 0000000..d37e911 --- /dev/null +++ b/tags.go @@ -0,0 +1,67 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type TagsService service + +type Tag struct { + Unique_Id string `json:"unique_id,omitempty"` + Name string `json:"name"` + Color string `json:"color"` + Team string `json:"team,omitempty"` + Creation_Date string `json:"creation_date,omitempty"` +} + +func (c *TagsService) CreateTag(team string, tags *Tag) (*Tag, error) { + path := fmt.Sprintf("/api/account/teams/%s/tags/", team) + body, err := c.client.newRequestDo("POST", 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) GetPriority(team string) ([]Tag, 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 + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *TagsService) GetPriorityById(team, id string) (*Tag, 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 + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *TagsService) DeletePriority(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 { + return err + } + return nil +} diff --git a/team.go b/team.go new file mode 100644 index 0000000..5eacd44 --- /dev/null +++ b/team.go @@ -0,0 +1,104 @@ +package zenduty + +import ( + "encoding/json" + "fmt" +) + +type TeamService service + +type User struct { + Username string `json:"username"` + First_Name string `json:"first_name"` + Last_Name string `json:"last_name"` + Email string `json:"email"` +} + +type members struct { + Unique_Id string `json:"unique_id"` + Team string `json:"team"` + User User `json:"user"` + Joining_Date string `json:"joining_date"` + Role int `json:"role"` +} + +type CreateTeams struct { + Name string `json:"name"` + Unique_Id string `json:"unique_id",omitempty` +} +type Team struct { + Unique_Id string `json:"unique_id"` + Name string `json:"name"` + Account string `json:"account"` + Creation_Date string `json:"creation_date"` + Owner string `json:"owner"` + Roles []Roles `json:"roles"` + Members []members `json:"members"` +} + +func (c *TeamService) CreateTeam(team *CreateTeams) (*Team, error) { + path := "/api/account/teams/" + body, err := c.client.newRequestDo("POST", path, team) + if err != nil { + return nil, err + } + var t Team + err = json.Unmarshal(body.BodyBytes, &t) + if err != nil { + return nil, err + } + return &t, nil +} + +func (c *TeamService) UpdateTeam(id string, team *CreateTeams) (*Team, error) { + + path := fmt.Sprintf("/api/account/teams/%s/", id) + res, err := c.client.newRequestDo("PATCH", path, team) + if err != nil { + return nil, err + } + var t Team + err = json.Unmarshal(res.BodyBytes, &t) + if err != nil { + return nil, err + } + return &t, nil +} + +func (c *TeamService) GetTeamById(uniqie_id string) (*Team, error) { + path := fmt.Sprintf("/api/account/teams/%s/", uniqie_id) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var t Team + err = json.Unmarshal(body.BodyBytes, &t) + if err != nil { + return nil, err + } + return &t, nil +} + +func (c *TeamService) GetTeams() ([]Team, error) { + path := "/api/account/teams/" + + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var t []Team + err = json.Unmarshal(body.BodyBytes, &t) + if err != nil { + return nil, err + } + return t, nil +} + +func (c *TeamService) DeleteTeam(id string) error { + path := fmt.Sprintf("/api/account/teams/%s/", id) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} diff --git a/users.go b/users.go new file mode 100644 index 0000000..88fdec6 --- /dev/null +++ b/users.go @@ -0,0 +1,44 @@ +package zenduty + +import ( + "encoding/json" +) + +type UserService service + +type UserObj struct { + Email string `json:"email"` + Username string `json:"username"` + First_Name string `json:"first_name"` + Last_Name string `json:"last_name"` +} + +type GetUsers struct { + Unique_Id string `json:"unique_id"` + User UserObj `json:"user"` +} + +func (c *UserService) GetUsers(email string) ([]GetUsers, error) { + path := "/api/account/users" + + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + + var i []GetUsers + + err = json.Unmarshal(body.BodyBytes, &i) + if err != nil { + return nil, err + } + + var j []GetUsers + + for _, v := range i { + if v.User.Email == email { + j = append(j, v) + } + } + return j, nil +}