Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
heimweh committed Jun 26, 2017
0 parents commit a458ce4
Show file tree
Hide file tree
Showing 34 changed files with 3,293 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
*.dll
*.exe
.DS_Store
example.tf
terraform.tfplan
terraform.tfstate
bin/
modules-dev/
/pkg/
website/.vagrant
website/.bundle
website/build
website/node_modules
.vagrant/
*.backup
./*.tfstate
.terraform/
*.log
*.bak
*~
.*.swp
.idea
*.iml
*.test
*.iml
coverage.out
373 changes: 373 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
SHELL=bash
OK_MSG = \x1b[32m ✔\x1b[0m
GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor)
GOLIST?=$$(go list ./... | grep -v vendor)

default: test

integration:
go test -v ./tests/integration

# tools fetches necessary dev requirements
tools:
go get -u github.com/robertkrimen/godocdown/godocdown
go get -u github.com/kardianos/govendor
go get -u honnef.co/go/tools/cmd/gosimple
go get -u honnef.co/go/tools/cmd/unused
go get -u honnef.co/go/tools/cmd/staticcheck
go get -u github.com/client9/misspell/cmd/misspell
go get -u github.com/golang/lint/golint

coverprofile:
@go test ./pagerduty/... -coverprofile coverage.out && go tool cover -html=coverage.out

lint:
@echo -n "==> Checking that code complies with golint requirements..."
@ret=0 && for pkg in $(GOLIST); do \
test -z "$$(golint $$pkg | tee /dev/stderr)" || ret=1; \
done ; exit $$ret
@echo -e "$(OK_MSG)"

# check combines all checks into a single command
check: fmtcheck vet misspell staticcheck simple unused lint checkdoc

# fmt formats Go code.
fmt:
gofmt -w $(GOFMT_FILES)

test: check
@echo "==> Checking that code complies with unit tests..."
@go test $(GOLIST) -cover

webdoc:
@echo "==> Starting webserver at http://localhost:6060"
@sleep 1 && open http://localhost:6060 &
@godoc -http=:6060

unused:
@echo -n "==> Checking that code complies with unused requirements..."
@unused $(GOLIST)
@echo -e "$(OK_MSG)"

fmtcheck:
@echo -n "==> Checking that code complies with gofmt requirements..."
@gofmt_files=$$(gofmt -l $(GOFMT_FILES)) ; if [[ -n "$$gofmt_files" ]]; then \
echo 'gofmt needs running on the following files:'; \
echo "$$gofmt_files"; \
echo "You can use the command: \`make fmt\` to reformat code."; \
exit 1; \
fi
@echo -e "$(OK_MSG)"

misspell:
@echo -n "==> Checking for misspelling errors..."
@misspell --error $(GOFMT_FILES)
@echo -e "$(OK_MSG)"

simple:
@echo -n "==> Checking that code complies with gosimple requirements..."
@gosimple $(GOLIST)
@echo -e "$(OK_MSG)"

staticcheck:
@echo -n "==> Checking that code complies with staticcheck requirements..."
@staticcheck $(GOLIST)
@echo -e "$(OK_MSG)"

vet:
@echo -n "==> Checking that code complies with go vet requirements..."
@go vet $(GOLIST) ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi
@echo -e "$(OK_MSG)"
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# go-pagerduty

## Installation
```bash
go get github.com/heimweh/go-pagerduty
```

## Example usage
```go
func main() {
client, err := pagerduty.NewClient(&Config{Token: "foo"})
if err != nil {
panic(err)
}

// List all users
resp, raw, err := client.Users.List(&pagerduty.ListUsersOptions{})
if err != nil {
panic(err)
}

for _, user := range resp.Users {
fmt.Println(user.Name)
}

// All calls returns the raw *http.Response for further inspection
fmt.Println(raw.StatusCode)
}
```

## Contributing
1. Fork it ( https://github.com/heimweh/go-pagerduty/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
31 changes: 31 additions & 0 deletions pagerduty/ability.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package pagerduty

import "fmt"

// AbilityService handles the communication with ability related methods
// of the PagerDuty API.
type AbilityService service

// ListAbilitiesResponse represents a list response of abilities.
type ListAbilitiesResponse struct {
Abilities []string `json:"abilities,omitempty"`
}

// Test tests whether the account has a given ability.
func (s *AbilityService) Test(id string) (*Response, error) {
u := fmt.Sprintf("/abilities/%s", id)
return s.client.newRequestDo("GET", u, nil, nil, nil)
}

// List lists available abilities.
func (s *AbilityService) List() (*ListAbilitiesResponse, *Response, error) {
u := "/abilities"
v := new(ListAbilitiesResponse)

resp, err := s.client.newRequestDo("GET", u, nil, nil, v)
if err != nil {
return nil, nil, err
}

return v, resp, nil
}
72 changes: 72 additions & 0 deletions pagerduty/ability_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package pagerduty

import (
"net/http"
"reflect"
"testing"
)

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

mux.HandleFunc("/abilities", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.Write([]byte(`{"abilities": ["sso"]}`))
})

abilities, _, err := client.Abilities.List()
if err != nil {
t.Fatal(err)
}

want := &ListAbilitiesResponse{
Abilities: []string{"sso"},
}

if !reflect.DeepEqual(abilities, want) {
t.Errorf("returned %#v; want %#v", abilities, want)
}
}

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

mux.HandleFunc("/abilities", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.WriteHeader(http.StatusForbidden)
})

if _, _, err := client.Abilities.List(); err == nil {
t.Fatal("expected error; got nil")
}
}

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

mux.HandleFunc("/abilities/sso", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.WriteHeader(http.StatusNoContent)
})

if _, err := client.Abilities.Test("sso"); err != nil {
t.Fatal(err)
}
}

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

mux.HandleFunc("/abilities/sso", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
w.WriteHeader(http.StatusForbidden)
})

if _, err := client.Abilities.Test("sso"); err == nil {
t.Fatal("expected error; got nil")
}
}
90 changes: 90 additions & 0 deletions pagerduty/addon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package pagerduty

import "fmt"

// AddonService handles the communication with add-on related methods
// of the PagerDuty API.
type AddonService service

// Addon represents a PagerDuty add-on.
type Addon struct {
Addon *Addon `json:"addon,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Self string `json:"self,omitempty"`
Src string `json:"src,omitempty"`
Summary string `json:"summary,omitempty"`
Type string `json:"type,omitempty"`
}

// ListAddonsOptions represents options when listing add-ons.
type ListAddonsOptions struct {
*Pagination
Filter string `url:"filter,omitempty"`
Include []string `url:"include,omitempty,brackets"`
ServiceIDs []string `url:"service_ids,omitempty,brackets"`
}

// ListAddonsResponse represents a list response of add-ons.
type ListAddonsResponse struct {
Pagination
Addons []*Addon `json:"addons,omitempty"`
}

// List lists installed add-ons.
func (s *AddonService) List(o *ListAddonsOptions) (*ListAddonsResponse, *Response, error) {
u := "/addons"
v := new(ListAddonsResponse)

resp, err := s.client.newRequestDo("GET", u, o, nil, &v)
if err != nil {
return nil, nil, err
}

return v, resp, nil
}

// Install installs an add-on.
func (s *AddonService) Install(addon *Addon) (*Addon, *Response, error) {
u := "/addons"
v := new(Addon)

resp, err := s.client.newRequestDo("POST", u, nil, &Addon{Addon: addon}, v)
if err != nil {
return nil, nil, err
}

return v.Addon, resp, nil
}

// Delete removes an existing add-on.
func (s *AddonService) Delete(id string) (*Response, error) {
u := fmt.Sprintf("/addons/%s", id)
return s.client.newRequestDo("DELETE", u, nil, nil, nil)
}

// Get retrieves information about an add-on.
func (s *AddonService) Get(id string) (*Addon, *Response, error) {
u := fmt.Sprintf("/addons/%s", id)
v := new(Addon)

resp, err := s.client.newRequestDo("GET", u, nil, nil, &v)
if err != nil {
return nil, nil, err
}

return v.Addon, resp, nil
}

// Update updates an existing add-on.
func (s *AddonService) Update(id string, addon *Addon) (*Addon, *Response, error) {
u := fmt.Sprintf("/addons/%s", id)
v := new(Addon)
resp, err := s.client.newRequestDo("PUT", u, nil, &Addon{Addon: addon}, &v)
if err != nil {
return nil, nil, err
}

return v.Addon, resp, nil
}
Loading

0 comments on commit a458ce4

Please sign in to comment.