Skip to content

Commit

Permalink
Merge pull request #162 from MattHodge/feat/159
Browse files Browse the repository at this point in the history
 Add AWS Integration
  • Loading branch information
ojongerius authored Aug 5, 2018
2 parents 922740a + f0e4c52 commit 114bbae
Show file tree
Hide file tree
Showing 11 changed files with 2,055 additions and 317 deletions.
124 changes: 124 additions & 0 deletions datadog-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7453,6 +7453,130 @@ func (i *ImageWidget) SetY(v int) {
i.Y = &v
}

// GetAccountID returns the AccountID field if non-nil, zero value otherwise.
func (i *IntegrationAWSAccount) GetAccountID() string {
if i == nil || i.AccountID == nil {
return ""
}
return *i.AccountID
}

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

// HasAccountID returns a boolean if a field has been set.
func (i *IntegrationAWSAccount) HasAccountID() bool {
if i != nil && i.AccountID != nil {
return true
}

return false
}

// SetAccountID allocates a new i.AccountID and returns the pointer to it.
func (i *IntegrationAWSAccount) SetAccountID(v string) {
i.AccountID = &v
}

// GetRoleName returns the RoleName field if non-nil, zero value otherwise.
func (i *IntegrationAWSAccount) GetRoleName() string {
if i == nil || i.RoleName == nil {
return ""
}
return *i.RoleName
}

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

// HasRoleName returns a boolean if a field has been set.
func (i *IntegrationAWSAccount) HasRoleName() bool {
if i != nil && i.RoleName != nil {
return true
}

return false
}

// SetRoleName allocates a new i.RoleName and returns the pointer to it.
func (i *IntegrationAWSAccount) SetRoleName(v string) {
i.RoleName = &v
}

// GetAccountID returns the AccountID field if non-nil, zero value otherwise.
func (i *IntegrationAWSAccountDeleteRequest) GetAccountID() string {
if i == nil || i.AccountID == nil {
return ""
}
return *i.AccountID
}

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

// HasAccountID returns a boolean if a field has been set.
func (i *IntegrationAWSAccountDeleteRequest) HasAccountID() bool {
if i != nil && i.AccountID != nil {
return true
}

return false
}

// SetAccountID allocates a new i.AccountID and returns the pointer to it.
func (i *IntegrationAWSAccountDeleteRequest) SetAccountID(v string) {
i.AccountID = &v
}

// GetRoleName returns the RoleName field if non-nil, zero value otherwise.
func (i *IntegrationAWSAccountDeleteRequest) GetRoleName() string {
if i == nil || i.RoleName == nil {
return ""
}
return *i.RoleName
}

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

// HasRoleName returns a boolean if a field has been set.
func (i *IntegrationAWSAccountDeleteRequest) HasRoleName() bool {
if i != nil && i.RoleName != nil {
return true
}

return false
}

// SetRoleName allocates a new i.RoleName and returns the pointer to it.
func (i *IntegrationAWSAccountDeleteRequest) SetRoleName(v string) {
i.RoleName = &v
}

// GetAPIToken returns the APIToken field if non-nil, zero value otherwise.
func (i *integrationPD) GetAPIToken() string {
if i == nil || i.APIToken == nil {
Expand Down
90 changes: 90 additions & 0 deletions integration/integrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,93 @@ func cleanUpIntegrationSlack(t *testing.T) {
t.Fatal("Fetching deleted Slack integration didn't lead to an error.")
}
}

/*
AWS Integration
*/

func TestIntegrationAWSCreateAndDelete(t *testing.T) {
// Validate creation of AWS Integration
expected := getTestIntegrationAWS()
createAwsIntegrationResponse := createTestIntegrationAWS(t)
defer cleanUpIntegrationAWS(t, getTestIntegrationAWSDeleteRequest())

assert.NotNil(t, createAwsIntegrationResponse.ExternalID, "An external ID should have been returned from Datadog on integration create.")

// Get the AWS Accounts from the AWS Integration
awsAccountsInDatadog, err := client.GetIntegrationAWS()
if err != nil {
t.Fatalf("Retrieving a AWS integration failed when it shouldn't: (%s)", err)
}

// Check the created AWS Account is in the slice
var createdAWSAccount datadog.IntegrationAWSAccount
for _, account := range *awsAccountsInDatadog {
if *account.AccountID == *expected.AccountID {
createdAWSAccount = account
}
}

// Test each property as slices order can change in FilterTags and HostTags
assert.Equal(t, expected.AccountID, createdAWSAccount.AccountID)
assert.Equal(t, expected.RoleName, createdAWSAccount.RoleName)
assert.ElementsMatch(t, expected.FilterTags, createdAWSAccount.FilterTags)
assert.ElementsMatch(t, expected.HostTags, createdAWSAccount.HostTags)
assert.Equal(t, expected.AccountSpecificNamespaceRules, createdAWSAccount.AccountSpecificNamespaceRules)
}

func getTestIntegrationAWS() *datadog.IntegrationAWSAccount {
return &datadog.IntegrationAWSAccount{
AccountID: datadog.String("1111111111111"),
RoleName: datadog.String("GoLangDatadogAWSIntegrationRole"),
FilterTags: []string{
"env:production",
"instance-type:c1.*",
"!region:us-east-1",
},
HostTags: []string{
"account:my_aws_account",
},
AccountSpecificNamespaceRules: map[string]bool{
"auto_scaling": false,
"opsworks": false,
},
}
}

func getTestIntegrationAWSDeleteRequest() *datadog.IntegrationAWSAccountDeleteRequest {
return &datadog.IntegrationAWSAccountDeleteRequest{
AccountID: datadog.String("1111111111111"),
RoleName: datadog.String("GoLangDatadogAWSIntegrationRole"),
}
}

func createTestIntegrationAWS(t *testing.T) *datadog.IntegrationAWSAccountCreateResponse {
awsIntegrationRequest := getTestIntegrationAWS()

result, err := client.CreateIntegrationAWS(awsIntegrationRequest)
if err != nil {
t.Fatalf("Creating a AWS integration failed when it shouldn't: %s", err.Error())
}

return result
}

func cleanUpIntegrationAWS(t *testing.T, awsAccount *datadog.IntegrationAWSAccountDeleteRequest) {
if err := client.DeleteIntegrationAWS(awsAccount); err != nil {
t.Fatalf("Deleting the AWS Account from the AWS integration failed when it shouldn't. Manual cleanup needed. (%s)", err)
}

awsIntegration, err := client.GetIntegrationAWS()

// check the account is no longer in Datadog
for _, account := range *awsIntegration {
if *account.AccountID == *awsAccount.AccountID {
t.Fatal("AWS Account in the AWS Integration hasn't been deleted when it should have been. Manual cleanup needed.")
}
}

if err != nil {
t.Fatalf("Getting AWS accounts from the AWS integration failed when it shouldn't: %s", err)
}
}
56 changes: 56 additions & 0 deletions integrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,59 @@ func (client *Client) GetIntegrationSlack() (*IntegrationSlackRequest, error) {
func (client *Client) DeleteIntegrationSlack() error {
return client.doJsonRequest("DELETE", "/v1/integration/slack", nil, nil)
}

/*
AWS Integration
*/

// IntegrationAWSAccount defines the request payload for
// creating & updating Datadog-AWS integration.
type IntegrationAWSAccount struct {
AccountID *string `json:"account_id"`
RoleName *string `json:"role_name"`
FilterTags []string `json:"filter_tags"`
HostTags []string `json:"host_tags"`
AccountSpecificNamespaceRules map[string]bool `json:"account_specific_namespace_rules"`
}

// IntegrationAWSAccountCreateResponse defines the response payload for
// creating & updating Datadog-AWS integration.
type IntegrationAWSAccountCreateResponse struct {
ExternalID string `json:"external_id"`
}

type IntegrationAWSAccountGetResponse struct {
Accounts []IntegrationAWSAccount `json:"accounts"`
}

type IntegrationAWSAccountDeleteRequest struct {
AccountID *string `json:"account_id"`
RoleName *string `json:"role_name"`
}

// CreateIntegrationAWS adds a new AWS Account in the AWS Integrations.
// Use this if you want to setup the integration for the first time
// or to add more accounts.
func (client *Client) CreateIntegrationAWS(awsAccount *IntegrationAWSAccount) (*IntegrationAWSAccountCreateResponse, error) {
var out IntegrationAWSAccountCreateResponse
if err := client.doJsonRequest("POST", "/v1/integration/aws", awsAccount, &out); err != nil {
return nil, err
}

return &out, nil
}

// GetIntegrationAWS gets all the AWS Accounts in the AWS Integrations from Datadog.
func (client *Client) GetIntegrationAWS() (*[]IntegrationAWSAccount, error) {
var response IntegrationAWSAccountGetResponse
if err := client.doJsonRequest("GET", "/v1/integration/aws", nil, &response); err != nil {
return nil, err
}

return &response.Accounts, nil
}

// DeleteIntegrationAWS removes a specific AWS Account from the AWS Integration.
func (client *Client) DeleteIntegrationAWS(awsAccount *IntegrationAWSAccountDeleteRequest) error {
return client.doJsonRequest("DELETE", "/v1/integration/aws", awsAccount, nil)
}
Loading

0 comments on commit 114bbae

Please sign in to comment.