Skip to content

Commit

Permalink
Merge pull request #233 from bkabrda/api-keys
Browse files Browse the repository at this point in the history
Add functionality to manipulate API keys
  • Loading branch information
bkabrda authored Apr 16, 2019
2 parents 8765ea1 + d3aa2dc commit 6de206f
Show file tree
Hide file tree
Showing 4 changed files with 440 additions and 0 deletions.
122 changes: 122 additions & 0 deletions api_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2019 by authors and contributors.
*/

package datadog

import (
"encoding/json"
"fmt"
"time"
)

var createdTimeLayout = "2006-01-02 15:04:05"

// APIKey represents and API key
type APIKey struct {
CreatedBy *string `json:"created_by,omitempty"`
Name *string `json:"name,omitemtpy"`
Key *string `json:"key,omitempty"`
Created *time.Time `json:"created,omitempty"`
}

// reqAPIKeys retrieves a slice of all APIKeys.
type reqAPIKeys struct {
APIKeys []APIKey `json:"api_keys,omitempty"`
}

// reqAPIKey is similar to reqAPIKeys, but used for values returned by /v1/api_key/<somekey>
// which contain one object (not list) "api_key" (not "api_keys") containing the found key
type reqAPIKey struct {
APIKey *APIKey `json:"api_key"`
}

// MarshalJSON is a custom method for handling datetime marshalling
func (k APIKey) MarshalJSON() ([]byte, error) {
// Approach for custom (un)marshalling borrowed from http://choly.ca/post/go-json-marshalling/
type Alias APIKey
return json.Marshal(&struct {
Created *string `json:"created,omitempty"`
*Alias
}{
Created: String(k.Created.Format(createdTimeLayout)),
Alias: (*Alias)(&k),
})
}

// UnmarshalJSON is a custom method for handling datetime unmarshalling
func (k *APIKey) UnmarshalJSON(data []byte) error {
type Alias APIKey
aux := &struct {
Created *string `json:"created,omitempty"`
*Alias
}{
Alias: (*Alias)(k),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}

if created, err := time.Parse(createdTimeLayout, *aux.Created); err != nil {
return err
} else {
k.Created = &created
}

return nil
}

// GetAPIKeys returns all API keys or error on failure
func (client *Client) GetAPIKeys() ([]APIKey, error) {
var out reqAPIKeys
if err := client.doJsonRequest("GET", "/v1/api_key", nil, &out); err != nil {
return nil, err
}

return out.APIKeys, nil
}

// GetAPIKey returns a single API key or error on failure
func (client *Client) GetAPIKey(key string) (*APIKey, error) {
var out reqAPIKey
if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/api_key/%s", key), nil, &out); err != nil {
return nil, err
}

return out.APIKey, nil
}

// CreateAPIKey creates an API key from given struct and fills the rest of its
// fileds, or returns an error on failure
func (client *Client) CreateAPIKey(name *string) (*APIKey, error) {
toPost := struct {
Name *string `json:"name,omitempty"`
}{
name,
}
var out reqAPIKey
if err := client.doJsonRequest("POST", "/v1/api_key", toPost, &out); err != nil {
return nil, err
}
return out.APIKey, nil
}

// UpdateAPIKey updates given API key (only Name can be updated), returns an error
func (client *Client) UpdateAPIKey(apikey *APIKey) error {
out := reqAPIKey{APIKey: apikey}
toPost := struct {
Name *string `json:"name,omitempty"`
}{
apikey.Name,
}
return client.doJsonRequest("PUT", fmt.Sprintf("/v1/api_key/%s", *apikey.Key), toPost, &out)
}

// DeleteAPIKey deletes API key given by key, returns an error
func (client *Client) DeleteAPIKey(key string) error {
return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/api_key/%s", key), nil, nil)
}
42 changes: 42 additions & 0 deletions api_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Datadog API for Go
*
* Please see the included LICENSE file for licensing information.
*
* Copyright 2019 by authors and contributors.
*/

package datadog_test

import (
"testing"
"time"

"encoding/json"

"github.com/stretchr/testify/assert"
dd "github.com/zorkian/go-datadog-api"
)

func TestAPIKeySerialization(t *testing.T) {

raw := `
{
"created_by": "[email protected]",
"name": "myCoolKey",
"key": "3111111111111111aaaaaaaaaaaaaaaa",
"created": "2019-04-05 09:47:00"
}`

var apikey dd.APIKey
err := json.Unmarshal([]byte(raw), &apikey)
assert.Equal(t, err, nil)
assert.Equal(t, *apikey.Name, "myCoolKey")
assert.Equal(t, *apikey.CreatedBy, "[email protected]")
assert.Equal(t, *apikey.Key, "3111111111111111aaaaaaaaaaaaaaaa")
assert.Equal(t, *apikey.Created, time.Date(2019, 4, 5, 9, 47, 0, 0, time.UTC))

// make sure that the date is correct after marshaling
res, _ := json.Marshal(apikey)
assert.Contains(t, string(res), "\"2019-04-05 09:47:00\"")
}
156 changes: 156 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package datadog

import (
"encoding/json"
"time"
)

// GetCreator returns the Creator field if non-nil, zero value otherwise.
Expand Down Expand Up @@ -726,6 +727,130 @@ func (a *AlertValueDefinition) SetUnit(v string) {
a.Unit = &v
}

// GetCreated returns the Created field if non-nil, zero value otherwise.
func (a *APIKey) GetCreated() time.Time {
if a == nil || a.Created == nil {
return time.Time{}
}
return *a.Created
}

// GetCreatedOk returns a tuple with the Created field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (a *APIKey) GetCreatedOk() (time.Time, bool) {
if a == nil || a.Created == nil {
return time.Time{}, false
}
return *a.Created, true
}

// HasCreated returns a boolean if a field has been set.
func (a *APIKey) HasCreated() bool {
if a != nil && a.Created != nil {
return true
}

return false
}

// SetCreated allocates a new a.Created and returns the pointer to it.
func (a *APIKey) SetCreated(v time.Time) {
a.Created = &v
}

// GetCreatedBy returns the CreatedBy field if non-nil, zero value otherwise.
func (a *APIKey) GetCreatedBy() string {
if a == nil || a.CreatedBy == nil {
return ""
}
return *a.CreatedBy
}

// GetCreatedByOk returns a tuple with the CreatedBy field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (a *APIKey) GetCreatedByOk() (string, bool) {
if a == nil || a.CreatedBy == nil {
return "", false
}
return *a.CreatedBy, true
}

// HasCreatedBy returns a boolean if a field has been set.
func (a *APIKey) HasCreatedBy() bool {
if a != nil && a.CreatedBy != nil {
return true
}

return false
}

// SetCreatedBy allocates a new a.CreatedBy and returns the pointer to it.
func (a *APIKey) SetCreatedBy(v string) {
a.CreatedBy = &v
}

// GetKey returns the Key field if non-nil, zero value otherwise.
func (a *APIKey) GetKey() string {
if a == nil || a.Key == nil {
return ""
}
return *a.Key
}

// GetKeyOk returns a tuple with the Key field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (a *APIKey) GetKeyOk() (string, bool) {
if a == nil || a.Key == nil {
return "", false
}
return *a.Key, true
}

// HasKey returns a boolean if a field has been set.
func (a *APIKey) HasKey() bool {
if a != nil && a.Key != nil {
return true
}

return false
}

// SetKey allocates a new a.Key and returns the pointer to it.
func (a *APIKey) SetKey(v string) {
a.Key = &v
}

// GetName returns the Name field if non-nil, zero value otherwise.
func (a *APIKey) GetName() string {
if a == nil || a.Name == nil {
return ""
}
return *a.Name
}

// GetNameOk returns a tuple with the Name field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (a *APIKey) GetNameOk() (string, bool) {
if a == nil || a.Name == nil {
return "", false
}
return *a.Name, true
}

// HasName returns a boolean if a field has been set.
func (a *APIKey) HasName() bool {
if a != nil && a.Name != nil {
return true
}

return false
}

// SetName allocates a new a.Name and returns the pointer to it.
func (a *APIKey) SetName(v string) {
a.Name = &v
}

// GetAggregation returns the Aggregation field if non-nil, zero value otherwise.
func (a *ApmOrLogQueryCompute) GetAggregation() string {
if a == nil || a.Aggregation == nil {
Expand Down Expand Up @@ -11297,6 +11422,37 @@ func (r *Recurrence) SetUntilOccurrences(v int) {
r.UntilOccurrences = &v
}

// GetAPIKey returns the APIKey field if non-nil, zero value otherwise.
func (r *reqAPIKey) GetAPIKey() APIKey {
if r == nil || r.APIKey == nil {
return APIKey{}
}
return *r.APIKey
}

// GetAPIKeyOk returns a tuple with the APIKey field if it's non-nil, zero value otherwise
// and a boolean to check if the value has been set.
func (r *reqAPIKey) GetAPIKeyOk() (APIKey, bool) {
if r == nil || r.APIKey == nil {
return APIKey{}, false
}
return *r.APIKey, true
}

// HasAPIKey returns a boolean if a field has been set.
func (r *reqAPIKey) HasAPIKey() bool {
if r != nil && r.APIKey != nil {
return true
}

return false
}

// SetAPIKey allocates a new r.APIKey and returns the pointer to it.
func (r *reqAPIKey) SetAPIKey(v APIKey) {
r.APIKey = &v
}

// GetComment returns the Comment field if non-nil, zero value otherwise.
func (r *reqComment) GetComment() Comment {
if r == nil || r.Comment == nil {
Expand Down
Loading

0 comments on commit 6de206f

Please sign in to comment.