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

Add Resources to Cloudflare Terraform Provider #2633

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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{
apiazza-dd marked this conversation as resolved.
Show resolved Hide resolved
apiazza-dd marked this conversation as resolved.
Show resolved Hide resolved
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`)
Copy link
Contributor

@maycmlee maycmlee Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `resources` (List of String) An allowlist of resources to restrict pulling metrics for including `web`, `dns`, `lb` (load balancer), `worker`)
- `resources` (List of String) An allowlist of resources, such as `web`, `dns`, `lb` (load balancer), `worker`, that restricts pulling metrics for those resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the restriction part is important here, by default, they're set to all, but if they want one turned off, then they use the resources list. Phrasing as an allow list makes it seem like if they specify one, they'll collect metrics for it, when it's the opposite.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apiazza-dd Ah okay, thanks for the clarification. I updated the suggestion, what do you think?

Copy link
Contributor

@maycmlee maycmlee Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apiazza-dd actually rereading your comment, I might have misunderstood.

but if they want one turned off, then they use the resources list.

Is the allowlist of resources, the resources that users want to pull metrics for or the resources they don't want pulling metrics for? If it's the latter, i think i'm throw off by it being called an allowlist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maycmlee These docs are autogenerated and have been updated to match our other repository. Does this look okay, or should I make a change in the other repo?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DM'd. Docs are autogenerated so the suggestion will be updated in the other repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


### Read-Only

Expand Down
Loading