|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package proto6server |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/internal/logging" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" |
| 17 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 18 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 19 | + "github.com/hashicorp/terraform-plugin-log/tfsdklogtest" |
| 20 | +) |
| 21 | + |
| 22 | +func TestServerGetResourceIdentitySchemas(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + testCases := map[string]struct { |
| 26 | + server *Server |
| 27 | + request *tfprotov6.GetResourceIdentitySchemasRequest |
| 28 | + expectedError error |
| 29 | + expectedResponse *tfprotov6.GetResourceIdentitySchemasResponse |
| 30 | + }{ |
| 31 | + "resource-identity-schemas": { |
| 32 | + server: &Server{ |
| 33 | + FrameworkServer: fwserver.Server{ |
| 34 | + Provider: &testprovider.Provider{ |
| 35 | + ResourcesMethod: func(_ context.Context) []func() resource.Resource { |
| 36 | + return []func() resource.Resource{ |
| 37 | + func() resource.Resource { |
| 38 | + return &testprovider.ResourceWithIdentity{ |
| 39 | + Resource: &testprovider.Resource{ |
| 40 | + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 41 | + resp.TypeName = "test_resource1" |
| 42 | + }, |
| 43 | + }, |
| 44 | + IdentitySchemaMethod: func(_ context.Context, _ resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { |
| 45 | + resp.IdentitySchema = identityschema.Schema{ |
| 46 | + Attributes: map[string]identityschema.Attribute{ |
| 47 | + "test1": identityschema.StringAttribute{ |
| 48 | + RequiredForImport: true, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + }, |
| 53 | + } |
| 54 | + }, |
| 55 | + func() resource.Resource { |
| 56 | + return &testprovider.ResourceWithIdentity{ |
| 57 | + Resource: &testprovider.Resource{ |
| 58 | + MetadataMethod: func(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 59 | + resp.TypeName = "test_resource2" |
| 60 | + }, |
| 61 | + }, |
| 62 | + IdentitySchemaMethod: func(_ context.Context, _ resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { |
| 63 | + resp.IdentitySchema = identityschema.Schema{ |
| 64 | + Attributes: map[string]identityschema.Attribute{ |
| 65 | + "test2": identityschema.BoolAttribute{ |
| 66 | + RequiredForImport: true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + }, |
| 71 | + } |
| 72 | + }, |
| 73 | + } |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + request: &tfprotov6.GetResourceIdentitySchemasRequest{}, |
| 79 | + expectedResponse: &tfprotov6.GetResourceIdentitySchemasResponse{ |
| 80 | + IdentitySchemas: map[string]*tfprotov6.ResourceIdentitySchema{ |
| 81 | + "test_resource1": { |
| 82 | + IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{ |
| 83 | + { |
| 84 | + Name: "test1", |
| 85 | + RequiredForImport: true, |
| 86 | + Type: tftypes.String, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + "test_resource2": { |
| 91 | + IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{ |
| 92 | + { |
| 93 | + Name: "test2", |
| 94 | + RequiredForImport: true, |
| 95 | + Type: tftypes.Bool, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + for name, testCase := range testCases { |
| 105 | + t.Run(name, func(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + got, err := testCase.server.GetResourceIdentitySchemas(context.Background(), new(tfprotov6.GetResourceIdentitySchemasRequest)) |
| 109 | + |
| 110 | + if diff := cmp.Diff(testCase.expectedError, err); diff != "" { |
| 111 | + t.Errorf("unexpected error difference: %s", diff) |
| 112 | + } |
| 113 | + |
| 114 | + if diff := cmp.Diff(testCase.expectedResponse, got); diff != "" { |
| 115 | + t.Errorf("unexpected response difference: %s", diff) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestServerGetResourceIdentitySchemas_logging(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + |
| 124 | + var output bytes.Buffer |
| 125 | + |
| 126 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 127 | + ctx = logging.InitContext(ctx) |
| 128 | + |
| 129 | + testServer := &Server{ |
| 130 | + FrameworkServer: fwserver.Server{ |
| 131 | + Provider: &testprovider.Provider{ |
| 132 | + ResourcesMethod: func(ctx context.Context) []func() resource.Resource { |
| 133 | + return []func() resource.Resource{ |
| 134 | + func() resource.Resource { |
| 135 | + return &testprovider.ResourceWithIdentity{ |
| 136 | + Resource: &testprovider.Resource{ |
| 137 | + MetadataMethod: func(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 138 | + resp.TypeName = "examplecloud_thing" |
| 139 | + }, |
| 140 | + }, |
| 141 | + IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { |
| 142 | + resp.IdentitySchema = identityschema.Schema{} |
| 143 | + }, |
| 144 | + } |
| 145 | + }, |
| 146 | + } |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + _, err := testServer.GetResourceIdentitySchemas(ctx, new(tfprotov6.GetResourceIdentitySchemasRequest)) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + t.Fatalf("unexpected error: %s", err) |
| 156 | + } |
| 157 | + |
| 158 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 159 | + |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 162 | + } |
| 163 | + |
| 164 | + expectedEntries := []map[string]interface{}{ |
| 165 | + { |
| 166 | + "@level": "trace", |
| 167 | + "@message": "Checking ResourceTypes lock", |
| 168 | + "@module": "sdk.framework", |
| 169 | + }, |
| 170 | + { |
| 171 | + "@level": "trace", |
| 172 | + "@message": "Checking ProviderTypeName lock", |
| 173 | + "@module": "sdk.framework", |
| 174 | + }, |
| 175 | + { |
| 176 | + "@level": "trace", |
| 177 | + "@message": "Calling provider defined Provider Metadata", |
| 178 | + "@module": "sdk.framework", |
| 179 | + }, |
| 180 | + { |
| 181 | + "@level": "trace", |
| 182 | + "@message": "Called provider defined Provider Metadata", |
| 183 | + "@module": "sdk.framework", |
| 184 | + }, |
| 185 | + { |
| 186 | + "@level": "trace", |
| 187 | + "@message": "Calling provider defined Provider Resources", |
| 188 | + "@module": "sdk.framework", |
| 189 | + }, |
| 190 | + { |
| 191 | + "@level": "trace", |
| 192 | + "@message": "Called provider defined Provider Resources", |
| 193 | + "@module": "sdk.framework", |
| 194 | + }, |
| 195 | + { |
| 196 | + "@level": "trace", |
| 197 | + "@message": "Found resource type", |
| 198 | + "@module": "sdk.framework", |
| 199 | + "tf_resource_type": "examplecloud_thing", |
| 200 | + }, |
| 201 | + { |
| 202 | + "@level": "trace", |
| 203 | + "@message": "Calling provider defined Resource IdentitySchema method", |
| 204 | + "@module": "sdk.framework", |
| 205 | + "tf_resource_type": "examplecloud_thing", |
| 206 | + }, |
| 207 | + { |
| 208 | + "@level": "trace", |
| 209 | + "@message": "Called provider defined Resource IdentitySchema method", |
| 210 | + "@module": "sdk.framework", |
| 211 | + "tf_resource_type": "examplecloud_thing", |
| 212 | + }, |
| 213 | + } |
| 214 | + |
| 215 | + if diff := cmp.Diff(entries, expectedEntries); diff != "" { |
| 216 | + t.Errorf("unexpected difference: %s", diff) |
| 217 | + } |
| 218 | +} |
0 commit comments