diff --git a/exporter/exporter_test.go b/exporter/exporter_test.go index ea96abe68e..29142bc88f 100644 --- a/exporter/exporter_test.go +++ b/exporter/exporter_test.go @@ -1575,13 +1575,13 @@ func TestImportingGlobalInitScripts(t *testing.T) { }, { Method: "GET", - Resource: "/api/2.0/global-init-scripts/C39FD6BAC8088BBC", + Resource: "/api/2.0/global-init-scripts/C39FD6BAC8088BBC?", ReuseRequest: true, Response: getJSONObject("test-data/global-init-script-get1.json"), }, { Method: "GET", - Resource: "/api/2.0/global-init-scripts/F931E63C248C1D8C", + Resource: "/api/2.0/global-init-scripts/F931E63C248C1D8C?", ReuseRequest: true, Response: getJSONObject("test-data/global-init-script-get2.json"), }, diff --git a/exporter/importables.go b/exporter/importables.go index 148b4953df..e2c30e0783 100644 --- a/exporter/importables.go +++ b/exporter/importables.go @@ -1258,25 +1258,25 @@ var resourcesMap map[string]importable = map[string]importable{ return name }, List: func(ic *importContext) error { - globalInitScripts, err := workspace.NewGlobalInitScriptsAPI(ic.Context, ic.Client).List() + globalInitScripts, err := ic.workspaceClient.GlobalInitScripts.ListAll(ic.Context) if err != nil { return err } for offset, gis := range globalInitScripts { ic.EmitIfUpdatedAfterMillis(&resource{ Resource: "databricks_global_init_script", - ID: gis.ScriptID, - }, gis.UpdatedAt, fmt.Sprintf("global init script '%s'", gis.Name)) + ID: gis.ScriptId, + }, int64(gis.UpdatedAt), fmt.Sprintf("global init script '%s'", gis.Name)) log.Printf("[INFO] Scanned %d of %d global init scripts", offset+1, len(globalInitScripts)) } return nil }, Import: func(ic *importContext, r *resource) error { - gis, err := workspace.NewGlobalInitScriptsAPI(ic.Context, ic.Client).Get(r.ID) + gis, err := ic.workspaceClient.GlobalInitScripts.GetByScriptId(ic.Context, r.ID) if err != nil { return err } - content, err := base64.StdEncoding.DecodeString(gis.ContentBase64) + content, err := base64.StdEncoding.DecodeString(gis.Script) if err != nil { return err } diff --git a/exporter/importables_test.go b/exporter/importables_test.go index 9418e4b3af..70f06c8de8 100644 --- a/exporter/importables_test.go +++ b/exporter/importables_test.go @@ -921,18 +921,18 @@ func TestGlobalInitScriptsBodyErrors(t *testing.T) { qa.HTTPFixturesApply(t, []qa.HTTPFixture{ { Method: "GET", - Resource: "/api/2.0/global-init-scripts/sad-emoji", - Response: workspace.GlobalInitScriptInfo{ - Name: "x.sh", - ContentBase64: "🥺", + Resource: "/api/2.0/global-init-scripts/sad-emoji?", + Response: compute.GlobalInitScriptDetailsWithContent{ + Name: "x.sh", + Script: "🥺", }, }, { Method: "GET", - Resource: "/api/2.0/global-init-scripts/second", - Response: workspace.GlobalInitScriptInfo{ - Name: "x.sh", - ContentBase64: "YWJj", + Resource: "/api/2.0/global-init-scripts/second?", + Response: compute.GlobalInitScriptDetailsWithContent{ + Name: "x.sh", + Script: "YWJj", }, }, }, func(ctx context.Context, client *common.DatabricksClient) { @@ -1212,11 +1212,12 @@ func TestGlobalInitScriptGeneration(t *testing.T) { { Method: "GET", ReuseRequest: true, - Resource: "/api/2.0/global-init-scripts/a", - Response: workspace.GlobalInitScriptInfo{ - Name: "New: Importing ^ Things", - Enabled: true, - ContentBase64: "YWJj", + Resource: "/api/2.0/global-init-scripts/a?", + Response: compute.GlobalInitScriptDetailsWithContent{ + ScriptId: "a", + Name: "New: Importing ^ Things", + Enabled: true, + Script: "YWJj", }, }, }, "workspace", false, func(ic *importContext) { diff --git a/workspace/global_init_scripts.go b/workspace/global_init_scripts.go deleted file mode 100644 index 4e20b74753..0000000000 --- a/workspace/global_init_scripts.go +++ /dev/null @@ -1,83 +0,0 @@ -package workspace - -import ( - "context" - - "github.com/databricks/terraform-provider-databricks/common" -) - -// NewGlobalInitScriptsAPI creates GlobalInitScriptsAPI instance from provider meta -func NewGlobalInitScriptsAPI(ctx context.Context, m any) GlobalInitScriptsAPI { - return GlobalInitScriptsAPI{ - client: m.(*common.DatabricksClient), - context: ctx, - } -} - -// GlobalInitScriptsAPI exposes the Global Init Scripts API: https://docs.databricks.com/dev-tools/api/latest/global-init-scripts.html# -type GlobalInitScriptsAPI struct { - client *common.DatabricksClient - context context.Context -} - -// GlobalInitScriptInfo contains information about registered global init script -type GlobalInitScriptInfo struct { - ScriptID string `json:"script_id"` - Name string `json:"name"` - Position int32 `json:"position,omitempty" tf:"computed"` - Enabled bool `json:"enabled,omitempty"` - CreatedBy string `json:"creator_user_name,omitempty"` - CreatedAt int64 `json:"created_at_timestamp,omitempty"` - UpdatedBy string `json:"updater_user_name,omitempty"` - UpdatedAt int64 `json:"updated_at_timestamp,omitempty"` - ContentBase64 string `json:"script,omitempty"` -} - -// GlobalInitScriptPayload contains information about registered global init script -type GlobalInitScriptPayload struct { - Name string `json:"name"` - Position int32 `json:"position"` - Enabled bool `json:"enabled"` - ContentBase64 string `json:"script"` -} - -type globalInitScriptCreateResponse struct { - ScriptID string `json:"script_id"` -} - -type globalInitScriptListResponse struct { - Scripts []GlobalInitScriptInfo `json:"scripts"` -} - -// List returns a list of registered global init scripts -func (a GlobalInitScriptsAPI) List() ([]GlobalInitScriptInfo, error) { - var giss globalInitScriptListResponse - err := a.client.Get(a.context, "/global-init-scripts", nil, &giss) - return giss.Scripts, err -} - -// Get returns information about specific global init scripts -func (a GlobalInitScriptsAPI) Get(scriptID string) (initScript GlobalInitScriptInfo, err error) { - err = a.client.Get(a.context, "/global-init-scripts/"+scriptID, nil, &initScript) - return -} - -// Delete deletes specific global init scripts -func (a GlobalInitScriptsAPI) Delete(scriptID string) error { - request := map[string]string{ - "script_id": scriptID, - } - return a.client.Delete(a.context, "/global-init-scripts/"+scriptID, request) -} - -// Create creates the global init script from the given payload. -func (a GlobalInitScriptsAPI) Create(payload GlobalInitScriptPayload) (string, error) { - var response globalInitScriptCreateResponse - err := a.client.Post(a.context, "/global-init-scripts", payload, &response) - return response.ScriptID, err -} - -// Update updates the specific global init script from the given payload. -func (a GlobalInitScriptsAPI) Update(scriptID string, payload GlobalInitScriptPayload) error { - return a.client.Patch(a.context, "/global-init-scripts/"+scriptID, payload) -} diff --git a/workspace/resource_global_init_script.go b/workspace/resource_global_init_script.go index f8b3fe7edd..37d915941f 100644 --- a/workspace/resource_global_init_script.go +++ b/workspace/resource_global_init_script.go @@ -6,6 +6,7 @@ import ( "fmt" "regexp" + "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/terraform-provider-databricks/common" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -53,22 +54,28 @@ func ResourceGlobalInitScript() common.Resource { return fmt.Errorf("size of the global init script (%d bytes) exceeds maximal allowed (%d bytes)", contentLen, maxScriptSize) } - globalInitScriptsAPI := NewGlobalInitScriptsAPI(ctx, c) - scriptID, err := globalInitScriptsAPI.Create(GlobalInitScriptPayload{ - ContentBase64: base64.StdEncoding.EncodeToString(content), - Enabled: d.Get("enabled").(bool), - Position: int32(d.Get("position").(int)), - Name: d.Get("name").(string), + w, err := c.WorkspaceClient() + if err != nil { + return err + } + created, err := w.GlobalInitScripts.Create(ctx, compute.GlobalInitScriptCreateRequest{ + Script: base64.StdEncoding.EncodeToString(content), + Enabled: d.Get("enabled").(bool), + Position: d.Get("position").(int), + Name: d.Get("name").(string), }) if err != nil { return err } - d.SetId(scriptID) + d.SetId(created.ScriptId) return nil }, Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { - globalInitScriptsAPI := NewGlobalInitScriptsAPI(ctx, c) - scriptStatus, err := globalInitScriptsAPI.Get(d.Id()) + w, err := c.WorkspaceClient() + if err != nil { + return err + } + scriptStatus, err := w.GlobalInitScripts.GetByScriptId(ctx, d.Id()) if err != nil { return err } @@ -83,16 +90,24 @@ func ResourceGlobalInitScript() common.Resource { return fmt.Errorf("size of the global init script (%d bytes) exceeds maximal allowed (%d bytes)", contentLen, maxScriptSize) } - globalInitScriptsAPI := NewGlobalInitScriptsAPI(ctx, c) - return globalInitScriptsAPI.Update(d.Id(), GlobalInitScriptPayload{ - ContentBase64: base64.StdEncoding.EncodeToString(content), - Enabled: d.Get("enabled").(bool), - Position: int32(d.Get("position").(int)), - Name: d.Get("name").(string), + w, err := c.WorkspaceClient() + if err != nil { + return err + } + return w.GlobalInitScripts.Update(ctx, compute.GlobalInitScriptUpdateRequest{ + ScriptId: d.Id(), + Script: base64.StdEncoding.EncodeToString(content), + Enabled: d.Get("enabled").(bool), + Position: d.Get("position").(int), + Name: d.Get("name").(string), }) }, Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error { - return NewGlobalInitScriptsAPI(ctx, c).Delete(d.Id()) + w, err := c.WorkspaceClient() + if err != nil { + return err + } + return w.GlobalInitScripts.DeleteByScriptId(ctx, d.Id()) }, Schema: s, SchemaVersion: 1, diff --git a/workspace/resource_global_init_script_test.go b/workspace/resource_global_init_script_test.go index c31ec7a8fc..57a0290270 100644 --- a/workspace/resource_global_init_script_test.go +++ b/workspace/resource_global_init_script_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/databricks/databricks-sdk-go/apierr" + "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/terraform-provider-databricks/qa" "github.com/stretchr/testify/assert" @@ -19,17 +20,17 @@ func TestResourceGlobalInitScriptRead(t *testing.T) { Fixtures: []qa.HTTPFixture{ { Method: http.MethodGet, - Resource: "/api/2.0/global-init-scripts/1234", - Response: GlobalInitScriptInfo{ - ScriptID: "1234", - Name: "Test", - Position: 0, - Enabled: true, - CreatedBy: "someuser@domain.com", - CreatedAt: 1612520583493, - UpdatedBy: "someuser@domain.com", - UpdatedAt: 1612520583493, - ContentBase64: "ZWNobyBoZWxsbw==", + Resource: "/api/2.0/global-init-scripts/1234?", + Response: compute.GlobalInitScriptDetailsWithContent{ + ScriptId: "1234", + Name: "Test", + Position: 0, + Enabled: true, + CreatedBy: "someuser@domain.com", + CreatedAt: 1612520583493, + UpdatedBy: "someuser@domain.com", + UpdatedAt: 1612520583493, + Script: "ZWNobyBoZWxsbw==", }, }, }, @@ -51,7 +52,7 @@ func TestResourceGlobalInitScriptDelete(t *testing.T) { Fixtures: []qa.HTTPFixture{ { Method: http.MethodDelete, - Resource: "/api/2.0/global-init-scripts/" + scriptID + "?script_id=" + scriptID, + Resource: "/api/2.0/global-init-scripts/1234?", Status: http.StatusOK, }, }, @@ -68,7 +69,7 @@ func TestResourceGlobalInitScriptRead_NotFound(t *testing.T) { Fixtures: []qa.HTTPFixture{ { // read log output for correct url... Method: "GET", - Resource: "/api/2.0/global-init-scripts/1234", + Resource: "/api/2.0/global-init-scripts/1234?", Response: apierr.APIErrorBody{ ErrorCode: "RESOURCE_DOES_NOT_EXIST", Message: "The global unit script with ID 1234 does not exist.", @@ -89,23 +90,22 @@ func TestResourceGlobalInitScriptCreate(t *testing.T) { { Method: "POST", Resource: "/api/2.0/global-init-scripts", - ExpectedRequest: GlobalInitScriptPayload{ - Name: "test", - ContentBase64: "ZWNobyBoZWxsbw==", + ExpectedRequest: compute.GlobalInitScriptCreateRequest{ + Name: "test", + Script: "ZWNobyBoZWxsbw==", }, - Response: globalInitScriptCreateResponse{ - ScriptID: "1234", + Response: compute.CreateResponse{ + ScriptId: "1234", }, }, { Method: "GET", - Resource: "/api/2.0/global-init-scripts/1234", + Resource: "/api/2.0/global-init-scripts/1234?", ReuseRequest: true, - Response: GlobalInitScriptInfo{ - ScriptID: "1234", - ContentBase64: "ZWNobyBoZWxsbw==", - Position: 0, - Name: "test", + Response: compute.GlobalInitScriptDetailsWithContent{ + ScriptId: "1234", + Script: "ZWNobyBoZWxsbw==", + Name: "test", }, }, }, @@ -156,24 +156,24 @@ func TestResourceGlobalInitScriptUpdate(t *testing.T) { { Method: "PATCH", Resource: "/api/2.0/global-init-scripts/1234", - ExpectedRequest: GlobalInitScriptPayload{ - Name: "test", - Position: 0, - ContentBase64: "ZWNobyBoZWxsbw==", + ExpectedRequest: compute.GlobalInitScriptUpdateRequest{ + Name: "test", + Position: 0, + Script: "ZWNobyBoZWxsbw==", }, - Response: globalInitScriptCreateResponse{ - ScriptID: "1234", + Response: compute.CreateResponse{ + ScriptId: "1234", }, }, { Method: "GET", - Resource: "/api/2.0/global-init-scripts/1234", + Resource: "/api/2.0/global-init-scripts/1234?", ReuseRequest: true, - Response: GlobalInitScriptInfo{ - ScriptID: "1234", - ContentBase64: "ZWNobyBoZWxsbw==", - Position: 0, - Name: "test", + Response: compute.GlobalInitScriptDetailsWithContent{ + ScriptId: "1234", + Script: "ZWNobyBoZWxsbw==", + Position: 0, + Name: "test", }, }, },