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 terraform resource and data source for Infrastructure Access Target #4077

Merged

Conversation

SaiDadireddy
Copy link
Contributor

@SaiDadireddy SaiDadireddy commented Sep 19, 2024

Adds the resource and data source that we need to CRUDL infrastructure access target resources using the cloudflare/cloudflare terraform provider.
Our OpenAPI schema is published in cloudflare/api-schemas viewable in this commit
Our API docs are accessible in production at https://developers.cloudflare.com/api/operations/infra-targets-list

This PR is dependent on cloudflare-go client changes found in this PR -> cloudflare/cloudflare-go#3184

New changes:

  • New resource cloudflare_infrastructure_access_target
  • New data source cloudflare_infrastructure_access_targets
  • Added schema and acceptance tests for both the resource and data source

Manual test cases covered for resources:

  • Creating a resource with ipv4 configuration, no ipv6.
  • Updating the resource to have a different ipv4. Then updating once more to add an ipv6 configuration.
  • Destroying the resource and cleaning up terraform state once deleted

Manual test cases covered for data sources:

  • Created data source to read all targets
  • Added new target and refreshed which updated data source
  • Tested creating data source with query parameters

Manual testing setup:

  1. Checkout sdadireddy/infrastructure-access branch in cloudflare-go
  2. Replace the cloudflare-go import in In terraform-provider-cloudflare by adding replace github.com/cloudflare/cloudflare-go => ../cloudflare-go
    Replace the cloudflare-go in go.mod
  3. In terraform-provider-cloudflare, run make build-dev build a new binary with the new changes
  4. Create a test repository and configure your ~/.terraformrc to add a development override that points terraform to the directory with the compiled binary (should just be the root of terraform-provider-cloudflare
  5. Create a main.tf file to create a new resource as such:
terraform {
    required_providers {
        cloudflare = {
            source = "cloudflare/cloudflare"
        }
    }
}

provider "cloudflare" {
    api_token = "" // create api token in dash.cloudflare
}

resource "cloudflare_infrastructure_access_target" "sai-tftest-target" {
    account_id = "" // account id can be found in dash.cloudflare
    hostname   = "sai-tf-test"
    ip {
	ipv4 = {
              ip_addr = "250.26.29.250"
              virtual_network_id = "01920a8c-dc14-7bb2-b67b-14c858494a54"
	}
    }
}
  1. Run terraform init and TF_LOG=DEBUG terraform plan to see the proposed new changes
  2. Run TF_LOG=DEBUG terraform apply and create the target
  3. Query the target via public api

go.mod Outdated Show resolved Hide resolved
@SaiDadireddy SaiDadireddy changed the title Add terraform resource and data source for infrastructure target Add terraform resource and data source for infrastructure access target Sep 23, 2024
@SaiDadireddy SaiDadireddy changed the title Add terraform resource and data source for infrastructure access target Add terraform resource and data source for Infrastructure Access Target Sep 23, 2024
@SaiDadireddy SaiDadireddy force-pushed the sdadireddy/infrastructure-access branch from 69b2cce to 0a7c223 Compare September 24, 2024 20:01
Copy link
Contributor

github-actions bot commented Sep 25, 2024

changelog detected ✅

resource.TestCheckResourceAttr("data.cloudflare_infrastructure_access_targets.all", "targets.1.ip.ipv4.ip_addr", "187.26.29.249"),
resource.TestCheckResourceAttr("data.cloudflare_infrastructure_access_targets.all", "targets.1.ip.ipv4.virtual_network_id", "b9c90134-52de-4903-81e8-004a3a06b435"),
resource.TestCheckResourceAttr("data.cloudflare_infrastructure_access_targets.all", "targets.1.ip.ipv6.ip_addr", "64c0:64e8:f0b4:8dbf:7104:72b0:ec8f:f5e0"),
resource.TestCheckResourceAttr("data.cloudflare_infrastructure_access_targets.all", "targets.1.ip.ipv6.virtual_network_id", "b9c90134-52de-4903-81e8-004a3a06b435"),
Copy link
Member

Choose a reason for hiding this comment

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

after fixing this, i think this is a legit failure

=== RUN   TestAccCloudflareInfraAccessTarget_DataSource
    data_source_test.go:17: Step 2/2 error: Check failed: Check 9/10 error: data.cloudflare_infrastructure_access_targets.all: Attribute 'targets.1.ip.ipv6.ip_addr' not found
--- FAIL: TestAccCloudflareInfraAccessTarget_DataSource (4.29s)
FAIL
FAIL	github.com/cloudflare/terraform-provider-cloudflare/internal/framework/service/infrastructure_access_target	5.389s
FAIL

Copy link
Contributor Author

@SaiDadireddy SaiDadireddy Sep 26, 2024

Choose a reason for hiding this comment

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

Tested the second test manually and it's working as expected. Asserting in this case is tricky with multiple resources in the returned data source since there is no strict ordering. Removed the second test, it's not necessary.

@jacobbednarz jacobbednarz merged commit a7fd46e into cloudflare:master Sep 26, 2024
9 checks passed
@github-actions github-actions bot added this to the v4.43.0 milestone Sep 26, 2024
Copy link
Contributor

This functionality has been released in v4.43.0 of the Terraform Cloudflare Provider.

Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants