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 2, 2024
1 parent de39050 commit 461511e
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 315 deletions.
63 changes: 62 additions & 1 deletion pkg/acceptance/helpers/account_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package helpers

import (
"context"
"fmt"
"strings"
"testing"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/snowflakeroles"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/snowflakedb/gosnowflake"
"github.com/stretchr/testify/require"
"testing"
)

type AccountClient struct {
Expand Down Expand Up @@ -67,6 +73,22 @@ func (c *AccountClient) Create(t *testing.T) *sdk.Account {
return account
}

func (c *AccountClient) CreateWithRequest(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateAccountOptions) *sdk.Account {
err := c.client().Create(context.Background(), id, opts)
require.NoError(t, err)
t.Cleanup(c.DropFunc(t, id))

account, err := c.client().ShowByID(context.Background(), id)
require.NoError(t, err)

return account
}

func (c *AccountClient) Alter(t *testing.T, opts *sdk.AlterAccountOptions) {
err := c.client().Alter(context.Background(), opts)
require.NoError(t, err)
}

func (c *AccountClient) DropFunc(t *testing.T, id sdk.AccountObjectIdentifier) func() {
t.Helper()
return func() {
Expand Down Expand Up @@ -118,3 +140,42 @@ func (c *AccountClient) ShowRegions(t *testing.T) []Region {
}
})
}

func (c *AccountClient) CreateAndLogIn(t *testing.T) (*sdk.Account, *sdk.Client) {
id := c.ids.RandomAccountObjectIdentifier()
name := c.ids.Alpha()
privateKey := random.GenerateRSAPrivateKey(t)
publicKey, _ := random.GenerateRSAPublicKeyBasedOnPrivateKey(t, privateKey)
email := random.Email()

account := c.CreateWithRequest(t, id, &sdk.CreateAccountOptions{
AdminName: name,
AdminRSAPublicKey: sdk.String(publicKey),
AdminUserType: sdk.Pointer(sdk.UserTypeService),
Email: email,
Edition: sdk.EditionStandard,
})

c.Alter(t, &sdk.AlterAccountOptions{
SetIsOrgAdmin: &sdk.AccountSetIsOrgAdmin{
Name: id,
OrgAdmin: sdk.Bool(true),
},
})

var client *sdk.Client
require.Eventually(t, func() bool {
newClient, err := sdk.NewClient(&gosnowflake.Config{
Account: fmt.Sprintf("%s-%s", account.OrganizationName, account.AccountName),
User: name,
Host: strings.TrimLeft(*account.AccountLocatorURL, `https://`),
Authenticator: gosnowflake.AuthTypeJwt,
PrivateKey: privateKey,
Role: snowflakeroles.Orgadmin.Name(),
})
client = newClient
return err == nil
}, 2*time.Minute, time.Second*15)

return account, client
}
35 changes: 35 additions & 0 deletions pkg/acceptance/helpers/packages_policy_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package helpers

import (
"context"
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type PackagesPolicyClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewPackagesPolicyClient(context *TestClientContext, idsGenerator *IdsGenerator) *PackagesPolicyClient {
return &PackagesPolicyClient{
context: context,
ids: idsGenerator,
}
}

func (c *PackagesPolicyClient) Create(t *testing.T) (sdk.SchemaObjectIdentifier, func()) {
t.Helper()

id := c.ids.RandomSchemaObjectIdentifier()
_, err := c.context.client.ExecUnsafe(context.Background(), fmt.Sprintf("CREATE PACKAGES POLICY %s LANGUAGE PYTHON", id.FullyQualifiedName()))
require.NoError(t, err)

return id, func() {
_, err = c.context.client.ExecUnsafe(context.Background(), fmt.Sprintf("DROP PACKAGES POLICY IF EXISTS %s", id.FullyQualifiedName()))
require.NoError(t, err)
}
}
9 changes: 9 additions & 0 deletions pkg/acceptance/helpers/random/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func generateRSAPublicKeyFromPrivateKey(t *testing.T, key *rsa.PrivateKey) (stri
return encode(t, "RSA PUBLIC KEY", b), hash(t, b)
}

func GenerateRSAPublicKeyBasedOnPrivateKey(t *testing.T, key *rsa.PrivateKey) (string, string) {
t.Helper()

pub := key.Public()
b, err := x509.MarshalPKIXPublicKey(pub.(*rsa.PublicKey))
require.NoError(t, err)
return encode(t, "RSA PUBLIC KEY", b), hash(t, b)
}

// GenerateRSAPrivateKey returns an RSA private key.
func GenerateRSAPrivateKey(t *testing.T) *rsa.PrivateKey {
t.Helper()
Expand Down
2 changes: 2 additions & 0 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type TestClient struct {
NetworkPolicy *NetworkPolicyClient
NetworkRule *NetworkRuleClient
NotificationIntegration *NotificationIntegrationClient
PackagesPolicy *PackagesPolicyClient
Parameter *ParameterClient
PasswordPolicy *PasswordPolicyClient
Pipe *PipeClient
Expand Down Expand Up @@ -115,6 +116,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
NetworkPolicy: NewNetworkPolicyClient(context, idsGenerator),
NetworkRule: NewNetworkRuleClient(context, idsGenerator),
NotificationIntegration: NewNotificationIntegrationClient(context, idsGenerator),
PackagesPolicy: NewPackagesPolicyClient(context, idsGenerator),
Parameter: NewParameterClient(context),
PasswordPolicy: NewPasswordPolicyClient(context, idsGenerator),
Pipe: NewPipeClient(context, idsGenerator),
Expand Down
2 changes: 1 addition & 1 deletion pkg/schemas/account_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 74 additions & 42 deletions pkg/sdk/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"database/sql"
"errors"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
)

var (
Expand All @@ -21,6 +22,7 @@ type Accounts interface {
ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Account, error)
Drop(ctx context.Context, id AccountObjectIdentifier, gracePeriodInDays int, opts *DropAccountOptions) error
Undrop(ctx context.Context, id AccountObjectIdentifier) error
ShowParameters(ctx context.Context) ([]*Parameter, error)
}

var _ Accounts = (*accounts)(nil)
Expand Down Expand Up @@ -291,26 +293,26 @@ func (opts *ShowAccountOptions) validate() error {
type Account struct {
OrganizationName string
AccountName string
RegionGroup *string
SnowflakeRegion string
Edition AccountEdition
AccountURL string
CreatedOn time.Time
Comment string
RegionGroup *string // shows only for organizations that span multiple region groups
Edition *AccountEdition
AccountURL *string
CreatedOn *time.Time
Comment *string
AccountLocator string
AccountLocatorURL string
ManagedAccounts int
ConsumptionBillingEntityName string
AccountLocatorURL *string
ManagedAccounts *int
ConsumptionBillingEntityName *string
MarketplaceConsumerBillingEntityName *string
MarketplaceProviderBillingEntityName *string
OldAccountURL string
IsOrgAdmin bool
OldAccountURL *string
IsOrgAdmin *bool
AccountOldUrlSavedOn *time.Time
AccountOldUrlLastUsed *time.Time
OrganizationOldUrl string
OrganizationOldUrl *string
OrganizationOldUrlSavedOn *time.Time
OrganizationOldUrlLastUsed *time.Time
IsEventsAccount bool
IsEventsAccount *bool
IsOrganizationAccount bool
// Available only with the History keyword set
DroppedOn *time.Time
Expand All @@ -334,24 +336,24 @@ type accountDBRow struct {
AccountName string `db:"account_name"`
RegionGroup sql.NullString `db:"region_group"`
SnowflakeRegion string `db:"snowflake_region"`
Edition string `db:"edition"`
AccountURL string `db:"account_url"`
CreatedOn time.Time `db:"created_on"`
Edition sql.NullString `db:"edition"`
AccountURL sql.NullString `db:"account_url"`
CreatedOn sql.NullTime `db:"created_on"`
Comment sql.NullString `db:"comment"`
AccountLocator string `db:"account_locator"`
AccountLocatorURL string `db:"account_locator_url"`
ManagedAccounts int `db:"managed_accounts"`
ConsumptionBillingEntityName string `db:"consumption_billing_entity_name"`
AccountLocatorURL sql.NullString `db:"account_locator_url"`
ManagedAccounts sql.NullInt32 `db:"managed_accounts"`
ConsumptionBillingEntityName sql.NullString `db:"consumption_billing_entity_name"`
MarketplaceConsumerBillingEntityName sql.NullString `db:"marketplace_consumer_billing_entity_name"`
MarketplaceProviderBillingEntityName sql.NullString `db:"marketplace_provider_billing_entity_name"`
OldAccountURL string `db:"old_account_url"`
IsOrgAdmin bool `db:"is_org_admin"`
OldAccountURL sql.NullString `db:"old_account_url"`
IsOrgAdmin sql.NullBool `db:"is_org_admin"`
AccountOldUrlSavedOn sql.NullTime `db:"account_old_url_saved_on"`
AccountOldUrlLastUsed sql.NullTime `db:"account_old_url_last_used"`
OrganizationOldUrl string `db:"organization_old_url"`
OrganizationOldUrl sql.NullString `db:"organization_old_url"`
OrganizationOldUrlSavedOn sql.NullTime `db:"organization_old_url_saved_on"`
OrganizationOldUrlLastUsed sql.NullTime `db:"organization_old_url_last_used"`
IsEventsAccount bool `db:"is_events_account"`
IsEventsAccount sql.NullBool `db:"is_events_account"`
IsOrganizationAccount bool `db:"is_organization_account"`
// Available only with the History keyword set
DroppedOn sql.NullTime `db:"dropped_on"`
Expand All @@ -364,32 +366,54 @@ type accountDBRow struct {

func (row accountDBRow) convert() *Account {
acc := &Account{
OrganizationName: row.OrganizationName,
AccountName: row.AccountName,
SnowflakeRegion: row.SnowflakeRegion,
Edition: AccountEdition(row.Edition),
AccountURL: row.AccountURL,
CreatedOn: row.CreatedOn,
Comment: row.Comment.String,
AccountLocator: row.AccountLocator,
AccountLocatorURL: row.AccountLocatorURL,
ManagedAccounts: row.ManagedAccounts,
ConsumptionBillingEntityName: row.ConsumptionBillingEntityName,
OldAccountURL: row.OldAccountURL,
IsOrgAdmin: row.IsOrgAdmin,
OrganizationOldUrl: row.OrganizationOldUrl,
IsEventsAccount: row.IsEventsAccount,
IsOrganizationAccount: row.IsOrganizationAccount,
OrganizationName: row.OrganizationName,
AccountName: row.AccountName,
SnowflakeRegion: row.SnowflakeRegion,
AccountLocator: row.AccountLocator,
IsOrganizationAccount: row.IsOrganizationAccount,
}
if row.RegionGroup.Valid {
acc.RegionGroup = &row.RegionGroup.String
}
if row.Edition.Valid {
acc.Edition = Pointer(AccountEdition(row.Edition.String))
}
if row.AccountURL.Valid {
acc.AccountURL = &row.AccountURL.String
}
if row.CreatedOn.Valid {
acc.CreatedOn = &row.CreatedOn.Time
}
if row.Comment.Valid {
acc.Comment = &row.Comment.String
}
if row.AccountLocatorURL.Valid {
acc.AccountLocatorURL = &row.AccountLocatorURL.String
}
if row.ManagedAccounts.Valid {
acc.ManagedAccounts = Int(int(row.ManagedAccounts.Int32))
}
if row.ConsumptionBillingEntityName.Valid {
acc.ConsumptionBillingEntityName = &row.ConsumptionBillingEntityName.String
}
if row.OldAccountURL.Valid {
acc.OldAccountURL = &row.OldAccountURL.String
}
if row.IsOrgAdmin.Valid {
acc.IsOrgAdmin = &row.IsOrgAdmin.Bool
}
if row.OrganizationOldUrl.Valid {
acc.OrganizationOldUrl = &row.OrganizationOldUrl.String
}
if row.IsEventsAccount.Valid {
acc.IsEventsAccount = &row.IsEventsAccount.Bool
}
if row.MarketplaceConsumerBillingEntityName.Valid {
acc.MarketplaceConsumerBillingEntityName = &row.MarketplaceConsumerBillingEntityName.String
}
if row.MarketplaceProviderBillingEntityName.Valid {
acc.MarketplaceProviderBillingEntityName = &row.MarketplaceProviderBillingEntityName.String
}
if row.RegionGroup.Valid {
acc.RegionGroup = &row.RegionGroup.String
}
if row.AccountOldUrlSavedOn.Valid {
acc.AccountOldUrlSavedOn = &row.AccountOldUrlSavedOn.Time
}
Expand Down Expand Up @@ -497,3 +521,11 @@ func (c *accounts) Undrop(ctx context.Context, id AccountObjectIdentifier) error
_, err = c.client.exec(ctx, sql)
return err
}

func (c *accounts) ShowParameters(ctx context.Context) ([]*Parameter, error) {
return c.client.Parameters.ShowParameters(ctx, &ShowParametersOptions{
In: &ParametersIn{
Account: Bool(true),
},
})
}
3 changes: 2 additions & 1 deletion pkg/sdk/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package sdk

import (
"fmt"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
)

func TestAccountCreate(t *testing.T) {
Expand Down
Loading

0 comments on commit 461511e

Please sign in to comment.