-
Notifications
You must be signed in to change notification settings - Fork 242
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
Add slack connection apis #432
base: main
Are you sure you want to change the base?
Changes from all commits
312c070
ca2c0c7
d66c620
13cbcf1
8ae4c32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/google/go-querystring/query" | ||
) | ||
|
||
// SlackConnectionConfig is the configuration of a Slack connection as per the documentation | ||
// https://developer.pagerduty.com/api-reference/c2NoOjExMjA5MzMy-slack-connection. | ||
type SlackConnectionConfig struct { | ||
Events []string `json:"events"` | ||
Urgency *string `json:"urgency"` | ||
Priorities []string `json:"priorities"` | ||
} | ||
|
||
// SlackConnection is an entity that represents a Slack connections as per the | ||
// documentation https://developer.pagerduty.com/api-reference/c2NoOjExMjA5MzMy-slack-connection. | ||
type SlackConnection struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type seems to be missing the |
||
SourceID string `json:"source_id"` | ||
SourceName string `json:"source_name"` | ||
SourceType string `json:"source_type"` | ||
|
||
ChannelID string `json:"channel_id"` | ||
ChannelName string `json:"channel_name"` | ||
NotificationType string `json:"notification_type"` | ||
|
||
Config SlackConnectionConfig `json:"config"` | ||
} | ||
|
||
// SlackConnectionObject is an API object returned by getter functions. | ||
type SlackConnectionObject struct { | ||
SlackConnection | ||
APIObject | ||
} | ||
Comment on lines
+34
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We intend to no longer use embedding in this project. Please list the fields on the type explicitly. See #318 for more details. That said, I think we should probably just elide this type entirely and put all of the fields in the |
||
|
||
// ListSlackConnectionsResponse is an API object returned by the list function. | ||
type ListSlackConnectionsResponse struct { | ||
Connections []SlackConnectionObject `json:"slack_connections"` | ||
APIListObject | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment around embedding as well. |
||
} | ||
|
||
// ListSlackConnectionsOptions is the data structure used when calling the ListSlackConnections API endpoint. | ||
type ListSlackConnectionsOptions struct { | ||
// Limit is the pagination parameter that limits the number of results per | ||
// page. PagerDuty defaults this value to 50 if omitted, and sets an upper | ||
// bound of 100. | ||
Limit uint `url:"limit,omitempty"` | ||
|
||
// Offset is the pagination parameter that specifies the offset at which to | ||
// start pagination results. When trying to request the next page of | ||
// results, the new Offset value should be currentOffset + Limit. | ||
Offset uint `url:"offset,omitempty"` | ||
} | ||
|
||
// CreateSlackConnectionWithContext creates a Slack connection. | ||
func (c *Client) CreateSlackConnectionWithContext(ctx context.Context, slackTeamID string, s SlackConnection) (SlackConnectionObject, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
d := map[string]SlackConnection{ | ||
"slack_connection": s, | ||
} | ||
|
||
resp, err := c.post(ctx, "/integration-slack/workspaces/"+slackTeamID+"/connections", d, nil) | ||
return getSlackConnectionFromResponse(c, resp, err) | ||
} | ||
|
||
// GetSlackConnection gets a Slack connection. | ||
func (c *Client) GetSlackConnectionWithContext(ctx context.Context, slackTeamID, connectionID string) (SlackConnectionObject, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
resp, err := c.get(ctx, "/integration-slack/workspaces/"+slackTeamID+"/connections/"+connectionID) | ||
return getSlackConnectionFromResponse(c, resp, err) | ||
} | ||
|
||
// DeleteSlackConnectionWithContext deletes a Slack connection. | ||
func (c *Client) DeleteSlackConnectionWithContext(ctx context.Context, slackTeamID, connectionID string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
_, err := c.delete(ctx, "/integration-slack/workspaces/"+slackTeamID+"/connections/"+connectionID) | ||
return err | ||
} | ||
|
||
// UpdateSlackConnectionWithContext updates an existing Slack connection. | ||
func (c *Client) UpdateSlackConnectionWithContext(ctx context.Context, slackTeamID, connectionID string, s SlackConnection) (SlackConnectionObject, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
d := map[string]SlackConnection{ | ||
"slack_connection": s, | ||
} | ||
|
||
resp, err := c.put(ctx, "/integration-slack/workspaces/"+slackTeamID+"/connections/"+connectionID, d, nil) | ||
return getSlackConnectionFromResponse(c, resp, err) | ||
} | ||
|
||
// ListSlackConnectionsWithContext lists Slack connections. | ||
func (c *Client) ListSlackConnectionsWithContext(ctx context.Context, slackTeamID string, o ListSlackConnectionsOptions) (*ListSlackConnectionsResponse, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
v, err := query.Values(o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.get(ctx, "/integration-slack/workspaces/"+slackTeamID+"/connections?"+v.Encode()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result ListSlackConnectionsResponse | ||
if err = c.decodeJSON(resp, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func getSlackConnectionFromResponse(c *Client, resp *http.Response, err error) (SlackConnectionObject, error) { | ||
if err != nil { | ||
return SlackConnectionObject{}, err | ||
} | ||
|
||
var target map[string]SlackConnectionObject | ||
if dErr := c.decodeJSON(resp, &target); dErr != nil { | ||
return SlackConnectionObject{}, fmt.Errorf("Could not decode JSON response: %v", dErr) | ||
} | ||
|
||
const rootNode = "slack_connection" | ||
|
||
t, nodeOK := target[rootNode] | ||
if !nodeOK { | ||
return SlackConnectionObject{}, fmt.Errorf("JSON response does not have %s field", rootNode) | ||
} | ||
|
||
return t, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please raise this chunk as an isolated PR?