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 support for cloudflare_web_analytics_site #632

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ existing binary, or you wish to provide a Terraform compatible binary (such as
Any resources not listed are currently not supported.

| Resource | Resource Scope | Generate Supported | Import Supported |
| ------------------------------------------------------------------------------------------------------------------------------------------------ | --------------- | ------------------ | ---------------- |
|--------------------------------------------------------------------------------------------------------------------------------------------------|-----------------| ------------------ | ---------------- |
| [cloudflare_access_application](https://www.terraform.io/docs/providers/cloudflare/r/access_application) | Account | ✅ | ❌ |
| [cloudflare_access_group](https://www.terraform.io/docs/providers/cloudflare/r/access_group) | Account | ✅ | ✅ |
| [cloudflare_access_identity_provider](https://www.terraform.io/docs/providers/cloudflare/r/access_identity_provider) | Account | ✅ | ❌ |
Expand Down Expand Up @@ -255,6 +255,7 @@ Any resources not listed are currently not supported.
| [cloudflare_waf_package](https://www.terraform.io/docs/providers/cloudflare/r/waf_package) | Zone | ✅ | ❌ |
| [cloudflare_waf_rule](https://www.terraform.io/docs/providers/cloudflare/r/waf_rule) | Zone | ❌ | ❌ |
| [cloudflare_waiting_room](https://www.terraform.io/docs/providers/cloudflare/r/waiting_room) | Zone | ✅ | ✅ |
| [cloudflare_web_analytics_site](https://www.terraform.io/docs/providers/cloudflare/r/web_analytics_site) | Account | ✅ | ✅ |
| [cloudflare_worker_cron_trigger](https://www.terraform.io/docs/providers/cloudflare/r/worker_cron_trigger) | Account | ❌ | ❌ |
| [cloudflare_worker_route](https://www.terraform.io/docs/providers/cloudflare/r/worker_route) | Zone | ✅ | ✅ |
| [cloudflare_worker_script](https://www.terraform.io/docs/providers/cloudflare/r/worker_script) | Account | ❌ | ❌ |
Expand Down
16 changes: 16 additions & 0 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,22 @@ func generateResources() func(cmd *cobra.Command, args []string) {
log.Fatal(err)
}
jsonStructData = []interface{}{jsonPayloadInterface}
case "cloudflare_web_analytics_site":
jsonPayload, _, err := api.ListWebAnalyticsSites(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListWebAnalyticsSitesParams{})
if err != nil {
log.Fatal(err)
}
resourceCount = len(jsonPayload)
m, _ := json.Marshal(jsonPayload)
err = json.Unmarshal(m, &jsonStructData)
if err != nil {
log.Fatal(err)
}

for i := 0; i < resourceCount; i++ {
jsonStructData[i].(map[string]interface{})["zone_tag"] = jsonStructData[i].(map[string]interface{})["ruleset"].(map[string]interface{})["zone_tag"]
Copy link
Member

Choose a reason for hiding this comment

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

updated this to include the remapping as site_tag doesn't exist at the top level in the API response but it does in the resource.

}

case "cloudflare_workers_kv_namespace":
jsonPayload, _, err := api.ListWorkersKVNamespaces(context.Background(), identifier, cloudflare.ListWorkersKVNamespacesParams{})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/app/cf-terraforming/cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func TestResourceGeneration(t *testing.T) {
"cloudflare waiting room event": {identiferType: "zone", resourceType: "cloudflare_waiting_room_event", testdataFilename: "cloudflare_waiting_room_event"},
"cloudflare waiting room rules": {identiferType: "zone", resourceType: "cloudflare_waiting_room_rules", testdataFilename: "cloudflare_waiting_room_rules"},
"cloudflare waiting room settings": {identiferType: "zone", resourceType: "cloudflare_waiting_room_settings", testdataFilename: "cloudflare_waiting_room_settings"},
"cloudflare web analytics site": {identiferType: "account", resourceType: "cloudflare_web_analytics_site", testdataFilename: "cloudflare_web_analytics_site"},
"cloudflare worker route": {identiferType: "zone", resourceType: "cloudflare_worker_route", testdataFilename: "cloudflare_worker_route"},
"cloudflare workers kv namespace": {identiferType: "account", resourceType: "cloudflare_workers_kv_namespace", testdataFilename: "cloudflare_workers_kv_namespace"},
"cloudflare zone lockdown": {identiferType: "zone", resourceType: "cloudflare_zone_lockdown", testdataFilename: "cloudflare_zone_lockdown"},
Expand Down
11 changes: 11 additions & 0 deletions internal/app/cf-terraforming/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var resourceImportStringFormats = map[string]string{
"cloudflare_turnstile_widget": ":account_id/:id",
"cloudflare_waf_override": ":zone_id/:id",
"cloudflare_waiting_room": ":zone_id/:id",
"cloudflare_web_analytics_site": ":account_id/:id",
Copy link
Member

@jacobbednarz jacobbednarz Nov 8, 2023

Choose a reason for hiding this comment

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

you'll have to manually test this; i'm unsure what id you're using here. i think you may want site_tag given that is what is used in the import command docs (https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/web_analytics_site#import)

Copy link
Member

Choose a reason for hiding this comment

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

...or are import docs wrong and you should use id to import?

"cloudflare_worker_route": ":zone_id/:id",
"cloudflare_workers_kv_namespace": ":id",
"cloudflare_zone_lockdown": ":zone_id/:id",
Expand Down Expand Up @@ -437,6 +438,16 @@ func runImport() func(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
case "cloudflare_web_analytics_site":
jsonPayload, _, err := api.ListWebAnalyticsSites(context.Background(), cloudflare.AccountIdentifier(accountID), cloudflare.ListWebAnalyticsSitesParams{})
if err != nil {
log.Fatal(err)
}
m, _ := json.Marshal(jsonPayload)
err = json.Unmarshal(m, &jsonStructData)
if err != nil {
log.Fatal(err)
}
case "cloudflare_workers_kv_namespace":
jsonPayload, _, err := api.ListWorkersKVNamespaces(context.Background(), identifier, cloudflare.ListWorkersKVNamespacesParams{})
if err != nil {
Expand Down
48 changes: 48 additions & 0 deletions testdata/cloudflare/cloudflare_web_analytics_site.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Content-Type:
- application/json
url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/rum/site_info/list?page=1&per_page=10
method: GET
response:
body: |
{
"success": true,
"errors": [],
"messages": [],
"result_info": {
"page": 1,
"per_page": 10,
"count": 1,
"total_count": 1,
"total_pages": 1
},
"result": [
{
"site_tag": "1a091d3b85d76e2384d0e153747c7a84",
"site_token": "b40e25e5fc304ed31ee72bf5370dbcc7",
"created": "2023-08-28T20:08:56.719351Z",
"snippet": "\u003c!-- Cloudflare Web Analytics --\u003e\u003cscript defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{\"token\": \"b40e25e5fc304ed31ee72bf5370dbcc7\"}'\u003e\u003c/script\u003e\u003c!-- End Cloudflare Web Analytics --\u003e",
"auto_install": true,
"ruleset": {
"zone_tag": "0da42c8d2132a9ddaf714f9e7c920711",
"zone_name": "example.com",
"enabled": true,
"id": "81568f25-8f05-4368-98c3-bbd40846d217"
}
}
]
}
headers:
Content-Type:
- application/json
Vary:
- Accept-Encoding
status: 200 OK
code: 200
duration: ""
7 changes: 7 additions & 0 deletions testdata/terraform/cloudflare_web_analytics_site/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
}
}
}
5 changes: 5 additions & 0 deletions testdata/terraform/cloudflare_web_analytics_site/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "cloudflare_web_analytics_site" "terraform_managed_resource" {
account_id = "f037e56e89293a057740de681ac9abbe"
auto_install = true
zone_tag = "0da42c8d2132a9ddaf714f9e7c920711"
}
Loading