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

Add support for extensions/extension schemas #193

Merged
merged 2 commits into from
Feb 5, 2020
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
85 changes: 85 additions & 0 deletions extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package pagerduty

import (
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)

type Extension struct {
APIObject
Name string `json:"name"`
EndpointURL string `json:"endpoint_url"`
ExtensionObjects []APIObject `json:"extension_objects"`
ExtensionSchema APIObject `json:"extension_schema"`
Config interface{} `json:"config"`
}

type ListExtensionResponse struct {
APIListObject
Extensions []Extension `json:"extensions"`
}

type ListExtensionOptions struct {
APIListObject
ExtensionObjectID string `url:"extension_object_id,omitempty"`
ExtensionSchemaID string `url:"extension_schema_id,omitempty"`
Query string `url:"query,omitempty"`
}

func (c *Client) ListExtensions(o ListExtensionOptions) (*ListExtensionResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}

resp, err := c.get("/extensions?" + v.Encode())
if err != nil {
return nil, err
}

var result ListExtensionResponse

return &result, c.decodeJSON(resp, &result)
}

func (c *Client) CreateExtension(e *Extension) (*Extension, error) {
resp, err := c.post("/extensions", e, nil)
return getExtensionFromResponse(c, resp, err)
}

func (c *Client) DeleteExtension(id string) error {
_, err := c.delete("/extensions/" + id)
return err
}

func (c *Client) GetExtension(id string) (*Extension, error) {
resp, err := c.get("/extensions/" + id)
return getExtensionFromResponse(c, resp, err)
}

func (c *Client) UpdateExtension(id string, e *Extension) (*Extension, error) {
resp, err := c.put("/extensions/"+id, e, nil)
return getExtensionFromResponse(c, resp, err)
}

func getExtensionFromResponse(c *Client, resp *http.Response, err error) (*Extension, error) {
if err != nil {
return nil, err
}

var target map[string]Extension
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}

rootNode := "extension"

t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, nil
}
70 changes: 70 additions & 0 deletions extension_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package pagerduty

import (
"fmt"
"net/http"

"github.com/google/go-querystring/query"
)

type ExtensionSchema struct {
APIObject
IconURL string `json:"icon_url"`
LogoURL string `json:"logo_url"`
Label string `json:"label"`
Key string `json:"key"`
Description string `json:"description"`
GuideURL string `json:"guide_url"`
SendTypes []string `json:"send_types"`
URL string `json:"url"`
}

type ListExtensionSchemaResponse struct {
APIListObject
ExtensionSchemas []ExtensionSchema `json:"extension_schemas"`
}

type ListExtensionSchemaOptions struct {
APIListObject
Query string `url:"query,omitempty"`
}

func (c *Client) ListExtensionSchemas(o ListExtensionSchemaOptions) (*ListExtensionSchemaResponse, error) {
v, err := query.Values(o)
if err != nil {
return nil, err
}

resp, err := c.get("/extension_schemas?" + v.Encode())
if err != nil {
return nil, err
}

var result ListExtensionSchemaResponse

return &result, c.decodeJSON(resp, &result)
}

func (c *Client) GetExtensionSchema(id string) (*ExtensionSchema, error) {
resp, err := c.get("/extension_schemas/" + id)
return getExtensionSchemaFromResponse(c, resp, err)
}

func getExtensionSchemaFromResponse(c *Client, resp *http.Response, err error) (*ExtensionSchema, error) {
if err != nil {
return nil, err
}

var target map[string]ExtensionSchema
if dErr := c.decodeJSON(resp, &target); dErr != nil {
return nil, fmt.Errorf("Could not decode JSON response: %v", dErr)
}

rootNode := "extension_schema"
t, nodeOK := target[rootNode]
if !nodeOK {
return nil, fmt.Errorf("JSON response does not have %s field", rootNode)
}

return &t, nil
}
78 changes: 78 additions & 0 deletions extension_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package pagerduty

import (
"net/http"
"testing"
)

func TestExtensionSchema_List(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/extension_schemas", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"extension_schemas":[{"id":"1","summary":"foo","send_types":["trigger", "acknowledge", "resolve"]}]}`))

})

var listObj = APIListObject{Limit: 0, Offset: 0, More: false, Total: 0}
var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}
var opts = ListExtensionSchemaOptions{
APIListObject: listObj,
Query: "foo",
}

res, err := client.ListExtensionSchemas(opts)

want := &ListExtensionSchemaResponse{
APIListObject: listObj,
ExtensionSchemas: []ExtensionSchema{
{
APIObject: APIObject{
ID: "1",
Summary: "foo",
},
SendTypes: []string{
"trigger",
"acknowledge",
"resolve",
},
},
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}

func TestExtensionSchema_Get(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/extension_schemas/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"extension_schema": {"name": "foo", "id": "1", "send_types": ["trigger", "acknowledge", "resolve"]}}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

res, err := client.GetExtensionSchema("1")

want := &ExtensionSchema{
APIObject: APIObject{
ID: "1",
},
SendTypes: []string{
"trigger",
"acknowledge",
"resolve",
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}
150 changes: 150 additions & 0 deletions extension_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package pagerduty

import (
"net/http"
"testing"
)

func TestExtension_List(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"extensions":[{"id":"1","summary":"foo","config": {"restrict": "any"}, "extension_objects":[{"id":"foo","summary":"foo"}]}]}`))

})

var listObj = APIListObject{Limit: 0, Offset: 0, More: false, Total: 0}
var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}
var opts = ListExtensionOptions{
APIListObject: listObj,
Query: "foo",
}

res, err := client.ListExtensions(opts)

want := &ListExtensionResponse{
APIListObject: listObj,
Extensions: []Extension{
{
APIObject: APIObject{
ID: "1",
Summary: "foo",
},
Config: map[string]interface{}{
"restrict": "any",
},
ExtensionObjects: []APIObject{
{
ID: "foo",
Summary: "foo",
},
},
},
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}

func TestExtension_Create(t *testing.T) {
setup()
defer teardown()

input := &Extension{Name: "foo"}

mux.HandleFunc("/extensions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.Write([]byte(`{"extension": {"name": "foo", "id": "1"}}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

res, err := client.CreateExtension(input)

want := &Extension{
Name: "foo",
APIObject: APIObject{
ID: "1",
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}

func TestExtension_Delete(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/extensions/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

if err := client.DeleteExtension("1"); err != nil {
t.Fatal(err)
}
}

func TestExtension_Get(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/extensions/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"extension": {"name": "foo", "id": "1"}}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

res, err := client.GetExtension("1")

want := &Extension{
Name: "foo",
APIObject: APIObject{
ID: "1",
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}

func TestExtension_Update(t *testing.T) {
setup()
defer teardown()

input := &Extension{Name: "foo"}

mux.HandleFunc("/extensions/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.Write([]byte(`{"extension": {"name": "foo", "id": "1"}}`))
})

var client = &Client{apiEndpoint: server.URL, authToken: "foo", HTTPClient: defaultHTTPClient}

res, err := client.UpdateExtension("1", input)

want := &Extension{
Name: "foo",
APIObject: APIObject{
ID: "1",
},
}

if err != nil {
t.Fatal(err)
}
testEqual(t, want, res)
}