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 client to github.com/databricks/databricks-sdk-go #1848

Merged
merged 27 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from 19 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
23 changes: 9 additions & 14 deletions access/acceptance/sql_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"os"
"testing"

"github.com/databricks/terraform-provider-databricks/commands"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/terraform-provider-databricks/internal/acceptance"
"github.com/databricks/terraform-provider-databricks/internal/compute"
"github.com/databricks/terraform-provider-databricks/qa"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -21,23 +19,20 @@ func TestAccTableACL(t *testing.T) {
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
}
t.Parallel()
client := common.CommonEnvironmentClient()
client.WithCommandExecutor(func(ctx context.Context,
dc *common.DatabricksClient) common.CommandExecutor {
return commands.NewCommandsAPI(ctx, dc)
})

shell := client.CommandExecutor(context.Background())
clusterInfo := compute.NewTinyClusterInCommonPoolPossiblyReused()
talbeName := qa.RandomName("table_acl_")
ctx := context.Background()
w := databricks.Must(databricks.NewWorkspaceClient())
info, err := w.Clusters.GetOrCreateRunningCluster(ctx, "tf-dummy")
require.NoError(t, err)

cr := shell.Execute(clusterInfo.ClusterID, "python",
talbeName := qa.RandomName("table_acl_")
cr := w.CommandExecutor.Execute(ctx, info.ClusterId, "python",
fmt.Sprintf("spark.range(10).write.saveAsTable('%s')",
talbeName))
require.False(t, cr.Failed(), cr.Error())
os.Setenv("TABLE_ACL_TEST_TABLE", talbeName)
defer func() {
cr := shell.Execute(clusterInfo.ClusterID, "sql",
cr := w.CommandExecutor.Execute(ctx, info.ClusterId, "sql",
fmt.Sprintf("DROP TABLE %s", talbeName))
assert.False(t, cr.Failed(), cr.Error())
}()
Expand All @@ -47,7 +42,7 @@ func TestAccTableACL(t *testing.T) {
Template: `
resource "databricks_sql_permissions" "this" {
table = "{env.TABLE_ACL_TEST_TABLE}"

privilege_assignments {
principal = "users"
privileges = ["SELECT"]
Expand Down
44 changes: 6 additions & 38 deletions access/resource_ip_access_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ package access
import (
"context"
"net/http"
"os"
"testing"

"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/qa"

"github.com/stretchr/testify/assert"
Expand All @@ -25,37 +24,6 @@ var (
TestingIPAddressesState = []any{"1.2.3.4", "1.2.4.0/24"}
)

func TestAccIPACL(t *testing.T) {
cloud := os.Getenv("CLOUD_ENV")
if cloud == "" {
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
}
t.Parallel()
client := common.NewClientFromEnvironment()
ctx := context.Background()
ipAccessListsAPI := NewIPAccessListsAPI(ctx, client)
res, err := ipAccessListsAPI.Create(createIPAccessListRequest{
Label: qa.RandomName("ipacl-"),
ListType: "BLOCK",
IPAddresses: TestingIPAddresses,
})
require.NoError(t, err)
defer func() {
err = ipAccessListsAPI.Delete(res.ListID)
require.NoError(t, err)
}()
err = ipAccessListsAPI.Update(res.ListID, ipAccessListUpdateRequest{
Label: res.Label,
ListType: res.ListType,
Enabled: true,
IPAddresses: []string{"4.3.2.1"},
})
require.NoError(t, err)
updated, err := ipAccessListsAPI.Read(res.ListID)
require.NoError(t, err)
assert.Equal(t, "4.3.2.1", updated.IPAddresses[0])
}

func TestIPACLCreate(t *testing.T) {
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
Expand Down Expand Up @@ -123,7 +91,7 @@ func TestAPIACLCreate_Error(t *testing.T) {
{
Method: http.MethodPost,
Resource: "/api/2.0/ip-access-lists",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "RESOURCE_ALREADY_EXISTS",
Message: "IP access list with type (" + TestingListTypeString + ") and label (" + TestingLabel + ") already exists",
},
Expand Down Expand Up @@ -206,7 +174,7 @@ func TestIPACLUpdate_Error(t *testing.T) {
{
Method: http.MethodPut,
Resource: "/api/2.0/ip-access-lists/" + TestingID,
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "SERVER_ERROR",
Message: "Something unexpected happened",
},
Expand Down Expand Up @@ -261,7 +229,7 @@ func TestIPACLRead_NotFound(t *testing.T) {
{
Method: http.MethodGet,
Resource: "/api/2.0/ip-access-lists/" + TestingID,
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "RESOURCE_DOES_NOT_EXIST",
Message: "Can't find an IP access list with id: " + TestingID + ".",
},
Expand All @@ -281,7 +249,7 @@ func TestIPACLRead_Error(t *testing.T) {
{
Method: http.MethodGet,
Resource: "/api/2.0/ip-access-lists/" + TestingID,
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "SERVER_ERROR",
Message: "Something unexpected happened",
},
Expand Down Expand Up @@ -319,7 +287,7 @@ func TestIPACLDelete_Error(t *testing.T) {
{
Method: http.MethodDelete,
Resource: "/api/2.0/ip-access-lists/" + TestingID,
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "INVALID_STATE",
Message: "Something went wrong",
},
Expand Down
3 changes: 2 additions & 1 deletion access/resource_permission_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -56,7 +57,7 @@ func (l PermissionAssignmentList) ForPrincipal(principalId int64) (res Permissio
}
return Permissions{v.Permissions}, nil
}
return res, common.NotFound(fmt.Sprintf("%d not found", principalId))
return res, apierr.NotFound(fmt.Sprintf("%d not found", principalId))
}

func (a PermissionAssignmentAPI) List() (list PermissionAssignmentList, err error) {
Expand Down
5 changes: 3 additions & 2 deletions access/resource_sql_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"strings"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/clusters"
"github.com/databricks/terraform-provider-databricks/common"

Expand Down Expand Up @@ -120,7 +121,7 @@ func (ta *SqlPermissions) read() error {
failure := currentGrantsOnThis.Error()
if strings.Contains(failure, "does not exist") ||
strings.Contains(failure, "RESOURCE_DOES_NOT_EXIST") {
return common.NotFound(failure)
return apierr.NotFound(failure)
}
return fmt.Errorf("cannot read current grants: %s", failure)
}
Expand Down Expand Up @@ -249,7 +250,7 @@ func (ta *SqlPermissions) initCluster(ctx context.Context, d *schema.ResourceDat
}
}
clusterInfo, err := clustersAPI.StartAndGetInfo(ta.ClusterID)
if common.IsMissing(err) {
if apierr.IsMissing(err) {
// cluster that was previously in a tfstate was deleted
ta.ClusterID, err = ta.getOrCreateCluster(clustersAPI)
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions access/resource_sql_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func TestTableACLGrants(t *testing.T) {
}

func TestDatabaseACLGrants(t *testing.T) {
ta := SqlPermissions{ Database: "default",
exec: mockData{
"SHOW GRANT ON DATABASE `default`": {
// principal, actionType, objType, objectKey
// Test with and without backticks
{"users", "SELECT", "database", "default"},
{"users", "USAGE", "database", "`default`"},
},
}}
ta := SqlPermissions{Database: "default",
exec: mockData{
"SHOW GRANT ON DATABASE `default`": {
// principal, actionType, objType, objectKey
// Test with and without backticks
{"users", "SELECT", "database", "default"},
{"users", "USAGE", "database", "`default`"},
},
}}
err := ta.read()
assert.NoError(t, err)
assert.Len(t, ta.PrivilegeAssignments, 1)
Expand Down
11 changes: 9 additions & 2 deletions aws/acceptance/instance_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package acceptance
import (
"context"

"github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/config"
"github.com/databricks/terraform-provider-databricks/aws"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/internal/acceptance"
Expand All @@ -17,9 +19,14 @@ func TestAccAwsGroupInstanceProfileResource(t *testing.T) {
t.Skip("Acceptance tests skipped unless env 'CLOUD_ENV' is set")
}
ctx := context.WithValue(context.Background(), common.Current, t.Name())
client := common.CommonEnvironmentClient()
arn := qa.GetEnvOrSkipTest(t, "TEST_EC2_INSTANCE_PROFILE")
instanceProfilesAPI := aws.NewInstanceProfilesAPI(ctx, client)
client, err := client.New(&config.Config{})
if err != nil {
t.Fatal(err)
}
instanceProfilesAPI := aws.NewInstanceProfilesAPI(ctx, &common.DatabricksClient{
DatabricksClient: client,
})
instanceProfilesAPI.Synchronized(arn, func() bool {
if instanceProfilesAPI.IsRegistered(arn) {
return false
Expand Down
3 changes: 2 additions & 1 deletion aws/resource_group_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/terraform-provider-databricks/scim"

Expand All @@ -21,7 +22,7 @@ func ResourceGroupInstanceProfile() *schema.Resource {
group, err := scim.NewGroupsAPI(ctx, c).Read(groupID)
hasRole := scim.ComplexValues(group.Roles).HasValue(roleARN)
if err == nil && !hasRole {
return common.NotFound("Group has no instance profile")
return apierr.NotFound("Group has no instance profile")
}
return err
},
Expand Down
14 changes: 7 additions & 7 deletions aws/resource_group_instance_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package aws
import (
"testing"

"github.com/databricks/terraform-provider-databricks/common"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/scim"

"github.com/databricks/terraform-provider-databricks/qa"
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestResourceGroupInstanceProfileCreate_Error(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
},
Expand All @@ -80,7 +80,7 @@ func TestResourceGroupInstanceProfileCreate_Error_InvalidARN(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
},
Expand All @@ -103,7 +103,7 @@ func TestResourceGroupInstanceProfileCreate_Error_OtherARN(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
},
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestResourceGroupInstanceProfileRead_NotFound(t *testing.T) {
{
Method: "GET",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "NOT_FOUND",
Message: "Item not found",
},
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestResourceGroupInstanceProfileRead_Error(t *testing.T) {
{
Method: "GET",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
},
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestResourceGroupInstanceProfileDelete_Error(t *testing.T) {
{
Method: "PATCH",
Resource: "/api/2.0/preview/scim/v2/Groups/abc",
Response: common.APIErrorBody{
Response: apierr.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
},
Expand Down
11 changes: 3 additions & 8 deletions aws/resource_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/terraform-provider-databricks/common"

"github.com/hashicorp/go-cty/cty"
Expand Down Expand Up @@ -60,13 +60,8 @@ func (a InstanceProfilesAPI) Read(instanceProfileARN string) (result InstancePro
return
}
}
err = common.APIError{
ErrorCode: "NOT_FOUND",
Message: fmt.Sprintf("Instance profile with name: %s not found in "+
"list of instance profiles in the workspace!", instanceProfileARN),
Resource: "/api/2.0/instance-profiles/list",
StatusCode: http.StatusNotFound,
}
err = apierr.NotFound(fmt.Sprintf("Instance profile with name: %s not found in "+
"list of instance profiles in the workspace!", instanceProfileARN))
return
}

Expand Down
Loading