-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from bkabrda/api-keys
Add functionality to manipulate API keys
- Loading branch information
Showing
4 changed files
with
440 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,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) | ||
} |
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,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\"") | ||
} |
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
Oops, something went wrong.