Skip to content

WIP GH-56 Refactor to use pointers for all fields. #83

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

Merged
merged 26 commits into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0675f9d
GH-56: Refactor to use pointers for all fields. Adds a few helpers to…
ojongerius Jan 27, 2017
10ab901
GH-56: Set default values for test structs.
ojongerius Jan 28, 2017
2a67c65
GH-56: Fix widget test. Rename tests to make testing groups more easy.
ojongerius Jan 28, 2017
1a5085b
GH-56: Fixup of user test.
ojongerius Jan 28, 2017
f18c0a7
GH-56: Add a few getters and tests.
ojongerius Jan 29, 2017
9e9f19d
GH-56: Update comments.
ojongerius Jan 29, 2017
8a1211a
Simplify Widget tests.
ojongerius Jan 30, 2017
b80c777
Small changes implementing PR feedback.
ojongerius Jan 31, 2017
7126deb
Be explicit in what is returned instead of using the default value.
ojongerius Jan 31, 2017
44f55f5
Make use of omitempty consistent accross all types.
ojongerius Jan 31, 2017
0984f79
Add support for new_host_delay in monitor definitions
Jan 31, 2017
2f75850
Be explicit in type conversion helper definitions
Jan 31, 2017
e1345fa
Getters and setters already declared in helpers.go (this change was c…
ojongerius Jan 31, 2017
809cc0c
Properly encode/decode Aggregator param.
m25n Nov 30, 2016
7313d4a
Generate accessors.
ojongerius Feb 16, 2017
0f5ff63
Add generated code.
ojongerius Feb 16, 2017
826e56f
Gofmt.
ojongerius Feb 16, 2017
52b66b3
fmt.
ojongerius Feb 16, 2017
cccf633
Move generate comments to generate.go, renamed generation source file…
ojongerius Feb 16, 2017
59ba811
Add full stop and end of Godoc string.
ojongerius Feb 16, 2017
dcb6df3
Regenerate code.
ojongerius Feb 16, 2017
edd7699
Update README to reflect API changes and inform users on how to switc…
ojongerius Feb 17, 2017
cfa8d69
Update README with correction on how to use gopkg.in, and instruction…
ojongerius Feb 17, 2017
53aa1ca
Update example to use Int helper.
ojongerius Feb 17, 2017
495a0f0
Improve Readme.
ojongerius Feb 17, 2017
ecbc43f
Wording on code generation.
ojongerius Feb 17, 2017
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ GOFMT_FILES?=$$(find . -name '*.go' | grep -v vendor)

default: test fmt

generate:
go generate

# test runs the unit tests and vets the code
test:
go test . $(TESTARGS) -v -timeout=30s -parallel=4
Expand Down
80 changes: 65 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,78 @@ status](https://travis-ci.org/zorkian/go-datadog-api.svg)](https://travis-ci.org

# Datadog API in Go

Hi!
**This is the v2.0 version of the API, and has breaking changes. Use the main or v1.0 branch if you need
legacy code to be supported.**

This is a Go wrapper for the Datadog API. You should use this library if you need to interact
A Go wrapper for the Datadog API. Use this library if you need to interact
with the Datadog system. You can post metrics with it if you want, but this library is probably
mostly used for automating dashboards/alerting and retrieving data (events, etc).

The source API documentation is here: <http://docs.datadoghq.com/api/>

## Installation
To use the default branch, include it in your code like:
```go
import "github.com/zorkian/go-datadog-api"
```

## USAGE

To use this project, include it in your code like:
Or, if you need to control which version to use, import using [gopkg.in](http://labix.org/gopkg.in). Like so:
```go
import "gopkg.in/github.com/zorkian/go-datadog-api.v2"
```

``` go
import "github.com/zorkian/go-datadog-api"
Using go get:
```bash
go get gopkg.in/zorkian/go-datadog-api.v2
```

Then, you can work with it:
## USAGE
This library uses pointers to be able to verify if values are set or not (vs the default value for the type). Like
protobuf there are helpers to enhance the API. You can decide to not use them, but you'll have to be careful handling
nil pointers.

``` go
Using the client:
```go
client := datadog.NewClient("api key", "application key")

dash, err := client.GetDashboard(10880)
dash, err := client.GetDashboard(datadog.Int(10880))
if err != nil {
log.Fatalf("fatal: %s\n", err)
}
log.Printf("dashboard %d: %s\n", dash.Id, dash.Title)

log.Printf("dashboard %d: %s\n", dash.GetId(), dash.GetTitle())
```

That's all; it's pretty easy to use. Check out the Godoc link for the
available API methods and, if you can't find the one you need,
An example using datadog.String(), which allocates a pointer for you:
```go
m := datadog.Monitor{
Name: datadog.String("Monitor other things"),
Creator: &datadog.Creator{
Name: datadog.String("Joe Creator"),
},
}
```

An example using the SetXx, HasXx, GetXx and GetXxOk accessors:
```go
m := datadog.Monitor{}
m.SetName("Monitor all the things")
m.SetMessage("Electromagnetic energy loss")

// Use HasMessage(), to verify we have interest in the message.
// Using GetMessage() always safe as it returns the actual or, if never set, default value for that type.
if m.HasMessage() {
fmt.Printf("Found message %s\n", m.GetMessage())
}

// Alternatively, use GetMessageOk(), it returns a tuple with the (default) value and a boolean expressing
// if it was set at all:
if v, ok := m.GetMessageOk(); ok {
fmt.Printf("Found message %s\n", v)
}
```

Check out the Godoc link for the available API methods and, if you can't find the one you need,
let us know (or patches welcome)!

## DOCUMENTATION
Expand All @@ -52,7 +93,7 @@ Github:
Thanks in advance! And, as always, patches welcome!

## DEVELOPMENT

### Running tests
* Run tests tests with `make test`.
* Integration tests can be run with `make testacc`. Run specific integration tests with `make testacc TESTARGS='-run=TestCreateAndDeleteMonitor'`

Expand All @@ -61,8 +102,17 @@ in your environment variables.

*Warning: the integrations tests will create and remove real resources in your Datadog account.*

### Regenerating code
Accessors `HasXx`, `GetXx`, `GetOkXx` and `SetXx` are generated for each struct field type type that contains pointers.
When structs are updated a contributor has to regenerate these using `go generate` and commit these changes.
Optionally there is a make target for the generation:

```bash
make generate
```

## COPYRIGHT AND LICENSE

Please see the LICENSE file for the included license information.

Copyright 2013 by authors and contributors.
Copyright 2017 by authors and contributors.
16 changes: 8 additions & 8 deletions alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
// Alert represents the data of an alert: a query that can fire and send a
// message to the users.
type Alert struct {
Id int `json:"id,omitempty"`
Creator int `json:"creator,omitempty"`
Query string `json:"query,omitempty"`
Name string `json:"name,omitempty"`
Message string `json:"message,omitempty"`
Silenced bool `json:"silenced,omitempty"`
NotifyNoData bool `json:"notify_no_data,omitempty"`
State string `json:"state,omitempty"`
Id *int `json:"id,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to your changes here but I'm noticing that it looks like inconsistent usage of omitempty. The Alert struct uses it on every member but Comment only uses it on optional members.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I'll add omitempty to all fields 🤞

Creator *int `json:"creator,omitempty"`
Query *string `json:"query,omitempty"`
Name *string `json:"name,omitempty"`
Message *string `json:"message,omitempty"`
Silenced *bool `json:"silenced,omitempty"`
NotifyNoData *bool `json:"notify_no_data,omitempty"`
State *string `json:"state,omitempty"`
}

// reqAlerts receives a slice of all alerts.
Expand Down
10 changes: 5 additions & 5 deletions checks.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package datadog

type Check struct {
Check string `json:"check"`
HostName string `json:"host_name"`
Status Status `json:"status"`
Timestamp string `json:"timestamp,omitempty"`
Message string `json:"message,omitempty"`
Check *string `json:"check,omitempty"`
HostName *string `json:"host_name,omitempty"`
Status *Status `json:"status,omitempty"`
Timestamp *string `json:"timestamp,omitempty"`
Message *string `json:"message,omitempty"`
Tags []string `json:"tags,omitempty"`
}

Expand Down
Loading