Skip to content

Commit 42b9c9c

Browse files
authored
Add SDKv2 resource with identity (#327)
* add resource with identity for SDKv2 provider implementation * Bump terraform-provider-sdk to v2.37.0-alpha.1 * also run apply in test * Bump terraform-provider-sdk to latest main and switch to SchemaFunc for identity * Use ConfigStateChecks instead of Check
1 parent 52e22f4 commit 42b9c9c

File tree

6 files changed

+179
-3
lines changed

6 files changed

+179
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/hashicorp/terraform-plugin-framework-validators v0.17.0
1616
github.com/hashicorp/terraform-plugin-go v0.27.0-alpha.1
1717
github.com/hashicorp/terraform-plugin-mux v0.19.0-alpha.1
18-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1
18+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0-alpha.1.0.20250327143321-dee0dd622735
1919
github.com/hashicorp/terraform-plugin-testing v1.12.0
2020
github.com/zclconf/go-cty v1.16.2
2121
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9T
105105
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
106106
github.com/hashicorp/terraform-plugin-mux v0.19.0-alpha.1 h1:WCzSBsp719WKEV/+j+4/o742paM0twYm7B84y7x8pOM=
107107
github.com/hashicorp/terraform-plugin-mux v0.19.0-alpha.1/go.mod h1:iKph9LFBiD4a33AJLgqg7IKSVg2kdlYvx0IRd+ys3Ig=
108-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1 h1:WNMsTLkZf/3ydlgsuXePa3jvZFwAJhruxTxP/c1Viuw=
109-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.36.1/go.mod h1:P6o64QS97plG44iFzSM6rAn6VJIC/Sy9a9IkEtl79K4=
108+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0-alpha.1.0.20250327143321-dee0dd622735 h1:QxZAQUW2TWsmYUIx4nJDs74ixxER2teNIIrDvVOzvLY=
109+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0-alpha.1.0.20250327143321-dee0dd622735/go.mod h1:+nyHvYq7kSpjTX7ayK0iLov9qyVC1SitewemvhAvSx4=
110110
github.com/hashicorp/terraform-plugin-testing v1.12.0 h1:tpIe+T5KBkA1EO6aT704SPLedHUo55RenguLHcaSBdI=
111111
github.com/hashicorp/terraform-plugin-testing v1.12.0/go.mod h1:jbDQUkT9XRjAh1Bvyufq+PEH1Xs4RqIdpOQumSgSXBM=
112112
github.com/hashicorp/terraform-registry-address v0.2.4 h1:JXu/zHB2Ymg/TGVCRu10XqNa4Sh2bWcqCNyKWjnCPJA=

internal/sdkv2provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func New() *schema.Provider {
2727
},
2828
ResourcesMap: map[string]*schema.Resource{
2929
"corner_user": resourceUser(),
30+
"corner_user_identity": resourceUserIdentity(),
3031
"corner_writeonly": resourceWriteOnly(),
3132
"corner_writeonly_import": resourceWriteOnlyImport(),
3233
"corner_writeonly_upgrade": resourceWriteOnlyUpgrade(0),

internal/sdkv2provider/provider_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestAccTests(t *testing.T) {
4545
// public map of test cases that can be imported by Core/SDK etc.
4646
var TestCases = map[string]func(*testing.T) resource.TestCase{
4747
"corner_user": testAccResourceUser,
48+
"corner_user_identity": testAccResourceUserIdentity,
4849
"corner_regions": testAccDataSourceRegions,
4950
"corner_bigint_data": testAccDataSourceBigint,
5051
"corner_bigint": testAccResourceBigint,
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
//nolint:forcetypeassert // Test SDK provider
5+
package sdkv2
6+
7+
import (
8+
"context"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-provider-corner/internal/backend"
13+
)
14+
15+
func resourceUserIdentity() *schema.Resource {
16+
return &schema.Resource{
17+
CreateContext: resourceUserIdentityCreate,
18+
ReadContext: resourceUserIdentityRead,
19+
UpdateContext: resourceUserIdentityUpdate,
20+
DeleteContext: resourceUserIdentityDelete,
21+
22+
Schema: map[string]*schema.Schema{
23+
"email": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
},
28+
"name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
"age": {
33+
Type: schema.TypeInt,
34+
Required: true,
35+
},
36+
},
37+
38+
Identity: &schema.ResourceIdentity{
39+
Version: 1,
40+
SchemaFunc: func() map[string]*schema.Schema {
41+
return map[string]*schema.Schema{
42+
"email": {
43+
Type: schema.TypeString,
44+
RequiredForImport: true,
45+
},
46+
}
47+
},
48+
},
49+
}
50+
}
51+
52+
func resourceUserIdentityCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
53+
client := meta.(*backend.Client)
54+
newUser := &backend.User{
55+
Email: d.Get("email").(string),
56+
Name: d.Get("name").(string),
57+
Age: d.Get("age").(int),
58+
}
59+
60+
err := client.CreateUser(newUser)
61+
if err != nil {
62+
return diag.FromErr(err)
63+
}
64+
65+
return resourceUserIdentityRead(ctx, d, meta)
66+
}
67+
68+
func resourceUserIdentityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
69+
client := meta.(*backend.Client)
70+
71+
email := d.Get("email").(string)
72+
73+
p, err := client.ReadUser(email)
74+
if err != nil {
75+
return diag.FromErr(err)
76+
}
77+
78+
if p == nil {
79+
return nil
80+
}
81+
82+
d.SetId(email)
83+
84+
err = d.Set("name", p.Name)
85+
if err != nil {
86+
return diag.FromErr(err)
87+
}
88+
err = d.Set("age", p.Age)
89+
if err != nil {
90+
return diag.FromErr(err)
91+
}
92+
93+
identity, err := d.Identity()
94+
if err != nil {
95+
return diag.FromErr(err)
96+
}
97+
err = identity.Set("email", email)
98+
if err != nil {
99+
return diag.FromErr(err)
100+
}
101+
102+
return nil
103+
}
104+
105+
func resourceUserIdentityUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
106+
client := meta.(*backend.Client)
107+
108+
user := &backend.User{
109+
Email: d.Get("email").(string),
110+
Name: d.Get("name").(string),
111+
Age: d.Get("age").(int),
112+
}
113+
114+
err := client.UpdateUser(user)
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
119+
return nil
120+
}
121+
122+
func resourceUserIdentityDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
123+
client := meta.(*backend.Client)
124+
125+
user := &backend.User{
126+
Email: d.Get("email").(string),
127+
Name: d.Get("name").(string),
128+
Age: d.Get("age").(int),
129+
}
130+
131+
err := client.DeleteUser(user)
132+
if err != nil {
133+
return diag.FromErr(err)
134+
}
135+
136+
return nil
137+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package sdkv2
5+
6+
import (
7+
"regexp"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
12+
"github.com/hashicorp/terraform-plugin-testing/statecheck"
13+
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
14+
)
15+
16+
func testAccResourceUserIdentity(t *testing.T) resource.TestCase {
17+
return resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: configResourceBasicIdentity,
23+
ConfigStateChecks: []statecheck.StateCheck{
24+
statecheck.ExpectKnownValue("corner_user_identity.foo", tfjsonpath.New("name"), knownvalue.StringRegexp(regexp.MustCompile(`^For`))),
25+
},
26+
},
27+
},
28+
}
29+
}
30+
31+
const configResourceBasicIdentity = `
32+
resource "corner_user_identity" "foo" {
33+
34+
name = "Ford Prefect"
35+
age = 200
36+
}
37+
`

0 commit comments

Comments
 (0)