Skip to content
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

Migrate AWS logs to use the official Go Client #497

Merged
merged 9 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
146 changes: 92 additions & 54 deletions datadog/cassettes/TestAccDatadogIntegrationAWS.yaml

Large diffs are not rendered by default.

248 changes: 158 additions & 90 deletions datadog/cassettes/TestAccDatadogIntegrationAWSLambdaArn.yaml

Large diffs are not rendered by default.

176 changes: 88 additions & 88 deletions datadog/cassettes/TestAccDatadogIntegrationAWSLogCollection.yaml

Large diffs are not rendered by default.

61 changes: 28 additions & 33 deletions datadog/resource_datadog_integration_aws_lambda_arn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strings"

datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/zorkian/go-datadog-api"
)

func accountAndLambdaArnFromID(id string) (string, string, error) {
Expand All @@ -16,15 +16,12 @@ func accountAndLambdaArnFromID(id string) (string, string, error) {
return result[0], result[1], nil
}

func buildDatadogIntegrationAwsLambdaArnStruct(d *schema.ResourceData) *datadog.IntegrationAWSLambdaARNRequest {
func buildDatadogIntegrationAwsLambdaArnStruct(d *schema.ResourceData) *datadogV1.AWSAccountAndLambdaRequest {
accountID := d.Get("account_id").(string)
lambdaArn := d.Get("lambda_arn").(string)

attachLambdaArnRequest := datadog.IntegrationAWSLambdaARNRequest{
AccountID: &accountID,
LambdaARN: &lambdaArn,
}
return &attachLambdaArnRequest
attachLambdaArnRequest := datadogV1.NewAWSAccountAndLambdaRequest(accountID, lambdaArn)
return attachLambdaArnRequest
}

func resourceDatadogIntegrationAwsLambdaArn() *schema.Resource {
Expand Down Expand Up @@ -56,9 +53,10 @@ func resourceDatadogIntegrationAwsLambdaArnExists(d *schema.ResourceData, meta i
// Exists - This is called to verify a resource still exists. It is called prior to Read,
// and lowers the burden of Read to be able to assume the resource exists.
providerConf := meta.(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

logCollections, err := client.GetIntegrationAWSLogCollection()
logCollections, _, err := datadogClientV1.AWSLogsIntegrationApi.ListAWSLogsIntegrations(authV1).Execute()
gzussa marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return false, translateClientError(err, "error getting aws log integrations for datadog account.")
}
Expand All @@ -68,10 +66,10 @@ func resourceDatadogIntegrationAwsLambdaArnExists(d *schema.ResourceData, meta i
return false, translateClientError(err, fmt.Sprintf("error getting aws account ID and lambda ARN from id: %s", d.Id()))
}

for _, logCollection := range *logCollections {
if logCollection.GetAccountID() == accountID {
for _, logCollectionLambdaArn := range logCollection.LambdaARNs {
if lambdaArn == logCollectionLambdaArn.GetLambdaARN() {
for _, logCollection := range logCollections {
if logCollection.GetAccountId() == accountID {
for _, logCollectionLambdaArn := range logCollection.GetLambdas() {
if lambdaArn == logCollectionLambdaArn.GetArn() {
return true, nil
}
}
Expand All @@ -82,39 +80,40 @@ func resourceDatadogIntegrationAwsLambdaArnExists(d *schema.ResourceData, meta i

func resourceDatadogIntegrationAwsLambdaArnCreate(d *schema.ResourceData, meta interface{}) error {
providerConf := meta.(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

attachLambdaArnRequest := buildDatadogIntegrationAwsLambdaArnStruct(d)
err := client.AttachLambdaARNIntegrationAWS(attachLambdaArnRequest)

_, _, err := datadogClientV1.AWSLogsIntegrationApi.CreateAWSLambdaARN(authV1).Body(*attachLambdaArnRequest).Execute()
if err != nil {
return translateClientError(err, "error attaching Lambda ARN to AWS integration account")
}

d.SetId(fmt.Sprintf("%s %s", *attachLambdaArnRequest.AccountID, *attachLambdaArnRequest.LambdaARN))
d.SetId(fmt.Sprintf("%s %s", attachLambdaArnRequest.GetAccountId(), attachLambdaArnRequest.GetLambdaArn()))

return resourceDatadogIntegrationAwsLambdaArnRead(d, meta)
}

func resourceDatadogIntegrationAwsLambdaArnRead(d *schema.ResourceData, meta interface{}) error {
providerConf := meta.(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

accountID, lambdaArn, err := accountAndLambdaArnFromID(d.Id())
if err != nil {
return translateClientError(err, fmt.Sprintf("error getting aws account ID and lambda ARN from id: %s", d.Id()))
}

logCollections, err := client.GetIntegrationAWSLogCollection()
logCollections, _, err := datadogClientV1.AWSLogsIntegrationApi.ListAWSLogsIntegrations(authV1).Execute()
if err != nil {
return translateClientError(err, "error getting aws log integrations for datadog account.")
}
for _, logCollection := range *logCollections {
if logCollection.GetAccountID() == accountID {
for _, logCollectionLambdaArn := range logCollection.LambdaARNs {
if lambdaArn == logCollectionLambdaArn.GetLambdaARN() {
d.Set("account_id", logCollection.GetAccountID())
d.Set("lambda_arn", logCollectionLambdaArn.GetLambdaARN())
for _, logCollection := range logCollections {
if logCollection.GetAccountId() == accountID {
for _, logCollectionLambdaArn := range logCollection.GetLambdas() {
if lambdaArn == logCollectionLambdaArn.GetArn() {
d.Set("account_id", logCollection.GetAccountId())
d.Set("lambda_arn", logCollectionLambdaArn.GetArn())
return nil
}
}
Expand All @@ -125,20 +124,16 @@ func resourceDatadogIntegrationAwsLambdaArnRead(d *schema.ResourceData, meta int

func resourceDatadogIntegrationAwsLambdaArnDelete(d *schema.ResourceData, meta interface{}) error {
providerConf := meta.(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

accountID, lambdaArn, err := accountAndLambdaArnFromID(d.Id())
if err != nil {
return translateClientError(err, fmt.Sprintf("error parsing account ID and lamdba ARN from ID: %s", d.Id()))
}

attachLambdaArnRequest := datadog.IntegrationAWSLambdaARNRequest{
AccountID: &accountID,
LambdaARN: &lambdaArn,
}

err = client.DeleteAWSLogCollection(&attachLambdaArnRequest)

attachLambdaArnRequest := datadogV1.NewAWSAccountAndLambdaRequest(accountID, lambdaArn)
_, _, err = datadogClientV1.AWSLogsIntegrationApi.DeleteAWSLambdaARN(authV1).Body(*attachLambdaArnRequest).Execute()
if err != nil {
return translateClientError(err, "error deleting an AWS integration Lambda ARN")
}
Expand Down
35 changes: 19 additions & 16 deletions datadog/resource_datadog_integration_aws_lambda_arn_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package datadog

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/zorkian/go-datadog-api"
"strings"
"testing"

datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

Expand Down Expand Up @@ -84,14 +85,15 @@ func TestAccDatadogIntegrationAWSLambdaArn(t *testing.T) {
func checkIntegrationAWSLambdaArnExists(accProvider *schema.Provider) func(*terraform.State) error {
return func(s *terraform.State) error {
providerConf := accProvider.Meta().(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

return checkIntegrationAwsLambdaArnExistsHelper(s, client)
return checkIntegrationAwsLambdaArnExistsHelper(authV1, s, datadogClientV1)
}
}

func checkIntegrationAwsLambdaArnExistsHelper(s *terraform.State, client *datadog.Client) error {
logCollections, err := client.GetIntegrationAWSLogCollection()
func checkIntegrationAwsLambdaArnExistsHelper(authV1 context.Context, s *terraform.State, datadogClientV1 *datadogV1.APIClient) error {
logCollections, _, err := datadogClientV1.AWSLogsIntegrationApi.ListAWSLogsIntegrations(authV1).Execute()
if err != nil {
return err
}
Expand All @@ -100,9 +102,9 @@ func checkIntegrationAwsLambdaArnExistsHelper(s *terraform.State, client *datado
if strings.Contains(resourceType, "datadog_integration_aws_lambda_arn") {
accountId := r.Primary.Attributes["account_id"]
lambdaArn := r.Primary.Attributes["lambda_arn"]
for _, logCollection := range *logCollections {
for _, logCollectionLambdaArn := range logCollection.LambdaARNs {
if *logCollection.AccountID == accountId && *logCollectionLambdaArn.LambdaARN == lambdaArn {
for _, logCollection := range logCollections {
for _, logCollectionLambdaArn := range logCollection.GetLambdas() {
if logCollection.GetAccountId() == accountId && logCollectionLambdaArn.GetArn() == lambdaArn {
return nil
}
}
Expand All @@ -116,23 +118,24 @@ func checkIntegrationAwsLambdaArnExistsHelper(s *terraform.State, client *datado
func checkIntegrationAWSLambdaArnDestroy(accProvider *schema.Provider) func(*terraform.State) error {
return func(s *terraform.State) error {
providerConf := accProvider.Meta().(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

return checkIntegrationAWSLambdaArnDestroyHelper(s, client)
return checkIntegrationAWSLambdaArnDestroyHelper(authV1, s, datadogClientV1)
}
}

func checkIntegrationAWSLambdaArnDestroyHelper(s *terraform.State, client *datadog.Client) error {
logCollections, err := client.GetIntegrationAWSLogCollection()
func checkIntegrationAWSLambdaArnDestroyHelper(authV1 context.Context, s *terraform.State, datadogClientV1 *datadogV1.APIClient) error {
logCollections, _, err := datadogClientV1.AWSLogsIntegrationApi.ListAWSLogsIntegrations(authV1).Execute()
if err != nil {
return err
}
for _, r := range s.RootModule().Resources {
accountId := r.Primary.Attributes["account_id"]
lambdaArn := r.Primary.Attributes["lambda_arn"]
for _, logCollection := range *logCollections {
for _, logCollectionLambdaArn := range logCollection.LambdaARNs {
if *logCollection.AccountID == accountId && *logCollectionLambdaArn.LambdaARN == lambdaArn {
for _, logCollection := range logCollections {
for _, logCollectionLambdaArn := range logCollection.GetLambdas() {
if logCollection.GetAccountId() == accountId && logCollectionLambdaArn.GetArn() == lambdaArn {
return fmt.Errorf("The AWS Lambda ARN is still attached to the account: accountId=%s, lambdaArn=%s", accountId, lambdaArn)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func resourceDatadogIntegrationAwsLogCollectionRead(d *schema.ResourceData, meta
for _, logCollection := range logCollections {
if logCollection.GetAccountId() == accountID {
d.Set("account_id", logCollection.GetAccountId())
d.Set("services", logCollection.Services)
d.Set("services", logCollection.GetServices())
return nil
}
}
Expand Down
32 changes: 18 additions & 14 deletions datadog/resource_datadog_integration_aws_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package datadog

import (
"context"
"fmt"
"testing"

datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/zorkian/go-datadog-api"
"testing"
)

func TestAccountAndRoleFromID(t *testing.T) {
Expand Down Expand Up @@ -76,21 +78,22 @@ func TestAccDatadogIntegrationAWS(t *testing.T) {
func checkIntegrationAWSExists(accProvider *schema.Provider) func(*terraform.State) error {
return func(s *terraform.State) error {
providerConf := accProvider.Meta().(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

return checkIntegrationAWSExistsHelper(s, client)
return checkIntegrationAWSExistsHelper(authV1, s, datadogClientV1)
}
}

func checkIntegrationAWSExistsHelper(s *terraform.State, client *datadog.Client) error {
integrations, err := client.GetIntegrationAWS()
func checkIntegrationAWSExistsHelper(authV1 context.Context, s *terraform.State, datadogClientV1 *datadogV1.APIClient) error {
integrations, _, err := datadogClientV1.AWSIntegrationApi.ListAWSAccounts(authV1).Execute()
if err != nil {
return err
}
for _, r := range s.RootModule().Resources {
accountId := r.Primary.Attributes["account_id"]
for _, integration := range *integrations {
if *integration.AccountID == accountId {
for _, account := range integrations.GetAccounts() {
if account.GetAccountId() == accountId {
return nil
}
}
Expand All @@ -102,21 +105,22 @@ func checkIntegrationAWSExistsHelper(s *terraform.State, client *datadog.Client)
func checkIntegrationAWSDestroy(accProvider *schema.Provider) func(*terraform.State) error {
return func(s *terraform.State) error {
providerConf := accProvider.Meta().(*ProviderConfiguration)
client := providerConf.CommunityClient
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1

return checkIntegrationAWSDestroyHelper(s, client)
return checkIntegrationAWSDestroyHelper(authV1, s, datadogClientV1)
}
}

func checkIntegrationAWSDestroyHelper(s *terraform.State, client *datadog.Client) error {
integrations, err := client.GetIntegrationAWS()
func checkIntegrationAWSDestroyHelper(authV1 context.Context, s *terraform.State, datadogClientV1 *datadogV1.APIClient) error {
integrations, _, err := datadogClientV1.AWSIntegrationApi.ListAWSAccounts(authV1).Execute()
if err != nil {
return err
}
for _, r := range s.RootModule().Resources {
accountId := r.Primary.Attributes["account_id"]
for _, integration := range *integrations {
if *integration.AccountID == accountId {
for _, account := range integrations.GetAccounts() {
if account.GetAccountId() == accountId {
return fmt.Errorf("The AWS integration still exists for account: accountId=%s", accountId)
}
}
Expand Down
16 changes: 8 additions & 8 deletions datadog/resource_datadog_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,21 +281,21 @@ func buildMonitorStruct(d *schema.ResourceData) *datadogV1.Monitor {
}

monitorType := datadogV1.MonitorType(d.Get("type").(string))
m := datadogV1.NewMonitor()
m.SetType(monitorType)
m.SetQuery(d.Get("query").(string))
m.SetName(d.Get("name").(string))
m.SetMessage(d.Get("message").(string))
m.SetOptions(o)

if m.GetType() == datadogV1.MONITORTYPE_LOG_ALERT {
if monitorType == datadogV1.MONITORTYPE_LOG_ALERT {
if attr, ok := d.GetOk("enable_logs_sample"); ok {
o.SetEnableLogsSample(attr.(bool))
} else {
o.SetEnableLogsSample(false)
}
}

m := datadogV1.NewMonitor()
m.SetType(monitorType)
m.SetQuery(d.Get("query").(string))
m.SetName(d.Get("name").(string))
m.SetMessage(d.Get("message").(string))
m.SetOptions(o)

tags := make([]string, 0)
if attr, ok := d.GetOk("tags"); ok {
for _, s := range attr.(*schema.Set).List() {
Expand Down