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

Ignore network diff for GCP workspaces with DB-managed VPC #1847

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions mws/resource_mws_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,20 +248,31 @@ var workspaceRunningUpdatesAllowed = []string{"credentials_id", "network_id", "s
// UpdateRunning will update running workspace with couple of possible fields
func (a WorkspacesAPI) UpdateRunning(ws Workspace, timeout time.Duration) error {
workspacesAPIPath := fmt.Sprintf("/accounts/%s/workspaces/%d", ws.AccountID, ws.WorkspaceID)
request := map[string]string{
"credentials_id": ws.CredentialsID,
// The ID of the workspace's network configuration object. Used only if you already use a customer-managed VPC.
// This change is supported only if you specified a network configuration ID when the workspace was created.
// In other words, you cannot switch from a Databricks-managed VPC to a customer-managed VPC. This parameter
// is available for updating both failed and running workspaces.
"network_id": ws.NetworkID,
request := map[string]string{}

if ws.CredentialsID != "" {
request["credentials_id"] = ws.CredentialsID
}

// The ID of the workspace's network configuration object. Used only if you already use a customer-managed VPC.
// This change is supported only if you specified a network configuration ID when the workspace was created.
// In other words, you cannot switch from a Databricks-managed VPC to a customer-managed VPC. This parameter
// is available for updating both failed and running workspaces.
if ws.NetworkID != "" {
request["network_id"] = ws.NetworkID
}

if ws.PrivateAccessSettingsID != "" {
request["private_access_settings_id"] = ws.PrivateAccessSettingsID
}
if ws.StorageCustomerManagedKeyID != "" {
request["storage_customer_managed_key_id"] = ws.StorageCustomerManagedKeyID
}

if len(request) == 0 {
return nil
}

err := a.client.Patch(a.context, workspacesAPIPath, request)
if err != nil {
return err
Expand All @@ -279,6 +290,13 @@ func (a WorkspacesAPI) Read(mwsAcctID, workspaceID string) (Workspace, error) {
host := generateWorkspaceHostname(a.client, mwsWorkspace)
mwsWorkspace.WorkspaceURL = fmt.Sprintf("https://%s", host)
}

if err == nil && mwsWorkspace.Network != nil {
//null out all network properties for GCP managed VPC
if mwsWorkspace.Network.NetworkID == "" {
mwsWorkspace.Network = nil
}
}
return mwsWorkspace, err
}

Expand Down Expand Up @@ -450,6 +468,7 @@ func ResourceMwsWorkspaces() *schema.Resource {
s["is_no_public_ip_enabled"].DiffSuppressFunc = func(k, old, new string, d *schema.ResourceData) bool {
return old != ""
}

s["customer_managed_key_id"].Deprecated = "Use managed_services_customer_managed_key_id instead"
s["customer_managed_key_id"].ConflictsWith = []string{"managed_services_customer_managed_key_id", "storage_customer_managed_key_id"}
s["managed_services_customer_managed_key_id"].ConflictsWith = []string{"customer_managed_key_id"}
Expand Down
131 changes: 129 additions & 2 deletions mws/resource_mws_workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,7 @@ func TestResourceWorkspaceRemovePAS_NotAllowed(t *testing.T) {
"private_access_settings_id": "pas",
},
State: map[string]any{
"account_id": "abc",

"account_id": "abc",
"aws_region": "us-east-1",
"credentials_id": "bcd",
"managed_services_customer_managed_key_id": "def",
Expand All @@ -1339,3 +1338,131 @@ func TestResourceWorkspaceRemovePAS_NotAllowed(t *testing.T) {
ID: "abc/1234",
}.ExpectError(t, "cannot remove private access setting from workspace")
}

func TestResourceWorkspaceCreateGcpManagedVPC(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "POST",
Resource: "/api/2.0/accounts/abc/workspaces",
// retreating to raw JSON, as certain fields don't work well together
ExpectedRequest: map[string]any{
"account_id": "abc",
"cloud": "gcp",
"cloud_resource_bucket": map[string]any{
"gcp": map[string]any{
"project_id": "def",
},
},
"location": "bcd",
"workspace_name": "labdata",
},
Response: Workspace{
WorkspaceID: 1234,
AccountID: "abc",
DeploymentName: "900150983cd24fb0",
WorkspaceName: "labdata",
},
},
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/accounts/abc/workspaces/1234",
Response: Workspace{
AccountID: "abc",
WorkspaceID: 1234,
WorkspaceStatus: WorkspaceStatusRunning,
DeploymentName: "900150983cd24fb0",
WorkspaceName: "labdata",
Network: &GCPNetwork{
GCPManagedNetworkConfig: &GCPManagedNetworkConfig{
SubnetCIDR: "a",
GKEClusterPodIPRange: "b",
GKEClusterServiceIPRange: "c",
},
GCPCommonNetworkConfig: &GCPCommonNetworkConfig{
GKEConnectivityType: "d",
GKEClusterMasterIPRange: "e",
},
},
},
},
},
Resource: ResourceMwsWorkspaces(),
HCL: `
account_id = "abc"
workspace_name = "labdata"
deployment_name = "900150983cd24fb0"
location = "bcd"
cloud_resource_bucket {
gcp {
project_id = "def"
}
}
`,
Gcp: true,
Create: true,
}.Apply(t)
assert.NoError(t, err, err)
assert.Equal(t, []any([]any{}), d.Get("network"), "Network configuration should be ignored")
}

func TestResourceWorkspaceUpdateGcpManagedVPCNoChange(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "GET",
ReuseRequest: true,
Resource: "/api/2.0/accounts/abc/workspaces/1234",
Response: Workspace{
AccountID: "abc",
Cloud: "gcp",
WorkspaceID: 1234,
WorkspaceStatus: WorkspaceStatusRunning,
DeploymentName: "900150983cd24fb0",
WorkspaceName: "labdata",
Network: &GCPNetwork{
GCPManagedNetworkConfig: &GCPManagedNetworkConfig{
SubnetCIDR: "a",
GKEClusterPodIPRange: "b",
GKEClusterServiceIPRange: "c",
},
GCPCommonNetworkConfig: &GCPCommonNetworkConfig{
GKEConnectivityType: "d",
GKEClusterMasterIPRange: "e",
},
},
},
},
},
Resource: ResourceMwsWorkspaces(),
InstanceState: map[string]string{
"account_id": "abc",
"workspace_name": "labdata",
"deployment_name": "900150983cd24fb0",
"location": "bcd",
"workspace_id": "1234",
"is_no_public_ip_enabled": "false",
"cloud_resource_bucket.#": "1",
"cloud_resource_bucket.0.gcp.0.project_id": "def",
"cloud": "gcp",
},
HCL: `
account_id = "abc"
workspace_name = "labdata"
deployment_name = "900150983cd24fb0"
location = "bcd"
cloud_resource_bucket {
gcp {
project_id = "def"
}
}
`,
Gcp: true,
Update: true,
ID: "abc/1234",
}.Apply(t)
assert.NoError(t, err, err)
assert.Equal(t, "abc/1234", d.Id(), "Id should be the same as in reading")
assert.Equal(t, []any([]any{}), d.Get("network"), "Network configuration should be ignored")
}