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

Fix and changes for release 0.2.23 #246

Merged
merged 3 commits into from
Apr 8, 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
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

All notable changes to this project will be documented in this file.

## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.22...HEAD)
## [Unreleased](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.23...HEAD)

## [0.2.23](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.22...v0.2.23)

## Changes

- [#244](https://github.com/dbt-labs/terraform-provider-dbtcloud/pull/244) Better error handling when GitLab repositories are created with a User Token

## Fixes

- [#245](https://github.com/dbt-labs/terraform-provider-dbtcloud/issues/245) Issues on `dbtcloud_job` when modifying an existing job schedule

## [0.2.22](https://github.com/dbt-labs/terraform-provider-dbtcloud/compare/v0.2.21...v0.2.22)

Expand Down
2 changes: 2 additions & 0 deletions pkg/dbt_cloud/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (c *Client) CreateJob(
}
if scheduleType == "days_of_week" {
date.Days = &scheduleDays
date.Cron = nil
} else if scheduleCron != "" {
date.Cron = &scheduleCron
}
Expand Down Expand Up @@ -263,6 +264,7 @@ func (c *Client) CreateJob(
}

func (c *Client) UpdateJob(jobId string, job Job) (*Job, error) {

jobData, err := json.Marshal(job)
if err != nil {
return nil, err
Expand Down
32 changes: 23 additions & 9 deletions pkg/resources/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var jobSchema = map[string]*schema.Schema{
Default: 1,
Description: "Number of hours between job executions if running on a schedule",
ValidateFunc: validation.IntBetween(1, 23),
ConflictsWith: []string{"schedule_hours"},
ConflictsWith: []string{"schedule_hours", "schedule_cron"},
},
"schedule_hours": &schema.Schema{
Type: schema.TypeList,
Expand All @@ -122,7 +122,7 @@ var jobSchema = map[string]*schema.Schema{
Type: schema.TypeInt,
},
Description: "List of hours to execute the job at if running on a schedule",
ConflictsWith: []string{"schedule_interval"},
ConflictsWith: []string{"schedule_interval", "schedule_cron"},
},
"schedule_days": &schema.Schema{
Type: schema.TypeList,
Expand All @@ -134,9 +134,10 @@ var jobSchema = map[string]*schema.Schema{
Description: "List of days of week as numbers (0 = Sunday, 7 = Saturday) to execute the job at if running on a schedule",
},
"schedule_cron": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Custom cron expression for schedule",
Type: schema.TypeString,
Optional: true,
Description: "Custom cron expression for schedule",
ConflictsWith: []string{"schedule_interval", "schedule_hours"},
},
"deferring_job_id": &schema.Schema{
Type: schema.TypeInt,
Expand Down Expand Up @@ -531,10 +532,7 @@ func resourceJobUpdate(
return diag.FromErr(fmt.Errorf("schedule was not provided"))
}
}
if d.HasChange("schedule_type") {
scheduleType := d.Get("schedule_type").(string)
job.Schedule.Date.Type = scheduleType
}

if d.HasChange("schedule_interval") {
scheduleInterval := d.Get("schedule_interval").(int)
job.Schedule.Time.Interval = scheduleInterval
Expand All @@ -551,6 +549,7 @@ func resourceJobUpdate(
job.Schedule.Time.Interval = 0
} else {
job.Schedule.Time.Hours = nil
job.Schedule.Time.Interval = d.Get("schedule_interval").(int)
job.Schedule.Time.Type = "every_hour"
}
}
Expand All @@ -567,6 +566,21 @@ func resourceJobUpdate(
scheduleCron := d.Get("schedule_cron").(string)
job.Schedule.Date.Cron = &scheduleCron
}

// we set this after the subfields to remove the fields not matching the schedule type
// if it was before, some of those fields would be set again
if d.HasChange("schedule_type") {
scheduleType := d.Get("schedule_type").(string)
job.Schedule.Date.Type = scheduleType

if scheduleType == "days_of_week" || scheduleType == "every_day" {
job.Schedule.Date.Cron = nil
}
if scheduleType == "custom_cron" || scheduleType == "every_day" {
job.Schedule.Date.Days = nil
}
}

if d.HasChange("deferring_job_id") {
deferringJobId := d.Get("deferring_job_id").(int)
if deferringJobId != 0 {
Expand Down
116 changes: 116 additions & 0 deletions pkg/resources/job_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,122 @@ resource "dbtcloud_job" "test_job_3" {
`, projectName, environmentName, DBT_CLOUD_VERSION, jobName, DBT_CLOUD_VERSION, jobName2, deferParam, jobName3, selfDefer)
}

func TestAccDbtCloudJobResourceSchedules(t *testing.T) {

jobName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
environmentName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDbtCloudJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccDbtCloudJobResourceScheduleConfig(
jobName,
projectName,
environmentName,
"every_day",
),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job"),
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "name", jobName),
),
},
// MODIFY SCHEDULE
{
Config: testAccDbtCloudJobResourceScheduleConfig(
jobName,
projectName,
environmentName,
"days_of_week",
),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job"),
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "name", jobName),
),
},
// MODIFY SCHEDULE
{
Config: testAccDbtCloudJobResourceScheduleConfig(
jobName,
projectName,
environmentName,
"custom_cron",
),
Check: resource.ComposeTestCheckFunc(
testAccCheckDbtCloudJobExists("dbtcloud_job.test_job"),
resource.TestCheckResourceAttr("dbtcloud_job.test_job", "name", jobName),
),
},

// IMPORT
{
ResourceName: "dbtcloud_job.test_job",
ImportState: true,
ImportStateVerify: true,
// we don't check triggers.custom_branch_only as we currently allow people to keep triggers.custom_branch_only in their config to not break peopple's Terraform project
ImportStateVerifyIgnore: []string{
"triggers.%",
"triggers.custom_branch_only",
},
},
},
})
}

func testAccDbtCloudJobResourceScheduleConfig(
jobName, projectName, environmentName, scheduleType string,
) string {

scheduleConfig := ""
if scheduleType == "every_day" {
scheduleConfig = `
schedule_type = "every_day"
schedule_hours = [1,2,3]`
} else if scheduleType == "days_of_week" {
scheduleConfig = `
schedule_type = "days_of_week"
schedule_interval = 2
schedule_days = [1,4]`
} else if scheduleType == "custom_cron" {
scheduleConfig = `
schedule_cron = "0 21 * * *"
schedule_type = "custom_cron"`
} else {
panic("Incorrect schedule type")
}

return fmt.Sprintf(`
resource "dbtcloud_project" "test_job_project" {
name = "%s"
}

resource "dbtcloud_environment" "test_job_environment" {
project_id = dbtcloud_project.test_job_project.id
name = "%s"
dbt_version = "%s"
type = "development"
}

resource "dbtcloud_job" "test_job" {
name = "%s"
project_id = dbtcloud_project.test_job_project.id
environment_id = dbtcloud_environment.test_job_environment.environment_id
execute_steps = [
"dbt test"
]
triggers = {
"github_webhook": false,
"git_provider_webhook": false,
"schedule": false,
}
%s
}
`, projectName, environmentName, DBT_CLOUD_VERSION, jobName, scheduleConfig)
}

func testAccCheckDbtCloudJobExists(resource string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[resource]
Expand Down
17 changes: 17 additions & 0 deletions pkg/resources/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ func resourceRepositoryCreate(
return diag.FromErr(err)
}

// checking potential issues with the creation of GitLab repositories with service tokens
if repository.RepositoryCredentialsID == nil && gitlabProjectID != 0 {

repositoryIDString := fmt.Sprintf("%d", *repository.ID)
projectIDString := fmt.Sprintf("%d", repository.ProjectID)
_, err := c.DeleteRepository(repositoryIDString, projectIDString)
if err != nil {
return diag.FromErr(err)
}

return diag.FromErr(
fmt.Errorf(
"`repository_credentials_id` is not set after creating the repository. This is likely due to creating the repository with a service token. Only user tokens / personal access tokens are supported for GitLab at the moment",
),
)
}

d.SetId(fmt.Sprintf("%d%s%d", repository.ProjectID, dbt_cloud.ID_DELIMITER, *repository.ID))

resourceRepositoryRead(ctx, d, m)
Expand Down
Loading