Skip to content

Commit 63dc825

Browse files
committed
protov6 + tests
1 parent 0f63086 commit 63dc825

11 files changed

+1226
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
10+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
11+
)
12+
13+
// GetResourceIdentitySchemasRequest returns the *fwserver.GetResourceIdentitySchemasRequest
14+
// equivalent of a *tfprotov6.GetResourceIdentitySchemasRequest.
15+
func GetResourceIdentitySchemasRequest(ctx context.Context, proto6 *tfprotov6.GetResourceIdentitySchemasRequest) *fwserver.GetResourceIdentitySchemasRequest {
16+
if proto6 == nil {
17+
return nil
18+
}
19+
20+
fw := &fwserver.GetResourceIdentitySchemasRequest{}
21+
22+
return fw
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package fromproto6_test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
)
15+
16+
func TestGetResourceIdentitySchemasRequest(t *testing.T) {
17+
t.Parallel()
18+
19+
testCases := map[string]struct {
20+
input *tfprotov6.GetResourceIdentitySchemasRequest
21+
expected *fwserver.GetResourceIdentitySchemasRequest
22+
}{
23+
"nil": {
24+
input: nil,
25+
expected: nil,
26+
},
27+
"empty": {
28+
input: &tfprotov6.GetResourceIdentitySchemasRequest{},
29+
expected: &fwserver.GetResourceIdentitySchemasRequest{},
30+
},
31+
}
32+
33+
for name, testCase := range testCases {
34+
t.Run(name, func(t *testing.T) {
35+
t.Parallel()
36+
37+
got := fromproto6.GetResourceIdentitySchemasRequest(context.Background(), testCase.input)
38+
39+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
40+
t.Errorf("unexpected difference: %s", diff)
41+
}
42+
})
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package proto6server
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
11+
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
12+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
13+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
14+
)
15+
16+
// GetResourceIdentitySchemas satisfies the tfprotov6.ProviderServer interface.
17+
func (s *Server) GetResourceIdentitySchemas(ctx context.Context, proto6Req *tfprotov6.GetResourceIdentitySchemasRequest) (*tfprotov6.GetResourceIdentitySchemasResponse, error) {
18+
ctx = s.registerContext(ctx)
19+
ctx = logging.InitContext(ctx)
20+
21+
fwReq := fromproto6.GetResourceIdentitySchemasRequest(ctx, proto6Req)
22+
fwResp := &fwserver.GetResourceIdentitySchemasResponse{}
23+
24+
s.FrameworkServer.GetResourceIdentitySchemas(ctx, fwReq, fwResp)
25+
26+
return toproto6.GetResourceIdentitySchemasResponse(ctx, fwResp), nil
27+
}
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package proto6server
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
)
11+
12+
// UpgradeResourceIdentity satisfies the tfprotov6.ProviderServer interface.
13+
func (s *Server) UpgradeResourceIdentity(ctx context.Context, proto6Req *tfprotov6.UpgradeResourceIdentityRequest) (*tfprotov6.UpgradeResourceIdentityResponse, error) {
14+
panic("unimplemented") // TODO: implement
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package toproto6
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
10+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
11+
)
12+
13+
// GetResourceIdentitySchemasResponse returns the *tfprotov6.GetResourceIdentitySchemasResponse
14+
// equivalent of a *fwserver.GetResourceIdentitySchemasResponse.
15+
func GetResourceIdentitySchemasResponse(ctx context.Context, fw *fwserver.GetResourceIdentitySchemasResponse) *tfprotov6.GetResourceIdentitySchemasResponse {
16+
if fw == nil {
17+
return nil
18+
}
19+
20+
protov6 := &tfprotov6.GetResourceIdentitySchemasResponse{
21+
Diagnostics: Diagnostics(ctx, fw.Diagnostics),
22+
IdentitySchemas: make(map[string]*tfprotov6.ResourceIdentitySchema, len(fw.IdentitySchemas)),
23+
}
24+
25+
var err error
26+
27+
for resourceType, identitySchema := range fw.IdentitySchemas {
28+
protov6.IdentitySchemas[resourceType], err = IdentitySchema(ctx, identitySchema)
29+
30+
if err != nil {
31+
protov6.Diagnostics = append(protov6.Diagnostics, &tfprotov6.Diagnostic{
32+
Severity: tfprotov6.DiagnosticSeverityError,
33+
Summary: "Error converting resource identity schema",
34+
Detail: "The identity schema for the resource \"" + resourceType + "\" couldn't be converted into a usable type. This is always a problem with the provider. Please report the following to the provider developer:\n\n" + err.Error(),
35+
})
36+
37+
return protov6
38+
}
39+
}
40+
41+
return protov6
42+
}

0 commit comments

Comments
 (0)