Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate databricks_global_init_script to Go SDK #2036

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
Expand Down
10 changes: 5 additions & 5 deletions exporter/importables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 14 additions & 13 deletions exporter/importables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
83 changes: 0 additions & 83 deletions workspace/global_init_scripts.go

This file was deleted.

47 changes: 31 additions & 16 deletions workspace/resource_global_init_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
72 changes: 36 additions & 36 deletions workspace/resource_global_init_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: "[email protected]",
CreatedAt: 1612520583493,
UpdatedBy: "[email protected]",
UpdatedAt: 1612520583493,
ContentBase64: "ZWNobyBoZWxsbw==",
Resource: "/api/2.0/global-init-scripts/1234?",
Response: compute.GlobalInitScriptDetailsWithContent{
ScriptId: "1234",
Name: "Test",
Position: 0,
Enabled: true,
CreatedBy: "[email protected]",
CreatedAt: 1612520583493,
UpdatedBy: "[email protected]",
UpdatedAt: 1612520583493,
Script: "ZWNobyBoZWxsbw==",
},
},
},
Expand All @@ -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,
},
},
Expand All @@ -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.",
Expand All @@ -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",
},
},
},
Expand Down Expand Up @@ -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",
},
},
},
Expand Down
Loading