-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for license and licenses allocated based operations
- Loading branch information
1 parent
2d652e0
commit 49066f3
Showing
2 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/go-querystring/query" | ||
) | ||
|
||
type License struct { | ||
APIObject | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
ValidRoles []string `json:"valid_roles"` | ||
RoleGroup string `json:"role_group"` | ||
Summary string `json:"summary"` | ||
CurrentValue int `json:"current_value"` | ||
AllocationsAvailable int `json:"allocations_available"` | ||
} | ||
|
||
type LicenseAllocated struct { | ||
APIObject | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
ValidRoles []string `json:"valid_roles"` | ||
RoleGroup string `json:"role_group"` | ||
Summary string `json:"summary"` | ||
} | ||
|
||
type LicenseAllocation struct { | ||
AllocatedAt string `json:"allocated_at"` | ||
User APIObject | ||
License LicenseAllocated `json:"license"` | ||
} | ||
|
||
type ListLicensesResponse struct { | ||
Licenses []License `json:"licenses"` | ||
} | ||
|
||
type ListLicenseAllocationsResponse struct { | ||
APIListObject | ||
LicenseAllocations []LicenseAllocation `json:"license_allocations"` | ||
} | ||
|
||
type ListLicenseAllocationsOptions struct { | ||
Limit int `url:"limit,omitempty"` | ||
Offset int `url:"offset,omitempty"` | ||
} | ||
|
||
func (c *Client) ListLicenses() (*ListLicensesResponse, error) { | ||
return c.ListLicensesWithContext() | ||
} | ||
|
||
func (c *Client) ListLicenseAllocations(o ListLicenseAllocationsOptions) (*ListLicenseAllocationsResponse, error) { | ||
return c.ListLicenseAllocationsWithContext(o) | ||
} | ||
|
||
func (c *Client) ListLicensesWithContext() (*ListLicensesResponse, error) { | ||
|
||
resp, err := c.get(context.Background(), "/licenses") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result ListLicensesResponse | ||
err = c.decodeJSON(resp, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} | ||
|
||
func (c *Client) ListLicenseAllocationsWithContext(o ListLicenseAllocationsOptions) (*ListLicenseAllocationsResponse, error) { | ||
|
||
v, err := query.Values(o) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.get(context.Background(), "/license_allocations?"+v.Encode()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result ListLicenseAllocationsResponse | ||
err = c.decodeJSON(resp, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package pagerduty | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestLicenses_List(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/licenses", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
_, _ = w.Write([]byte(`{ "licenses": [ { "id": "PIP248G", "name": "Business (Full User)", "description": "Event Intelligence", "current_value": 234, "allocations_available": 4766, "valid_roles": [ "owner", "admin", "user", "limited_user", "observer", "restricted_access" ], "role_group": "FullUser", "summary": "Business (Full User)", "type": "license", "self": null, "html_url": null } ] }`)) | ||
}) | ||
|
||
client := defaultTestClient(server.URL, "foo") | ||
|
||
res, err := client.ListLicenses() | ||
|
||
want := &ListLicensesResponse{ | ||
Licenses: []License{ | ||
{ | ||
|
||
APIObject: APIObject{ | ||
ID: "PIP248G", | ||
Type: "license", | ||
}, | ||
Name: "Business (Full User)", | ||
Description: "Event Intelligence", | ||
CurrentValue: 234, | ||
AllocationsAvailable: 4766, | ||
ValidRoles: []string{ | ||
"owner", | ||
"admin", | ||
"user", | ||
"limited_user", | ||
"observer", | ||
"restricted_access", | ||
}, | ||
RoleGroup: "FullUser", | ||
Summary: "Business (Full User)", | ||
}, | ||
}, | ||
} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
testEqual(t, want, res) | ||
} | ||
|
||
func TestLicenseAllocations_List(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/license_allocations", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, "GET") | ||
_, _ = w.Write([]byte(`{ | ||
"license_allocations": [ | ||
{ | ||
"license": { | ||
"id": "PIP248G", | ||
"name": "Business (Full User)", | ||
"description": "Event Intelligence", | ||
"valid_roles": [ | ||
"owner", | ||
"admin", | ||
"user", | ||
"limited_user", | ||
"observer", | ||
"restricted_access" | ||
], | ||
"role_group": "FullUser", | ||
"summary": "Business (Full User)", | ||
"type": "license", | ||
"self": null, | ||
"html_url": null | ||
}, | ||
"user": { | ||
"id": "PIP248H", | ||
"type": "user_reference" | ||
}, | ||
"allocated_at": "2021-06-01T00:00:00-05:00" | ||
} | ||
], | ||
"offset": 0, | ||
"limit": 1, | ||
"more": false, | ||
"total": 2 | ||
}`)) | ||
}) | ||
|
||
client := defaultTestClient(server.URL, "foo") | ||
|
||
res, err := client.ListLicenseAllocations() | ||
|
||
want := &ListLicenseAllocationsResponse{ | ||
APIListObject: APIListObject{ | ||
Offset: 0, | ||
Limit: 1, | ||
More: false, | ||
Total: 2, | ||
}, | ||
LicenseAllocations: []LicenseAllocation{ | ||
{ | ||
License: LicenseAllocated{ | ||
APIObject: APIObject{ | ||
ID: "PIP248G", | ||
Type: "license", | ||
}, | ||
Name: "Business (Full User)", | ||
Description: "Event Intelligence", | ||
ValidRoles: []string{ | ||
"owner", | ||
"admin", | ||
"user", | ||
"limited_user", | ||
"observer", | ||
"restricted_access", | ||
}, | ||
RoleGroup: "FullUser", | ||
Summary: "Business (Full User)", | ||
}, | ||
AllocatedAt: "2021-06-01T00:00:00-05:00", | ||
User: APIObject{ | ||
ID: "PIP248H", | ||
Type: "user_reference", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
testEqual(t, want, res) | ||
} |