Skip to content
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

Migrate databricks_mws_private_access_settings to Go SDK #3135

Merged
merged 5 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/databricks-sdk-go/service/iam"
"github.com/golang-jwt/jwt/v4"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type cachedMe struct {
Expand Down Expand Up @@ -81,7 +82,7 @@ func (c *DatabricksClient) SetAccountClient(a *databricks.AccountClient) {
c.cachedAccountClient = a
}

func (c *DatabricksClient) SetAccountId(accountId string) error {
func (c *DatabricksClient) setAccountId(accountId string) error {
c.mu.Lock()
defer c.mu.Unlock()
if accountId == "" {
Expand Down Expand Up @@ -109,6 +110,33 @@ func (c *DatabricksClient) AccountClient() (*databricks.AccountClient, error) {
return acc, nil
}

func (c *DatabricksClient) AccountClientWithAccountIdFromConfig(d *schema.ResourceData) (*databricks.AccountClient, error) {
accountID, ok := d.GetOk("account_id")
if ok {
err := c.setAccountId(accountID.(string))
if err != nil {
return nil, err
}
}
return c.AccountClient()
}

func (c *DatabricksClient) AccountClientWithAccountIdFromPair(d *schema.ResourceData, p *Pair) (*databricks.AccountClient, string, error) {
accountID, resourceId, err := p.Unpack(d)
if err != nil {
return nil, "", err
}
err = c.setAccountId(accountID)
if err != nil {
return nil, "", err
}
a, err := c.AccountClient()
if err != nil {
return nil, "", err
}
return a, resourceId, nil
}

func (c *DatabricksClient) AccountOrWorkspaceRequest(accCallback func(*databricks.AccountClient) error, wsCallback func(*databricks.WorkspaceClient) error) error {
if c.Config.IsAccountClient() {
a, err := c.AccountClient()
Expand Down
9 changes: 9 additions & 0 deletions common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,12 @@ func NoCustomize(m map[string]*schema.Schema) map[string]*schema.Schema {
var NoAuth string = "default auth: cannot configure default credentials, " +
"please check https://docs.databricks.com/en/dev-tools/auth.html#databricks-client-unified-authentication " +
"to configure credentials for your preferred authentication method"

func AddAccountIdField(s map[string]*schema.Schema) map[string]*schema.Schema {
s["account_id"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Deprecated: "Configuring `account_id` at the resource-level is deprecated; please specify it in the `provider {}` configuration block instead",
}
return s
}
6 changes: 1 addition & 5 deletions mws/resource_mws_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ func ResourceMwsCredentials() *schema.Resource {
p := common.NewPairSeparatedID("account_id", "credentials_id", "/")
return common.Resource{
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
err := c.SetAccountId(d.Get("account_id").(string))
if err != nil {
return err
}
acc, err := c.AccountClient()
acc, err := c.AccountClientWithAccountIdFromConfig(d)
if err != nil {
return err
}
Expand Down
87 changes: 27 additions & 60 deletions mws/resource_mws_private_access_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,77 @@ package mws

import (
"context"
"fmt"

"github.com/databricks/databricks-sdk-go/service/provisioning"

"github.com/databricks/terraform-provider-databricks/common"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// NewPrivateAccessSettingsAPI creates VPCEndpointAPI instance from provider meta
func NewPrivateAccessSettingsAPI(ctx context.Context, m any) PrivateAccessSettingsAPI {
return PrivateAccessSettingsAPI{m.(*common.DatabricksClient), ctx}
}

// PrivateAccessSettingsAPI exposes the PAS API
type PrivateAccessSettingsAPI struct {
client *common.DatabricksClient
context context.Context
}

// Create creates the PAS ceation process
func (a PrivateAccessSettingsAPI) Create(pas *PrivateAccessSettings) error {
pasAPIPath := fmt.Sprintf("/accounts/%s/private-access-settings", pas.AccountID)
return a.client.Post(a.context, pasAPIPath, pas, &pas)
}

// Read returns the PAS object along with metadata and any additional errors
func (a PrivateAccessSettingsAPI) Read(mwsAcctID, pasID string) (PrivateAccessSettings, error) {
var pas PrivateAccessSettings
pasAPIPath := fmt.Sprintf("/accounts/%s/private-access-settings/%s", mwsAcctID, pasID)
err := a.client.Get(a.context, pasAPIPath, nil, &pas)
return pas, err
}

func (a PrivateAccessSettingsAPI) Update(pas *PrivateAccessSettings) error {
pasAPIPath := fmt.Sprintf("/accounts/%s/private-access-settings/%s", pas.AccountID, pas.PasID)
return a.client.Put(a.context, pasAPIPath, pas)
}

// Delete deletes the PAS object given a pas id
func (a PrivateAccessSettingsAPI) Delete(mwsAcctID, pasID string) error {
pasAPIPath := fmt.Sprintf("/accounts/%s/private-access-settings/%s", mwsAcctID, pasID)
if err := a.client.Delete(a.context, pasAPIPath, nil); err != nil {
return err
}
return nil
}

// List lists all the available PAS objects in the mws account
func (a PrivateAccessSettingsAPI) List(mwsAcctID string) ([]PrivateAccessSettings, error) {
var pasList []PrivateAccessSettings
pasAPIPath := fmt.Sprintf("/accounts/%s/private-access-settings", mwsAcctID)
err := a.client.Get(a.context, pasAPIPath, nil, &pasList)
return pasList, err
}

func ResourceMwsPrivateAccessSettings() *schema.Resource {
s := common.StructToSchema(PrivateAccessSettings{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
s := common.StructToSchema(provisioning.PrivateAccessSettings{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
// nolint
s["private_access_settings_name"].ValidateFunc = validation.StringLenBetween(4, 256)
common.SetRequired(s["private_access_settings_name"])
common.SetRequired(s["region"])

s["private_access_level"].ValidateFunc = validation.StringInSlice([]string{"ACCOUNT", "ENDPOINT"}, true)
common.SetDefault(s["private_access_level"], "ACCOUNT")

s["private_access_settings_id"].Computed = true

common.AddAccountIdField(s)
return s
})
p := common.NewPairSeparatedID("account_id", "private_access_settings_id", "/")
return common.Resource{
Schema: s,
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
var pas PrivateAccessSettings
a, err := c.AccountClientWithAccountIdFromConfig(d)
if err != nil {
return err
}
var pas provisioning.UpsertPrivateAccessSettingsRequest
common.DataToStructPointer(d, s, &pas)
common.SetForceSendFields(&pas, d, []string{"public_access_enabled"})
if err := NewPrivateAccessSettingsAPI(ctx, c).Create(&pas); err != nil {
res, err := a.PrivateAccess.Create(ctx, pas)
if err != nil {
return err
}
d.Set("private_access_settings_id", pas.PasID)
d.Set("private_access_settings_id", res.PrivateAccessSettingsId)
p.Pack(d)
return nil
},
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
accountID, pasID, err := p.Unpack(d)
a, pasID, err := c.AccountClientWithAccountIdFromPair(d, p)
if err != nil {
return err
}
pas, err := NewPrivateAccessSettingsAPI(ctx, c).Read(accountID, pasID)
pas, err := a.PrivateAccess.GetByPrivateAccessSettingsId(ctx, pasID)
if err != nil {
return err
}
return common.StructToData(pas, s, d)
},
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
_, pasID, err := p.Unpack(d)
a, pasID, err := c.AccountClientWithAccountIdFromPair(d, p)
if err != nil {
return err
}
var pas PrivateAccessSettings
var pas provisioning.UpsertPrivateAccessSettingsRequest
common.DataToStructPointer(d, s, &pas)
common.SetForceSendFields(&pas, d, []string{"public_access_enabled"})
pas.PasID = pasID
return NewPrivateAccessSettingsAPI(ctx, c).Update(&pas)
pas.PrivateAccessSettingsId = pasID
return a.PrivateAccess.Replace(ctx, pas)
},
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
accountID, pasID, err := p.Unpack(d)
a, pasID, err := c.AccountClientWithAccountIdFromPair(d, p)
if err != nil {
return err
}
return NewPrivateAccessSettingsAPI(ctx, c).Delete(accountID, pasID)
return a.PrivateAccess.DeleteByPrivateAccessSettingsId(ctx, pasID)
},
}.ToResource()
}