|
| 1 | +package fwresource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 17 | +) |
| 18 | + |
| 19 | +func TestParseImportId(t *testing.T) { |
| 20 | + testSchema := schema.Schema{ |
| 21 | + Attributes: map[string]schema.Attribute{ |
| 22 | + "project": schema.StringAttribute{ |
| 23 | + Optional: true, |
| 24 | + Computed: true, |
| 25 | + PlanModifiers: []planmodifier.String{ |
| 26 | + stringplanmodifier.UseStateForUnknown(), |
| 27 | + }, |
| 28 | + }, |
| 29 | + "name": schema.StringAttribute{ |
| 30 | + Required: true, |
| 31 | + }, |
| 32 | + "zone": schema.StringAttribute{ |
| 33 | + Required: true, |
| 34 | + }, |
| 35 | + "instance_id": schema.Int64Attribute{ |
| 36 | + Required: true, |
| 37 | + PlanModifiers: []planmodifier.Int64{ |
| 38 | + int64planmodifier.RequiresReplace(), |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + cases := map[string]struct { |
| 45 | + importId string |
| 46 | + idRegexes []string |
| 47 | + resourceSchema schema.Schema |
| 48 | + providerConfig *transport_tpg.Config |
| 49 | + expectedAttributes map[string]attr.Value |
| 50 | + expectError bool |
| 51 | + errorContains string |
| 52 | + }{ |
| 53 | + "successfully parses full resource ID format": { |
| 54 | + importId: "projects/my-project/zones/us-central1-a/instances/12345", |
| 55 | + idRegexes: []string{ |
| 56 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<instance_id>[^/]+)", |
| 57 | + "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<instance_id>[^/]+)", |
| 58 | + }, |
| 59 | + resourceSchema: testSchema, |
| 60 | + providerConfig: &transport_tpg.Config{}, |
| 61 | + expectedAttributes: map[string]attr.Value{ |
| 62 | + "project": types.StringValue("my-project"), |
| 63 | + "zone": types.StringValue("us-central1-a"), |
| 64 | + "instance_id": types.Int64Value(12345), |
| 65 | + }, |
| 66 | + }, |
| 67 | + "successfully parses shorter ID format": { |
| 68 | + importId: "my-project/us-central1-a/12345", |
| 69 | + idRegexes: []string{ |
| 70 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<instance_id>[^/]+)", |
| 71 | + "(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<instance_id>[^/]+)", |
| 72 | + }, |
| 73 | + resourceSchema: testSchema, |
| 74 | + providerConfig: &transport_tpg.Config{}, |
| 75 | + expectedAttributes: map[string]attr.Value{ |
| 76 | + "project": types.StringValue("my-project"), |
| 77 | + "zone": types.StringValue("us-central1-a"), |
| 78 | + "instance_id": types.Int64Value(12345), |
| 79 | + }, |
| 80 | + }, |
| 81 | + "successfully uses provider default for project": { |
| 82 | + importId: "us-central1-a/my-instance/12345", |
| 83 | + idRegexes: []string{ |
| 84 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<name>[^/]+)/(?P<instance_id>[^/]+)", // Most specific |
| 85 | + "(?P<zone>[^/]+)/(?P<name>[^/]+)/(?P<instance_id>[^/]+)", |
| 86 | + }, |
| 87 | + resourceSchema: testSchema, |
| 88 | + providerConfig: &transport_tpg.Config{ |
| 89 | + Project: "default-provider-project", |
| 90 | + }, |
| 91 | + expectedAttributes: map[string]attr.Value{ |
| 92 | + "project": types.StringValue("default-provider-project"), |
| 93 | + "zone": types.StringValue("us-central1-a"), |
| 94 | + "name": types.StringValue("my-instance"), |
| 95 | + "instance_id": types.Int64Value(12345), |
| 96 | + }, |
| 97 | + }, |
| 98 | + "returns error for non-matching ID": { |
| 99 | + importId: "invalid-id-format", |
| 100 | + idRegexes: []string{ |
| 101 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<instance_id>[^/]+)", |
| 102 | + }, |
| 103 | + resourceSchema: testSchema, |
| 104 | + providerConfig: &transport_tpg.Config{}, |
| 105 | + expectError: true, |
| 106 | + errorContains: "doesn't match any of the accepted formats", |
| 107 | + }, |
| 108 | + "returns error for value that cannot be converted to type": { |
| 109 | + importId: "projects/my-project/zones/us-central1-a/instances/not-an-integer", |
| 110 | + idRegexes: []string{ |
| 111 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<instance_id>[^/]+)", |
| 112 | + }, |
| 113 | + resourceSchema: testSchema, |
| 114 | + providerConfig: &transport_tpg.Config{}, |
| 115 | + expectError: true, |
| 116 | + errorContains: "Failed to parse \"not-an-integer\" as an integer", |
| 117 | + }, |
| 118 | + "returns error for invalid regex pattern": { |
| 119 | + importId: "any/id", |
| 120 | + idRegexes: []string{ |
| 121 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/+", // Invalid regex with unclosed bracket |
| 122 | + }, |
| 123 | + resourceSchema: testSchema, |
| 124 | + providerConfig: &transport_tpg.Config{}, |
| 125 | + expectError: true, |
| 126 | + errorContains: "could not compile regex", |
| 127 | + }, |
| 128 | + "warns about field in regex not present in schema": { |
| 129 | + importId: "projects/my-project/zones/us-central1-a/instances/12345/extra/field", |
| 130 | + idRegexes: []string{ |
| 131 | + "projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<instance_id>[^/]+)/extra/(?P<extra_field>[^/]+)", |
| 132 | + }, |
| 133 | + resourceSchema: testSchema, |
| 134 | + providerConfig: &transport_tpg.Config{}, |
| 135 | + // We expect success, but with a warning diagnostic. The valid fields should still be parsed. |
| 136 | + expectedAttributes: map[string]attr.Value{ |
| 137 | + "project": types.StringValue("my-project"), |
| 138 | + "zone": types.StringValue("us-central1-a"), |
| 139 | + "instance_id": types.Int64Value(12345), |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + for name, tc := range cases { |
| 145 | + t.Run(name, func(t *testing.T) { |
| 146 | + ctx := context.Background() |
| 147 | + req := resource.ImportStateRequest{ |
| 148 | + ID: tc.importId, |
| 149 | + } |
| 150 | + |
| 151 | + parsedAttributes, diags := ParseImportId(ctx, req, tc.resourceSchema, tc.providerConfig, tc.idRegexes) |
| 152 | + |
| 153 | + if diags.HasError() { |
| 154 | + if tc.expectError { |
| 155 | + // Check if the error message contains the expected substring. |
| 156 | + if tc.errorContains != "" { |
| 157 | + found := false |
| 158 | + for _, d := range diags.Errors() { |
| 159 | + if strings.Contains(d.Detail(), tc.errorContains) { |
| 160 | + found = true |
| 161 | + break |
| 162 | + } |
| 163 | + } |
| 164 | + if !found { |
| 165 | + t.Fatalf("expected error to contain %q, but it did not. Got: %v", tc.errorContains, diags.Errors()) |
| 166 | + } |
| 167 | + } |
| 168 | + // Correctly handled an expected error. |
| 169 | + return |
| 170 | + } |
| 171 | + t.Fatalf("unexpected error: %v", diags) |
| 172 | + } |
| 173 | + |
| 174 | + if tc.expectError { |
| 175 | + t.Fatal("expected an error, but got none") |
| 176 | + } |
| 177 | + |
| 178 | + if !reflect.DeepEqual(tc.expectedAttributes, parsedAttributes) { |
| 179 | + t.Fatalf("incorrect attributes parsed.\n- got: %v\n- want: %v", parsedAttributes, tc.expectedAttributes) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments