Skip to content

Commit

Permalink
Merge pull request russellcardullo#25 from partamonov/master
Browse files Browse the repository at this point in the history
Team Service and Readme update
  • Loading branch information
russellcardullo authored May 10, 2018
2 parents 1aec58c + 671ae99 commit 3448ee0
Show file tree
Hide file tree
Showing 7 changed files with 531 additions and 2 deletions.
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ values for the underlying type.

More information on Maintenances from Pingdom: https://www.pingdom.com/resources/api/2.1#ResourceMaintenance

Get a list of all maintenancess:
Get a list of all maintenances:

```go
maintenances, err := client.Maintenances.List()
Expand Down Expand Up @@ -135,7 +135,7 @@ updatedMaintenance := pingdom.MaintenanceWindow{
msg, err := client.Maintenances.Update(12345, &updatedMaintenance)
```

Delete a check:
Delete a maintenance:

Note: that only future maintenance window can be deleted. This means that both `To` and `From` should be in future.

Expand Down Expand Up @@ -179,3 +179,51 @@ for _, probe := range probes {
fmt.Println("Probe region:", probe.Region) // Probe region: EU
}
```

### TeamService ###

This service manages pingdom Teams which are represented by the `Team` struct.
When creating or updating Teams you must specify at a minimum the `Name`.
Other fields are optional but if not set will be given the zero
values for the underlying type.

More information on Teams from Pingdom: https://www.pingdom.com/resources/api/2.1#ResourceTeam

Get a list of all teams:

```go
teams, err := client.Teams.List()
fmt.Println("Teams:", teams) // [{ID Name Users} ...]
```

Create a new Team:

```go
t := pingdom.TeamData{
Name: "Team",
}
team, err := client.Teams.Create(&t)
fmt.Println("Created Team:", team) // {ID Name Users}
```

Get details for a specific team:

```go
team, err := client.Teams.Read(12345)
```

Update a team:

```go
modifyTeam := pingdom.TeamData{
Name: "New Name"
UserIDs: "123,678",
}
team, err := client.Teams.Update(12345, &modifyTeam)
```

Delete a team:

```go
team, err := client.Teams.Delete(12345)
```
27 changes: 27 additions & 0 deletions pingdom/api_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ type ProbeResponse struct {
Region string `json:"region"`
}

// TeamResponse represents the json response for teams from the PIngdom API
type TeamResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Users []TeamUserResponse
}

// TeamUserResponse represents the json response for users in teams from the PIngdom API
type TeamUserResponse struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}

// TeamDeleteResponse represents the json response for delete team from the PIngdom API
type TeamDeleteResponse struct {
Success bool `json:"success"`
}

func (c *CheckResponseType) UnmarshalJSON(b []byte) error {
var raw interface{}

Expand Down Expand Up @@ -147,6 +166,10 @@ type listProbesJsonResponse struct {
Probes []ProbeResponse `json:"probes"`
}

type listTeamsJsonResponse struct {
Teams []TeamResponse `json:"teams"`
}

type checkDetailsJsonResponse struct {
Check *CheckResponse `json:"check"`
}
Expand All @@ -155,6 +178,10 @@ type maintenanceDetailsJsonResponse struct {
Maintenance *MaintenanceResponse `json:"maintenance"`
}

type teamDetailsJsonResponse struct {
Team *TeamResponse `json:"team"`
}

type errorJsonResponse struct {
Error *PingdomError `json:"error"`
}
2 changes: 2 additions & 0 deletions pingdom/pingdom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Client struct {
Checks *CheckService
Maintenances *MaintenanceService
Probes *ProbeService
Teams *TeamService
}

// NewClient returns a Pingdom client with a default base URL and HTTP client
Expand All @@ -35,6 +36,7 @@ func NewClient(user string, password string, key string) *Client {
c.Checks = &CheckService{client: c}
c.Maintenances = &MaintenanceService{client: c}
c.Probes = &ProbeService{client: c}
c.Teams = &TeamService{client: c}
return c
}

Expand Down
109 changes: 109 additions & 0 deletions pingdom/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package pingdom

import (
"encoding/json"
"io/ioutil"
"strconv"
)

// TeamService provides an interface to Pingdom teams
type TeamService struct {
client *Client
}

type Team interface {
PutParams() map[string]string
PostParams() map[string]string
Valid() error
}

// List return a list of teams from Pingdom.
func (cs *TeamService) List() ([]TeamResponse, error) {
req, err := cs.client.NewRequest("GET", "/teams", nil)
if err != nil {
return nil, err
}

resp, err := cs.client.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if err := validateResponse(resp); err != nil {
return nil, err
}

bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)

t := &listTeamsJsonResponse{}
err = json.Unmarshal([]byte(bodyString), &t)

return t.Teams, err
}

// Read return a team object from Pingdom
func (cs *TeamService) Read(id int) (*TeamResponse, error) {
req, err := cs.client.NewRequest("GET", "/teams/"+strconv.Itoa(id), nil)
if err != nil {
return nil, err
}

t := &teamDetailsJsonResponse{}
_, err = cs.client.Do(req, t)
if err != nil {
return nil, err
}

return t.Team, err
}

// Create is used to create a new team
func (cs *TeamService) Create(team Team) (*TeamResponse, error) {
if err := team.Valid(); err != nil {
return nil, err
}

req, err := cs.client.NewRequest("POST", "/teams", team.PostParams())
if err != nil {
return nil, err
}

t := &TeamResponse{}
_, err = cs.client.Do(req, t)
if err != nil {
return nil, err
}
return t, err
}

// Update is used to update existing team.
func (cs *TeamService) Update(id int, team Team) (*TeamResponse, error) {
req, err := cs.client.NewRequest("PUT", "/teams/"+strconv.Itoa(id), team.PutParams())
if err != nil {
return nil, err
}

t := &TeamResponse{}
_, err = cs.client.Do(req, t)
if err != nil {
return nil, err
}
return t, err
}

// Delete will delete the Team for the given ID.
func (cs *TeamService) Delete(id int) (*TeamDeleteResponse, error) {
req, err := cs.client.NewRequest("DELETE", "/teams/"+strconv.Itoa(id), nil)
if err != nil {
return nil, err
}

t := &TeamDeleteResponse{}
_, err = cs.client.Do(req, t)
if err != nil {
return nil, err
}
return t, err
}
Loading

0 comments on commit 3448ee0

Please sign in to comment.