From 67dca86867070751aecc36aac93a75ff29a461cd Mon Sep 17 00:00:00 2001 From: Sriharsha Tikkireddy Date: Mon, 20 Apr 2020 11:45:01 -0400 Subject: [PATCH] Fixed & refactored linting issues in client/service package --- client/service/apis.go | 14 ++ client/service/client.go | 24 ++- client/service/clusters.go | 8 +- client/service/clusters_test.go | 136 ++++++++--------- client/service/commands.go | 107 ++++++------- client/service/commands_integration_test.go | 18 +-- client/service/commands_test.go | 144 +++++++++--------- client/service/dbfs.go | 14 +- client/service/dbfs_test.go | 54 +++---- client/service/groups.go | 8 +- client/service/groups_test.go | 24 +-- client/service/instance_pools.go | 15 +- client/service/instance_pools_test.go | 12 +- client/service/instance_profiles.go | 10 +- client/service/instance_profiles_test.go | 18 +-- client/service/jobs.go | 24 +-- client/service/jobs_test.go | 32 ++-- client/service/libraries.go | 25 +-- client/service/libraries_integration_test.go | 10 +- client/service/libraries_test.go | 32 ++-- client/service/main_test.go | 9 +- client/service/mask_utils.go | 7 +- client/service/notebooks.go | 12 +- client/service/notebooks_test.go | 30 ++-- client/service/secret_acls.go | 2 +- client/service/secret_scopes.go | 10 +- client/service/secrets.go | 10 +- .../secrets_scopes_acls_integration_test.go | 6 +- client/service/tokens.go | 6 +- client/service/tokens_test.go | 34 ++--- client/service/users.go | 43 +++--- client/service/users_integration_test.go | 10 +- client/service/users_test.go | 126 +++++++-------- 33 files changed, 552 insertions(+), 482 deletions(-) diff --git a/client/service/apis.go b/client/service/apis.go index dea68e311..fd67e6ddb 100644 --- a/client/service/apis.go +++ b/client/service/apis.go @@ -4,6 +4,7 @@ var scimHeaders = map[string]string{ "Content-Type": "application/scim+json", } +// DBApiClient is the client struct that contains clients for all the services available on Databricks type DBApiClient struct { config *DBApiClientConfig } @@ -20,54 +21,67 @@ func (c DBApiClient) Clusters() ClustersAPI { return ClustersAPI{Client: c} } +// Secrets returns an instance of SecretsAPI func (c DBApiClient) Secrets() SecretsAPI { return SecretsAPI{Client: c} } +// SecretScopes returns an instance of SecretScopesAPI func (c DBApiClient) SecretScopes() SecretScopesAPI { return SecretScopesAPI{Client: c} } +// SecretAcls returns an instance of SecretAclsAPI func (c DBApiClient) SecretAcls() SecretAclsAPI { return SecretAclsAPI{Client: c} } +// Tokens returns an instance of TokensAPI func (c *DBApiClient) Tokens() TokensAPI { return TokensAPI{Client: c} } +// Users returns an instance of UsersAPI func (c DBApiClient) Users() UsersAPI { return UsersAPI{Client: c} } +// Groups returns an instance of GroupsAPI func (c DBApiClient) Groups() GroupsAPI { return GroupsAPI{Client: c} } +// Notebooks returns an instance of NotebooksAPI func (c DBApiClient) Notebooks() NotebooksAPI { return NotebooksAPI{Client: c} } +// Jobs returns an instance of JobsAPI func (c DBApiClient) Jobs() JobsAPI { return JobsAPI{Client: c} } +// DBFS returns an instance of DBFSAPI func (c DBApiClient) DBFS() DBFSAPI { return DBFSAPI{Client: c} } +// Libraries returns an instance of LibrariesAPI func (c DBApiClient) Libraries() LibrariesAPI { return LibrariesAPI{Client: c} } +// InstancePools returns an instance of InstancePoolsAPI func (c DBApiClient) InstancePools() InstancePoolsAPI { return InstancePoolsAPI{Client: c} } +// InstanceProfiles returns an instance of InstanceProfilesAPI func (c DBApiClient) InstanceProfiles() InstanceProfilesAPI { return InstanceProfilesAPI{Client: c} } +// Commands returns an instance of CommandsAPI func (c DBApiClient) Commands() CommandsAPI { return CommandsAPI{Client: c} } diff --git a/client/service/client.go b/client/service/client.go index c7c9fcc43..3f826675f 100644 --- a/client/service/client.go +++ b/client/service/client.go @@ -14,13 +14,16 @@ import ( "time" ) +// CloudServiceProvider is a custom type for different types of cloud service providers type CloudServiceProvider string +// List of CloudServiceProviders Databricks is available on const ( AWS CloudServiceProvider = "AmazonWebServices" Azure CloudServiceProvider = "Azure" ) +// DBApiErrorBody is a struct for a custom api error for all the services on databrickss. type DBApiErrorBody struct { ErrorCode string `json:"error_code,omitempty"` Message string `json:"message,omitempty"` @@ -29,18 +32,22 @@ type DBApiErrorBody struct { ScimStatus string `json:"status,omitempty"` } +// DBApiError is a generic struct for an api error on databricks type DBApiError struct { ErrorBody *DBApiErrorBody StatusCode int Err error } +// Error is a interface implementation of the error interface. func (r DBApiError) Error() string { return fmt.Sprintf("status %d: err %v", r.StatusCode, r.Err) } +// AuthType is a custom type for a type of authentication allowed on Databricks type AuthType string +// List of AuthTypes supported by this go sdk. const ( BasicAuth AuthType = "BASIC" ) @@ -131,19 +138,18 @@ func (c DBApiClientConfig) getRequestURI(path string, apiVersion string) (string func onlyNBytes(j string, numBytes int64) string { if len([]byte(j)) > int(numBytes) { return string([]byte(j)[:numBytes]) - } else { - return j } + return j } func auditNonGetPayload(method string, uri string, object interface{}, mask *SecretsMask) { logStmt := struct { Method string - Uri string + URI string Payload interface{} }{ Method: method, - Uri: uri, + URI: uri, Payload: object, } jsonStr, _ := json.Marshal(Mask(logStmt)) @@ -157,10 +163,10 @@ func auditNonGetPayload(method string, uri string, object interface{}, mask *Sec func auditGetPayload(uri string, mask *SecretsMask) { logStmt := struct { Method string - Uri string + URI string }{ Method: "GET", - Uri: uri, + URI: uri, } jsonStr, _ := json.Marshal(Mask(logStmt)) if mask != nil { @@ -170,7 +176,9 @@ func auditGetPayload(uri string, mask *SecretsMask) { } } -func PerformQuery(config *DBApiClientConfig, method, path string, apiVersion string, headers map[string]string, marshalJson bool, useRawPath bool, data interface{}, secretsMask *SecretsMask) (body []byte, err error) { +// PerformQuery is a generic function that accepts a config, method, path, apiversion, headers, +// and some flags to perform query against the Databricks api +func PerformQuery(config *DBApiClientConfig, method, path string, apiVersion string, headers map[string]string, marshalJSON bool, useRawPath bool, data interface{}, secretsMask *SecretsMask) (body []byte, err error) { var requestURL string if useRawPath { requestURL = path @@ -199,7 +207,7 @@ func PerformQuery(config *DBApiClientConfig, method, path string, apiVersion str auditGetPayload(requestURL, secretsMask) } else { - if marshalJson { + if marshalJSON { bodyBytes, err := json.Marshal(data) if err != nil { return nil, err diff --git a/client/service/clusters.go b/client/service/clusters.go index c62c3fe35..53baf405f 100644 --- a/client/service/clusters.go +++ b/client/service/clusters.go @@ -9,6 +9,7 @@ import ( "time" ) +// ClustersAPI is a struct that contains the Databricks api client to perform queries type ClustersAPI struct { Client DBApiClient } @@ -26,12 +27,13 @@ func (a ClustersAPI) Create(cluster model.Cluster) (model.ClusterInfo, error) { return clusterInfo, err } -// Update edits the configuration of a cluster to match the provided attributes and size +// Edit edits the configuration of a cluster to match the provided attributes and size func (a ClustersAPI) Edit(clusterInfo model.Cluster) error { _, err := a.Client.performQuery(http.MethodPost, "/clusters/edit", "2.0", nil, clusterInfo, nil) return err } +// ListZones returns the zones info sent by the cloud service provider func (a ClustersAPI) ListZones() (model.ZonesInfo, error) { var zonesInfo model.ZonesInfo resp, err := a.Client.performQuery(http.MethodGet, "/clusters/list-zones", "2.0", nil, nil, nil) @@ -64,6 +66,7 @@ func (a ClustersAPI) Restart(clusterID string) error { return err } +// WaitForClusterRunning will block main thread and wait till cluster is in a RUNNING state func (a ClustersAPI) WaitForClusterRunning(clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error { errChan := make(chan error, 1) go func() { @@ -93,6 +96,7 @@ func (a ClustersAPI) WaitForClusterRunning(clusterID string, sleepDurationSecond } } +// WaitForClusterTerminated will block main thread and wait till cluster is in a TERMINATED state func (a ClustersAPI) WaitForClusterTerminated(clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error { errChan := make(chan error, 1) go func() { @@ -149,7 +153,7 @@ func (a ClustersAPI) PermanentDelete(clusterID string) error { return err } -// Read retrieves the information for a cluster given its identifier +// Get retrieves the information for a cluster given its identifier func (a ClustersAPI) Get(clusterID string) (model.ClusterInfo, error) { var clusterInfo model.ClusterInfo diff --git a/client/service/clusters_test.go b/client/service/clusters_test.go index 9f299fc39..7b85266af 100644 --- a/client/service/clusters_test.go +++ b/client/service/clusters_test.go @@ -53,14 +53,14 @@ func TestClustersAPI_Create(t *testing.T) { func TestClustersAPI_Get(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` } tests := []struct { name string response string responseStatus int args args - wantUri string + wantURI string want interface{} wantErr bool }{ @@ -135,9 +135,9 @@ func TestClustersAPI_Get(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", }, - wantUri: "/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: "/api/2.0/clusters/get?cluster_id=11203-my-cluster", want: model.ClusterInfo{ AutoScale: &model.AutoScale{ MinWorkers: 2, @@ -208,9 +208,9 @@ func TestClustersAPI_Get(t *testing.T) { response: ``, responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", }, - wantUri: "/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: "/api/2.0/clusters/get?cluster_id=11203-my-cluster", want: model.ClusterInfo{}, wantErr: true, }, @@ -218,8 +218,8 @@ func TestClustersAPI_Get(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Clusters().Get(tt.args.ClusterId) + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Clusters().Get(tt.args.ClusterID) }) }) } @@ -231,7 +231,7 @@ func TestClustersAPI_List(t *testing.T) { name string response string responseStatus int - wantUri string + wantURI string want interface{} wantErr bool }{ @@ -260,7 +260,7 @@ func TestClustersAPI_List(t *testing.T) { ] }`, responseStatus: http.StatusOK, - wantUri: "/api/2.0/clusters/list?", + wantURI: "/api/2.0/clusters/list?", want: []model.ClusterInfo{ { ClusterName: "autoscaling-cluster", @@ -287,14 +287,14 @@ func TestClustersAPI_List(t *testing.T) { name: "List failure test", response: ``, responseStatus: http.StatusBadRequest, - wantUri: "/api/2.0/clusters/list?", + wantURI: "/api/2.0/clusters/list?", want: []model.ClusterInfo{}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertRequestWithMockServer(t, nil, http.MethodGet, tt.wantUri, nil, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, nil, http.MethodGet, tt.wantURI, nil, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Clusters().List() }) }) @@ -307,7 +307,7 @@ func TestClustersAPI_ListZones(t *testing.T) { name string response string responseStatus int - wantUri string + wantURI string want interface{} wantErr bool }{ @@ -322,7 +322,7 @@ func TestClustersAPI_ListZones(t *testing.T) { "default_zone": "us-west-2b" }`, responseStatus: http.StatusOK, - wantUri: "/api/2.0/clusters/list-zones?", + wantURI: "/api/2.0/clusters/list-zones?", want: model.ZonesInfo{ Zones: []string{"us-west-2b", @@ -336,14 +336,14 @@ func TestClustersAPI_ListZones(t *testing.T) { name: "ListZones failure test", response: ``, responseStatus: http.StatusBadRequest, - wantUri: "/api/2.0/clusters/list-zones?", + wantURI: "/api/2.0/clusters/list-zones?", want: model.ZonesInfo{}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertRequestWithMockServer(t, nil, http.MethodGet, tt.wantUri, nil, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, nil, http.MethodGet, tt.wantURI, nil, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Clusters().ListZones() }) }) @@ -356,7 +356,7 @@ func TestClustersAPI_ListNodeTypes(t *testing.T) { name string response string responseStatus int - wantUri string + wantURI string want interface{} wantErr bool }{ @@ -409,7 +409,7 @@ func TestClustersAPI_ListNodeTypes(t *testing.T) { ] }`, responseStatus: http.StatusOK, - wantUri: "/api/2.0/clusters/list-node-types?", + wantURI: "/api/2.0/clusters/list-node-types?", want: []model.NodeType{ { NodeTypeID: "r3.xlarge", @@ -434,14 +434,14 @@ func TestClustersAPI_ListNodeTypes(t *testing.T) { name: "ListNodeTypes failure test", response: ``, responseStatus: http.StatusBadRequest, - wantUri: "/api/2.0/clusters/list-node-types?", + wantURI: "/api/2.0/clusters/list-node-types?", want: []model.NodeType{}, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertRequestWithMockServer(t, nil, http.MethodGet, tt.wantUri, nil, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, nil, http.MethodGet, tt.wantURI, nil, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Clusters().ListNodeTypes() }) }) @@ -450,7 +450,7 @@ func TestClustersAPI_ListNodeTypes(t *testing.T) { func TestClustersAPI_WaitForClusterRunning(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id"` + ClusterID string `json:"cluster_id"` SleepDurationSeconds time.Duration `json:"sleep_duration_seconds"` TimeoutDurationMinutes time.Duration `json:"timeout_duration_minutes"` } @@ -460,7 +460,7 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { responseStatus []int requestMethod []string args []interface{} - wantUri []string + wantURI []string want []model.NotebookInfo wantErr bool }{ @@ -483,12 +483,12 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 0, TimeoutDurationMinutes: 1, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -513,12 +513,12 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 0, TimeoutDurationMinutes: 1, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -543,12 +543,12 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 0, TimeoutDurationMinutes: 1, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -573,12 +573,12 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 1, TimeoutDurationMinutes: 0, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -587,8 +587,8 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantUri, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().WaitForClusterRunning(tt.args[0].(*args).ClusterId, tt.args[0].(*args).SleepDurationSeconds, tt.args[0].(*args).TimeoutDurationMinutes) + AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantURI, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return nil, client.Clusters().WaitForClusterRunning(tt.args[0].(*args).ClusterID, tt.args[0].(*args).SleepDurationSeconds, tt.args[0].(*args).TimeoutDurationMinutes) }) }) } @@ -596,7 +596,7 @@ func TestClustersAPI_WaitForClusterRunning(t *testing.T) { func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id"` + ClusterID string `json:"cluster_id"` SleepDurationSeconds time.Duration `json:"sleep_duration_seconds"` TimeoutDurationMinutes time.Duration `json:"timeout_duration_minutes"` } @@ -606,7 +606,7 @@ func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { responseStatus []int requestMethod []string args []interface{} - wantUri []string + wantURI []string want []model.NotebookInfo wantErr bool }{ @@ -629,12 +629,12 @@ func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 0, TimeoutDurationMinutes: 1, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -659,12 +659,12 @@ func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 0, TimeoutDurationMinutes: 1, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -689,12 +689,12 @@ func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 0, TimeoutDurationMinutes: 1, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -719,12 +719,12 @@ func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { requestMethod: []string{http.MethodGet, http.MethodGet, http.MethodGet}, args: []interface{}{ &args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", SleepDurationSeconds: 1, TimeoutDurationMinutes: 0, }, }, - wantUri: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", + wantURI: []string{"/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster", "/api/2.0/clusters/get?cluster_id=11203-my-cluster"}, want: nil, @@ -733,8 +733,8 @@ func TestClustersAPI_WaitForClusterTerminated(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantUri, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().WaitForClusterTerminated(tt.args[0].(*args).ClusterId, tt.args[0].(*args).SleepDurationSeconds, tt.args[0].(*args).TimeoutDurationMinutes) + AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantURI, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return nil, client.Clusters().WaitForClusterTerminated(tt.args[0].(*args).ClusterID, tt.args[0].(*args).SleepDurationSeconds, tt.args[0].(*args).TimeoutDurationMinutes) }) }) } @@ -786,7 +786,7 @@ func TestClustersAPI_Edit(t *testing.T) { func TestClustersAPI_Start(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty"` } tests := []struct { @@ -802,7 +802,7 @@ func TestClustersAPI_Start(t *testing.T) { response: ``, responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: false, @@ -812,7 +812,7 @@ func TestClustersAPI_Start(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: true, @@ -822,7 +822,7 @@ func TestClustersAPI_Start(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/clusters/start", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().Start(tt.args.ClusterId) + return nil, client.Clusters().Start(tt.args.ClusterID) }) }) } @@ -830,7 +830,7 @@ func TestClustersAPI_Start(t *testing.T) { func TestClustersAPI_Restart(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty"` } tests := []struct { @@ -846,7 +846,7 @@ func TestClustersAPI_Restart(t *testing.T) { response: ``, responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: false, @@ -856,7 +856,7 @@ func TestClustersAPI_Restart(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: true, @@ -866,7 +866,7 @@ func TestClustersAPI_Restart(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/clusters/restart", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().Restart(tt.args.ClusterId) + return nil, client.Clusters().Restart(tt.args.ClusterID) }) }) } @@ -874,7 +874,7 @@ func TestClustersAPI_Restart(t *testing.T) { func TestClustersAPI_Pin(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty"` } tests := []struct { @@ -890,7 +890,7 @@ func TestClustersAPI_Pin(t *testing.T) { response: ``, responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: false, @@ -900,7 +900,7 @@ func TestClustersAPI_Pin(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: true, @@ -910,7 +910,7 @@ func TestClustersAPI_Pin(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/clusters/pin", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().Pin(tt.args.ClusterId) + return nil, client.Clusters().Pin(tt.args.ClusterID) }) }) } @@ -918,7 +918,7 @@ func TestClustersAPI_Pin(t *testing.T) { func TestClustersAPI_Unpin(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty"` } tests := []struct { @@ -934,7 +934,7 @@ func TestClustersAPI_Unpin(t *testing.T) { response: ``, responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: false, @@ -944,7 +944,7 @@ func TestClustersAPI_Unpin(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: true, @@ -954,7 +954,7 @@ func TestClustersAPI_Unpin(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/clusters/unpin", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().Unpin(tt.args.ClusterId) + return nil, client.Clusters().Unpin(tt.args.ClusterID) }) }) } @@ -962,7 +962,7 @@ func TestClustersAPI_Unpin(t *testing.T) { func TestClustersAPI_Delete(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty"` } tests := []struct { @@ -978,7 +978,7 @@ func TestClustersAPI_Delete(t *testing.T) { response: ``, responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: false, @@ -988,7 +988,7 @@ func TestClustersAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: true, @@ -998,7 +998,7 @@ func TestClustersAPI_Delete(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/clusters/delete", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().Delete(tt.args.ClusterId) + return nil, client.Clusters().Delete(tt.args.ClusterID) }) }) } @@ -1006,7 +1006,7 @@ func TestClustersAPI_Delete(t *testing.T) { func TestClustersAPI_PermanentDelete(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty"` } tests := []struct { @@ -1022,7 +1022,7 @@ func TestClustersAPI_PermanentDelete(t *testing.T) { response: ``, responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: false, @@ -1032,7 +1032,7 @@ func TestClustersAPI_PermanentDelete(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, want: nil, wantErr: true, @@ -1042,7 +1042,7 @@ func TestClustersAPI_PermanentDelete(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/clusters/permanent-delete", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Clusters().PermanentDelete(tt.args.ClusterId) + return nil, client.Clusters().PermanentDelete(tt.args.ClusterID) }) }) } diff --git a/client/service/commands.go b/client/service/commands.go index 92daf4ca5..c22b61c0d 100644 --- a/client/service/commands.go +++ b/client/service/commands.go @@ -10,70 +10,71 @@ import ( "time" ) -// TokensAPI exposes the Secrets API +// CommandsAPI exposes the Context & Commands API type CommandsAPI struct { Client DBApiClient } -func (a CommandsAPI) Execute(clusterId, langauge, commandStr string) (model.Command, error) { +// Execute creates a spark context and executes a command and then closes context +func (a CommandsAPI) Execute(clusterID, langauge, commandStr string) (model.Command, error) { var resp model.Command - context, err := a.createContext(langauge, clusterId) + context, err := a.createContext(langauge, clusterID) if err != nil { return resp, err } - err = a.waitForContextReady(context, clusterId, 1, 10) + err = a.waitForContextReady(context, clusterID, 1, 10) if err != nil { return resp, err } - commandId, err := a.createCommand(context, clusterId, langauge, commandStr) + commandID, err := a.createCommand(context, clusterID, langauge, commandStr) if err != nil { return resp, err } - err = a.waitForCommandFinished(commandId, context, clusterId, 5, 10) + err = a.waitForCommandFinished(commandID, context, clusterID, 5, 10) if err != nil { return resp, err } - command, err := a.getCommand(commandId, context, clusterId) + command, err := a.getCommand(commandID, context, clusterID) if err != nil { return resp, err } - err = a.deleteContext(context, clusterId) + err = a.deleteContext(context, clusterID) return command, err } -func (a CommandsAPI) createCommand(contextId, clusterId, language, commandStr string) (string, error) { +func (a CommandsAPI) createCommand(contextID, clusterID, language, commandStr string) (string, error) { var command struct { - Id string `json:"id,omitempty"` + ID string `json:"id,omitempty"` } commandRequest := struct { Language string `json:"language,omitempty"` - ClusterId string `json:"clusterId,omitempty"` - ContextId string `json:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty"` + ContextID string `json:"contextId,omitempty"` Command string `json:"command,omitempty"` }{ Language: language, - ClusterId: clusterId, - ContextId: contextId, + ClusterID: clusterID, + ContextID: contextID, Command: commandStr, } resp, err := a.Client.performQuery(http.MethodPost, "/commands/execute", "1.2", nil, commandRequest, nil) if err != nil { - return command.Id, err + return command.ID, err } err = json.Unmarshal(resp, &command) - return command.Id, err + return command.ID, err } -func (a CommandsAPI) getCommand(commandId, contextId, clusterId string) (model.Command, error) { +func (a CommandsAPI) getCommand(commandID, contextID, clusterID string) (model.Command, error) { var commandResp model.Command contextGetRequest := struct { - CommandId string `json:"commandId,omitempty" url:"commandId,omitempty"` - ContextId string `json:"contextId,omitempty" url:"contextId,omitempty"` - ClusterId string `json:"clusterId,omitempty" url:"clusterId,omitempty"` + CommandID string `json:"commandId,omitempty" url:"commandId,omitempty"` + ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"` }{ - CommandId: commandId, - ContextId: contextId, - ClusterId: clusterId, + CommandID: commandID, + ContextID: contextID, + ClusterID: clusterID, } resp, err := a.Client.performQuery(http.MethodGet, "/commands/status", "1.2", nil, contextGetRequest, nil) if err != nil { @@ -83,11 +84,11 @@ func (a CommandsAPI) getCommand(commandId, contextId, clusterId string) (model.C return commandResp, err } -func (a CommandsAPI) waitForCommandFinished(commandId, contextId, clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error { +func (a CommandsAPI) waitForCommandFinished(commandID, contextID, clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error { errChan := make(chan error, 1) go func() { for { - commandInfo, err := a.getCommand(commandId, contextId, clusterID) + commandInfo, err := a.getCommand(commandID, contextID, clusterID) if err != nil { errChan <- err return @@ -96,7 +97,7 @@ func (a CommandsAPI) waitForCommandFinished(commandId, contextId, clusterID stri errChan <- nil return } else if commandInfo.Status == "Cancelling" || commandInfo.Status == "Cancelled" || commandInfo.Status == "Error" { - errChan <- errors.New(fmt.Sprintf("Context is in a failure state: %s.", commandInfo.Status)) + errChan <- fmt.Errorf("context is in a failure state: %s", commandInfo.Status) return } log.Println(fmt.Sprintf("Waiting for command to finish, current state is: %s.", commandInfo.Status)) @@ -111,37 +112,37 @@ func (a CommandsAPI) waitForCommandFinished(commandId, contextId, clusterID stri } } -func (a CommandsAPI) deleteCommand(commandId, contextId, clusterId string) error { +func (a CommandsAPI) deleteCommand(commandID, contextID, clusterID string) error { contextDeleteRequest := struct { - CommandId string `json:"commandId,omitempty" url:"commandId,omitempty"` - ContextId string `json:"contextId,omitempty" url:"contextId,omitempty"` - ClusterId string `json:"clusterId,omitempty" url:"clusterId,omitempty"` + CommandID string `json:"commandId,omitempty" url:"commandId,omitempty"` + ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"` }{ - CommandId: commandId, - ContextId: contextId, - ClusterId: clusterId, + CommandID: commandID, + ContextID: contextID, + ClusterID: clusterID, } _, err := a.Client.performQuery(http.MethodPost, "/commands/cancel", "1.2", nil, contextDeleteRequest, nil) return err } -func (a CommandsAPI) deleteContext(contextId, clusterId string) error { +func (a CommandsAPI) deleteContext(contextID, clusterID string) error { contextDeleteRequest := struct { - ContextId string `json:"contextId,omitempty" url:"contextId,omitempty"` - ClusterId string `json:"clusterId,omitempty" url:"clusterId,omitempty"` + ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"` }{ - ContextId: contextId, - ClusterId: clusterId, + ContextID: contextID, + ClusterID: clusterID, } _, err := a.Client.performQuery(http.MethodPost, "/contexts/destroy", "1.2", nil, contextDeleteRequest, nil) return err } -func (a CommandsAPI) waitForContextReady(contextId, clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error { +func (a CommandsAPI) waitForContextReady(contextID, clusterID string, sleepDurationSeconds time.Duration, timeoutDurationMinutes time.Duration) error { errChan := make(chan error, 1) go func() { for { - status, err := a.getContext(contextId, clusterID) + status, err := a.getContext(contextID, clusterID) if err != nil { errChan <- err return @@ -150,7 +151,7 @@ func (a CommandsAPI) waitForContextReady(contextId, clusterID string, sleepDurat errChan <- nil return } else if status == "Error" { - errChan <- errors.New("Context is in a errored state.") + errChan <- errors.New("context is in a errored state") return } log.Println("Waiting for context to go to running, current state is pending.") @@ -165,17 +166,17 @@ func (a CommandsAPI) waitForContextReady(contextId, clusterID string, sleepDurat } } -func (a CommandsAPI) getContext(contextId, clusterId string) (string, error) { +func (a CommandsAPI) getContext(contextID, clusterID string) (string, error) { var contextStatus struct { - Id string `json:"id,omitempty"` + ID string `json:"id,omitempty"` Status string `json:"status,omitempty"` } contextGetRequest := struct { - ContextId string `json:"contextId,omitempty" url:"contextId,omitempty"` - ClusterId string `json:"clusterId,omitempty" url:"clusterId,omitempty"` + ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"` }{ - ContextId: contextId, - ClusterId: clusterId, + ContextID: contextID, + ClusterID: clusterID, } resp, err := a.Client.performQuery(http.MethodGet, "/contexts/status", "1.2", nil, contextGetRequest, nil) if err != nil { @@ -185,22 +186,22 @@ func (a CommandsAPI) getContext(contextId, clusterId string) (string, error) { return contextStatus.Status, err } -func (a CommandsAPI) createContext(language, clusterId string) (string, error) { +func (a CommandsAPI) createContext(language, clusterID string) (string, error) { var context struct { - Id string `json:"id,omitempty"` + ID string `json:"id,omitempty"` } contextRequest := struct { Language string `json:"language,omitempty"` - ClusterId string `json:"clusterId,omitempty"` + ClusterID string `json:"clusterId,omitempty"` }{ Language: language, - ClusterId: clusterId, + ClusterID: clusterID, } resp, err := a.Client.performQuery(http.MethodPost, "/contexts/create", "1.2", nil, contextRequest, nil) if err != nil { - return context.Id, err + return context.ID, err } err = json.Unmarshal(resp, &context) - return context.Id, err + return context.ID, err } diff --git a/client/service/commands_integration_test.go b/client/service/commands_integration_test.go index 63b0e756b..de6b70fff 100644 --- a/client/service/commands_integration_test.go +++ b/client/service/commands_integration_test.go @@ -38,35 +38,35 @@ func TestContext(t *testing.T) { assert.NoError(t, err, err) }() - clusterId := clusterInfo.ClusterID + clusterID := clusterInfo.ClusterID - err = client.Clusters().WaitForClusterRunning(clusterId, 10, 20) + err = client.Clusters().WaitForClusterRunning(clusterID, 10, 20) assert.NoError(t, err, err) - context, err := client.Commands().createContext("python", clusterId) + context, err := client.Commands().createContext("python", clusterID) assert.NoError(t, err, err) t.Log(context) - err = client.Commands().waitForContextReady(context, clusterId, 1, 1) + err = client.Commands().waitForContextReady(context, clusterID, 1, 1) assert.NoError(t, err, err) - status, err := client.Commands().getContext(context, clusterId) + status, err := client.Commands().getContext(context, clusterID) assert.NoError(t, err, err) assert.True(t, status == "Running") t.Log(status) - commandId, err := client.Commands().createCommand(context, clusterId, "python", "print('hello world')") + commandID, err := client.Commands().createCommand(context, clusterID, "python", "print('hello world')") assert.NoError(t, err, err) - err = client.Commands().waitForCommandFinished(commandId, context, clusterId, 5, 20) + err = client.Commands().waitForCommandFinished(commandID, context, clusterID, 5, 20) assert.NoError(t, err, err) - resp, err := client.Commands().getCommand(commandId, context, clusterId) + resp, err := client.Commands().getCommand(commandID, context, clusterID) assert.NoError(t, err, err) assert.NotNil(t, resp.Results.Data) // Testing the public api Execute - command, err := client.Commands().Execute(clusterId, "python", "print('hello world')") + command, err := client.Commands().Execute(clusterID, "python", "print('hello world')") assert.NoError(t, err, err) assert.NotNil(t, command.Results.Data) } diff --git a/client/service/commands_test.go b/client/service/commands_test.go index 25446f680..df9ca22e9 100644 --- a/client/service/commands_test.go +++ b/client/service/commands_test.go @@ -9,21 +9,21 @@ import ( func TestCommandsAPI_Execute(t *testing.T) { type context struct { Language string `json:"language,omitempty"` - ClusterId string `json:"clusterId,omitempty"` + ClusterID string `json:"clusterId,omitempty"` } type command struct { Language string `json:"language,omitempty"` - ClusterId string `json:"clusterId,omitempty"` - ContextId string `json:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty"` + ContextID string `json:"contextId,omitempty"` Command string `json:"command,omitempty"` } type contextDelete struct { - ContextId string `json:"contextId,omitempty" url:"contextId,omitempty"` - ClusterId string `json:"clusterId,omitempty" url:"clusterId,omitempty"` + ContextID string `json:"contextId,omitempty" url:"contextId,omitempty"` + ClusterID string `json:"clusterId,omitempty" url:"clusterId,omitempty"` } type params struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` Language string `json:"language,omitempty" url:"language,omitempty"` CommandStr string `json:"command_str,omitempty" url:"command_str,omitempty"` } @@ -35,14 +35,14 @@ func TestCommandsAPI_Execute(t *testing.T) { requestMethod []string postStructExpect []interface{} args []interface{} - wantUri []string + wantURI []string want interface{} wantErr bool }{ { name: "Execute test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -74,20 +74,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -98,7 +98,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -116,7 +116,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute context failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -148,20 +148,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -172,7 +172,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -184,7 +184,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute context status failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -216,20 +216,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -240,7 +240,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -252,7 +252,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute command create failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -284,20 +284,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -308,7 +308,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -320,7 +320,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute command status failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -352,20 +352,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -376,7 +376,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -388,7 +388,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute command results fetch failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -420,20 +420,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -444,7 +444,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -456,7 +456,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute context close failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -488,20 +488,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -512,7 +512,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -530,7 +530,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute context invalid state failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -562,20 +562,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -586,7 +586,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -598,7 +598,7 @@ func TestCommandsAPI_Execute(t *testing.T) { { name: "Execute command invalid state failure test", params: params{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Language: "python", CommandStr: `print("hello world")`, }, @@ -630,20 +630,20 @@ func TestCommandsAPI_Execute(t *testing.T) { args: []interface{}{ &context{ Language: "python", - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", }, nil, &command{ Language: "python", - ClusterId: "my-cluster-id", - ContextId: "my-context-id", + ClusterID: "my-cluster-id", + ContextID: "my-context-id", Command: `print("hello world")`, }, nil, nil, &contextDelete{ - ContextId: "my-context-id", - ClusterId: "my-cluster-id", + ContextID: "my-context-id", + ClusterID: "my-cluster-id", }, }, postStructExpect: []interface{}{ @@ -654,7 +654,7 @@ func TestCommandsAPI_Execute(t *testing.T) { nil, &contextDelete{}, }, - wantUri: []string{"/api/1.2/contexts/create", + wantURI: []string{"/api/1.2/contexts/create", "/api/1.2/contexts/status?clusterId=my-cluster-id&contextId=my-context-id", "/api/1.2/commands/execute", "/api/1.2/commands/status?clusterId=my-cluster-id&commandId=my-command-id&contextId=my-context-id", @@ -666,8 +666,8 @@ func TestCommandsAPI_Execute(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantUri, tt.postStructExpect, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Commands().Execute(tt.params.ClusterId, tt.params.Language, tt.params.CommandStr) + AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantURI, tt.postStructExpect, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Commands().Execute(tt.params.ClusterID, tt.params.Language, tt.params.CommandStr) }) }) } diff --git a/client/service/dbfs.go b/client/service/dbfs.go index d5b9587a0..8c69b84ea 100644 --- a/client/service/dbfs.go +++ b/client/service/dbfs.go @@ -8,11 +8,12 @@ import ( "net/http" ) -// TokensAPI exposes the Secrets API +// DBFSAPI exposes the DBFS API type DBFSAPI struct { Client DBApiClient } +// Create creates a file in DBFS given data string in base64 func (a DBFSAPI) Create(path string, overwrite bool, data string) (err error) { byteArr, err := base64.StdEncoding.DecodeString(data) if err != nil { @@ -36,6 +37,7 @@ func (a DBFSAPI) Create(path string, overwrite bool, data string) (err error) { return err } +// Read returns the contents of a file in DBFS as a base64 encoded string func (a DBFSAPI) Read(path string) (string, error) { var bytesFetched []byte fetchLoop := true @@ -57,6 +59,7 @@ func (a DBFSAPI) Read(path string) (string, error) { return resp, nil } +// Copy copies a file given a source location and a target location and a provided client for source location func (a DBFSAPI) Copy(src string, tgt string, client *DBApiClient, overwrite bool) error { handle, err := a.createHandle(tgt, overwrite) if err != nil { @@ -95,6 +98,7 @@ func (a DBFSAPI) Copy(src string, tgt string, client *DBApiClient, overwrite boo return err } +// Move moves the file between DBFS locations via DBFS api func (a DBFSAPI) Move(src string, tgt string) error { moveRequest := struct { SourcePath string `json:"source_path,omitempty" url:"source_path,omitempty"` @@ -107,6 +111,7 @@ func (a DBFSAPI) Move(src string, tgt string) error { return err } +// Delete deletes a file in DBFS via API func (a DBFSAPI) Delete(path string, recursive bool) error { deleteRequest := struct { Path string `json:"path,omitempty" url:"path,omitempty"` @@ -120,6 +125,7 @@ func (a DBFSAPI) Delete(path string, recursive bool) error { return err } +// ReadString reads a "block" of data in DBFS given a offset and length as a base64 encoded string func (a DBFSAPI) ReadString(path string, offset, length int64) (int64, string, error) { var readBytes struct { BytesRead int64 `json:"bytes_read,omitempty" url:"bytes_read,omitempty"` @@ -152,6 +158,7 @@ func (a DBFSAPI) read(path string, offset, length int64) (int64, []byte, error) return bytesRead, dataBytes, err } +// Status returns the status of a file in DBFS func (a DBFSAPI) Status(path string) (model.FileInfo, error) { var fileInfo model.FileInfo statusRequest := struct { @@ -167,6 +174,7 @@ func (a DBFSAPI) Status(path string) (model.FileInfo, error) { return fileInfo, err } +// List returns a list of files in DBFS and the recursive flag lets you recursively list files func (a DBFSAPI) List(path string, recursive bool) ([]model.FileInfo, error) { if recursive == true { var paths []model.FileInfo @@ -175,9 +183,8 @@ func (a DBFSAPI) List(path string, recursive bool) ([]model.FileInfo, error) { return nil, err } return paths, err - } else { - return a.list(path) } + return a.list(path) } func (a DBFSAPI) recursiveAddPaths(path string, pathList *[]model.FileInfo) error { @@ -216,6 +223,7 @@ func (a DBFSAPI) list(path string) ([]model.FileInfo, error) { return dbfsList.Files, err } +// Mkdirs makes the directories in DBFS include the parent paths func (a DBFSAPI) Mkdirs(path string) error { mkDirsRequest := struct { Path string `json:"path,omitempty" url:"path,omitempty"` diff --git a/client/service/dbfs_test.go b/client/service/dbfs_test.go index 5f95b0a8d..fe601cc0f 100644 --- a/client/service/dbfs_test.go +++ b/client/service/dbfs_test.go @@ -38,7 +38,7 @@ func TestDBFSAPI_Create(t *testing.T) { requestMethod []string postStructExpect []interface{} args []interface{} - wantUri []string + wantURI []string want interface{} wantErr bool }{ @@ -74,7 +74,7 @@ func TestDBFSAPI_Create(t *testing.T) { &block{}, &close{}, }, - wantUri: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/add-block", "/api/2.0/dbfs/close"}, + wantURI: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/add-block", "/api/2.0/dbfs/close"}, want: nil, wantErr: false, }, @@ -92,7 +92,7 @@ func TestDBFSAPI_Create(t *testing.T) { requestMethod: []string{}, args: []interface{}{}, postStructExpect: []interface{}{}, - wantUri: []string{}, + wantURI: []string{}, want: nil, wantErr: true, }, @@ -119,7 +119,7 @@ func TestDBFSAPI_Create(t *testing.T) { postStructExpect: []interface{}{ &handle{}, }, - wantUri: []string{"/api/2.0/dbfs/create"}, + wantURI: []string{"/api/2.0/dbfs/create"}, want: nil, wantErr: true, }, @@ -151,7 +151,7 @@ func TestDBFSAPI_Create(t *testing.T) { &handle{}, &block{}, }, - wantUri: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/add-block"}, + wantURI: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/add-block"}, want: nil, wantErr: true, }, @@ -187,14 +187,14 @@ func TestDBFSAPI_Create(t *testing.T) { &block{}, &close{}, }, - wantUri: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/add-block", "/api/2.0/dbfs/close"}, + wantURI: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/add-block", "/api/2.0/dbfs/close"}, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantUri, tt.postStructExpect, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantURI, tt.postStructExpect, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { return nil, client.DBFS().Create(tt.params.Path, tt.params.Overwrite, tt.params.Data) }) }) @@ -232,7 +232,7 @@ func TestDBFSAPI_Copy(t *testing.T) { requestMethod []string postStructExpect []interface{} args []interface{} - wantUri []string + wantURI []string want interface{} wantErr bool }{ @@ -277,14 +277,14 @@ func TestDBFSAPI_Copy(t *testing.T) { &addBlock{}, &closeHandle{}, }, - wantUri: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/read?length=1000000&path=my-path", "/api/2.0/dbfs/add-block", "/api/2.0/dbfs/close"}, + wantURI: []string{"/api/2.0/dbfs/create", "/api/2.0/dbfs/read?length=1000000&path=my-path", "/api/2.0/dbfs/add-block", "/api/2.0/dbfs/close"}, want: nil, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantUri, tt.postStructExpect, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantURI, tt.postStructExpect, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { return nil, client.DBFS().Copy(tt.params.Src, tt.params.Tgt, &client, tt.params.Overwrite) }) }) @@ -309,7 +309,7 @@ func TestDBFSAPI_Read(t *testing.T) { requestMethod []string postStructExpect []interface{} args []interface{} - wantUri []string + wantURI []string want interface{} wantErr bool }{ @@ -346,7 +346,7 @@ func TestDBFSAPI_Read(t *testing.T) { &read{}, &read{}, }, - wantUri: []string{"/api/2.0/dbfs/read?length=1000000&path=my-path", "/api/2.0/dbfs/read?length=1000000&offset=1000000&path=my-path"}, + wantURI: []string{"/api/2.0/dbfs/read?length=1000000&path=my-path", "/api/2.0/dbfs/read?length=1000000&offset=1000000&path=my-path"}, want: base64String, wantErr: false, }, @@ -383,14 +383,14 @@ func TestDBFSAPI_Read(t *testing.T) { &read{}, &read{}, }, - wantUri: []string{"/api/2.0/dbfs/read?length=1000000&path=my-path", "/api/2.0/dbfs/read?length=1000000&offset=1000000&path=my-path"}, + wantURI: []string{"/api/2.0/dbfs/read?length=1000000&path=my-path", "/api/2.0/dbfs/read?length=1000000&offset=1000000&path=my-path"}, want: "", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantUri, tt.postStructExpect, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertMultipleRequestsWithMockServer(t, tt.args, tt.requestMethod, tt.wantURI, tt.postStructExpect, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.DBFS().Read(tt.params.Path) }) }) @@ -531,7 +531,7 @@ func TestDBFSAPI_Status(t *testing.T) { name string response string responseStatus int - requestUri string + requestURI string args args want interface{} wantErr bool @@ -543,7 +543,7 @@ func TestDBFSAPI_Status(t *testing.T) { "is_dir": false, "file_size": 261 }`, - requestUri: "/api/2.0/dbfs/get-status?path=mypath", + requestURI: "/api/2.0/dbfs/get-status?path=mypath", responseStatus: http.StatusOK, args: args{ Path: "mypath", @@ -558,7 +558,7 @@ func TestDBFSAPI_Status(t *testing.T) { { name: "Status failure test", response: "", - requestUri: "/api/2.0/dbfs/get-status?path=mypath", + requestURI: "/api/2.0/dbfs/get-status?path=mypath", responseStatus: http.StatusBadRequest, args: args{ Path: "mypath", @@ -570,7 +570,7 @@ func TestDBFSAPI_Status(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.DBFS().Status(tt.args.Path) }) }) @@ -587,7 +587,7 @@ func TestDBFSAPI_ListNonRecursive(t *testing.T) { response string responseStatus int args args - wantUri string + wantURI string want []model.FileInfo wantErr bool }{ @@ -613,7 +613,7 @@ func TestDBFSAPI_ListNonRecursive(t *testing.T) { Path: "/", Recursive: false, }, - wantUri: "/api/2.0/dbfs/list?path=%2F", + wantURI: "/api/2.0/dbfs/list?path=%2F", want: []model.FileInfo{ { Path: "/a.cpp", @@ -637,7 +637,7 @@ func TestDBFSAPI_ListNonRecursive(t *testing.T) { Path: "/", Recursive: false, }, - wantUri: "/api/2.0/dbfs/list?path=%2F", + wantURI: "/api/2.0/dbfs/list?path=%2F", want: nil, wantErr: true, }, @@ -645,7 +645,7 @@ func TestDBFSAPI_ListNonRecursive(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.DBFS().List(tt.args.Path, tt.args.Recursive) }) }) @@ -662,7 +662,7 @@ func TestDBFSAPI_ListRecursive(t *testing.T) { response []string responseStatus []int args []interface{} - wantUri []string + wantURI []string want []model.FileInfo wantErr bool }{ @@ -699,7 +699,7 @@ func TestDBFSAPI_ListRecursive(t *testing.T) { Recursive: true, }, }, - wantUri: []string{"/api/2.0/dbfs/list?path=%2F", "/api/2.0/dbfs/list?path=%2Ffoldera"}, + wantURI: []string{"/api/2.0/dbfs/list?path=%2F", "/api/2.0/dbfs/list?path=%2Ffoldera"}, want: []model.FileInfo{ { Path: "/a.cpp", @@ -738,7 +738,7 @@ func TestDBFSAPI_ListRecursive(t *testing.T) { Recursive: true, }, }, - wantUri: []string{"/api/2.0/dbfs/list?path=%2F", "/api/2.0/dbfs/list?path=%2Ffoldera"}, + wantURI: []string{"/api/2.0/dbfs/list?path=%2F", "/api/2.0/dbfs/list?path=%2Ffoldera"}, want: nil, wantErr: true, }, @@ -752,14 +752,14 @@ func TestDBFSAPI_ListRecursive(t *testing.T) { Recursive: true, }, }, - wantUri: []string{"/api/2.0/dbfs/list?path=%2F"}, + wantURI: []string{"/api/2.0/dbfs/list?path=%2F"}, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodGet}, tt.wantUri, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodGet}, tt.wantURI, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.DBFS().List(tt.args[0].(*args).Path, tt.args[0].(*args).Recursive) }) }) diff --git a/client/service/groups.go b/client/service/groups.go index c78e8f6e7..30a22f7c4 100644 --- a/client/service/groups.go +++ b/client/service/groups.go @@ -14,6 +14,7 @@ type GroupsAPI struct { Client DBApiClient } +// Create creates a scim group in the Databricks workspace func (a GroupsAPI) Create(groupName string, members []string, roles []string, entitlements []string) (model.Group, error) { var group model.Group @@ -51,6 +52,7 @@ func (a GroupsAPI) Create(groupName string, members []string, roles []string, en return group, err } +// Read reads and returns a Group object via SCIM api func (a GroupsAPI) Read(groupID string) (model.Group, error) { var group model.Group groupPath := fmt.Sprintf("/preview/scim/v2/Groups/%v", groupID) @@ -81,7 +83,7 @@ func (a GroupsAPI) Read(groupID string) (model.Group, error) { return group, err } -// Not Crud Related +// GetAdminGroup returns the admin group in a given workspace by fetching with query "displayName+eq+admins" func (a GroupsAPI) GetAdminGroup() (model.Group, error) { var group model.Group var groups model.GroupList @@ -102,6 +104,7 @@ func (a GroupsAPI) GetAdminGroup() (model.Group, error) { return group, errors.New("Unable to identify the admin group! ") } +// Patch applys a patch request for a group given a path attribute func (a GroupsAPI) Patch(groupID string, addList []string, removeList []string, path model.GroupPathType) error { groupPath := fmt.Sprintf("/preview/scim/v2/Groups/%v", groupID) @@ -114,7 +117,7 @@ func (a GroupsAPI) Patch(groupID string, addList []string, removeList []string, } if addList == nil && removeList == nil { - return errors.New("Empty members list to add or to remove.") + return errors.New("empty members list to add or to remove") } if len(addList) > 0 { @@ -143,6 +146,7 @@ func (a GroupsAPI) Patch(groupID string, addList []string, removeList []string, return err } +// Delete deletes a group given a group id func (a GroupsAPI) Delete(groupID string) error { groupPath := fmt.Sprintf("/preview/scim/v2/Groups/%v", groupID) _, err := a.Client.performQuery(http.MethodDelete, groupPath, "2.0", scimHeaders, nil, nil) diff --git a/client/service/groups_test.go b/client/service/groups_test.go index fc0fe4c49..908cca27c 100644 --- a/client/service/groups_test.go +++ b/client/service/groups_test.go @@ -124,7 +124,7 @@ func TestScimGroupAPI_Patch(t *testing.T) { } response string args args - requestUri string + requestURI string responseStatus int want interface{} wantErr bool @@ -152,7 +152,7 @@ func TestScimGroupAPI_Patch(t *testing.T) { }, }, }, - requestUri: "/api/2.0/preview/scim/v2/Groups/my-group-id", + requestURI: "/api/2.0/preview/scim/v2/Groups/my-group-id", responseStatus: http.StatusOK, want: nil, wantErr: false, @@ -181,7 +181,7 @@ func TestScimGroupAPI_Patch(t *testing.T) { removeList []string path model.GroupPathType }{groupID: "my-group-id", addList: []string{"100"}, removeList: []string{"200"}, path: model.GroupMembersPath}, - requestUri: "/api/2.0/preview/scim/v2/Groups/my-group-id", + requestURI: "/api/2.0/preview/scim/v2/Groups/my-group-id", responseStatus: http.StatusBadRequest, wantErr: true, }, @@ -189,7 +189,7 @@ func TestScimGroupAPI_Patch(t *testing.T) { for _, tt := range tests { var input args t.Run(tt.name, func(t *testing.T) { - AssertRequestWithMockServer(t, &tt.args, http.MethodPatch, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, &tt.args, http.MethodPatch, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return nil, client.Groups().Patch(tt.params.groupID, tt.params.addList, tt.params.removeList, tt.params.path) }) }) @@ -198,13 +198,13 @@ func TestScimGroupAPI_Patch(t *testing.T) { func TestScimGroupAPI_Delete(t *testing.T) { type args struct { - GroupId string `json:"user_id,omitempty"` + GroupID string `json:"user_id,omitempty"` } tests := []struct { name string response string responseStatus int - requestUri string + requestURI string args args want interface{} wantErr bool @@ -214,9 +214,9 @@ func TestScimGroupAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - GroupId: "10030", + GroupID: "10030", }, - requestUri: "/api/2.0/preview/scim/v2/Groups/10030", + requestURI: "/api/2.0/preview/scim/v2/Groups/10030", want: nil, wantErr: false, }, @@ -225,9 +225,9 @@ func TestScimGroupAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - GroupId: "10030", + GroupID: "10030", }, - requestUri: "/api/2.0/preview/scim/v2/Groups/10030", + requestURI: "/api/2.0/preview/scim/v2/Groups/10030", want: nil, wantErr: true, }, @@ -235,8 +235,8 @@ func TestScimGroupAPI_Delete(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, &tt.args, http.MethodDelete, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Groups().Delete(tt.args.GroupId) + AssertRequestWithMockServer(t, &tt.args, http.MethodDelete, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return nil, client.Groups().Delete(tt.args.GroupID) }) }) } diff --git a/client/service/instance_pools.go b/client/service/instance_pools.go index 508f7ce92..7210e1068 100644 --- a/client/service/instance_pools.go +++ b/client/service/instance_pools.go @@ -6,6 +6,7 @@ import ( "net/http" ) +// InstancePoolsAPI exposes the instance pools api type InstancePoolsAPI struct { Client DBApiClient } @@ -29,13 +30,13 @@ func (a InstancePoolsAPI) Update(instancePoolInfo model.InstancePoolInfo) error } // Read retrieves the information for a instance pool given its identifier -func (a InstancePoolsAPI) Read(instancePoolId string) (model.InstancePoolInfo, error) { +func (a InstancePoolsAPI) Read(instancePoolID string) (model.InstancePoolInfo, error) { var instancePoolInfo model.InstancePoolInfo data := struct { - InstancePoolId string `json:"instance_pool_id,omitempty" url:"instance_pool_id,omitempty"` + InstancePoolID string `json:"instance_pool_id,omitempty" url:"instance_pool_id,omitempty"` }{ - instancePoolId, + instancePoolID, } resp, err := a.Client.performQuery(http.MethodGet, "/instance-pools/get", "2.0", nil, data, nil) if err != nil { @@ -46,12 +47,12 @@ func (a InstancePoolsAPI) Read(instancePoolId string) (model.InstancePoolInfo, e return instancePoolInfo, err } -// Terminate terminates a instance pool given its ID -func (a InstancePoolsAPI) Delete(instancePoolId string) error { +// Delete terminates a instance pool given its ID +func (a InstancePoolsAPI) Delete(instancePoolID string) error { data := struct { - InstancePoolId string `json:"instance_pool_id,omitempty" url:"instance_pool_id,omitempty"` + InstancePoolID string `json:"instance_pool_id,omitempty" url:"instance_pool_id,omitempty"` }{ - instancePoolId, + instancePoolID, } _, err := a.Client.performQuery(http.MethodPost, "/instance-pools/delete", "2.0", nil, data, nil) return err diff --git a/client/service/instance_pools_test.go b/client/service/instance_pools_test.go index 5b434462b..53bc7719b 100644 --- a/client/service/instance_pools_test.go +++ b/client/service/instance_pools_test.go @@ -59,7 +59,7 @@ func TestInstancePoolsAPI_Create(t *testing.T) { func TestInstancePoolsAPI_Delete(t *testing.T) { type args struct { - InstancePoolId string `json:"instance_pool_id"` + InstancePoolID string `json:"instance_pool_id"` } tests := []struct { name string @@ -71,7 +71,7 @@ func TestInstancePoolsAPI_Delete(t *testing.T) { name: "Basic test", response: "", args: args{ - InstancePoolId: "0101-120000-brick1-pool-ABCD1234", + InstancePoolID: "0101-120000-brick1-pool-ABCD1234", }, wantErr: false, }, @@ -80,7 +80,7 @@ func TestInstancePoolsAPI_Delete(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/instance-pools/delete", &input, tt.response, http.StatusOK, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.InstancePools().Delete(tt.args.InstancePoolId) + return nil, client.InstancePools().Delete(tt.args.InstancePoolID) }) }) } @@ -131,7 +131,7 @@ func TestInstancePoolsAPI_Update(t *testing.T) { func TestInstancePoolsAPI_Read(t *testing.T) { type args struct { - InstancePoolId string `json:"instance_pool_id"` + InstancePoolID string `json:"instance_pool_id"` } tests := []struct { name string @@ -179,7 +179,7 @@ func TestInstancePoolsAPI_Read(t *testing.T) { "status": {} }`, args: args{ - InstancePoolId: "101-120000-brick1-pool-ABCD1234", + InstancePoolID: "101-120000-brick1-pool-ABCD1234", }, want: model.InstancePoolInfo{ InstancePoolID: "101-120000-brick1-pool-ABCD1234", @@ -223,7 +223,7 @@ func TestInstancePoolsAPI_Read(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input model.InstancePoolInfo AssertRequestWithMockServer(t, &tt.args, http.MethodGet, "/api/2.0/instance-pools/get?instance_pool_id=101-120000-brick1-pool-ABCD1234", &input, tt.response, http.StatusOK, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.InstancePools().Read(tt.args.InstancePoolId) + return client.InstancePools().Read(tt.args.InstancePoolID) }) }) } diff --git a/client/service/instance_profiles.go b/client/service/instance_profiles.go index bc68d385d..d2afaa71e 100644 --- a/client/service/instance_profiles.go +++ b/client/service/instance_profiles.go @@ -2,16 +2,17 @@ package service import ( "encoding/json" - "errors" "fmt" "github.com/databrickslabs/databricks-terraform/client/model" "net/http" ) +// InstanceProfilesAPI exposes the instance profiles api on the AWS deployment of Databricks type InstanceProfilesAPI struct { Client DBApiClient } +// Create creates an instance profile record on Databricks func (a InstanceProfilesAPI) Create(instanceProfileARN string, skipValidation bool) error { addInstanceProfileRequest := struct { InstanceProfileArn string `json:"instance_profile_arn,omitempty" url:"instance_profile_arn,omitempty"` @@ -24,6 +25,7 @@ func (a InstanceProfilesAPI) Create(instanceProfileARN string, skipValidation bo return err } +// Read returns the ARN back if it exists on the Databricks workspace func (a InstanceProfilesAPI) Read(instanceProfileARN string) (string, error) { var response string instanceProfiles, err := a.List() @@ -37,10 +39,11 @@ func (a InstanceProfilesAPI) Read(instanceProfileARN string) (string, error) { } } - return response, errors.New(fmt.Sprintf("Instance profile with name: %s not found in "+ - "list of instance profiles in the workspace!", instanceProfileARN)) + return response, fmt.Errorf("Instance profile with name: %s not found in "+ + "list of instance profiles in the workspace!", instanceProfileARN) } +// List lists all the instance profiles in the workspace func (a InstanceProfilesAPI) List() ([]model.InstanceProfileInfo, error) { var instanceProfilesArnList struct { @@ -55,6 +58,7 @@ func (a InstanceProfilesAPI) List() ([]model.InstanceProfileInfo, error) { return instanceProfilesArnList.InstanceProfiles, err } +// Delete deletes the instance profile given an instance profile arn func (a InstanceProfilesAPI) Delete(instanceProfileARN string) error { deleteInstanceProfileRequest := struct { InstanceProfileArn string `json:"instance_profile_arn,omitempty" url:"instance_profile_arn,omitempty"` diff --git a/client/service/instance_profiles_test.go b/client/service/instance_profiles_test.go index 8ee788a4c..fa98a4e45 100644 --- a/client/service/instance_profiles_test.go +++ b/client/service/instance_profiles_test.go @@ -98,7 +98,7 @@ func TestInstanceProfilesAPI_List(t *testing.T) { response string responseStatus int args args - wantUri string + wantURI string want []model.InstanceProfileInfo wantErr bool }{ @@ -111,7 +111,7 @@ func TestInstanceProfilesAPI_List(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{}, - wantUri: "/api/2.0/instance-profiles/list?", + wantURI: "/api/2.0/instance-profiles/list?", want: []model.InstanceProfileInfo{ { InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role1", @@ -130,7 +130,7 @@ func TestInstanceProfilesAPI_List(t *testing.T) { response: ``, responseStatus: http.StatusBadRequest, args: args{}, - wantUri: "/api/2.0/instance-profiles/list?", + wantURI: "/api/2.0/instance-profiles/list?", want: nil, wantErr: true, }, @@ -138,7 +138,7 @@ func TestInstanceProfilesAPI_List(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.InstanceProfiles().List() }) }) @@ -154,7 +154,7 @@ func TestInstanceProfilesAPI_Read(t *testing.T) { response string responseStatus int args args - wantUri string + wantURI string want string wantErr bool }{ @@ -169,7 +169,7 @@ func TestInstanceProfilesAPI_Read(t *testing.T) { args: args{ InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role1", }, - wantUri: "/api/2.0/instance-profiles/list?", + wantURI: "/api/2.0/instance-profiles/list?", want: "arn:aws:iam::123456789:instance-profile/datascience-role1", wantErr: false, }, @@ -184,7 +184,7 @@ func TestInstanceProfilesAPI_Read(t *testing.T) { args: args{ InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role4", }, - wantUri: "/api/2.0/instance-profiles/list?", + wantURI: "/api/2.0/instance-profiles/list?", want: "", wantErr: true, }, @@ -195,7 +195,7 @@ func TestInstanceProfilesAPI_Read(t *testing.T) { args: args{ InstanceProfileArn: "arn:aws:iam::123456789:instance-profile/datascience-role1", }, - wantUri: "/api/2.0/instance-profiles/list?", + wantURI: "/api/2.0/instance-profiles/list?", want: "", wantErr: true, }, @@ -203,7 +203,7 @@ func TestInstanceProfilesAPI_Read(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.InstanceProfiles().Read(tt.args.InstanceProfileArn) }) }) diff --git a/client/service/jobs.go b/client/service/jobs.go index d7c9847b0..c4fef0936 100644 --- a/client/service/jobs.go +++ b/client/service/jobs.go @@ -6,11 +6,12 @@ import ( "net/http" ) -// TokensAPI exposes the Secrets API +// JobsAPI exposes the Jobs API type JobsAPI struct { Client DBApiClient } +// Create creates a job on the workspace given the job settings func (a JobsAPI) Create(jobSettings model.JobSettings) (model.Job, error) { var job model.Job resp, err := a.Client.performQuery(http.MethodPost, "/jobs/create", "2.0", nil, jobSettings, nil) @@ -22,19 +23,21 @@ func (a JobsAPI) Create(jobSettings model.JobSettings) (model.Job, error) { return job, err } -func (a JobsAPI) Update(jobId int64, jobSettings model.JobSettings) error { +// Update updates a job given the id and a new set of job settings +func (a JobsAPI) Update(jobID int64, jobSettings model.JobSettings) error { jobResetRequest := struct { - JobId int64 `json:"job_id,omitempty" url:"job_id,omitempty"` + JobID int64 `json:"job_id,omitempty" url:"job_id,omitempty"` NewSettings *model.JobSettings `json:"new_settings,omitempty" url:"new_settings,omitempty"` - }{JobId: jobId, NewSettings: &jobSettings} + }{JobID: jobID, NewSettings: &jobSettings} _, err := a.Client.performQuery(http.MethodPost, "/jobs/reset", "2.0", nil, jobResetRequest, nil) return err } -func (a JobsAPI) Read(jobId int64) (model.Job, error) { +// Read returns the job object with all the attributes +func (a JobsAPI) Read(jobID int64) (model.Job, error) { jobGetRequest := struct { - JobId int64 `json:"job_id,omitempty" url:"job_id,omitempty"` - }{JobId: jobId} + JobID int64 `json:"job_id,omitempty" url:"job_id,omitempty"` + }{JobID: jobID} var job model.Job @@ -48,10 +51,11 @@ func (a JobsAPI) Read(jobId int64) (model.Job, error) { return job, err } -func (a JobsAPI) Delete(jobId int64) error { +// Delete deletes the job given a job id +func (a JobsAPI) Delete(jobID int64) error { jobDeleteRequest := struct { - JobId int64 `json:"job_id,omitempty" url:"job_id,omitempty"` - }{JobId: jobId} + JobID int64 `json:"job_id,omitempty" url:"job_id,omitempty"` + }{JobID: jobID} _, err := a.Client.performQuery(http.MethodPost, "/jobs/delete", "2.0", nil, jobDeleteRequest, nil) diff --git a/client/service/jobs_test.go b/client/service/jobs_test.go index 52ca07f82..3de1e3d77 100644 --- a/client/service/jobs_test.go +++ b/client/service/jobs_test.go @@ -54,7 +54,7 @@ func TestJobsAPI_Create(t *testing.T) { func TestJobsAPI_Update(t *testing.T) { type args struct { - JobId int64 `json:"job_id,omitempty" url:"job_id,omitempty"` + JobID int64 `json:"job_id,omitempty" url:"job_id,omitempty"` NewSettings model.JobSettings `json:"new_settings,omitempty" url:"new_settings,omitempty"` } @@ -73,7 +73,7 @@ func TestJobsAPI_Update(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{ - JobId: 1, + JobID: 1, NewSettings: model.JobSettings{}, }, want: nil, @@ -84,7 +84,7 @@ func TestJobsAPI_Update(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - JobId: 0, + JobID: 0, NewSettings: model.JobSettings{}, }, want: nil, @@ -95,7 +95,7 @@ func TestJobsAPI_Update(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/jobs/reset", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Jobs().Update(tt.args.JobId, tt.args.NewSettings) + return nil, client.Jobs().Update(tt.args.JobID, tt.args.NewSettings) }) }) } @@ -103,7 +103,7 @@ func TestJobsAPI_Update(t *testing.T) { func TestJobsAPI_Delete(t *testing.T) { type args struct { - JobId int64 `json:"job_id,omitempty" url:"job_id,omitempty"` + JobID int64 `json:"job_id,omitempty" url:"job_id,omitempty"` } tests := []struct { @@ -118,7 +118,7 @@ func TestJobsAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - JobId: 0, + JobID: 0, }, wantErr: false, }, @@ -127,7 +127,7 @@ func TestJobsAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - JobId: 0, + JobID: 0, }, wantErr: true, }, @@ -136,7 +136,7 @@ func TestJobsAPI_Delete(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/jobs/delete", &input, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Jobs().Delete(tt.args.JobId) + return nil, client.Jobs().Delete(tt.args.JobID) }) }) } @@ -144,14 +144,14 @@ func TestJobsAPI_Delete(t *testing.T) { func TestJobsAPI_Read(t *testing.T) { type args struct { - JobId int64 `json:"job_id,omitempty" url:"job_id,omitempty"` + JobID int64 `json:"job_id,omitempty" url:"job_id,omitempty"` } tests := []struct { name string response string responseStatus int args args - wantUri string + wantURI string want interface{} wantErr bool }{ @@ -198,9 +198,9 @@ func TestJobsAPI_Read(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{ - JobId: 1, + JobID: 1, }, - wantUri: "/api/2.0/jobs/get?job_id=1", + wantURI: "/api/2.0/jobs/get?job_id=1", want: model.Job{ JobID: 1, Settings: &model.JobSettings{ @@ -247,9 +247,9 @@ func TestJobsAPI_Read(t *testing.T) { response: ``, responseStatus: http.StatusBadRequest, args: args{ - JobId: 1, + JobID: 1, }, - wantUri: "/api/2.0/jobs/get?job_id=1", + wantURI: "/api/2.0/jobs/get?job_id=1", want: model.Job{}, wantErr: true, }, @@ -257,8 +257,8 @@ func TestJobsAPI_Read(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Jobs().Read(tt.args.JobId) + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Jobs().Read(tt.args.JobID) }) }) } diff --git a/client/service/libraries.go b/client/service/libraries.go index 2226fbe02..7c18e0fac 100644 --- a/client/service/libraries.go +++ b/client/service/libraries.go @@ -6,17 +6,18 @@ import ( "net/http" ) -// TokensAPI exposes the Secrets API +// LibrariesAPI exposes the Library API type LibrariesAPI struct { Client DBApiClient } -func (a LibrariesAPI) Create(clusterId string, libraries []model.Library) error { +// Create installs the list of libraries given a cluster id +func (a LibrariesAPI) Create(clusterID string, libraries []model.Library) error { var libraryInstallRequest = struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` Libraries []model.Library `json:"libraries,omitempty" url:"libraries,omitempty"` }{ - ClusterId: clusterId, + ClusterID: clusterID, Libraries: libraries, } @@ -25,12 +26,13 @@ func (a LibrariesAPI) Create(clusterId string, libraries []model.Library) error return err } -func (a LibrariesAPI) Delete(clusterId string, libraries []model.Library) error { +// Delete deletes the list of given libraries from the cluster given the cluster id +func (a LibrariesAPI) Delete(clusterID string, libraries []model.Library) error { var libraryInstallRequest = struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` Libraries []model.Library `json:"libraries,omitempty" url:"libraries,omitempty"` }{ - ClusterId: clusterId, + ClusterID: clusterID, Libraries: libraries, } @@ -39,15 +41,16 @@ func (a LibrariesAPI) Delete(clusterId string, libraries []model.Library) error return err } -func (a LibrariesAPI) List(clusterId string) ([]model.LibraryStatus, error) { +// List lists all the libraries given a cluster id +func (a LibrariesAPI) List(clusterID string) ([]model.LibraryStatus, error) { var libraryStatusListResp struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` LibraryStatuses []model.LibraryStatus `json:"library_statuses,omitempty" url:"libraries,omitempty"` } var libraryInstallRequest = struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` }{ - ClusterId: clusterId, + ClusterID: clusterID, } resp, err := a.Client.performQuery(http.MethodGet, "/libraries/cluster-status", "2.0", nil, libraryInstallRequest, nil) diff --git a/client/service/libraries_integration_test.go b/client/service/libraries_integration_test.go index 5f8b2c90e..bda701ccc 100644 --- a/client/service/libraries_integration_test.go +++ b/client/service/libraries_integration_test.go @@ -38,9 +38,9 @@ func TestLibraryCreate(t *testing.T) { assert.NoError(t, err, err) }() - clusterId := clusterInfo.ClusterID + clusterID := clusterInfo.ClusterID - err = client.Clusters().WaitForClusterRunning(clusterId, 10, 20) + err = client.Clusters().WaitForClusterRunning(clusterID, 10, 20) assert.NoError(t, err, err) libraries := []model.Library{ @@ -56,15 +56,15 @@ func TestLibraryCreate(t *testing.T) { }, } - err = client.Libraries().Create(clusterId, libraries) + err = client.Libraries().Create(clusterID, libraries) assert.NoError(t, err, err) defer func() { - err = client.Libraries().Delete(clusterId, libraries) + err = client.Libraries().Delete(clusterID, libraries) assert.NoError(t, err, err) }() - libraryStatusList, err := client.Libraries().List(clusterId) + libraryStatusList, err := client.Libraries().List(clusterID) assert.NoError(t, err, err) assert.Equal(t, len(libraryStatusList), len(libraries)) } diff --git a/client/service/libraries_test.go b/client/service/libraries_test.go index cfaab7dc1..c550115a3 100644 --- a/client/service/libraries_test.go +++ b/client/service/libraries_test.go @@ -8,7 +8,7 @@ import ( func TestLibrariesAPI_Create(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` Libraries []model.Library `json:"libraries,omitempty" url:"libraries,omitempty"` } @@ -24,7 +24,7 @@ func TestLibrariesAPI_Create(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Libraries: []model.Library{ { Whl: "dbfs:/my/dbfs/wheel.whl", @@ -38,7 +38,7 @@ func TestLibrariesAPI_Create(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Libraries: []model.Library{ { Whl: "dbfs:/my/dbfs/wheel.whl", @@ -52,7 +52,7 @@ func TestLibrariesAPI_Create(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/libraries/install", &input, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Libraries().Create(tt.args.ClusterId, tt.args.Libraries) + return nil, client.Libraries().Create(tt.args.ClusterID, tt.args.Libraries) }) }) } @@ -60,7 +60,7 @@ func TestLibrariesAPI_Create(t *testing.T) { func TestLibrariesAPI_Delete(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` Libraries []model.Library `json:"libraries,omitempty" url:"libraries,omitempty"` } @@ -76,7 +76,7 @@ func TestLibrariesAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Libraries: []model.Library{ { Whl: "dbfs:/my/dbfs/wheel.whl", @@ -90,7 +90,7 @@ func TestLibrariesAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "my-cluster-id", + ClusterID: "my-cluster-id", Libraries: []model.Library{ { Whl: "dbfs:/my/dbfs/wheel.whl", @@ -104,7 +104,7 @@ func TestLibrariesAPI_Delete(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/libraries/uninstall", &input, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Libraries().Delete(tt.args.ClusterId, tt.args.Libraries) + return nil, client.Libraries().Delete(tt.args.ClusterID, tt.args.Libraries) }) }) } @@ -112,14 +112,14 @@ func TestLibrariesAPI_Delete(t *testing.T) { func TestLibrariesAPI_List(t *testing.T) { type args struct { - ClusterId string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` + ClusterID string `json:"cluster_id,omitempty" url:"cluster_id,omitempty"` } tests := []struct { name string response string responseStatus int args args - wantUri string + wantURI string want []model.LibraryStatus wantErr bool }{ @@ -161,9 +161,9 @@ func TestLibrariesAPI_List(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", }, - wantUri: "/api/2.0/libraries/cluster-status?cluster_id=11203-my-cluster", + wantURI: "/api/2.0/libraries/cluster-status?cluster_id=11203-my-cluster", want: []model.LibraryStatus{ { Library: &model.Library{ @@ -202,9 +202,9 @@ func TestLibrariesAPI_List(t *testing.T) { response: ``, responseStatus: http.StatusBadRequest, args: args{ - ClusterId: "11203-my-cluster", + ClusterID: "11203-my-cluster", }, - wantUri: "/api/2.0/libraries/cluster-status?cluster_id=11203-my-cluster", + wantURI: "/api/2.0/libraries/cluster-status?cluster_id=11203-my-cluster", want: nil, wantErr: true, }, @@ -212,8 +212,8 @@ func TestLibrariesAPI_List(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Libraries().List(tt.args.ClusterId) + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Libraries().List(tt.args.ClusterID) }) }) } diff --git a/client/service/main_test.go b/client/service/main_test.go index d913c3936..007e08ef6 100644 --- a/client/service/main_test.go +++ b/client/service/main_test.go @@ -24,7 +24,7 @@ func TestMain(m *testing.M) { os.Exit(code) } -func DeserializeJson(req *http.Request, m interface{}) error { +func DeserializeJSON(req *http.Request, m interface{}) error { dec := json.NewDecoder(req.Body) dec.DisallowUnknownFields() @@ -53,9 +53,8 @@ func GetIntegrationDBAPIClient() *DBApiClient { func GetCloudInstanceType(c *DBApiClient) string { if strings.Contains(c.config.Host, "azure") { return "Standard_DS3_v2" - } else { - return "m4.large" } + return "m4.large" } func AssertRequestWithMockServer(t *testing.T, rawPayloadArgs interface{}, requestMethod string, requestURI string, input interface{}, response string, responseStatus int, want interface{}, wantErr bool, apiCall func(client DBApiClient) (interface{}, error)) { @@ -64,7 +63,7 @@ func AssertRequestWithMockServer(t *testing.T, rawPayloadArgs interface{}, reque assert.Equal(t, requestMethod, req.Method) assert.Equal(t, requestURI, req.RequestURI) if requestMethod == http.MethodPost || requestMethod == http.MethodPatch || requestMethod == http.MethodPut { - err := DeserializeJson(req, &input) + err := DeserializeJSON(req, &input) assert.NoError(t, err, err) compare(t, rawPayloadArgs, input) } @@ -103,7 +102,7 @@ func AssertMultipleRequestsWithMockServer(t *testing.T, rawPayloadArgs interface assert.Equal(t, requestMethod[counter], req.Method) assert.Equal(t, requestURI[counter], req.RequestURI) if requestMethod[counter] == http.MethodPost || requestMethod[counter] == http.MethodPatch || requestMethod[counter] == http.MethodPut { - err := DeserializeJson(req, &(input.([]interface{})[counter])) + err := DeserializeJSON(req, &(input.([]interface{})[counter])) assert.NoError(t, err, err) compare(t, rawPayloadArgs.([]interface{})[counter], input.([]interface{})[counter]) } diff --git a/client/service/mask_utils.go b/client/service/mask_utils.go index 26ccbc0ff..381b3ae9a 100644 --- a/client/service/mask_utils.go +++ b/client/service/mask_utils.go @@ -6,6 +6,7 @@ import ( "strings" ) +// Mask is a func given a struct it will mask everything with "[REDACTED]" if there are mask struct tags added func Mask(obj interface{}) interface{} { // Wrap the original in a reflect.Value original := reflect.ValueOf(obj) @@ -55,7 +56,7 @@ func maskRecursive(copy, original reflect.Value, mask bool) { // If it is a struct we Mask each field case reflect.Struct: - for i := 0; i < original.NumField(); i += 1 { + for i := 0; i < original.NumField(); i++ { //log.Println() maskValue, maskInStruct := original.Type().Field(i).Tag.Lookup("mask") maskIsTrue, _ := strconv.ParseBool(maskValue) @@ -65,7 +66,7 @@ func maskRecursive(copy, original reflect.Value, mask bool) { // If it is a slice we create a new slice and Mask each element case reflect.Slice: copy.Set(reflect.MakeSlice(original.Type(), original.Len(), original.Cap())) - for i := 0; i < original.Len(); i += 1 { + for i := 0; i < original.Len(); i++ { maskRecursive(copy.Index(i), original.Index(i), false) } @@ -97,10 +98,12 @@ func maskRecursive(copy, original reflect.Value, mask bool) { } +// SecretsMask is a struct that contains a list of secret strings to be redacted type SecretsMask struct { Secrets []string } +// MaskString given a SecretsMask and a string value return the string value with the secrets redacted func (a SecretsMask) MaskString(str string) string { placeHolder := str for _, secret := range a.Secrets { diff --git a/client/service/notebooks.go b/client/service/notebooks.go index c38f5087f..474846cd0 100644 --- a/client/service/notebooks.go +++ b/client/service/notebooks.go @@ -6,11 +6,12 @@ import ( "net/http" ) -// TokensAPI exposes the Secrets API +// NotebooksAPI exposes the Notebooks API type NotebooksAPI struct { Client DBApiClient } +// Create creates a notebook given the content and path func (a NotebooksAPI) Create(path string, content string, language model.Language, format model.ExportFormat, overwrite bool) error { notebookCreateRequest := struct { Content string `json:"content,omitempty" mask:"true"` @@ -29,6 +30,7 @@ func (a NotebooksAPI) Create(path string, content string, language model.Languag return err } +// Read returns the notebook metadata and not the contents func (a NotebooksAPI) Read(path string) (model.NotebookInfo, error) { var notebookInfo model.NotebookInfo notebookGetStatusRequest := struct { @@ -44,6 +46,7 @@ func (a NotebooksAPI) Read(path string) (model.NotebookInfo, error) { return notebookInfo, err } +// Export returns the notebook content as a base64 string func (a NotebooksAPI) Export(path string, format model.ExportFormat) (string, error) { var notebookContent map[string]string notebookExportRequest := struct { @@ -61,6 +64,7 @@ func (a NotebooksAPI) Export(path string, format model.ExportFormat) (string, er return notebookContent["content"], err } +// Mkdirs will make folders in a workspace recursively given a path func (a NotebooksAPI) Mkdirs(path string) error { mkDirsRequest := struct { Path string `json:"path,omitempty" url:"path,omitempty"` @@ -72,6 +76,8 @@ func (a NotebooksAPI) Mkdirs(path string) error { return err } +// List will list all objects in a path on the workspace and with the recursive flag it will recursively list +// all the objects func (a NotebooksAPI) List(path string, recursive bool) ([]model.NotebookInfo, error) { if recursive == true { var paths []model.NotebookInfo @@ -80,9 +86,8 @@ func (a NotebooksAPI) List(path string, recursive bool) ([]model.NotebookInfo, e return nil, err } return paths, err - } else { - return a.list(path) } + return a.list(path) } func (a NotebooksAPI) recursiveAddPaths(path string, pathList *[]model.NotebookInfo) error { @@ -121,6 +126,7 @@ func (a NotebooksAPI) list(path string) ([]model.NotebookInfo, error) { return notebookList.Objects, err } +// Delete will delete folders given a path and recursive flag func (a NotebooksAPI) Delete(path string, recursive bool) error { notebookDelete := struct { Path string `json:"path,omitempty"` diff --git a/client/service/notebooks_test.go b/client/service/notebooks_test.go index 591d40e66..0a01bf152 100644 --- a/client/service/notebooks_test.go +++ b/client/service/notebooks_test.go @@ -117,7 +117,7 @@ func TestNotebooksAPI_ListNonRecursive(t *testing.T) { response string responseStatus int args args - wantUri string + wantURI string want []model.NotebookInfo wantErr bool }{ @@ -144,7 +144,7 @@ func TestNotebooksAPI_ListNonRecursive(t *testing.T) { Path: "/test/path", Recursive: false, }, - wantUri: "/api/2.0/workspace/list?path=%2Ftest%2Fpath", + wantURI: "/api/2.0/workspace/list?path=%2Ftest%2Fpath", want: []model.NotebookInfo{ { ObjectID: 123, @@ -164,7 +164,7 @@ func TestNotebooksAPI_ListNonRecursive(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Notebooks().List(tt.args.Path, tt.args.Recursive) }) }) @@ -181,7 +181,7 @@ func TestNotebooksAPI_ListRecursive(t *testing.T) { response []string responseStatus []int args []interface{} - wantUri []string + wantURI []string want []model.NotebookInfo wantErr bool }{ @@ -220,7 +220,7 @@ func TestNotebooksAPI_ListRecursive(t *testing.T) { Recursive: true, }, }, - wantUri: []string{"/api/2.0/workspace/list?path=%2Ftest%2Fpath", "/api/2.0/workspace/list?path=%2FUsers%2Fuser%40example.com%2Fproject"}, + wantURI: []string{"/api/2.0/workspace/list?path=%2Ftest%2Fpath", "/api/2.0/workspace/list?path=%2FUsers%2Fuser%40example.com%2Fproject"}, want: []model.NotebookInfo{ { ObjectID: 457, @@ -263,14 +263,14 @@ func TestNotebooksAPI_ListRecursive(t *testing.T) { Recursive: true, }, }, - wantUri: []string{"/api/2.0/workspace/list?path=%2Ftest%2Fpath", "/api/2.0/workspace/list?path=%2FUsers%2Fuser%40example.com%2Fproject"}, + wantURI: []string{"/api/2.0/workspace/list?path=%2Ftest%2Fpath", "/api/2.0/workspace/list?path=%2FUsers%2Fuser%40example.com%2Fproject"}, want: nil, wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodGet}, tt.wantUri, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodGet}, tt.wantURI, []interface{}{&args{}}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Notebooks().List(tt.args[0].(*args).Path, tt.args[0].(*args).Recursive) }) }) @@ -286,7 +286,7 @@ func TestNotebooksAPI_Read(t *testing.T) { response string args args responseStatus int - wantUri string + wantURI string want model.NotebookInfo wantErr bool }{ @@ -308,7 +308,7 @@ func TestNotebooksAPI_Read(t *testing.T) { Path: "/Users/user@example.com/project/ScalaExampleNotebook", Language: model.Scala, }, - wantUri: "/api/2.0/workspace/get-status?path=%2Ftest%2Fpath", + wantURI: "/api/2.0/workspace/get-status?path=%2Ftest%2Fpath", wantErr: false, }, @@ -320,14 +320,14 @@ func TestNotebooksAPI_Read(t *testing.T) { }, responseStatus: http.StatusBadRequest, want: model.NotebookInfo{}, - wantUri: "/api/2.0/workspace/get-status?path=%2Ftest%2Fpath", + wantURI: "/api/2.0/workspace/get-status?path=%2Ftest%2Fpath", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Notebooks().Read(tt.args.Path) }) }) @@ -344,7 +344,7 @@ func TestNotebooksAPI_Export(t *testing.T) { response string args args responseStatus int - wantUri string + wantURI string want string wantErr bool }{ @@ -359,7 +359,7 @@ func TestNotebooksAPI_Export(t *testing.T) { }, responseStatus: http.StatusOK, want: "Ly8gRGF0YWJyaWNrcyBub3RlYm9vayBzb3VyY2UKMSsx", - wantUri: "/api/2.0/workspace/export?format=DBC&path=%2Ftest%2Fpath", + wantURI: "/api/2.0/workspace/export?format=DBC&path=%2Ftest%2Fpath", wantErr: false, }, { @@ -371,14 +371,14 @@ func TestNotebooksAPI_Export(t *testing.T) { }, responseStatus: http.StatusBadRequest, want: "", - wantUri: "/api/2.0/workspace/export?format=DBC&path=%2Ftest%2Fpath", + wantURI: "/api/2.0/workspace/export?format=DBC&path=%2Ftest%2Fpath", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Notebooks().Export(tt.args.Path, tt.args.Format) }) }) diff --git a/client/service/secret_acls.go b/client/service/secret_acls.go index 49d8fe4e1..b8a3d4aa5 100644 --- a/client/service/secret_acls.go +++ b/client/service/secret_acls.go @@ -6,7 +6,7 @@ import ( "net/http" ) -// SecretsAPI exposes the Secrets API +// SecretAclsAPI exposes the Secret ACL API type SecretAclsAPI struct { Client DBApiClient } diff --git a/client/service/secret_scopes.go b/client/service/secret_scopes.go index 41d284274..f9fb8682d 100644 --- a/client/service/secret_scopes.go +++ b/client/service/secret_scopes.go @@ -2,18 +2,17 @@ package service import ( "encoding/json" - "errors" "fmt" "github.com/databrickslabs/databricks-terraform/client/model" "net/http" ) -// SecretsAPI exposes the Secrets API +// SecretScopesAPI exposes the Secret Scopes API type SecretScopesAPI struct { Client DBApiClient } -// CreateSecretScope creates a new secret scope +// Create creates a new secret scope func (a SecretScopesAPI) Create(scope string, initialManagePrincipal string) error { data := struct { Scope string `json:"scope,omitempty"` @@ -26,7 +25,7 @@ func (a SecretScopesAPI) Create(scope string, initialManagePrincipal string) err return err } -// DeleteSecretScope deletes a secret scope +// Delete deletes a secret scope func (a SecretScopesAPI) Delete(scope string) error { data := struct { Scope string `json:"scope,omitempty" ` @@ -52,6 +51,7 @@ func (a SecretScopesAPI) List() ([]model.SecretScope, error) { return listSecretScopesResponse.Scopes, err } +// Read will return the metadata for the secret scope func (a SecretScopesAPI) Read(scopeName string) (model.SecretScope, error) { var secretScope model.SecretScope scopes, err := a.List() @@ -63,5 +63,5 @@ func (a SecretScopesAPI) Read(scopeName string) (model.SecretScope, error) { return scope, nil } } - return secretScope, errors.New(fmt.Sprintf("No Secret Scope found with scope name %s.", scopeName)) + return secretScope, fmt.Errorf("no Secret Scope found with scope name %s", scopeName) } diff --git a/client/service/secrets.go b/client/service/secrets.go index e4d765284..a939111ef 100644 --- a/client/service/secrets.go +++ b/client/service/secrets.go @@ -2,7 +2,6 @@ package service import ( "encoding/json" - "errors" "fmt" "github.com/databrickslabs/databricks-terraform/client/model" "net/http" @@ -13,7 +12,7 @@ type SecretsAPI struct { Client DBApiClient } -// PutSecretString creates or modifies a string secret depends on the type of scope backend +// Create creates or modifies a string secret depends on the type of scope backend func (a SecretsAPI) Create(stringValue, scope, key string) error { data := struct { StringValue string `json:"string_value,omitempty" mask:"true"` @@ -28,7 +27,7 @@ func (a SecretsAPI) Create(stringValue, scope, key string) error { return err } -// DeleteSecret deletes a secret depends on the type of scope backend +// Delete deletes a secret depends on the type of scope backend func (a SecretsAPI) Delete(scope, key string) error { data := struct { Scope string `json:"scope,omitempty"` @@ -41,7 +40,7 @@ func (a SecretsAPI) Delete(scope, key string) error { return err } -// ListSecrets lists the secret keys that are stored at this scope +// List lists the secret keys that are stored at this scope func (a SecretsAPI) List(scope string) ([]model.SecretMetadata, error) { var secretsList struct { Secrets []model.SecretMetadata `json:"secrets,omitempty"` @@ -62,6 +61,7 @@ func (a SecretsAPI) List(scope string) ([]model.SecretMetadata, error) { return secretsList.Secrets, err } +// Read returns the metadata for the secret and not the contents of the secret func (a SecretsAPI) Read(scope string, key string) (model.SecretMetadata, error) { var secretMeta model.SecretMetadata secrets, err := a.List(scope) @@ -73,5 +73,5 @@ func (a SecretsAPI) Read(scope string, key string) (model.SecretMetadata, error) return secret, nil } } - return secretMeta, errors.New(fmt.Sprintf("No Secret Scope found with secret metadata scope name: %s and key: %s.", scope, key)) + return secretMeta, fmt.Errorf("no Secret Scope found with secret metadata scope name: %s and key: %s", scope, key) } diff --git a/client/service/secrets_scopes_acls_integration_test.go b/client/service/secrets_scopes_acls_integration_test.go index 8bec5bb57..181af6eb4 100644 --- a/client/service/secrets_scopes_acls_integration_test.go +++ b/client/service/secrets_scopes_acls_integration_test.go @@ -54,10 +54,10 @@ func TestSecretsScopesAclsIntegration(t *testing.T) { assert.NoError(t, err, err) assert.True(t, len(secretAcls) > 0, "Secrets acls are empty list") - secretAcl, err := client.SecretAcls().Read(testScope, testPrincipal) + secretACL, err := client.SecretAcls().Read(testScope, testPrincipal) assert.NoError(t, err, err) - assert.Equal(t, testPrincipal, secretAcl.Principal, "Secret lookup does not yield same key") - assert.Equal(t, model.ACLPermissionManage, secretAcl.Permission, "Secret lookup does not yield same key") + assert.Equal(t, testPrincipal, secretACL.Principal, "Secret lookup does not yield same key") + assert.Equal(t, model.ACLPermissionManage, secretACL.Permission, "Secret lookup does not yield same key") err = client.Secrets().Delete(testScope, testKey) assert.NoError(t, err, err) diff --git a/client/service/tokens.go b/client/service/tokens.go index 6d0ad9f78..ed376733b 100644 --- a/client/service/tokens.go +++ b/client/service/tokens.go @@ -12,6 +12,7 @@ type TokensAPI struct { Client *DBApiClient } +// Create creates a api token given a expiration duration and a comment func (a TokensAPI) Create(lifeTimeSeconds int32, comment string) (model.TokenResponse, error) { var tokenData model.TokenResponse @@ -32,6 +33,7 @@ func (a TokensAPI) Create(lifeTimeSeconds int32, comment string) (model.TokenRes return tokenData, err } +// List will list all the token metadata and not the content of the tokens in the workspace func (a TokensAPI) List() ([]model.TokenInfo, error) { var tokenListResult struct { TokenInfos []model.TokenInfo `json:"token_infos,omitempty"` @@ -44,6 +46,7 @@ func (a TokensAPI) List() ([]model.TokenInfo, error) { return tokenListResult.TokenInfos, err } +// Read will return the token metadata and not the content of the token func (a TokensAPI) Read(tokenID string) (model.TokenInfo, error) { var tokenInfo model.TokenInfo tokenList, err := a.List() @@ -58,9 +61,10 @@ func (a TokensAPI) Read(tokenID string) (model.TokenInfo, error) { return tokenInfo, errors.New("Unable to locate token: " + tokenID) } +// Delete will delete the token given a token id func (a TokensAPI) Delete(tokenID string) error { tokenDeleteRequest := struct { - TokenId string `json:"token_id,omitempty"` + TokenID string `json:"token_id,omitempty"` }{ tokenID, } diff --git a/client/service/tokens_test.go b/client/service/tokens_test.go index 67388250b..5b99aebcc 100644 --- a/client/service/tokens_test.go +++ b/client/service/tokens_test.go @@ -70,7 +70,7 @@ func TestTokensAPI_Create(t *testing.T) { func TestTokensAPI_Delete(t *testing.T) { type args struct { - TokenId string `json:"token_id,omitempty"` + TokenID string `json:"token_id,omitempty"` } tests := []struct { name string @@ -85,7 +85,7 @@ func TestTokensAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - TokenId: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", + TokenID: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", }, want: nil, wantErr: false, @@ -95,7 +95,7 @@ func TestTokensAPI_Delete(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var input args AssertRequestWithMockServer(t, &tt.args, http.MethodPost, "/api/2.0/token/delete", &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Tokens().Delete(tt.args.TokenId) + return nil, client.Tokens().Delete(tt.args.TokenID) }) }) } @@ -108,7 +108,7 @@ func TestTokensAPI_List(t *testing.T) { response string responseStatus int args *args - wantUri string + wantURI string want []model.TokenInfo wantErr bool }{ @@ -132,7 +132,7 @@ func TestTokensAPI_List(t *testing.T) { }`, responseStatus: http.StatusOK, args: nil, - wantUri: "/api/2.0/token/list?", + wantURI: "/api/2.0/token/list?", want: []model.TokenInfo{ { TokenID: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", @@ -154,7 +154,7 @@ func TestTokensAPI_List(t *testing.T) { response: ``, responseStatus: http.StatusBadRequest, args: nil, - wantUri: "/api/2.0/token/list?", + wantURI: "/api/2.0/token/list?", want: nil, wantErr: true, }, @@ -162,7 +162,7 @@ func TestTokensAPI_List(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertRequestWithMockServer(t, tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { return client.Tokens().List() }) }) @@ -171,14 +171,14 @@ func TestTokensAPI_List(t *testing.T) { func TestTokensAPI_Read(t *testing.T) { type args struct { - TokenId string `json:"token_id,omitempty"` + TokenID string `json:"token_id,omitempty"` } tests := []struct { name string response string args args responseStatus int - wantUri string + wantURI string want model.TokenInfo wantErr bool }{ @@ -201,7 +201,7 @@ func TestTokensAPI_Read(t *testing.T) { ] }`, args: args{ - TokenId: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", + TokenID: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", }, responseStatus: http.StatusOK, want: model.TokenInfo{ @@ -210,18 +210,18 @@ func TestTokensAPI_Read(t *testing.T) { ExpiryTime: 1513120616294, Comment: "this is an example token", }, - wantUri: "/api/2.0/token/list?", + wantURI: "/api/2.0/token/list?", wantErr: false, }, { name: "Read list fails", response: ``, args: args{ - TokenId: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", + TokenID: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", }, responseStatus: http.StatusBadRequest, want: model.TokenInfo{}, - wantUri: "/api/2.0/token/list?", + wantURI: "/api/2.0/token/list?", wantErr: true, }, { @@ -243,19 +243,19 @@ func TestTokensAPI_Read(t *testing.T) { ] }`, args: args{ - TokenId: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", + TokenID: "5715498424f15ee0213be729257b53fc35a47d5953e3bdfd8ed22a0b93b339f4", }, responseStatus: http.StatusOK, want: model.TokenInfo{}, - wantUri: "/api/2.0/token/list?", + wantURI: "/api/2.0/token/list?", wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.wantUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Tokens().Read(tt.args.TokenId) + AssertRequestWithMockServer(t, &tt.args, http.MethodGet, tt.wantURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Tokens().Read(tt.args.TokenID) }) }) } diff --git a/client/service/users.go b/client/service/users.go index a52c341e9..9a4f07d31 100644 --- a/client/service/users.go +++ b/client/service/users.go @@ -13,6 +13,7 @@ type UsersAPI struct { Client DBApiClient } +// Create given a username, displayname, entitlements, and roles will create a scim user via SCIM api func (a UsersAPI) Create(userName string, displayName string, entitlements []string, roles []string) (model.User, error) { var user model.User scimUserRequest := struct { @@ -43,8 +44,9 @@ func (a UsersAPI) Create(userName string, displayName string, entitlements []str return user, err } -func (a UsersAPI) Read(userId string) (model.User, error) { - user, err := a.read(userId) +// Read returns the user object and all the attributes of a scim user +func (a UsersAPI) Read(userID string) (model.User, error) { + user, err := a.read(userID) if err != nil { return user, err } @@ -64,9 +66,9 @@ func (a UsersAPI) Read(userId string) (model.User, error) { return user, err } -func (a UsersAPI) read(userId string) (model.User, error) { +func (a UsersAPI) read(userID string) (model.User, error) { var user model.User - userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userId) + userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userID) resp, err := a.Client.performQuery(http.MethodGet, userPath, "2.0", scimHeaders, nil, nil) if err != nil { @@ -77,8 +79,9 @@ func (a UsersAPI) read(userId string) (model.User, error) { return user, err } -func (a UsersAPI) Update(userId string, userName string, displayName string, entitlements []string, roles []string) error { - userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userId) +// Update will update the user given the user id, username, display name, entitlements and roles +func (a UsersAPI) Update(userID string, userName string, displayName string, entitlements []string, roles []string) error { + userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userID) scimUserUpdateRequest := struct { Schemas []model.URN `json:"schemas,omitempty"` UserName string `json:"userName,omitempty"` @@ -99,7 +102,7 @@ func (a UsersAPI) Update(userId string, userName string, displayName string, ent scimUserUpdateRequest.Roles = append(scimUserUpdateRequest.Roles, model.RoleListItem{Value: role}) } //Get any existing groups that the user is part of - user, err := a.read(userId) + user, err := a.read(userID) if err != nil { return err } @@ -108,16 +111,18 @@ func (a UsersAPI) Update(userId string, userName string, displayName string, ent return err } -func (a UsersAPI) Delete(userId string) error { +// Delete will delete the user given the user id +func (a UsersAPI) Delete(userID string) error { - userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userId) + userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userID) _, err := a.Client.performQuery(http.MethodDelete, userPath, "2.0", scimHeaders, nil, nil) return err } -func (a UsersAPI) SetUserAsAdmin(userId string, adminGroupId string) error { - userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userId) +// SetUserAsAdmin will add the user to a admin group given the admin group id and user id +func (a UsersAPI) SetUserAsAdmin(userID string, adminGroupID string) error { + userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userID) var addOperations model.UserPatchOperations @@ -129,7 +134,7 @@ func (a UsersAPI) SetUserAsAdmin(userId string, adminGroupId string) error { addOperations = model.UserPatchOperations{ Op: "add", Value: &model.GroupsValue{ - Groups: []model.ValueListItem{model.ValueListItem{Value: adminGroupId}}, + Groups: []model.ValueListItem{model.ValueListItem{Value: adminGroupID}}, }, } userPatchRequest.Operations = append(userPatchRequest.Operations, addOperations) @@ -139,13 +144,14 @@ func (a UsersAPI) SetUserAsAdmin(userId string, adminGroupId string) error { return err } -func (a UsersAPI) VerifyUserAsAdmin(userId string, adminGroupId string) (bool, error) { - user, err := a.read(userId) +// VerifyUserAsAdmin will verify the user belongs to the admin group given the admin group id and user id +func (a UsersAPI) VerifyUserAsAdmin(userID string, adminGroupID string) (bool, error) { + user, err := a.read(userID) if err != nil { return false, err } for _, group := range user.Groups { - if group.Value == adminGroupId { + if group.Value == adminGroupID { return true, nil } } @@ -153,8 +159,9 @@ func (a UsersAPI) VerifyUserAsAdmin(userId string, adminGroupId string) (bool, e return false, nil } -func (a UsersAPI) RemoveUserAsAdmin(userId string, adminGroupId string) error { - userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userId) +// RemoveUserAsAdmin will remove the user from the admin group given the admin group id and user id +func (a UsersAPI) RemoveUserAsAdmin(userID string, adminGroupID string) error { + userPath := fmt.Sprintf("/preview/scim/v2/Users/%v", userID) var removeOperations model.UserPatchOperations @@ -163,7 +170,7 @@ func (a UsersAPI) RemoveUserAsAdmin(userId string, adminGroupId string) error { Operations: []model.UserPatchOperations{}, } - path := fmt.Sprintf("groups[value eq \"%s\"]", adminGroupId) + path := fmt.Sprintf("groups[value eq \"%s\"]", adminGroupID) removeOperations = model.UserPatchOperations{ Op: "remove", Path: path, diff --git a/client/service/users_integration_test.go b/client/service/users_integration_test.go index 4944e0891..eb134cf80 100644 --- a/client/service/users_integration_test.go +++ b/client/service/users_integration_test.go @@ -57,20 +57,20 @@ func TestCreateAdminUser(t *testing.T) { group, err := client.Groups().GetAdminGroup() assert.NoError(t, err, err) - adminGroupId := group.ID + adminGroupID := group.ID - err = client.Users().SetUserAsAdmin(user.ID, adminGroupId) + err = client.Users().SetUserAsAdmin(user.ID, adminGroupID) assert.NoError(t, err, err) - userIsAdmin, err := client.Users().VerifyUserAsAdmin(user.ID, adminGroupId) + userIsAdmin, err := client.Users().VerifyUserAsAdmin(user.ID, adminGroupID) assert.NoError(t, err, err) assert.True(t, userIsAdmin == true) log.Println(userIsAdmin) - err = client.Users().RemoveUserAsAdmin(user.ID, adminGroupId) + err = client.Users().RemoveUserAsAdmin(user.ID, adminGroupID) assert.NoError(t, err, err) - userIsAdmin, err = client.Users().VerifyUserAsAdmin(user.ID, adminGroupId) + userIsAdmin, err = client.Users().VerifyUserAsAdmin(user.ID, adminGroupID) assert.NoError(t, err, err) assert.True(t, userIsAdmin == false) log.Println(userIsAdmin) diff --git a/client/service/users_test.go b/client/service/users_test.go index cf6136b93..8c5a65db9 100644 --- a/client/service/users_test.go +++ b/client/service/users_test.go @@ -81,7 +81,7 @@ func TestScimUserAPI_Update(t *testing.T) { response []string responseStatus []int args []interface{} - wantUri []string + wantURI []string wantErr bool }{ { @@ -93,7 +93,7 @@ func TestScimUserAPI_Update(t *testing.T) { "", }, responseStatus: []int{http.StatusOK, http.StatusOK}, - wantUri: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Users/101030"}, + wantURI: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Users/101030"}, args: []interface{}{ nil, &args{ @@ -112,7 +112,7 @@ func TestScimUserAPI_Update(t *testing.T) { "", }, responseStatus: []int{http.StatusBadRequest, http.StatusOK}, - wantUri: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Users/101030"}, + wantURI: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Users/101030"}, args: []interface{}{ nil, &args{ @@ -128,7 +128,7 @@ func TestScimUserAPI_Update(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodPut}, tt.wantUri, []interface{}{nil, &args{}}, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { + AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodPut}, tt.wantURI, []interface{}{nil, &args{}}, tt.response, tt.responseStatus, nil, tt.wantErr, func(client DBApiClient) (interface{}, error) { return nil, client.Users().Update("101030", tt.args[1].(*args).UserName, tt.args[1].(*args).DisplayName, []string{string(tt.args[1].(*args).Entitlements[0].Value)}, []string{tt.args[1].(*args).Roles[0].Value}) }) }) @@ -137,13 +137,13 @@ func TestScimUserAPI_Update(t *testing.T) { func TestScimUserAPI_Delete(t *testing.T) { type args struct { - UserId string `json:"user_id,omitempty"` + UserID string `json:"user_id,omitempty"` } tests := []struct { name string response string responseStatus int - requestUri string + requestURI string args args want interface{} wantErr bool @@ -153,9 +153,9 @@ func TestScimUserAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - UserId: "10030", + UserID: "10030", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030", + requestURI: "/api/2.0/preview/scim/v2/Users/10030", want: nil, wantErr: false, }, @@ -164,9 +164,9 @@ func TestScimUserAPI_Delete(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - UserId: "10030", + UserID: "10030", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030", + requestURI: "/api/2.0/preview/scim/v2/Users/10030", want: nil, wantErr: true, }, @@ -174,8 +174,8 @@ func TestScimUserAPI_Delete(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertRequestWithMockServer(t, &tt.args, http.MethodDelete, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Users().Delete(tt.args.UserId) + AssertRequestWithMockServer(t, &tt.args, http.MethodDelete, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return nil, client.Users().Delete(tt.args.UserID) }) }) } @@ -183,14 +183,14 @@ func TestScimUserAPI_Delete(t *testing.T) { func TestScimUserAPI_SetUserAsAdmin(t *testing.T) { type args struct { - UserId string `json:"user_id,omitempty"` - AdminGroupId string `json:"user_id,omitempty"` + UserID string `json:"user_id,omitempty"` + AdminGroupID string `json:"user_id,omitempty"` } tests := []struct { name string response string responseStatus int - requestUri string + requestURI string args args want interface{} wantErr bool @@ -200,10 +200,10 @@ func TestScimUserAPI_SetUserAsAdmin(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - UserId: "10030", - AdminGroupId: "10000", + UserID: "10030", + AdminGroupID: "10000", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030", + requestURI: "/api/2.0/preview/scim/v2/Users/10030", want: nil, wantErr: false, }, @@ -212,10 +212,10 @@ func TestScimUserAPI_SetUserAsAdmin(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - UserId: "10030", - AdminGroupId: "10000", + UserID: "10030", + AdminGroupID: "10000", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030", + requestURI: "/api/2.0/preview/scim/v2/Users/10030", want: nil, wantErr: true, }, @@ -228,12 +228,12 @@ func TestScimUserAPI_SetUserAsAdmin(t *testing.T) { Operations: []model.UserPatchOperations{ { Op: "add", - Value: &model.GroupsValue{Groups: []model.ValueListItem{model.ValueListItem{Value: tt.args.AdminGroupId}}}, + Value: &model.GroupsValue{Groups: []model.ValueListItem{model.ValueListItem{Value: tt.args.AdminGroupID}}}, }, }, } - AssertRequestWithMockServer(t, &expectedPatchRequest, http.MethodPatch, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Users().SetUserAsAdmin(tt.args.UserId, tt.args.AdminGroupId) + AssertRequestWithMockServer(t, &expectedPatchRequest, http.MethodPatch, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return nil, client.Users().SetUserAsAdmin(tt.args.UserID, tt.args.AdminGroupID) }) }) } @@ -241,14 +241,14 @@ func TestScimUserAPI_SetUserAsAdmin(t *testing.T) { func TestScimUserAPI_VerifyUserAsAdmin(t *testing.T) { type args struct { - UserId string `json:"user_id,omitempty"` - AdminGroupId string `json:"user_id,omitempty"` + UserID string `json:"user_id,omitempty"` + AdminGroupID string `json:"user_id,omitempty"` } tests := []struct { name string response string responseStatus int - requestUri string + requestURI string args args want interface{} wantErr bool @@ -279,10 +279,10 @@ func TestScimUserAPI_VerifyUserAsAdmin(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{ - UserId: "10030", - AdminGroupId: "100002", + UserID: "10030", + AdminGroupID: "100002", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030?", + requestURI: "/api/2.0/preview/scim/v2/Users/10030?", want: true, wantErr: false, }, @@ -312,10 +312,10 @@ func TestScimUserAPI_VerifyUserAsAdmin(t *testing.T) { }`, responseStatus: http.StatusOK, args: args{ - UserId: "10030", - AdminGroupId: "10000", + UserID: "10030", + AdminGroupID: "10000", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030?", + requestURI: "/api/2.0/preview/scim/v2/Users/10030?", want: false, wantErr: false, }, @@ -324,10 +324,10 @@ func TestScimUserAPI_VerifyUserAsAdmin(t *testing.T) { response: "{}", responseStatus: http.StatusBadRequest, args: args{ - UserId: "10030", - AdminGroupId: "10000", + UserID: "10030", + AdminGroupID: "10000", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030?", + requestURI: "/api/2.0/preview/scim/v2/Users/10030?", want: false, wantErr: true, }, @@ -340,12 +340,12 @@ func TestScimUserAPI_VerifyUserAsAdmin(t *testing.T) { Operations: []model.UserPatchOperations{ { Op: "add", - Value: &model.GroupsValue{Groups: []model.ValueListItem{model.ValueListItem{Value: tt.args.AdminGroupId}}}, + Value: &model.GroupsValue{Groups: []model.ValueListItem{model.ValueListItem{Value: tt.args.AdminGroupID}}}, }, }, } - AssertRequestWithMockServer(t, &expectedPatchRequest, http.MethodGet, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Users().VerifyUserAsAdmin(tt.args.UserId, tt.args.AdminGroupId) + AssertRequestWithMockServer(t, &expectedPatchRequest, http.MethodGet, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Users().VerifyUserAsAdmin(tt.args.UserID, tt.args.AdminGroupID) }) }) } @@ -353,14 +353,14 @@ func TestScimUserAPI_VerifyUserAsAdmin(t *testing.T) { func TestScimUserAPI_RemoveUserAsAdmin(t *testing.T) { type args struct { - UserId string `json:"user_id,omitempty"` - AdminGroupId string `json:"user_id,omitempty"` + UserID string `json:"user_id,omitempty"` + AdminGroupID string `json:"user_id,omitempty"` } tests := []struct { name string response string responseStatus int - requestUri string + requestURI string args args want interface{} wantErr bool @@ -370,10 +370,10 @@ func TestScimUserAPI_RemoveUserAsAdmin(t *testing.T) { response: "", responseStatus: http.StatusOK, args: args{ - UserId: "10030", - AdminGroupId: "10000", + UserID: "10030", + AdminGroupID: "10000", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030", + requestURI: "/api/2.0/preview/scim/v2/Users/10030", want: nil, wantErr: false, }, @@ -382,10 +382,10 @@ func TestScimUserAPI_RemoveUserAsAdmin(t *testing.T) { response: "", responseStatus: http.StatusBadRequest, args: args{ - UserId: "10030", - AdminGroupId: "10000", + UserID: "10030", + AdminGroupID: "10000", }, - requestUri: "/api/2.0/preview/scim/v2/Users/10030", + requestURI: "/api/2.0/preview/scim/v2/Users/10030", want: nil, wantErr: true, }, @@ -398,12 +398,12 @@ func TestScimUserAPI_RemoveUserAsAdmin(t *testing.T) { Operations: []model.UserPatchOperations{ { Op: "remove", - Path: fmt.Sprintf("groups[value eq \"%s\"]", tt.args.AdminGroupId), + Path: fmt.Sprintf("groups[value eq \"%s\"]", tt.args.AdminGroupID), }, }, } - AssertRequestWithMockServer(t, &expectedPatchRequest, http.MethodPatch, tt.requestUri, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return nil, client.Users().RemoveUserAsAdmin(tt.args.UserId, tt.args.AdminGroupId) + AssertRequestWithMockServer(t, &expectedPatchRequest, http.MethodPatch, tt.requestURI, &input, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return nil, client.Users().RemoveUserAsAdmin(tt.args.UserID, tt.args.AdminGroupID) }) }) } @@ -411,14 +411,14 @@ func TestScimUserAPI_RemoveUserAsAdmin(t *testing.T) { func TestScimUserAPI_Read(t *testing.T) { type args struct { - UserId string `json:"user_id"` + UserID string `json:"user_id"` } tests := []struct { name string response []string responseStatus []int args []args - wantUri []string + wantURI []string want model.User wantErr bool }{ @@ -490,10 +490,10 @@ func TestScimUserAPI_Read(t *testing.T) { responseStatus: []int{http.StatusOK, http.StatusOK, http.StatusOK}, args: []args{ { - UserId: "101030", + UserID: "101030", }, }, - wantUri: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Groups/100002?", "/api/2.0/preview/scim/v2/Groups/101355?"}, + wantURI: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Groups/100002?", "/api/2.0/preview/scim/v2/Groups/101355?"}, want: model.User{ ID: "101030", DisplayName: "test.user@databricks.com", @@ -535,10 +535,10 @@ func TestScimUserAPI_Read(t *testing.T) { responseStatus: []int{http.StatusBadRequest}, args: []args{ { - UserId: "101030", + UserID: "101030", }, }, - wantUri: []string{"/api/2.0/preview/scim/v2/Users/101030?"}, + wantURI: []string{"/api/2.0/preview/scim/v2/Users/101030?"}, want: model.User{}, wantErr: true, }, @@ -550,10 +550,10 @@ func TestScimUserAPI_Read(t *testing.T) { responseStatus: []int{http.StatusOK}, args: []args{ { - UserId: "101030", + UserID: "101030", }, }, - wantUri: []string{"/api/2.0/preview/scim/v2/Users/101030?"}, + wantURI: []string{"/api/2.0/preview/scim/v2/Users/101030?"}, want: model.User{}, wantErr: true, }, @@ -586,10 +586,10 @@ func TestScimUserAPI_Read(t *testing.T) { responseStatus: []int{http.StatusOK, http.StatusBadRequest}, args: []args{ { - UserId: "101030", + UserID: "101030", }, }, - wantUri: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Groups/100002?"}, + wantURI: []string{"/api/2.0/preview/scim/v2/Users/101030?", "/api/2.0/preview/scim/v2/Groups/100002?"}, want: model.User{ ID: "101030", DisplayName: "test.user@databricks.com", @@ -614,8 +614,8 @@ func TestScimUserAPI_Read(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var input args - AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodGet, http.MethodGet}, tt.wantUri, []args{input}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { - return client.Users().Read(tt.args[0].UserId) + AssertMultipleRequestsWithMockServer(t, tt.args, []string{http.MethodGet, http.MethodGet, http.MethodGet}, tt.wantURI, []args{input}, tt.response, tt.responseStatus, tt.want, tt.wantErr, func(client DBApiClient) (interface{}, error) { + return client.Users().Read(tt.args[0].UserID) }) }) }