Skip to content

Commit

Permalink
Add Resources to Cloudflare Terraform Provider (#2633)
Browse files Browse the repository at this point in the history
* Allow customers to provision which resources they collect when configuring a Cloudflare account using the Terraform Provider and update tests accordingly.
  • Loading branch information
apiazza-dd authored Nov 5, 2024
1 parent 8bcd826 commit 9ffde5c
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
frameworkPath "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -26,10 +28,11 @@ type integrationCloudflareAccountResource struct {
}

type integrationCloudflareAccountModel struct {
ID types.String `tfsdk:"id"`
ApiKey types.String `tfsdk:"api_key"`
Email types.String `tfsdk:"email"`
Name types.String `tfsdk:"name"`
ID types.String `tfsdk:"id"`
ApiKey types.String `tfsdk:"api_key"`
Email types.String `tfsdk:"email"`
Name types.String `tfsdk:"name"`
Resources types.List `tfsdk:"resources"`
}

func NewIntegrationCloudflareAccountResource() resource.Resource {
Expand Down Expand Up @@ -67,6 +70,13 @@ func (r *integrationCloudflareAccountResource) Schema(_ context.Context, _ resou
},
},
"id": utils.ResourceIDAttribute(),
"resources": schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
Computed: true,
Description: "An allowlist of resources to restrict pulling metrics for including `web`, `dns`, `lb` (load balancer), `worker`)",
Default: listdefault.StaticValue(types.ListValueMust(types.StringType, []attr.Value{})),
},
},
}
}
Expand Down Expand Up @@ -193,6 +203,10 @@ func (r *integrationCloudflareAccountResource) updateState(ctx context.Context,
if name, ok := attributes.GetNameOk(); ok {
state.Name = types.StringValue(*name)
}

if resources, ok := attributes.GetResourcesOk(); ok {
state.Resources, _ = types.ListValueFrom(ctx, types.StringType, resources)
}
}

func (r *integrationCloudflareAccountResource) buildIntegrationCloudflareAccountRequestBody(ctx context.Context, state *integrationCloudflareAccountModel) (*datadogV2.CloudflareAccountCreateRequest, diag.Diagnostics) {
Expand All @@ -205,6 +219,12 @@ func (r *integrationCloudflareAccountResource) buildIntegrationCloudflareAccount
}
attributes.SetName(state.Name.ValueString())

if !state.Resources.IsNull() {
var resources []string
diags.Append(state.Resources.ElementsAs(ctx, &resources, false)...)
attributes.SetResources(resources)
}

req := datadogV2.NewCloudflareAccountCreateRequestWithDefaults()
req.Data = *datadogV2.NewCloudflareAccountCreateRequestDataWithDefaults()
req.Data.SetAttributes(*attributes)
Expand All @@ -221,6 +241,12 @@ func (r *integrationCloudflareAccountResource) buildIntegrationCloudflareAccount
attributes.SetEmail(state.Email.ValueString())
}

if !state.Resources.IsNull() {
var resources []string
diags.Append(state.Resources.ElementsAs(ctx, &resources, false)...)
attributes.SetResources(resources)
}

req := datadogV2.NewCloudflareAccountUpdateRequestWithDefaults()
req.Data = *datadogV2.NewCloudflareAccountUpdateRequestDataWithDefaults()
req.Data.SetAttributes(*attributes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024-10-01T09:42:13.446934-04:00
2024-10-31T15:31:32.714325-04:00

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023-04-10T08:26:30.816762-04:00
2024-10-31T16:13:12.292368-04:00
163 changes: 107 additions & 56 deletions datadog/tests/cassettes/TestAccIntegrationCloudflareAccountBasic.yaml
Original file line number Diff line number Diff line change
@@ -1,58 +1,109 @@
---
version: 2
interactions:
- request:
body: |
{"data":{"attributes":{"api_key":"1234567891012331asdd","email":"[email protected]","name":"tf-TestAccIntegrationCloudflareAccountBasic-local-1681129590"},"type":"cloudflare-accounts"}}
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
url: https://api.datadoghq.com/api/v2/integrations/cloudflare/accounts
method: POST
id: 0
response:
body: |
{"data":{"type":"cloudflare-accounts","attributes":{"email":"[email protected]","name":"tf-TestAccIntegrationCloudflareAccountBasic-local-1681129590"},"id":"9e9054f8a21fd8dd20feee8e9fa31447"}}
headers:
Content-Type:
- application/json
status: 201 Created
code: 201
duration: "0ms"
- request:
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v2/integrations/cloudflare/accounts/9e9054f8a21fd8dd20feee8e9fa31447
method: GET
id: 1
response:
body: |
{"data":{"type":"cloudflare-accounts","id":"9e9054f8a21fd8dd20feee8e9fa31447","attributes":{"email":"[email protected]","name":"tf-TestAccIntegrationCloudflareAccountBasic-local-1681129590"}}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: "0ms"
- request:
body: ""
form: {}
headers:
Accept:
- '*/*'
url: https://api.datadoghq.com/api/v2/integrations/cloudflare/accounts/9e9054f8a21fd8dd20feee8e9fa31447
method: DELETE
id: 2
response:
body: ""
headers:
Content-Type:
- text/html; charset=utf-8
status: 204 No Content
code: 204
duration: "0ms"
- id: 0
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 226
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: |
{"data":{"attributes":{"api_key":"12345","email":"[email protected]","name":"tf-TestAccIntegrationCloudflareAccountBasic-local-1730405592","resources":["web"]},"type":"cloudflare-accounts"}}
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
url: https://api.datadoghq.com/api/v2/integrations/cloudflare/accounts
method: POST
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"data":{"type":"cloudflare-accounts","attributes":{"resources":["web"],"email":"[email protected]","zones":[],"name":"tf-TestAccIntegrationCloudflareAccountBasic-local-1730405592"},"id":"0b7e1a40578e2e4742c5183aa3b0502f9116ab249b16dc10ff09135a51663560"}}
headers:
Content-Type:
- application/json
status: 201 Created
code: 201
duration: 554.743709ms
- id: 1
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- application/json
url: https://api.datadoghq.com/api/v2/integrations/cloudflare/accounts/0b7e1a40578e2e4742c5183aa3b0502f9116ab249b16dc10ff09135a51663560
method: GET
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding:
- chunked
trailer: {}
content_length: -1
uncompressed: true
body: |
{"data":{"type":"cloudflare-accounts","attributes":{"zones":[],"email":"[email protected]","resources":["web"],"name":"tf-TestAccIntegrationCloudflareAccountBasic-local-1730405592"},"id":"0b7e1a40578e2e4742c5183aa3b0502f9116ab249b16dc10ff09135a51663560"}}
headers:
Content-Type:
- application/json
status: 200 OK
code: 200
duration: 85.251292ms
- id: 2
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.datadoghq.com
remote_addr: ""
request_uri: ""
body: ""
form: {}
headers:
Accept:
- '*/*'
url: https://api.datadoghq.com/api/v2/integrations/cloudflare/accounts/0b7e1a40578e2e4742c5183aa3b0502f9116ab249b16dc10ff09135a51663560
method: DELETE
response:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
transfer_encoding: []
trailer: {}
content_length: 0
uncompressed: false
body: ""
headers:
Content-Type:
- text/html; charset=utf-8
status: 204 No Content
code: 204
duration: 194.638084ms
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ func TestAccIntegrationCloudflareAccountBasic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogIntegrationCloudflareAccountExists(providers.frameworkProvider),
resource.TestCheckResourceAttr(
"datadog_integration_cloudflare_account.foo", "api_key", "1234567891012331asdd"),
"datadog_integration_cloudflare_account.foo", "api_key", "12345"),
resource.TestCheckResourceAttr(
"datadog_integration_cloudflare_account.foo", "email", "test-email@example.com"),
"datadog_integration_cloudflare_account.foo", "email", "test@email.com"),
resource.TestCheckResourceAttr(
"datadog_integration_cloudflare_account.foo", "name", uniq),
resource.TestCheckResourceAttr(
"datadog_integration_cloudflare_account.foo", "resources.#", "1"),
resource.TestCheckResourceAttr(
"datadog_integration_cloudflare_account.foo", "resources.0", "web"),
),
},
},
Expand All @@ -43,9 +47,10 @@ func TestAccIntegrationCloudflareAccountBasic(t *testing.T) {
func testAccCheckDatadogIntegrationCloudflareAccount(uniq string) string {
return fmt.Sprintf(`
resource "datadog_integration_cloudflare_account" "foo" {
api_key = "1234567891012331asdd"
email = "test-email@example.com"
api_key = "12345"
email = "test@email.com"
name = "%s"
resources = ["web"]
}`, uniq)
}

Expand Down
1 change: 1 addition & 0 deletions docs/resources/integration_cloudflare_account.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resource "datadog_integration_cloudflare_account" "foo" {
### Optional

- `email` (String) The email associated with the Cloudflare account. If an API key is provided (and not a token), this field is also required.
- `resources` (List of String) An allowlist of resources to restrict pulling metrics for including `web`, `dns`, `lb` (load balancer), `worker`)

### Read-Only

Expand Down

0 comments on commit 9ffde5c

Please sign in to comment.