-
Notifications
You must be signed in to change notification settings - Fork 13
TFECO-9155: Support importing by resource identity for sdkv2 resource #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e439c2a
support importing by resource identity for sdkv2 resource
ansgarm 6b77bce
add steps to test import from identity
ansgarm 1cf238e
updated the version
austinvalle 15f5185
tested resource identity upgrade by manually changing identity version
ansgarm 98c6806
Apply suggestions from code review
ansgarm 22cec49
split identity schema upgrade test into it's own resource and test case
ansgarm 699bf42
fix identity upgrader version
ansgarm 7b30e43
update to latest main of plugin-sdk, drop unused upgrade function, ti…
ansgarm bf7d463
use tagged switch to please linter
ansgarm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
internal/sdkv2provider/resource_user_identity_upgrade.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| //nolint:forcetypeassert // Test SDK provider | ||
| package sdkv2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tftypes" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "github.com/hashicorp/terraform-provider-corner/internal/backend" | ||
| ) | ||
|
|
||
| func resourceUserIdentityUpgrade(version int) *schema.Resource { | ||
| return &schema.Resource{ | ||
| CreateContext: resourceUserIdentityUpgradeCreate(version), | ||
| ReadContext: resourceUserIdentityUpgradeRead(version), | ||
| UpdateContext: resourceUserIdentityUpgradeUpdate(version), | ||
| DeleteContext: resourceUserIdentityUpgradeDelete(version), | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "email": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| }, | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "age": { | ||
| Type: schema.TypeInt, | ||
| Required: true, | ||
| }, | ||
| }, | ||
|
|
||
| Identity: &schema.ResourceIdentity{ | ||
| Version: int64(version), | ||
| SchemaFunc: func() map[string]*schema.Schema { | ||
| if version == 0 { | ||
| return map[string]*schema.Schema{ | ||
| "email": { | ||
| Type: schema.TypeString, | ||
| RequiredForImport: true, | ||
| }, | ||
| } | ||
| } | ||
| if version == 1 { | ||
| return map[string]*schema.Schema{ | ||
| "local_part": { | ||
| Type: schema.TypeString, | ||
| RequiredForImport: true, | ||
| }, | ||
| "domain": { | ||
| Type: schema.TypeString, | ||
| RequiredForImport: true, | ||
| }, | ||
| } | ||
| } | ||
| panic(fmt.Sprintf("unknown version %d", version)) | ||
| }, | ||
| IdentityUpgraders: []schema.IdentityUpgrader{ | ||
| { | ||
| Version: 0, | ||
| Type: tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "email": tftypes.String, | ||
| }, | ||
| }, | ||
| Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { | ||
| email := rawState["email"].(string) | ||
| parts := strings.Split(email, "@") | ||
| if len(parts) != 2 { | ||
| return nil, fmt.Errorf("invalid email format: %s", email) | ||
| } | ||
| return map[string]interface{}{ | ||
| "local_part": parts[0], | ||
| "domain": parts[1], | ||
| }, nil | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceUserIdentityUpgradeCreate(version int) schema.CreateContextFunc { | ||
| return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| client := meta.(*backend.Client) | ||
| newUser := &backend.User{ | ||
| Email: d.Get("email").(string), | ||
| Name: d.Get("name").(string), | ||
| Age: d.Get("age").(int), | ||
| } | ||
|
|
||
| err := client.CreateUser(newUser) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return resourceUserIdentityUpgradeRead(version)(ctx, d, meta) | ||
| } | ||
| } | ||
|
|
||
| func resourceUserIdentityUpgradeRead(version int) schema.ReadContextFunc { | ||
| return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| client := meta.(*backend.Client) | ||
|
|
||
| email := d.Get("email").(string) | ||
|
|
||
| p, err := client.ReadUser(email) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if p == nil { | ||
| return nil | ||
| } | ||
|
|
||
| d.SetId(email) | ||
|
|
||
| err = d.Set("name", p.Name) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| err = d.Set("age", p.Age) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| identity, err := d.Identity() | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if version == 0 { | ||
| err = identity.Set("email", email) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } else if version == 1 { | ||
| parts := strings.Split(email, "@") | ||
| if len(parts) != 2 { | ||
| return diag.FromErr(fmt.Errorf("invalid email format: %s", email)) | ||
| } | ||
|
|
||
| err = identity.Set("local_part", parts[0]) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| err = identity.Set("domain", parts[1]) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func resourceUserIdentityUpgradeUpdate(version int) schema.UpdateContextFunc { | ||
| return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| client := meta.(*backend.Client) | ||
|
|
||
| user := &backend.User{ | ||
| Email: d.Get("email").(string), | ||
| Name: d.Get("name").(string), | ||
| Age: d.Get("age").(int), | ||
| } | ||
|
|
||
| err := client.UpdateUser(user) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func resourceUserIdentityUpgradeDelete(version int) schema.DeleteContextFunc { | ||
| return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| client := meta.(*backend.Client) | ||
|
|
||
| user := &backend.User{ | ||
| Email: d.Get("email").(string), | ||
| Name: d.Get("name").(string), | ||
| Age: d.Get("age").(int), | ||
| } | ||
|
|
||
| err := client.DeleteUser(user) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
internal/sdkv2provider/resource_user_identity_upgrade_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package sdkv2 | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
| "github.com/hashicorp/terraform-plugin-testing/statecheck" | ||
| "github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
| ) | ||
|
|
||
| func testAccResourceUserIdentityUpgrade(t *testing.T) resource.TestCase { | ||
| return resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheck(t) }, | ||
| TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
| tfversion.SkipBelow(tfversion.Version1_12_0), | ||
| }, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
| "corner": func() (tfprotov5.ProviderServer, error) { //nolint | ||
| return NewWithIdentityUpgradeVersion(0).GRPCProvider(), nil | ||
| }, | ||
| }, | ||
| Config: `resource "corner_user_identity_upgrade" "foo" { | ||
| email = "ford@prefect.co" | ||
| name = "Ford Prefect" | ||
| age = 200 | ||
| }`, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectIdentity("corner_user_identity_upgrade.foo", map[string]knownvalue.Check{ | ||
| "email": knownvalue.StringExact("ford@prefect.co"), | ||
| }), | ||
| }, | ||
| }, | ||
| { | ||
| ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
| "corner": func() (tfprotov5.ProviderServer, error) { //nolint | ||
| return NewWithIdentityUpgradeVersion(1).GRPCProvider(), nil | ||
| }, | ||
| }, | ||
| Config: `resource "corner_user_identity_upgrade" "foo" { | ||
| email = "ford@prefect.co" | ||
| name = "Ford Prefect" | ||
| age = 200 | ||
| }`, | ||
| ConfigStateChecks: []statecheck.StateCheck{ | ||
| statecheck.ExpectIdentity("corner_user_identity_upgrade.foo", map[string]knownvalue.Check{ | ||
| "local_part": knownvalue.StringExact("ford"), | ||
| "domain": knownvalue.StringExact("prefect.co"), | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.