-
Notifications
You must be signed in to change notification settings - Fork 393
/
Copy pathresource_datadog_integration_aws_lambda_arn.go
140 lines (118 loc) · 5.94 KB
/
resource_datadog_integration_aws_lambda_arn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package datadog
import (
"context"
"fmt"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/validators"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func buildDatadogIntegrationAwsLambdaArnStruct(d *schema.ResourceData) *datadogV1.AWSAccountAndLambdaRequest {
accountID := d.Get("account_id").(string)
lambdaArn := d.Get("lambda_arn").(string)
attachLambdaArnRequest := datadogV1.NewAWSAccountAndLambdaRequest(accountID, lambdaArn)
return attachLambdaArnRequest
}
func resourceDatadogIntegrationAwsLambdaArn() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "**This resource is deprecated - use the `datadog_integration_aws_account` resource instead**: https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/integration_aws_account",
Description: DeprecatedDocumentation("Provides a Datadog - Amazon Web Services integration Lambda ARN resource. This can be used to create and manage the log collection Lambdas for an account.\n\nUpdate operations are currently not supported with datadog API so any change forces a new resource.\n\n**Note**: If you are using AWS GovCloud or the AWS China* region, update the `lambda_arn` parameter for your environment.\n\n *\\*All use of Datadog Services in (or in connection with environments within) mainland China is subject to the disclaimer published in the <a href=\"https://www.datadoghq.com/legal/restricted-service-locations/\">Restricted Service Locations</a> section on our website.*", Ptr("datadog_integration_aws_account")),
CreateContext: resourceDatadogIntegrationAwsLambdaArnCreate,
ReadContext: resourceDatadogIntegrationAwsLambdaArnRead,
DeleteContext: resourceDatadogIntegrationAwsLambdaArnDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaFunc: func() map[string]*schema.Schema {
return map[string]*schema.Schema{
"account_id": {
Description: "Your AWS Account ID without dashes.",
Type: schema.TypeString,
Required: true,
ForceNew: true, // waits for update API call support
ValidateDiagFunc: validators.ValidateAWSAccountID,
},
"lambda_arn": {
Description: "The ARN of the Datadog forwarder Lambda.",
Type: schema.TypeString,
Required: true,
ForceNew: true, // waits for update API call support
},
}
},
}
}
func resourceDatadogIntegrationAwsLambdaArnCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
// shared with datadog_integration_aws resource
utils.IntegrationAwsMutex.Lock()
defer utils.IntegrationAwsMutex.Unlock()
attachLambdaArnRequest := buildDatadogIntegrationAwsLambdaArnStruct(d)
response, httpresp, err := apiInstances.GetAWSLogsIntegrationApiV1().CreateAWSLambdaARN(auth, *attachLambdaArnRequest)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error attaching Lambda ARN to AWS integration account")
}
if err := utils.CheckForUnparsed(response); err != nil {
return diag.FromErr(err)
}
res := response.(map[string]interface{})
if status, ok := res["status"]; ok && status == "error" {
return diag.FromErr(fmt.Errorf("error attaching Lambda ARN to AWS integration account: %s", httpresp.Body))
}
d.SetId(fmt.Sprintf("%s %s", attachLambdaArnRequest.GetAccountId(), attachLambdaArnRequest.GetLambdaArn()))
readDiag := resourceDatadogIntegrationAwsLambdaArnRead(ctx, d, meta)
if !readDiag.HasError() && d.Id() == "" {
return diag.FromErr(fmt.Errorf("aws integration lambda arn with account id `%s` and lambda arn `%s` not found after creation", attachLambdaArnRequest.GetAccountId(), attachLambdaArnRequest.GetLambdaArn()))
}
return readDiag
}
func resourceDatadogIntegrationAwsLambdaArnRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
accountID, lambdaArn, err := utils.AccountAndLambdaArnFromID(d.Id())
if err != nil {
return diag.FromErr(err)
}
logCollections, httpresp, err := apiInstances.GetAWSLogsIntegrationApiV1().ListAWSLogsIntegrations(auth)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error getting aws log integrations for datadog account.")
}
if err := utils.CheckForUnparsed(logCollections); err != nil {
return diag.FromErr(err)
}
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
}
}
}
}
d.SetId("")
return nil
}
func resourceDatadogIntegrationAwsLambdaArnDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
apiInstances := providerConf.DatadogApiInstances
auth := providerConf.Auth
// shared with datadog_integration_aws resource
utils.IntegrationAwsMutex.Lock()
defer utils.IntegrationAwsMutex.Unlock()
accountID, lambdaArn, err := utils.AccountAndLambdaArnFromID(d.Id())
if err != nil {
return diag.FromErr(err)
}
attachLambdaArnRequest := datadogV1.NewAWSAccountAndLambdaRequest(accountID, lambdaArn)
_, httpresp, err := apiInstances.GetAWSLogsIntegrationApiV1().DeleteAWSLambdaARN(auth, *attachLambdaArnRequest)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error deleting an AWS integration Lambda ARN")
}
return nil
}