Skip to content

Commit

Permalink
Merge pull request #2503 from cloudflare/handle-missing-import-parts
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz authored Jun 6, 2023
2 parents 507bf48 + 3be26ba commit 9b88c6d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/2503.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/cloudflare_ruleset: handle `Import` operations where the required values are missing for providing a nicer error message
```
4 changes: 2 additions & 2 deletions internal/framework/service/rulesets/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,15 @@ func (r *RulesetResource) Delete(ctx context.Context, req resource.DeleteRequest

func (r *RulesetResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, "/")
resourceLevel, resourceIdentifier, rulesetID := idParts[0], idParts[1], idParts[2]

if len(idParts) != 3 || resourceLevel == "" || resourceIdentifier == "" || rulesetID == "" {
if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" {
resp.Diagnostics.AddError(
"invalid import identifier",
fmt.Sprintf("expected import identifier to be resourceLevel/resourceIdentifier/rulesetID. got: %q", req.ID),
)
return
}
resourceLevel, resourceIdentifier, rulesetID := idParts[0], idParts[1], idParts[2]

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), rulesetID)...)
if resourceLevel == "zone" {
Expand Down
21 changes: 21 additions & 0 deletions internal/framework/service/rulesets/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,27 @@ func TestAccCloudflareRuleset_CacheSettingsDefinedQueryStringIncludeKeys(t *test
})
}

func TestAccCloudflareRuleset_ImportHandlesMissingValues(t *testing.T) {
rnd := utils.GenerateRandomResourceName()
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
zoneName := os.Getenv("CLOUDFLARE_DOMAIN")
name := "cloudflare_ruleset." + rnd

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckCloudflareRulesetTransformationRuleResponseHeaders(rnd, "broken", zoneID, zoneName),
ExpectError: regexp.MustCompile(`invalid import identifier`),
ImportState: true,
ImportStateId: rnd,
ResourceName: name,
},
},
})
}

func testAccCheckCloudflareRulesetMagicTransitSingle(rnd, name, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_ruleset" "%[1]s" {
Expand Down

0 comments on commit 9b88c6d

Please sign in to comment.