From c79db155ddcb9b8470816e4abc2ece898f9bb65d Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Tue, 9 Jan 2024 19:59:04 -0600 Subject: [PATCH 01/13] Adding teams resources except for account (more complicated change) --- .gitignore | 5 +- README.md | 4 + internal/app/cf-terraforming/cmd/generate.go | 44 +++++++++ .../app/cf-terraforming/cmd/generate_test.go | 4 + internal/app/cf-terraforming/cmd/import.go | 4 + .../cloudflare/cloudflare_teams_list.yaml | 80 +++++++++++++++ .../cloudflare/cloudflare_teams_location.yaml | 49 ++++++++++ .../cloudflare_teams_proxy_endpoint.yaml | 44 +++++++++ .../cloudflare/cloudflare_teams_rule.yaml | 98 +++++++++++++++++++ .../cloudflare_teams_list/provider.tf | 7 ++ .../terraform/cloudflare_teams_list/test.tf | 7 ++ .../cloudflare_teams_location/provider.tf | 7 ++ .../cloudflare_teams_location/test.tf | 9 ++ .../provider.tf | 7 ++ .../cloudflare_teams_proxy_endpoint/test.tf | 5 + .../cloudflare_teams_rule/provider.tf | 7 ++ .../terraform/cloudflare_teams_rule/test.tf | 53 ++++++++++ 17 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 testdata/cloudflare/cloudflare_teams_list.yaml create mode 100644 testdata/cloudflare/cloudflare_teams_location.yaml create mode 100644 testdata/cloudflare/cloudflare_teams_proxy_endpoint.yaml create mode 100644 testdata/cloudflare/cloudflare_teams_rule.yaml create mode 100644 testdata/terraform/cloudflare_teams_list/provider.tf create mode 100644 testdata/terraform/cloudflare_teams_list/test.tf create mode 100644 testdata/terraform/cloudflare_teams_location/provider.tf create mode 100644 testdata/terraform/cloudflare_teams_location/test.tf create mode 100644 testdata/terraform/cloudflare_teams_proxy_endpoint/provider.tf create mode 100644 testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf create mode 100644 testdata/terraform/cloudflare_teams_rule/provider.tf create mode 100644 testdata/terraform/cloudflare_teams_rule/test.tf diff --git a/.gitignore b/.gitignore index 922a0dbe2..d7671e84e 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,7 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out -.idea/ \ No newline at end of file +.idea/ + +*.terraform +*.terraform.lock.hcl \ No newline at end of file diff --git a/README.md b/README.md index 0efddb585..c0e2e6abf 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,10 @@ Any resources not listed are currently not supported. | [cloudflare_ruleset](https://www.terraform.io/docs/providers/cloudflare/r/ruleset) | Account or Zone | ✅ | ✅ | | [cloudflare_spectrum_application](https://www.terraform.io/docs/providers/cloudflare/r/spectrum_application) | Zone | ✅ | ✅ | | [cloudflare_tiered_cache](https://www.terraform.io/docs/providers/cloudflare/r/tiered_cache) | Zone | ✅ | ❌ | +| [cloudflare_teams_list](https://www.terraform.io/docs/providers/cloudflare/r/teams_list) | Account | ✅ | ✅ | +| [cloudflare_teams_location](https://www.terraform.io/docs/providers/cloudflare/r/teams_location) | Account | ✅ | ✅ | +| [cloudflare_teams_proxy_endpoint](https://www.terraform.io/docs/providers/cloudflare/r/teams_proxy_endpoint) | Account | ✅ | ✅ | +| [cloudflare_teams_rule](https://www.terraform.io/docs/providers/cloudflare/r/teams_rule) | Account | ✅ | ✅ | | [cloudflare_tunnel](https://www.terraform.io/docs/providers/cloudflare/r/tunnel) | Account | ✅ | ✅ | | [cloudflare_turnstile_widget](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/turnstile_widget) | Account | ✅ | ✅ | | [cloudflare_url_normalization_settings](https://www.terraform.io/docs/providers/cloudflare/r/url_normalization_settings) | Zone | ✅ | ❌ | diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index 2a0310c69..e5ab53f09 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -873,6 +873,50 @@ func generateResources() func(cmd *cobra.Command, args []string) { log.Fatal(err) } + resourceCount = len(jsonPayload) + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_list": + jsonPayload, _, err := api.ListTeamsLists(context.Background(), &cloudflare.ResourceContainer{Identifier: accountID}, cloudflare.ListTeamListsParams{}) + if err != nil { + log.Fatal(err) + } + resourceCount = len(jsonPayload) + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_location": + jsonPayload, _, err := api.TeamsLocations(context.Background(), accountID) + if err != nil { + log.Fatal(err) + } + resourceCount = len(jsonPayload) + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_proxy_endpoint": + jsonPayload, _, err := api.TeamsProxyEndpoints(context.Background(), accountID) + if err != nil { + log.Fatal(err) + } + resourceCount = len(jsonPayload) + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_rule": + jsonPayload, err := api.TeamsRules(context.Background(), accountID) + if err != nil { + log.Fatal(err) + } resourceCount = len(jsonPayload) m, _ := json.Marshal(jsonPayload) err = json.Unmarshal(m, &jsonStructData) diff --git a/internal/app/cf-terraforming/cmd/generate_test.go b/internal/app/cf-terraforming/cmd/generate_test.go index 7f3a82620..e0dc4c003 100644 --- a/internal/app/cf-terraforming/cmd/generate_test.go +++ b/internal/app/cf-terraforming/cmd/generate_test.go @@ -138,6 +138,10 @@ func TestResourceGeneration(t *testing.T) { "cloudflare ruleset (rewrite to empty query string)": {identiferType: "zone", resourceType: "cloudflare_ruleset", testdataFilename: "cloudflare_ruleset_zone_rewrite_to_empty_query_parameter"}, "cloudflare ruleset": {identiferType: "zone", resourceType: "cloudflare_ruleset", testdataFilename: "cloudflare_ruleset_zone"}, "cloudflare spectrum application": {identiferType: "zone", resourceType: "cloudflare_spectrum_application", testdataFilename: "cloudflare_spectrum_application"}, + "cloudflare teams list": {identiferType: "account", resourceType: "cloudflare_teams_list", testdataFilename: "cloudflare_teams_list"}, + "cloudflare teams location": {identiferType: "account", resourceType: "cloudflare_teams_location", testdataFilename: "cloudflare_teams_location"}, + "cloudflare teams proxy endpoint": {identiferType: "account", resourceType: "cloudflare_teams_proxy_endpoint", testdataFilename: "cloudflare_teams_proxy_endpoint"}, + "cloudflare teams rule": {identiferType: "account", resourceType: "cloudflare_teams_rule", testdataFilename: "cloudflare_teams_rule"}, "cloudflare tunnel": {identiferType: "account", resourceType: "cloudflare_tunnel", testdataFilename: "cloudflare_tunnel"}, "cloudflare turnstile_widget": {identiferType: "account", resourceType: "cloudflare_turnstile_widget", testdataFilename: "cloudflare_turnstile_widget"}, "cloudflare turnstile_widget_no_domains": {identiferType: "account", resourceType: "cloudflare_turnstile_widget", testdataFilename: "cloudflare_turnstile_widget_no_domains"}, diff --git a/internal/app/cf-terraforming/cmd/import.go b/internal/app/cf-terraforming/cmd/import.go index d02dc5236..92594e60e 100644 --- a/internal/app/cf-terraforming/cmd/import.go +++ b/internal/app/cf-terraforming/cmd/import.go @@ -40,6 +40,10 @@ var resourceImportStringFormats = map[string]string{ "cloudflare_record": ":zone_id/:id", "cloudflare_ruleset": ":identifier_type/:identifier_value/:id", "cloudflare_spectrum_application": ":zone_id/:id", + "cloudflare_teams_list": ":account_id/:id", + "cloudflare_teams_location": ":account_id/:id", + "cloudflare_teams_proxy_endpoint": ":account_id/:id", + "cloudflare_teams_rule": ":account_id/:id", "cloudflare_tunnel": ":account_id/:id", "cloudflare_turnstile_widget": ":account_id/:id", "cloudflare_waf_override": ":zone_id/:id", diff --git a/testdata/cloudflare/cloudflare_teams_list.yaml b/testdata/cloudflare/cloudflare_teams_list.yaml new file mode 100644 index 000000000..051b6e1e2 --- /dev/null +++ b/testdata/cloudflare/cloudflare_teams_list.yaml @@ -0,0 +1,80 @@ +--- +version: 1 +interactions: +- request: + body: "" + form: {} + headers: + Content-Type: + - application/json + url: https://api.cloudflare.com/client/v4/zones/f037e56e89293a057740de681ac9abbe/gateway/lists + method: GET + response: + body: | + { + "errors": [], + "messages": [], + "result": [ + { + "count": 1, + "created_at": "2014-01-01T05:20:00.12345Z", + "description": "The serial numbers for administrators", + "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + "name": "Admin Serial Numbers", + "type": "SERIAL", + "updated_at": "2014-01-01T05:20:00.12345Z" + } + ], + "success": true, + "result_info": { + "count": 1, + "page": 1, + "per_page": 20, + "total_count": 2000 + } + } + headers: + Content-Type: + - application/json + Vary: + - Accept-Encoding + status: 200 OK + code: 200 + duration: "" +- request: + body: "" + form: {} + headers: + Content-Type: + - application/json + url: https://api.cloudflare.com/client/v4/zones/f037e56e89293a057740de681ac9abbe/gateway/lists/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/items + method: GET + response: + body: | + { + "errors": [], + "messages": [], + "result": [ + [ + { + "created_at": "2014-01-01T05:20:00.12345Z", + "value": "8GE8721REF" + } + ] + ], + "success": true, + "result_info": { + "count": 1, + "page": 1, + "per_page": 20, + "total_count": 2000 + } + } + headers: + Content-Type: + - application/json + Vary: + - Accept-Encoding + status: 200 OK + code: 200 + duration: "" \ No newline at end of file diff --git a/testdata/cloudflare/cloudflare_teams_location.yaml b/testdata/cloudflare/cloudflare_teams_location.yaml new file mode 100644 index 000000000..bfec458a2 --- /dev/null +++ b/testdata/cloudflare/cloudflare_teams_location.yaml @@ -0,0 +1,49 @@ +--- +version: 1 +interactions: +- request: + body: "" + form: {} + headers: + Content-Type: + - application/json + url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/locations + method: GET + response: + body: | + { + "errors": [], + "messages": [], + "result": [ + { + "client_default": false, + "created_at": "2014-01-01T05:20:00.12345Z", + "doh_subdomain": "oli3n9zkz5", + "ecs_support": false, + "id": "ed35569b41ce4d1facfe683550f54086", + "ip": "2001:0db8:85a3:0000:0000:8a2e:0370:7334", + "name": "Austin Office Location", + "networks": [ + { + "network": "192.0.2.1/32" + } + ], + "updated_at": "2014-01-01T05:20:00.12345Z" + } + ], + "success": true, + "result_info": { + "count": 1, + "page": 1, + "per_page": 20, + "total_count": 2000 + } + } + headers: + Content-Type: + - application/json + Vary: + - Accept-Encoding + status: 200 OK + code: 200 + duration: "" diff --git a/testdata/cloudflare/cloudflare_teams_proxy_endpoint.yaml b/testdata/cloudflare/cloudflare_teams_proxy_endpoint.yaml new file mode 100644 index 000000000..d7c613c79 --- /dev/null +++ b/testdata/cloudflare/cloudflare_teams_proxy_endpoint.yaml @@ -0,0 +1,44 @@ +--- +version: 1 +interactions: +- request: + body: "" + form: {} + headers: + Content-Type: + - application/json + url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/proxy_endpoints + method: GET + response: + body: | + { + "errors": [], + "messages": [], + "result": [ + { + "created_at": "2014-01-01T05:20:00.12345Z", + "id": "ed35569b41ce4d1facfe683550f54086", + "ips": [ + "192.0.2.1/32" + ], + "name": "Devops team", + "subdomain": "oli3n9zkz5.proxy.cloudflare-gateway.com", + "updated_at": "2014-01-01T05:20:00.12345Z" + } + ], + "success": true, + "result_info": { + "count": 1, + "page": 1, + "per_page": 20, + "total_count": 2000 + } + } + headers: + Content-Type: + - application/json + Vary: + - Accept-Encoding + status: 200 OK + code: 200 + duration: "" diff --git a/testdata/cloudflare/cloudflare_teams_rule.yaml b/testdata/cloudflare/cloudflare_teams_rule.yaml new file mode 100644 index 000000000..d1d84f6ca --- /dev/null +++ b/testdata/cloudflare/cloudflare_teams_rule.yaml @@ -0,0 +1,98 @@ +--- +version: 1 +interactions: +- request: + body: "" + form: {} + headers: + Content-Type: + - application/json + url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/rules + method: GET + response: + body: | + { + "errors": [], + "messages": [], + "result": [ + { + "action": "block", + "created_at": "2014-01-01T05:20:00.12345Z", + "deleted_at": "2019-08-24T14:15:22Z", + "description": "Block bad websites based on their host name.", + "device_posture": "any(device_posture.checks.passed[*] in {\"1308749e-fcfb-4ebc-b051-fe022b632644\"})", + "enabled": true, + "filters": [ + "http" + ], + "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", + "identity": "any(identity.groups.name[*] in {\"finance\"})", + "name": "block bad websites", + "precedence": 0, + "rule_settings": { + "add_headers": { + "X-Custom-Header-Name": [ + "somecustomvalue" + ] + }, + "allow_child_bypass": false, + "audit_ssh": { + "command_logging": false + }, + "biso_admin_controls": { + "dcp": false, + "dd": false, + "dk": false, + "dp": false, + "du": false + }, + "block_page_enabled": true, + "block_reason": "This website is a security risk", + "bypass_parent_rule": false, + "check_session": { + "duration": "300s", + "enforce": true + }, + "egress": { + "ipv4": "192.0.2.2", + "ipv4_fallback": "192.0.2.3", + "ipv6": "2001:DB8::/64" + }, + "insecure_disable_dnssec_validation": false, + "ip_categories": true, + "l4override": { + "ip": "1.1.1.1", + "port": 0 + }, + "override_host": "example.com", + "override_ips": [ + "1.1.1.1", + "2.2.2.2" + ], + "payload_log": { + "enabled": true + }, + "untrusted_cert": { + "action": "error" + } + }, + "traffic": "http.request.uri matches \".*a/partial/uri.*\" and http.request.host in $01302951-49f9-47c9-a400-0297e60b6a10", + "updated_at": "2014-01-01T05:20:00.12345Z" + } + ], + "success": true, + "result_info": { + "count": 1, + "page": 1, + "per_page": 20, + "total_count": 2000 + } + } + headers: + Content-Type: + - application/json + Vary: + - Accept-Encoding + status: 200 OK + code: 200 + duration: "" diff --git a/testdata/terraform/cloudflare_teams_list/provider.tf b/testdata/terraform/cloudflare_teams_list/provider.tf new file mode 100644 index 000000000..6d4066837 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_list/provider.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + } + } +} diff --git a/testdata/terraform/cloudflare_teams_list/test.tf b/testdata/terraform/cloudflare_teams_list/test.tf new file mode 100644 index 000000000..9feac9521 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_list/test.tf @@ -0,0 +1,7 @@ +resource "cloudflare_teams_list" "terraform_managed_resource" { + account_id = "f037e56e89293a057740de681ac9abbe" + name = "Admin Serial Numbers" + type = "SERIAL" + description = "Serial numbers for all administrators." + items = ["8GE8721REF"] +} diff --git a/testdata/terraform/cloudflare_teams_location/provider.tf b/testdata/terraform/cloudflare_teams_location/provider.tf new file mode 100644 index 000000000..6d4066837 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_location/provider.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + } + } +} diff --git a/testdata/terraform/cloudflare_teams_location/test.tf b/testdata/terraform/cloudflare_teams_location/test.tf new file mode 100644 index 000000000..b49fd8476 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_location/test.tf @@ -0,0 +1,9 @@ +resource "cloudflare_teams_location" "terraform_managed_resource" { + account_id = "f037e56e89293a057740de681ac9abbe" + name = "Austin Office Location" + client_default = false + + networks { + network = "192.0.2.1/32" + } +} diff --git a/testdata/terraform/cloudflare_teams_proxy_endpoint/provider.tf b/testdata/terraform/cloudflare_teams_proxy_endpoint/provider.tf new file mode 100644 index 000000000..6d4066837 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_proxy_endpoint/provider.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + } + } +} diff --git a/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf b/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf new file mode 100644 index 000000000..a72838bc8 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf @@ -0,0 +1,5 @@ +resource "cloudflare_teams_proxy_endpoint" "terraform_managed_resource" { + account_id = "f037e56e89293a057740de681ac9abbe" + name = "Devops team" + ips = ["192.0.2.1/32"] +} diff --git a/testdata/terraform/cloudflare_teams_rule/provider.tf b/testdata/terraform/cloudflare_teams_rule/provider.tf new file mode 100644 index 000000000..6d4066837 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_rule/provider.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + } + } +} diff --git a/testdata/terraform/cloudflare_teams_rule/test.tf b/testdata/terraform/cloudflare_teams_rule/test.tf new file mode 100644 index 000000000..f0c3f8842 --- /dev/null +++ b/testdata/terraform/cloudflare_teams_rule/test.tf @@ -0,0 +1,53 @@ +resource "cloudflare_teams_rule" "terraform_managed_resource" { + account_id = "f037e56e89293a057740de681ac9abbe" + name = "block bad websites" + description = "Block bad websites based on their host name." + precedence = 0 + action = "block" + filters = ["http"] + device_posture = "any(device_posture.checks.passed[*] in {\"1308749e-fcfb-4ebc-b051-fe022b632644\"})" + identity = "any(identity.groups.name[*] in {\"finance\"})" + traffic = "http.request.uri matches \".*a/partial/uri.*\" and http.request.host in $01302951-49f9-47c9-a400-0297e60b6a10" + rule_settings { + add_headers = { + "x-Custom-Header-Name" : "somecustomvalue" + } + allow_child_bypass = false + audit_ssh { + command_logging = false + } + biso_admin_controls { + disable_copy_paste = false + disable_download = false + disable_keyboard = false + disable_printing = false + disable_upload = false + } + block_page_enabled = true + block_page_reason = "This website is a security risk" + bypass_parent_rule = true + check_session { + duration ="300s" + enforce = true + } + egress { + ipv4 = "192.0.2.2" + ipv6 = "2001:DB8::/64" + ipv4_fallback = "192.0.2.3" + } + insecure_disable_dnssec_validation = false + ip_categories = true + l4override { + ip = "1.1.1.1" + port = 0 + } + override_host = "example.com" + override_ips = ["1.1.1.1", "2.2.2.2"] + payload_log { + enabled = true + } + untrusted_cert { + action = "error" + } + } +} \ No newline at end of file From 411b8f4a5585faae06c6298247a07680f079b219 Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Thu, 11 Jan 2024 21:38:03 -0600 Subject: [PATCH 02/13] adding imports for objects still working on getting list working --- internal/app/cf-terraforming/cmd/generate.go | 23 +++++++++ internal/app/cf-terraforming/cmd/import.go | 44 +++++++++++++++++ .../cloudflare/cloudflare_teams_list.yaml | 4 +- .../cloudflare/cloudflare_teams_rule.yaml | 4 +- .../cloudflare_teams_location/test.tf | 3 +- .../cloudflare_teams_proxy_endpoint/test.tf | 2 +- .../terraform/cloudflare_teams_rule/test.tf | 49 ++++++++----------- 7 files changed, 93 insertions(+), 36 deletions(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index e5ab53f09..b6b99f02b 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -890,6 +890,22 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } + // add items field for each list + for i := 0; i < resourceCount; i++ { + items_struct, _, err := api.ListTeamsListItems( + context.Background(), + &cloudflare.ResourceContainer{Identifier: accountID}, + cloudflare.ListTeamsListItemsParams{ListID: jsonStructData[i].(map[string]interface{})["id"].(string)}) + if err != nil { + log.Fatal(err) + } + // convert to slice of strings + var items []string + for _, item_struct := range items_struct { + items = append(items, item_struct.Value) + } + jsonStructData[i].(map[string]interface{})["items"] = items + } case "cloudflare_teams_location": jsonPayload, _, err := api.TeamsLocations(context.Background(), accountID) if err != nil { @@ -923,6 +939,13 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } + // check for empty descriptions + for i := 0; i < resourceCount; i++ { + if jsonStructData[i].(map[string]interface{})["description"] == "" { + jsonStructData[i].(map[string]interface{})["description"] = "default" + } + + } case "cloudflare_tunnel": log.Debug("only requesting the first 1000 active Cloudflare Tunnels due to the service not providing correct pagination responses") jsonPayload, _, err := api.ListTunnels( diff --git a/internal/app/cf-terraforming/cmd/import.go b/internal/app/cf-terraforming/cmd/import.go index 92594e60e..7e7151ae4 100644 --- a/internal/app/cf-terraforming/cmd/import.go +++ b/internal/app/cf-terraforming/cmd/import.go @@ -372,6 +372,50 @@ func runImport() func(cmd *cobra.Command, args []string) { log.Fatal(err) } + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_list": + jsonPayload, _, err := api.ListTeamsLists(context.Background(), &cloudflare.ResourceContainer{Identifier: accountID}, cloudflare.ListTeamListsParams{}) + if err != nil { + log.Fatal(err) + } + + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_location": + jsonPayload, _, err := api.TeamsLocations(context.Background(), accountID) + if err != nil { + log.Fatal(err) + } + + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_proxy_endpoint": + jsonPayload, _, err := api.TeamsProxyEndpoints(context.Background(), accountID) + if err != nil { + log.Fatal(err) + } + + m, _ := json.Marshal(jsonPayload) + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) + } + case "cloudflare_teams_rule": + jsonPayload, err := api.TeamsRules(context.Background(), accountID) + if err != nil { + log.Fatal(err) + } + m, _ := json.Marshal(jsonPayload) err = json.Unmarshal(m, &jsonStructData) if err != nil { diff --git a/testdata/cloudflare/cloudflare_teams_list.yaml b/testdata/cloudflare/cloudflare_teams_list.yaml index 051b6e1e2..a5853aa23 100644 --- a/testdata/cloudflare/cloudflare_teams_list.yaml +++ b/testdata/cloudflare/cloudflare_teams_list.yaml @@ -7,7 +7,7 @@ interactions: headers: Content-Type: - application/json - url: https://api.cloudflare.com/client/v4/zones/f037e56e89293a057740de681ac9abbe/gateway/lists + url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/lists method: GET response: body: | @@ -47,7 +47,7 @@ interactions: headers: Content-Type: - application/json - url: https://api.cloudflare.com/client/v4/zones/f037e56e89293a057740de681ac9abbe/gateway/lists/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/items + url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/lists/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/items method: GET response: body: | diff --git a/testdata/cloudflare/cloudflare_teams_rule.yaml b/testdata/cloudflare/cloudflare_teams_rule.yaml index d1d84f6ca..67532e98e 100644 --- a/testdata/cloudflare/cloudflare_teams_rule.yaml +++ b/testdata/cloudflare/cloudflare_teams_rule.yaml @@ -61,8 +61,8 @@ interactions: "insecure_disable_dnssec_validation": false, "ip_categories": true, "l4override": { - "ip": "1.1.1.1", - "port": 0 + "ip": "1.1.1.1", + "port": 53 }, "override_host": "example.com", "override_ips": [ diff --git a/testdata/terraform/cloudflare_teams_location/test.tf b/testdata/terraform/cloudflare_teams_location/test.tf index b49fd8476..3ce993f6c 100644 --- a/testdata/terraform/cloudflare_teams_location/test.tf +++ b/testdata/terraform/cloudflare_teams_location/test.tf @@ -1,8 +1,7 @@ resource "cloudflare_teams_location" "terraform_managed_resource" { account_id = "f037e56e89293a057740de681ac9abbe" - name = "Austin Office Location" client_default = false - + name = "Austin Office Location" networks { network = "192.0.2.1/32" } diff --git a/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf b/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf index a72838bc8..e44abb212 100644 --- a/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf +++ b/testdata/terraform/cloudflare_teams_proxy_endpoint/test.tf @@ -1,5 +1,5 @@ resource "cloudflare_teams_proxy_endpoint" "terraform_managed_resource" { account_id = "f037e56e89293a057740de681ac9abbe" - name = "Devops team" ips = ["192.0.2.1/32"] + name = "Devops team" } diff --git a/testdata/terraform/cloudflare_teams_rule/test.tf b/testdata/terraform/cloudflare_teams_rule/test.tf index f0c3f8842..9ed78702e 100644 --- a/testdata/terraform/cloudflare_teams_rule/test.tf +++ b/testdata/terraform/cloudflare_teams_rule/test.tf @@ -1,48 +1,39 @@ resource "cloudflare_teams_rule" "terraform_managed_resource" { - account_id = "f037e56e89293a057740de681ac9abbe" - name = "block bad websites" - description = "Block bad websites based on their host name." - precedence = 0 - action = "block" - filters = ["http"] + account_id = "f037e56e89293a057740de681ac9abbe" + action = "block" + description = "Block bad websites based on their host name." device_posture = "any(device_posture.checks.passed[*] in {\"1308749e-fcfb-4ebc-b051-fe022b632644\"})" - identity = "any(identity.groups.name[*] in {\"finance\"})" - traffic = "http.request.uri matches \".*a/partial/uri.*\" and http.request.host in $01302951-49f9-47c9-a400-0297e60b6a10" - rule_settings { - add_headers = { - "x-Custom-Header-Name" : "somecustomvalue" - } + enabled = true + filters = ["http"] + identity = "any(identity.groups.name[*] in {\"finance\"})" + name = "block bad websites" + precedence = 0 + traffic = "http.request.uri matches \".*a/partial/uri.*\" and http.request.host in $01302951-49f9-47c9-a400-0297e60b6a10" + rule_settings { + add_headers = {} allow_child_bypass = false audit_ssh { command_logging = false } - biso_admin_controls { - disable_copy_paste = false - disable_download = false - disable_keyboard = false - disable_printing = false - disable_upload = false - } block_page_enabled = true - block_page_reason = "This website is a security risk" - bypass_parent_rule = true + bypass_parent_rule = false check_session { - duration ="300s" - enforce = true + duration = "5m0s" + enforce = true } egress { - ipv4 = "192.0.2.2" - ipv6 = "2001:DB8::/64" + ipv4 = "192.0.2.2" ipv4_fallback = "192.0.2.3" + ipv6 = "2001:DB8::/64" } insecure_disable_dnssec_validation = false - ip_categories = true + ip_categories = true l4override { - ip = "1.1.1.1" - port = 0 + ip = "1.1.1.1" + port = 53 } override_host = "example.com" - override_ips = ["1.1.1.1", "2.2.2.2"] + override_ips = ["1.1.1.1", "2.2.2.2"] payload_log { enabled = true } From bc25d16e494c30aaccd3e04ce9c0daea2cb660ef Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Thu, 11 Jan 2024 22:36:15 -0600 Subject: [PATCH 03/13] fixed the test and completed the cloudflare_teams_list resources --- internal/app/cf-terraforming/cmd/generate.go | 62 ++++++++++++++----- .../cloudflare/cloudflare_teams_list.yaml | 31 +++++----- .../terraform/cloudflare_teams_list/test.tf | 8 +-- 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index b6b99f02b..d6af0b797 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -8,6 +8,7 @@ import ( "os" "sort" "strings" + "time" cloudflare "github.com/cloudflare/cloudflare-go" "github.com/hashicorp/go-version" @@ -884,28 +885,61 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - resourceCount = len(jsonPayload) - m, _ := json.Marshal(jsonPayload) - err = json.Unmarshal(m, &jsonStructData) - if err != nil { - log.Fatal(err) + // creating our own listItems struct because Items nees to be list of string to match + // terraform resource defenition for TeamsList + var tfTeamsList []struct{ + ID string `json:"id,omitempty"` + Name string `json:"name"` + Type string `json:"type"` + Description string `json:"description,omitempty"` + Items []string `json:"items,omitempty"` + Count uint64 `json:"count,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + UpdatedAt *time.Time `json:"updated_at,omitempty"` } - // add items field for each list - for i := 0; i < resourceCount; i++ { + // get items and set them to the object in the struct + for _, cfList := range jsonPayload { items_struct, _, err := api.ListTeamsListItems( context.Background(), - &cloudflare.ResourceContainer{Identifier: accountID}, - cloudflare.ListTeamsListItemsParams{ListID: jsonStructData[i].(map[string]interface{})["id"].(string)}) + cloudflare.AccountIdentifier(accountID), + cloudflare.ListTeamsListItemsParams{ListID: cfList.ID }) if err != nil { log.Fatal(err) } - // convert to slice of strings - var items []string - for _, item_struct := range items_struct { - items = append(items, item_struct.Value) + // turn items into slice of strings + var strItems []string + for _, item := range items_struct { + strItems = append(strItems, item.Value) } - jsonStructData[i].(map[string]interface{})["items"] = items + tfTeamsList = append(tfTeamsList, struct { + ID string "json:\"id,omitempty\"" + Name string "json:\"name\"" + Type string "json:\"type\"" + Description string "json:\"description,omitempty\"" + Items []string "json:\"items,omitempty\"" + Count uint64 "json:\"count,omitempty\"" + CreatedAt *time.Time "json:\"created_at,omitempty\"" + UpdatedAt *time.Time "json:\"updated_at,omitempty\"" + }{ + ID: cfList.ID, + Name: cfList.Name, + Type: cfList.Type, + Description: cfList.Description, + Items: strItems, + Count: cfList.Count, + CreatedAt: cfList.CreatedAt, + UpdatedAt: cfList.UpdatedAt, + }) + } + m, err := json.Marshal(tfTeamsList) + if err != nil { + log.Fatal(err) + } + err = json.Unmarshal(m, &jsonStructData) + if err != nil { + log.Fatal(err) } + resourceCount = len(tfTeamsList) case "cloudflare_teams_location": jsonPayload, _, err := api.TeamsLocations(context.Background(), accountID) if err != nil { diff --git a/testdata/cloudflare/cloudflare_teams_list.yaml b/testdata/cloudflare/cloudflare_teams_list.yaml index a5853aa23..fd20eb98c 100644 --- a/testdata/cloudflare/cloudflare_teams_list.yaml +++ b/testdata/cloudflare/cloudflare_teams_list.yaml @@ -18,10 +18,10 @@ interactions: { "count": 1, "created_at": "2014-01-01T05:20:00.12345Z", - "description": "The serial numbers for administrators", - "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415", - "name": "Admin Serial Numbers", - "type": "SERIAL", + "description": "we like domains here", + "id": "971fc4e8-388e-4ab9-b377-16430c0fc018", + "name": "STUFF TO DO WITH DOMAINS", + "type": "DOMAIN", "updated_at": "2014-01-01T05:20:00.12345Z" } ], @@ -47,27 +47,26 @@ interactions: headers: Content-Type: - application/json - url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/lists/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/items + url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/lists/971fc4e8-388e-4ab9-b377-16430c0fc018/items?page=1&per_page=50 method: GET response: body: | { - "errors": [], - "messages": [], "result": [ - [ - { - "created_at": "2014-01-01T05:20:00.12345Z", - "value": "8GE8721REF" - } - ] + { + "value": "cloudfront.net", + "created_at": "2024-01-11T22:57:26Z" + } ], "success": true, + "errors": [], + "messages": [], "result_info": { - "count": 1, "page": 1, - "per_page": 20, - "total_count": 2000 + "per_page": 50, + "count": 15, + "total_count": 15, + "total_pages": 1 } } headers: diff --git a/testdata/terraform/cloudflare_teams_list/test.tf b/testdata/terraform/cloudflare_teams_list/test.tf index 9feac9521..edb283726 100644 --- a/testdata/terraform/cloudflare_teams_list/test.tf +++ b/testdata/terraform/cloudflare_teams_list/test.tf @@ -1,7 +1,7 @@ resource "cloudflare_teams_list" "terraform_managed_resource" { account_id = "f037e56e89293a057740de681ac9abbe" - name = "Admin Serial Numbers" - type = "SERIAL" - description = "Serial numbers for all administrators." - items = ["8GE8721REF"] + description = "we like domains here" + items = ["cloudfront.net"] + name = "STUFF TO DO WITH DOMAINS" + type = "DOMAIN" } From 88e95cbebc3e439fd6b9f8ab4510c85fa4160e0b Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Thu, 11 Jan 2024 22:48:56 -0600 Subject: [PATCH 04/13] adding additional local testing information I found useful --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index c0e2e6abf..c5ebc7fed 100644 --- a/README.md +++ b/README.md @@ -279,6 +279,41 @@ test. The Terraform files then allow us to build what the resource structure is expected to look like and once the tool parses the API response, we can compare that to the static file. +Suggested local testing steps: + +1. Create a main file (do not commit this up) + + ```bash + cat > main.tf < Date: Tue, 16 Jan 2024 11:57:59 -0700 Subject: [PATCH 05/13] Update README.md to be more clear Co-authored-by: Jacob Bednarz --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c5ebc7fed..2339d5ff3 100644 --- a/README.md +++ b/README.md @@ -281,7 +281,7 @@ that to the static file. Suggested local testing steps: -1. Create a main file (do not commit this up) +1. Create a file with the basic provider configuration (do not commit this file) ```bash cat > main.tf < Date: Tue, 16 Jan 2024 11:58:58 -0700 Subject: [PATCH 06/13] Update internal/app/cf-terraforming/cmd/generate.go to use reusable identifier Co-authored-by: Jacob Bednarz --- internal/app/cf-terraforming/cmd/generate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index d6af0b797..559ce2149 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -881,7 +881,7 @@ func generateResources() func(cmd *cobra.Command, args []string) { log.Fatal(err) } case "cloudflare_teams_list": - jsonPayload, _, err := api.ListTeamsLists(context.Background(), &cloudflare.ResourceContainer{Identifier: accountID}, cloudflare.ListTeamListsParams{}) + jsonPayload, _, err := api.ListTeamsLists(context.Background(), identifier, cloudflare.ListTeamListsParams{}) if err != nil { log.Fatal(err) } From 667dd91a8f215f0351e474ba5656e5d78e5b02e4 Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Tue, 16 Jan 2024 11:59:15 -0700 Subject: [PATCH 07/13] Update internal/app/cf-terraforming/cmd/import.go reuse identifier Co-authored-by: Jacob Bednarz --- internal/app/cf-terraforming/cmd/import.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/cf-terraforming/cmd/import.go b/internal/app/cf-terraforming/cmd/import.go index 24c998d8f..52197d673 100644 --- a/internal/app/cf-terraforming/cmd/import.go +++ b/internal/app/cf-terraforming/cmd/import.go @@ -378,7 +378,7 @@ func runImport() func(cmd *cobra.Command, args []string) { log.Fatal(err) } case "cloudflare_teams_list": - jsonPayload, _, err := api.ListTeamsLists(context.Background(), &cloudflare.ResourceContainer{Identifier: accountID}, cloudflare.ListTeamListsParams{}) + jsonPayload, _, err := api.ListTeamsLists(context.Background(), identifier, cloudflare.ListTeamListsParams{}) if err != nil { log.Fatal(err) } From 31778f094742621654c33a6ed953913f506e19c5 Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Tue, 16 Jan 2024 14:17:59 -0700 Subject: [PATCH 08/13] Fixed indentation of code blocks in README.md and instructions for testing. Also added path for testing to make file --- .gitignore | 5 ++++- Makefile | 2 ++ README.md | 40 ++++++++++++++++++++-------------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index d7671e84e..f6a42225f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ .idea/ *.terraform -*.terraform.lock.hcl \ No newline at end of file +*.terraform.lock.hcl + +# ignore for testing locally +main.tf \ No newline at end of file diff --git a/Makefile b/Makefile index 6090fc792..0c0a6f0a4 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ CLOUDFLARE_ZONE_ID ?= 00deadb33f000000000000000000000000000 CLOUDFLARE_ACCOUNT_ID ?= 00deadb33f000000000000000000000000000 VERSION ?= $$(git describe --tags --abbrev=0)-dev+$$(git rev-parse --short=12 HEAD) ROOT_DIR = $$PWD +CLOUDFLARE_TERRAFORM_INSTALL_PATH=$$PWD HASHICORP_CHECKPOINT_TIMEMOUT ?= 30000 @@ -20,6 +21,7 @@ test: @CI=true \ USE_STATIC_RESOURCE_IDS=true \ CHECKPOINT_TIMEOUT=$(HASHICORP_CHECKPOINT_TIMEMOUT) \ + CLOUDFLARE_TERRAFORM_INSTALL_PATH=$(CLOUDFLARE_TERRAFORM_INSTALL_PATH) \ CLOUDFLARE_EMAIL="$(CLOUDFLARE_EMAIL)" \ CLOUDFLARE_API_KEY="$(CLOUDFLARE_API_KEY)" \ CLOUDFLARE_ZONE_ID="$(CLOUDFLARE_ZONE_ID)" \ diff --git a/README.md b/README.md index 2339d5ff3..9665aaacf 100644 --- a/README.md +++ b/README.md @@ -283,30 +283,30 @@ Suggested local testing steps: 1. Create a file with the basic provider configuration (do not commit this file) - ```bash - cat > main.tf < main.tf < Date: Tue, 16 Jan 2024 14:22:17 -0700 Subject: [PATCH 09/13] Updating that the env var isn't needed for testing (included in make file) --- README.md | 4 ++-- internal/app/cf-terraforming/cmd/generate.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9665aaacf..1edd8e30d 100644 --- a/README.md +++ b/README.md @@ -305,13 +305,13 @@ terraform init 3. Run tests (Cloudflare Install path should be path to repository) ```bash -CLOUDFLARE_TERRAFORM_INSTALL_PATH=~/gh/cf-terraforming make test +make test ``` If you want to run a specific test case you can do so with the TESTARGS variable and -run flag ```bash -CLOUDFLARE_TERRAFORM_INSTALL_PATH=~/gh/cf-terraforming TESTARGS="-run '^TestResourceGeneration/cloudflare_teams_list'" make test +TESTARGS="-run '^TestResourceGeneration/cloudflare_teams_list'" make test ``` ## Updating VCR cassettes diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index 559ce2149..218a76598 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -901,7 +901,7 @@ func generateResources() func(cmd *cobra.Command, args []string) { for _, cfList := range jsonPayload { items_struct, _, err := api.ListTeamsListItems( context.Background(), - cloudflare.AccountIdentifier(accountID), + identifier, cloudflare.ListTeamsListItemsParams{ListID: cfList.ID }) if err != nil { log.Fatal(err) From 3abeffb9465eeffebcc2e05c599c77280243dd0b Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Tue, 16 Jan 2024 14:23:58 -0700 Subject: [PATCH 10/13] go format --- internal/app/cf-terraforming/cmd/generate.go | 32 ++++++++++---------- internal/app/cf-terraforming/cmd/import.go | 6 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index 218a76598..2b348b116 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -885,24 +885,24 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - // creating our own listItems struct because Items nees to be list of string to match + // creating our own listItems struct because Items nees to be list of string to match // terraform resource defenition for TeamsList - var tfTeamsList []struct{ - ID string `json:"id,omitempty"` - Name string `json:"name"` - Type string `json:"type"` - Description string `json:"description,omitempty"` - Items []string `json:"items,omitempty"` - Count uint64 `json:"count,omitempty"` - CreatedAt *time.Time `json:"created_at,omitempty"` - UpdatedAt *time.Time `json:"updated_at,omitempty"` + var tfTeamsList []struct { + ID string `json:"id,omitempty"` + Name string `json:"name"` + Type string `json:"type"` + Description string `json:"description,omitempty"` + Items []string `json:"items,omitempty"` + Count uint64 `json:"count,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + UpdatedAt *time.Time `json:"updated_at,omitempty"` } // get items and set them to the object in the struct for _, cfList := range jsonPayload { items_struct, _, err := api.ListTeamsListItems( - context.Background(), - identifier, - cloudflare.ListTeamsListItemsParams{ListID: cfList.ID }) + context.Background(), + identifier, + cloudflare.ListTeamsListItemsParams{ListID: cfList.ID}) if err != nil { log.Fatal(err) } @@ -939,7 +939,7 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - resourceCount = len(tfTeamsList) + resourceCount = len(tfTeamsList) case "cloudflare_teams_location": jsonPayload, _, err := api.TeamsLocations(context.Background(), accountID) if err != nil { @@ -978,8 +978,8 @@ func generateResources() func(cmd *cobra.Command, args []string) { if jsonStructData[i].(map[string]interface{})["description"] == "" { jsonStructData[i].(map[string]interface{})["description"] = "default" } - - } + + } case "cloudflare_tunnel": log.Debug("only requesting the first 1000 active Cloudflare Tunnels due to the service not providing correct pagination responses") jsonPayload, _, err := api.ListTunnels( diff --git a/internal/app/cf-terraforming/cmd/import.go b/internal/app/cf-terraforming/cmd/import.go index 52197d673..2c824349e 100644 --- a/internal/app/cf-terraforming/cmd/import.go +++ b/internal/app/cf-terraforming/cmd/import.go @@ -382,7 +382,7 @@ func runImport() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - + m, _ := json.Marshal(jsonPayload) err = json.Unmarshal(m, &jsonStructData) if err != nil { @@ -393,7 +393,7 @@ func runImport() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - + m, _ := json.Marshal(jsonPayload) err = json.Unmarshal(m, &jsonStructData) if err != nil { @@ -404,7 +404,7 @@ func runImport() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - + m, _ := json.Marshal(jsonPayload) err = json.Unmarshal(m, &jsonStructData) if err != nil { From c11cbc968b4d8c15d4e1b0633ed22e42f24221b4 Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Tue, 16 Jan 2024 14:25:26 -0700 Subject: [PATCH 11/13] Fixed all extra non-need identifier creations to use the main one --- internal/app/cf-terraforming/cmd/generate.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index 2b348b116..ac5409edd 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -984,7 +984,7 @@ func generateResources() func(cmd *cobra.Command, args []string) { log.Debug("only requesting the first 1000 active Cloudflare Tunnels due to the service not providing correct pagination responses") jsonPayload, _, err := api.ListTunnels( context.Background(), - cloudflare.AccountIdentifier(accountID), + identifier, cloudflare.TunnelListParams{ IsDeleted: cloudflare.BoolPtr(false), ResultInfo: cloudflare.ResultInfo{ @@ -1006,7 +1006,7 @@ func generateResources() func(cmd *cobra.Command, args []string) { for i := 0; i < resourceCount; i++ { secret, err := api.GetTunnelToken( context.Background(), - cloudflare.AccountIdentifier(accountID), + identifier, jsonStructData[i].(map[string]interface{})["id"].(string), ) if err != nil { From f238937e871c0d23be3fc845b44012bc3661241f Mon Sep 17 00:00:00 2001 From: Devin Ferguson Date: Tue, 16 Jan 2024 18:24:21 -0700 Subject: [PATCH 12/13] Updated to make the remapping of the object similar to other remap and updated test to example.com --- internal/app/cf-terraforming/cmd/generate.go | 61 ++++++------------- .../cloudflare/cloudflare_teams_list.yaml | 2 +- .../terraform/cloudflare_teams_list/test.tf | 2 +- 3 files changed, 21 insertions(+), 44 deletions(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index ac5409edd..7c929642a 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -8,7 +8,6 @@ import ( "os" "sort" "strings" - "time" cloudflare "github.com/cloudflare/cloudflare-go" "github.com/hashicorp/go-version" @@ -885,53 +884,19 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - // creating our own listItems struct because Items nees to be list of string to match - // terraform resource defenition for TeamsList - var tfTeamsList []struct { - ID string `json:"id,omitempty"` - Name string `json:"name"` - Type string `json:"type"` - Description string `json:"description,omitempty"` - Items []string `json:"items,omitempty"` - Count uint64 `json:"count,omitempty"` - CreatedAt *time.Time `json:"created_at,omitempty"` - UpdatedAt *time.Time `json:"updated_at,omitempty"` - } - // get items and set them to the object in the struct - for _, cfList := range jsonPayload { + // get items for the lists and add it the specific list struct + for i, TeamsList := range jsonPayload { items_struct, _, err := api.ListTeamsListItems( context.Background(), identifier, - cloudflare.ListTeamsListItemsParams{ListID: cfList.ID}) + cloudflare.ListTeamsListItemsParams{ListID: TeamsList.ID}) if err != nil { log.Fatal(err) } - // turn items into slice of strings - var strItems []string - for _, item := range items_struct { - strItems = append(strItems, item.Value) - } - tfTeamsList = append(tfTeamsList, struct { - ID string "json:\"id,omitempty\"" - Name string "json:\"name\"" - Type string "json:\"type\"" - Description string "json:\"description,omitempty\"" - Items []string "json:\"items,omitempty\"" - Count uint64 "json:\"count,omitempty\"" - CreatedAt *time.Time "json:\"created_at,omitempty\"" - UpdatedAt *time.Time "json:\"updated_at,omitempty\"" - }{ - ID: cfList.ID, - Name: cfList.Name, - Type: cfList.Type, - Description: cfList.Description, - Items: strItems, - Count: cfList.Count, - CreatedAt: cfList.CreatedAt, - UpdatedAt: cfList.UpdatedAt, - }) + TeamsList.Items = append(TeamsList.Items, items_struct...) + jsonPayload[i] = TeamsList } - m, err := json.Marshal(tfTeamsList) + m, err := json.Marshal(jsonPayload) if err != nil { log.Fatal(err) } @@ -939,7 +904,19 @@ func generateResources() func(cmd *cobra.Command, args []string) { if err != nil { log.Fatal(err) } - resourceCount = len(tfTeamsList) + resourceCount = len(jsonPayload) + + // converting the items to value field and not the otherway around + for i := 0; i < resourceCount; i++ { + if jsonStructData[i].(map[string]interface{})["items"] != nil && len(jsonStructData[i].(map[string]interface{})["items"].([]interface{})) > 0 { + // new interface for storing data + var newItems []interface{} + for _, item := range jsonStructData[i].(map[string]interface{})["items"].([]interface{}) { + newItems = append(newItems, item.(map[string]interface{})["value"]) + } + jsonStructData[i].(map[string]interface{})["items"] = newItems + } + } case "cloudflare_teams_location": jsonPayload, _, err := api.TeamsLocations(context.Background(), accountID) if err != nil { diff --git a/testdata/cloudflare/cloudflare_teams_list.yaml b/testdata/cloudflare/cloudflare_teams_list.yaml index fd20eb98c..c8f914c59 100644 --- a/testdata/cloudflare/cloudflare_teams_list.yaml +++ b/testdata/cloudflare/cloudflare_teams_list.yaml @@ -54,7 +54,7 @@ interactions: { "result": [ { - "value": "cloudfront.net", + "value": "example.com", "created_at": "2024-01-11T22:57:26Z" } ], diff --git a/testdata/terraform/cloudflare_teams_list/test.tf b/testdata/terraform/cloudflare_teams_list/test.tf index edb283726..98378952b 100644 --- a/testdata/terraform/cloudflare_teams_list/test.tf +++ b/testdata/terraform/cloudflare_teams_list/test.tf @@ -1,7 +1,7 @@ resource "cloudflare_teams_list" "terraform_managed_resource" { account_id = "f037e56e89293a057740de681ac9abbe" description = "we like domains here" - items = ["cloudfront.net"] + items = ["example.com"] name = "STUFF TO DO WITH DOMAINS" type = "DOMAIN" } From 7ecd28fd6d39c53d6be177baf1c104896b799768 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Wed, 17 Jan 2024 14:51:28 +1100 Subject: [PATCH 13/13] remove extra newline --- internal/app/cf-terraforming/cmd/generate.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/app/cf-terraforming/cmd/generate.go b/internal/app/cf-terraforming/cmd/generate.go index 7c929642a..44ccf67ce 100644 --- a/internal/app/cf-terraforming/cmd/generate.go +++ b/internal/app/cf-terraforming/cmd/generate.go @@ -955,7 +955,6 @@ func generateResources() func(cmd *cobra.Command, args []string) { if jsonStructData[i].(map[string]interface{})["description"] == "" { jsonStructData[i].(map[string]interface{})["description"] = "default" } - } case "cloudflare_tunnel": log.Debug("only requesting the first 1000 active Cloudflare Tunnels due to the service not providing correct pagination responses")