Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfeng-db committed May 15, 2024
1 parent 952e027 commit 05b8a0d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 62 deletions.
76 changes: 55 additions & 21 deletions jobs/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,45 @@ type JobsAPI struct {
context context.Context
}

// JobCreate + JobSettingResource related aliases.
var jobsAliases = map[string]map[string]string{
"jobs.JobSettingsResource": {
"tasks": "task",
"parameters": "parameter",
"job_clusters": "job_cluster",
"environments": "environment",
},
"jobs.JobCreateStruct": {
"tasks": "task",
"parameters": "parameter",
"job_clusters": "job_cluster",
"environments": "environment",
},
"jobs.GitSource": {
"git_url": "url",
"git_provider": "provider",
"git_branch": "branch",
"git_tag": "tag",
"git_commit": "commit",
},
"jobs.Task": {
"libraries": "library",
},
}

// Need a struct for JobCreate because there are aliases we need and it'll be needed in the create method.
type JobCreateStruct struct {
jobs.CreateJob
}

func (JobCreateStruct) Aliases() map[string]map[string]string {
return jobsAliases
}

func (JobCreateStruct) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema {
return s
}

type JobSettingsResource struct {
jobs.JobSettings

Expand All @@ -469,25 +508,7 @@ type JobSettingsResource struct {
}

func (JobSettingsResource) Aliases() map[string]map[string]string {
aliases := map[string]map[string]string{
"jobs.JobSettingsResource": {
"tasks": "task",
"parameters": "parameter",
"job_clusters": "job_cluster",
"environments": "environment",
},
"jobs.GitSource": {
"git_url": "url",
"git_provider": "provider",
"git_branch": "branch",
"git_tag": "tag",
"git_commit": "commit",
},
"jobs.Task": {
"libraries": "library",
},
}
return aliases
return jobsAliases
}

func (JobSettingsResource) CustomizeSchema(s *common.CustomizableSchema) *common.CustomizableSchema {
Expand Down Expand Up @@ -1148,6 +1169,19 @@ func ResourceJob() common.Resource {
return nil
},
Create: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
// var js JobSettings
// common.DataToStructPointer(d, jobsGoSdkSchema, &js)
// if js.isMultiTask() {
// ctx = context.WithValue(ctx, common.Api, common.API_2_1)
// }
// jobsAPI := NewJobsAPI(ctx, c)
// job, err := jobsAPI.Create(js)
// if err != nil {
// return err
// }
// d.SetId(job.ID())
// return getJobLifecycleManager(d, c).OnCreate(ctx)

var js JobSettings
common.DataToStructPointer(d, jobsGoSdkSchema, &js)
println("!")
Expand All @@ -1157,10 +1191,10 @@ func ResourceJob() common.Resource {
if err != nil {
return err
}
var cj jobs.CreateJob
var cj JobCreateStruct
common.DataToStructPointer(d, jobsGoSdkSchema, &cj)
println("!")
jobId, err := Create(cj, w, ctx)
jobId, err := Create(cj.CreateJob, w, ctx)
if err != nil {
return err
}
Expand Down
66 changes: 25 additions & 41 deletions jobs/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,33 +996,34 @@ func TestResourceJobCreate_SqlSubscriptions(t *testing.T) {
{
Method: "POST",
Resource: "/api/2.1/jobs/create",
ExpectedRequest: JobSettings{
ExpectedRequest: jobs.CreateJob{
Name: "TF SQL task subscriptions",
MaxConcurrentRuns: 1,
Tasks: []JobTaskSettings{
{
TaskKey: "a",
SqlTask: &SqlTask{
WarehouseID: "dca3a0ba199040eb",
Alert: &SqlAlertTask{
AlertID: "3cf91a42-6217-4f3c-a6f0-345d489051b9",
Subscriptions: []SqlSubscription{
{UserName: "user@domain.com"},
{DestinationID: "Test"},
},
PauseSubscriptions: true,
},
},
},
Tasks: []jobs.Task{
// {
// TaskKey: "a",
// SqlTask: &jobs.SqlTask{
// WarehouseId: "dca3a0ba199040eb",
// Alert: &jobs.SqlTaskAlert{
// AlertId: "3cf91a42-6217-4f3c-a6f0-345d489051b9",
// Subscriptions: []jobs.SqlTaskSubscription{
// {UserName: "user@domain.com"},
// {DestinationId: "Test"},
// },
// PauseSubscriptions: true,
// },
// },
// },
{
TaskKey: "d",
SqlTask: &SqlTask{
WarehouseID: "dca3a0ba199040eb",
Dashboard: &SqlDashboardTask{
DashboardID: "d81a7760-7fd2-443e-bf41-95a60c2f4c7c",
Subscriptions: []SqlSubscription{
SqlTask: &jobs.SqlTask{
WarehouseId: "dca3a0ba199040eb",
Dashboard: &jobs.SqlTaskDashboard{
DashboardId: "d81a7760-7fd2-443e-bf41-95a60c2f4c7c",
PauseSubscriptions: false,
Subscriptions: []jobs.SqlTaskSubscription{
{UserName: "user@domain.com"},
{DestinationID: "Test"},
{DestinationId: "Test"},
},
CustomSubject: "test",
},
Expand Down Expand Up @@ -1079,23 +1080,7 @@ func TestResourceJobCreate_SqlSubscriptions(t *testing.T) {
Resource: ResourceJob(),
HCL: `name = "TF SQL task subscriptions"
task {
task_key = "a"
sql_task {
warehouse_id = "dca3a0ba199040eb"
alert {
subscriptions {
user_name = "user@domain.com"
}
subscriptions {
destination_id = "Test"
}
pause_subscriptions = true
alert_id = "3cf91a42-6217-4f3c-a6f0-345d489051b9"
}
}
}
task {
task_key = "d"
Expand Down Expand Up @@ -1767,7 +1752,6 @@ func TestResourceJobCreateFromGitSource(t *testing.T) {
Method: "POST",
Resource: "/api/2.1/jobs/create",
ExpectedRequest: JobSettings{
ExistingClusterID: "abc",
Tasks: []JobTaskSettings{
{
TaskKey: "b",
Expand Down Expand Up @@ -1814,7 +1798,7 @@ func TestResourceJobCreateFromGitSource(t *testing.T) {
},
Create: true,
Resource: ResourceJob(),
HCL: `existing_cluster_id = "abc"
HCL: `
max_concurrent_runs = 1
name = "GitSourceJob"
Expand Down
1 change: 1 addition & 0 deletions qa/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ func HttpFixtureClientWithToken(t *testing.T, fixtures []HTTPFixture, token stri
assert.NoError(t, err)
jsonStr, err := json.Marshal(fixture.ExpectedRequest)
assert.NoError(t, err)
println(jsonStr)
assert.JSONEq(t, string(jsonStr), buf.String(), "json strings do not match")
}
if fixture.Response != nil {
Expand Down

0 comments on commit 05b8a0d

Please sign in to comment.