Skip to content

Conversation

@tamas-jozsa
Copy link
Collaborator

@tamas-jozsa tamas-jozsa commented Jan 14, 2026

Summary

Implements migration support for the name field in zero_trust_device_posture_rule resource when upgrading from provider v4 to v5. The name field was optional in v4 but became required in v5, requiring special handling during migration.

Problem

In v4, the name field was optional for device posture rules. When users didn't provide a name, the API stored an empty string. In v5, the name field is required, causing migration failures for resources that were created without names.

Solution

Implements a three-tier fallback strategy to automatically populate missing names during migration:

  1. State lookup: Check v4 state for existing name
  2. API fallback: Query Cloudflare API for the actual resource name if state is empty
  3. Resource name fallback: Use Terraform resource name as last resort

This ensures all v4 configurations migrate successfully to v5 without requiring manual intervention.

Implementation Details

Core Changes

v4_to_v5.go:

  • Added name field detection and population logic in TransformConfig()
  • Implemented fetchNameFromAPI() to retrieve resource names from Cloudflare API
  • Direct parsing of ctx.StateJSON using gjson for state lookups
  • Fallback chain ensures valid config generation in all scenarios

main.go:

  • Added user-visible logging for state file operations:
    • ✓ Loaded state file for config cross-referencing
    • ⚠ Failed to read state file
    • ℹ No state file specified

Test Coverage

v4_to_v5_test.go:

  • Test: Name field populated from state when missing in config
  • Test: Name field not overwritten when present in config
  • Test: Graceful fallback to resource name when state is nil
  • Simplified test structure to use standard TransformConfig() method

Testing

All unit tests pass:
✓ TestConfigTransformation (11 subtests)
✓ TestConfigTransformationWithState (3 subtests)
✓ TestStateTransformation (12 subtests)

Acceptance test passes with real API:
✓ TestMigrateDevicePostureRuleWithoutName - Verifies v4 resources without names successfully migrate to v5

Migration Behavior

Before:

# v4 config (name optional, not specified)
resource "cloudflare_device_posture_rule" "example" {
  account_id = "abc123"
  type       = "os_version"
  # name not specified
}

After Migration:
# v5 config (name required, auto-generated)
resource "cloudflare_zero_trust_device_posture_rule" "example" {
  account_id = "abc123"
  type       = "os_version"
  name       = "example"  # ← Automatically added using resource name
}

The migration tool now handles this transformation automatically, preventing migration failures.

@tamas-jozsa tamas-jozsa changed the title feat(migrate): handle optional→required name field for device posture… feat(zero_trust_device_posture_rule): improve v4 to v5 migration Jan 14, 2026
… rules

  Add intelligent name field migration for zero_trust_device_posture_rule
  when upgrading from v4 to v5. The name field was optional in v4 but is
  required in v5.

  Implements three-tier fallback strategy:
  1. Use name from v4 state if present
  2. Fetch name from Cloudflare API if state is empty
  3. Use Terraform resource name as final fallback

  This ensures all v4 configurations can be successfully migrated to v5
  without manual intervention, even when the name field was not explicitly
  set in v4 configs.

  Changes:
  - Add name field population logic in TransformConfig
  - Add fetchNameFromAPI method to retrieve names from API
  - Add user-visible logging for state file operations
  - Add comprehensive test coverage for name field scenarios
  - Simplify implementation to use ctx.StateJSON directly
@tamas-jozsa tamas-jozsa force-pushed the zero_trust_device_posture_rule branch from 93494eb to d545325 Compare January 14, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant