Skip to content

Commit

Permalink
Fixed & refactored linting issues in client/service package
Browse files Browse the repository at this point in the history
  • Loading branch information
stikkireddy committed Apr 20, 2020
1 parent 0bbf3e4 commit 67dca86
Show file tree
Hide file tree
Showing 33 changed files with 552 additions and 482 deletions.
14 changes: 14 additions & 0 deletions client/service/apis.go
Expand Up @@ -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
}
Expand All @@ -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}
}
Expand Down
24 changes: 16 additions & 8 deletions client/service/client.go
Expand Up @@ -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"`
Expand All @@ -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"
)
Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions client/service/clusters.go
Expand Up @@ -9,6 +9,7 @@ import (
"time"
)

// ClustersAPI is a struct that contains the Databricks api client to perform queries
type ClustersAPI struct {
Client DBApiClient
}
Expand All @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 67dca86

Please sign in to comment.