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

Adding Support for 4 new cloudflare_teams_* resources and additional README.md Improvements #669

Merged
merged 14 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
.idea/
.idea/

*.terraform
*.terraform.lock.hcl
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ✅ | ❌ |
Expand Down Expand Up @@ -275,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)
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved

```bash
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
cat > main.tf <<EOF
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4"
}
}
}
EOF
```

2. Initialize terraform

```bash
terraform init
```

3. Run tests (Cloudflare Install path should be path to repository)
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved

```bash
CLOUDFLARE_TERRAFORM_INSTALL_PATH=~/gh/cf-terraforming 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
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
```

## Updating VCR cassettes

Periodically, it is a good idea to recreate the VCR cassettes used in our
Expand Down
101 changes: 101 additions & 0 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"os"
"sort"
"strings"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -879,6 +880,106 @@
if err != nil {
log.Fatal(err)
}
case "cloudflare_teams_list":
jsonPayload, _, err := api.ListTeamsLists(context.Background(), &cloudflare.ResourceContainer{Identifier: accountID}, cloudflare.ListTeamListsParams{})
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatal(err)
}
// creating our own listItems struct because Items nees to be list of string to match

Check failure on line 888 in internal/app/cf-terraforming/cmd/generate.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
// 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 {
items_struct, _, err := api.ListTeamsListItems(
context.Background(),
cloudflare.AccountIdentifier(accountID),
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
cloudflare.ListTeamsListItemsParams{ListID: cfList.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,
})
}
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 {
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)
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(
Expand Down
4 changes: 4 additions & 0 deletions internal/app/cf-terraforming/cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
48 changes: 48 additions & 0 deletions internal/app/cf-terraforming/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"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",
Expand Down Expand Up @@ -368,6 +372,50 @@
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{})
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatal(err)
}

Check failure on line 385 in internal/app/cf-terraforming/cmd/import.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
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 {
Expand Down
79 changes: 79 additions & 0 deletions testdata/cloudflare/cloudflare_teams_list.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Content-Type:
- application/json
url: https://api.cloudflare.com/client/v4/accounts/f037e56e89293a057740de681ac9abbe/gateway/lists
method: GET
response:
body: |
{
"errors": [],
"messages": [],
"result": [
{
"count": 1,
"created_at": "2014-01-01T05:20:00.12345Z",
"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"
}
],
"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/accounts/f037e56e89293a057740de681ac9abbe/gateway/lists/971fc4e8-388e-4ab9-b377-16430c0fc018/items?page=1&per_page=50
method: GET
response:
body: |
{
"result": [
{
"value": "cloudfront.net",
devinbfergy marked this conversation as resolved.
Show resolved Hide resolved
"created_at": "2024-01-11T22:57:26Z"
}
],
"success": true,
"errors": [],
"messages": [],
"result_info": {
"page": 1,
"per_page": 50,
"count": 15,
"total_count": 15,
"total_pages": 1
}
}
headers:
Content-Type:
- application/json
Vary:
- Accept-Encoding
status: 200 OK
code: 200
duration: ""
49 changes: 49 additions & 0 deletions testdata/cloudflare/cloudflare_teams_location.yaml
Original file line number Diff line number Diff line change
@@ -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: ""
Loading
Loading