Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Dec 6, 2024
1 parent fd03f33 commit 2bcec11
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/snowflakeroles"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"strings"

Expand Down Expand Up @@ -161,7 +162,30 @@ func Account() *schema.Resource {
},

// TODO: State upgrader
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Version: 0,
// setting type to cty.EmptyObject is a bit hacky here but following https://developer.hashicorp.com/terraform/plugin/framework/migrating/resources/state-upgrade#sdkv2-1 would require lots of repetitive code; this should work with cty.EmptyObject
Type: cty.EmptyObject,
Upgrade: v0_99_0_AccountStateUpgrader,
},
},
}
}

func v0_99_0_AccountStateUpgrader(ctx context.Context, state map[string]any, meta any) (map[string]any, error) {
client := meta.(*provider.Context).Client
state["must_change_password"] = booleanStringFromBool(state["must_change_password"].(bool))
state["is_org_admin"] = booleanStringFromBool(state["is_org_admin"].(bool))
account, err := client.Accounts.ShowByID(ctx, sdk.NewAccountObjectIdentifier(state["name"].(string)))
if err != nil {
return nil, err
}

state["id"] = helpers.EncodeResourceIdentifier(sdk.NewAccountIdentifier(account.OrganizationName, account.AccountName))

return state, nil
}

func ImportAccount(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
Expand Down
103 changes: 103 additions & 0 deletions pkg/resources/account_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resources_test

import (
"fmt"
acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/resourceassert"
Expand Down Expand Up @@ -545,4 +546,106 @@ func TestAcc_Account_InvalidValues(t *testing.T) {
})
}

func TestAcc_Account_UpgradeFrom_v0_99_0(t *testing.T) {
_ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance)
_ = testenvs.GetOrSkipTest(t, testenvs.TestAccountCreate)

email := random.Email()
name := random.AdminName()
adminName := random.AdminName()
adminPassword := random.Password()
firstName := random.AdminName()
lastName := random.AdminName()
region := acc.TestClient().Context.CurrentRegion(t)
comment := random.Comment()

configModel := model.Account("test", adminName, string(sdk.UserTypeService), string(sdk.EditionStandard), email, 3, name).
WithAdminPassword(adminPassword).
WithFirstName(firstName).
WithLastName(lastName).
WithMustChangePassword(r.BooleanTrue).
WithRegion(region).
WithIsOrgAdmin(r.BooleanFalse).
WithComment(comment)

resource.Test(t, resource.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.Account),
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"snowflake": {
VersionConstraint: "=0.99.0",
Source: "Snowflake-Labs/snowflake",
},
},
Config: accountConfig_v0_99_0(name, adminName, adminPassword, email, sdk.EditionStandard, firstName, lastName, true, region, 3, comment),
},
{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
Config: config.FromModel(t, configModel),
Check: assert.AssertThat(t,
resourceassert.AccountResource(t, configModel.ResourceReference()).
HasNameString(name).
HasAdminNameString(adminName).
HasAdminPasswordString(adminPassword).
HasEmailString(email).
HasFirstNameString(firstName).
HasLastNameString(lastName).
HasMustChangePasswordString(r.BooleanTrue).
HasRegionGroupString("").
HasRegionString(region).
HasCommentString(comment).
HasIsOrgAdminString(r.BooleanFalse).
HasGracePeriodInDaysString("3"),
),
},
},
})
}

func accountConfig_v0_99_0(
name string,
adminName string,
adminPassword string,
email string,
edition sdk.AccountEdition,
firstName string,
lastName string,
mustChangePassword bool,
region string,
gracePeriodInDays int,
comment string,
) string {
return fmt.Sprintf(`
resource "snowflake_account" "test" {
name = "%[1]s"
admin_name = "%[2]s"
admin_password = "%[3]s"
email = "%[4]s"
edition = "%[5]s"
first_name = "%[6]s"
last_name = "%[7]s"
must_change_password = %[8]t
region = "%[9]s"
grace_period_in_days = %[10]d
comment = "%[11]s"
}
`,
name,
adminName,
adminPassword,
email,
edition,
firstName,
lastName,
mustChangePassword,
region,
gracePeriodInDays,
comment,
)
}

// TODO: State upgrader

0 comments on commit 2bcec11

Please sign in to comment.