diff --git a/README.md b/README.md index 8b0f894..25b1171 100644 --- a/README.md +++ b/README.md @@ -11,14 +11,14 @@ go get github.com/Zenduty/zenduty-go-sdk Before you begin making use of the SDK, make sure you have your Zenduty Access Token. ``` -import "github.com/Zenduty/zenduty-go-sdk" +import "github.com/Zenduty/zenduty-go-sdk/client" ``` 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 @@ -32,21 +32,21 @@ package main import ( "fmt" - "github.com/Zenduty/zenduty-go-sdk" + "github.com/Zenduty/zenduty-go-sdk/client" ) func main() { config := &client.Config{ - Token: "", // enter token for authentication + Token: "", } c, err := client.NewClient(config) if err != nil { panic(err) } - newteam := &client.Team{} - newteam.Name = "test" + newteam := &client.CreateTeams{} + newteam.Name = "demoteam" resp, err := c.Teams.CreateTeam(newteam) if err != nil { diff --git a/client/client.go b/client/client.go index cc0ce81..f68c6d9 100644 --- a/client/client.go +++ b/client/client.go @@ -41,6 +41,8 @@ type Client struct { Priority *PriorityService Tags *TagsService MaintenanceWindow *MaintenanceWindowService + NotificationRules *NotificationRulesService + ContactMethod *ContactMethodService } type Response struct { @@ -81,6 +83,8 @@ func NewClient(config *Config) (*Client, error) { c.Priority = &PriorityService{c} c.Tags = &TagsService{c} c.MaintenanceWindow = &MaintenanceWindowService{c} + c.NotificationRules = &NotificationRulesService{c} + c.ContactMethod = &ContactMethodService{c} return c, nil @@ -159,7 +163,7 @@ func (c *Client) decodeErrorResponse(res *Response) error { v := &errorResponse{Error: &Error{ErrorResponse: res, Code: res.Response.StatusCode}} if err := c.DecodeJSON(res, v); err != nil { - return fmt.Errorf("%s APIs 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 error: %s", res.Response.Request.Method, res.Response.Request.URL.String(), res.Response.Status, string(res.BodyBytes)) } return v.Error diff --git a/client/contact_method.go b/client/contact_method.go new file mode 100644 index 0000000..30cae5e --- /dev/null +++ b/client/contact_method.go @@ -0,0 +1,44 @@ +package client + +import ( + "encoding/json" + "fmt" +) + +type ContactMethodService service + +type ContactMethod struct { + UniqueID string `json:"unique_id"` + Name string `json:"name"` + ContactType int `json:"contact_type"` + Value string `json:"value"` + CreationDate string `json:"creation_date"` +} + +func (c *ContactMethodService) GetContactMethods(username string) ([]ContactMethod, error) { + path := fmt.Sprintf("/api/account/users/%s/contacts/", username) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []ContactMethod + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *ContactMethodService) GetContactMethodByID(username string, contactMethodID string) (*ContactMethod, error) { + path := fmt.Sprintf("/api/account/users/%s/contacts/%s/", username, contactMethodID) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s1 ContactMethod + err = json.Unmarshal(body.BodyBytes, &s1) + if err != nil { + return nil, err + } + return &s1, nil +} diff --git a/client/error.go b/client/error.go index d7013d7..1172fd0 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. 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) + 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, string(e.ErrorResponse.BodyBytes), e.Message) } diff --git a/client/esp.go b/client/esp.go index c56df70..c20e282 100644 --- a/client/esp.go +++ b/client/esp.go @@ -19,7 +19,7 @@ type Rules struct { type EscalationPolicy struct { Name string `json:"name"` - Description string `json:"description"` + Description string `json:"description,omitempty"` Summary string `json:"summary"` Team string `json:"team"` Unique_Id string `json:"unique_id"` diff --git a/client/integrations.go b/client/integrations.go index 089f5a5..a9620c1 100644 --- a/client/integrations.go +++ b/client/integrations.go @@ -22,9 +22,12 @@ type ApplicationReference struct { } type IntegrationCreate struct { - Name string `json:"name"` - Summary string `json:"summary"` - Application string `json:"application"` + Name string `json:"name"` + Summary string `json:"summary"` + Application string `json:"application"` + Is_Enabled bool `json:"is_enabled"` + Create_Incident_For int `json:"create_incidents_for"` + Default_Urgency int `json:"default_urgency"` } type Integration struct { @@ -40,9 +43,9 @@ type Integration struct { Webhook_url string `json:"webhook_url"` Created_By string `json:"created_by"` Is_Enabled bool `json:"is_enabled"` - Create_Incident_For int `json:"create_incident_for"` + Create_Incident_For int `json:"create_incidents_for"` Integration_Type int `json:"integration_type"` - Default_Urgency int `json:"default_urggency"` + Default_Urgency int `json:"default_urgency"` } func (c *IntegrationServerice) CreateIntegration(team string, service_id string, integration *IntegrationCreate) (*Integration, error) { @@ -60,6 +63,21 @@ func (c *IntegrationServerice) CreateIntegration(team string, service_id string, return &i, nil } +func (c *IntegrationServerice) UpdateIntegration(team string, service_id string, integration_id string, integration *IntegrationCreate) (*Integration, error) { + path := fmt.Sprintf("/api/account/teams/%s/services/%s/integrations/%s/", team, service_id, integration_id) + + body, err := c.client.newRequestDo("PATCH", 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) diff --git a/client/notification_rules.go b/client/notification_rules.go new file mode 100644 index 0000000..acf1f6a --- /dev/null +++ b/client/notification_rules.go @@ -0,0 +1,88 @@ +package client + +import ( + "encoding/json" + "fmt" +) + +type NotificationRulesService service + +type NotificationRules struct { + UniqueID string `json:"unique_id"` + StartDelay int `json:"start_delay"` + Type string `json:"type"` + Contact string `json:"contact"` + Urgency int `json:"urgency"` +} + +type CreateNotificationRules struct { + Contact string `json:"contact"` + StartDelay int `json:"start_delay"` + Urgency int `json:"urgency"` +} + +func (c *NotificationRulesService) CreateNotificationRules(username string, notificationRule *CreateNotificationRules) (*NotificationRules, error) { + path := fmt.Sprintf("/api/account/users/%s/notification_rules/", username) + body, err := c.client.newRequestDo("POST", path, notificationRule) + if err != nil { + return nil, err + } + var s NotificationRules + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *NotificationRulesService) GetNotificationRules(username string) ([]NotificationRules, error) { + path := fmt.Sprintf("/api/account/users/%s/notification_rules/", username) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s []NotificationRules + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return s, nil +} + +func (c *NotificationRulesService) GetNotificationRulesByID(username, notificationRuleID string) (*NotificationRules, error) { + path := fmt.Sprintf("/api/account/users/%s/notification_rules/%s/", username, notificationRuleID) + body, err := c.client.newRequestDo("GET", path, nil) + if err != nil { + return nil, err + } + var s NotificationRules + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil +} + +func (c *NotificationRulesService) DeleteNotificationRules(username string, notificationRuleID string) error { + path := fmt.Sprintf("/api/account/users/%s/notification_rules/%s/", username, notificationRuleID) + _, err := c.client.newRequestDo("DELETE", path, nil) + if err != nil { + return err + } + return nil +} + +func (c *NotificationRulesService) UpdateNotificationRules(username string, notificationRuleID string, notificationRule *NotificationRules) (*NotificationRules, error) { + path := fmt.Sprintf("/api/account/users/%s/notification_rules/%s/", username, notificationRuleID) + body, err := c.client.newRequestDo("PUT", path, notificationRule) + if err != nil { + return nil, err + } + var s NotificationRules + err = json.Unmarshal(body.BodyBytes, &s) + if err != nil { + return nil, err + } + return &s, nil + +} diff --git a/client/roles.go b/client/roles.go index 29654a2..fc0a81f 100644 --- a/client/roles.go +++ b/client/roles.go @@ -45,6 +45,21 @@ func (c *RoleService) GetRoles(team string) ([]Roles, error) { return r, nil } +func (c *RoleService) GetRolesById(team, id string) (*Roles, error) { + path := fmt.Sprintf("/api/account/teams/%s/roles/%s/", team, id) + 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) diff --git a/client/services.go b/client/services.go index 7f33e36..3427c46 100644 --- a/client/services.go +++ b/client/services.go @@ -12,19 +12,19 @@ type Services struct { 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` + 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` + 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` + Under_Maintenance bool `json:"under_maintenance,omitempty"` } func (c *Service) CreateService(team string, service *Services) (*Services, error) {