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

feat: Add scheduled project trigger #620

Merged
merged 17 commits into from
Mar 27, 2024
Merged

Conversation

tleed5
Copy link
Collaborator

@tleed5 tleed5 commented Mar 22, 2024

Background

The terraform provider currently doesn't support scheduled project triggers

Internal Story: sc-73797

Results

fixes #607

This PR adds support for the scheduled project trigger type by adding a new resource octopusdeploy_project_scheduled_trigger

Example

Once daily schedule

resource "octopusdeploy_project_scheduled_trigger" "once_daily" {
  name        = "Once Daily"
  description = "This is a once daily schedule"
  project_id  = octopusdeploy_project.non_tenanted.id
  space_id    = octopusdeploy_project.non_tenanted.space_id
  deploy_new_release_action {
    destination_environment_id = octopusdeploy_environment.env_1.id
  }
  once_daily_schedule {
    start_time   = "2024-03-22T09:00:00"
    days_of_week = ["Tuesday", "Wednesday", "Monday"]
  }
}

Tenanted Schedule

resource "octopusdeploy_project_scheduled_trigger" "tenanted_trigger" {
  name = "Cron Tenanted"
  project_id = octopusdeploy_project.tenanted.id
  space_id = octopusdeploy_project.tenanted.space_id
  tenant_ids = [octopusdeploy_tenant.tenant_team_a.id, octopusdeploy_tenant.tenant_team_b.id]
  deploy_new_release_action {
    destination_environment_id = octopusdeploy_environment.env_1.id
  }
  cron_expression_schedule {
    cron_expression = "0 0 06 * * Mon-Fri"
  }
}

Runbook Schedule

resource "octopusdeploy_project_scheduled_trigger" "runbook" {
  name        = "Cron Runbook"
  description = "This is a Cron schedule"
  project_id  = octopusdeploy_project.non_tenanted.id
  space_id    = octopusdeploy_project.non_tenanted.space_id
  run_runbook_action {
    target_environment_ids = [octopusdeploy_environment.env_1.id]
    runbook_id             = octopusdeploy_runbook.non_tenanted_runbook.id
  }
  cron_expression_schedule {
    cron_expression = "0 0 06 * * Mon-Fri"
  }
}

@tleed5 tleed5 marked this pull request as ready for review March 26, 2024 03:17
description: |-
This resource manages External feed triggers (release creation type) in Octopus Deploy.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this removed unintentionally as a result of running the terraform-plugin-docs tool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i'm guessing so, ill have a look to see why it was removed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be because I didn't have the example listed in the examples folder with the import.sh thing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured it out, you have to specify the description in resource definition. Ill include this and the adding the example to the examples directory in this PR

	return &schema.Resource{
		CreateContext: resourceExternalFeedCreateReleaseTriggerCreate,
		DeleteContext: resourceExternalFeedCreateReleaseTriggerDelete,
		Description:   "This resource manages External feed triggers (release creation type) in Octopus Deploy.",
		Importer:      getImporter(),
		ReadContext:   resourceExternalFeedCreateReleaseTriggerRead,
		Schema:        getExternalFeedCreateReleaseTriggerSchema(),
		UpdateContext: resourceExternalFeedCreateReleaseTriggerUpdate,
	}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -80,3 +80,5 @@ Read-Only:

- `display_name` (String)
- `value` (String)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these newlines were also added automatically by the terraform-plugin-docs tool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Did it do the same thing for you when you updated the docs last time?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for me, but we might have used different versions of the tool 🤷

docs/resources/project_scheduled_trigger.md Show resolved Hide resolved
@@ -0,0 +1 @@
terraform import [options] octopusdeploy_project_scheduled_trigger.<name> <trigger-id>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh is this how the examples in the docs are supposed to be populated by the docs generator tool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup i've added one for the external feed so the doc generator won't overwrite the changes again

integration_test.go Outdated Show resolved Hide resolved
octopusdeploy/resource_project_scheduled_trigger.go Outdated Show resolved Hide resolved
flattenedProjectScheduledTrigger["tenant_ids"] = deployLatestReleaseAction.Tenants
flattenedProjectScheduledTrigger["deploy_latest_release_action"] = []map[string]interface{}{
{
"source_environment_id": deployLatestReleaseAction.SourceEnvironments[0],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it expected to only take the first source environment from the list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, for some reason the API contract has SourceEnvironments as an array but it can only have one element in it otherwise it fails validation. Even on the frontend you can only select one environment. So rather than duplicating that logic on the provider I opted to just having a string property

octopusdeploy/schema_project_scheduled_trigger.go Outdated Show resolved Hide resolved
Required: true,
Type: schema.TypeString,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace),
ForceNew: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do these ForceNew attributes mean for the schema's properties?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It causes the resource to be destroyed and recreated instead of updated when this property changes. So in this case if the project ID changes it will destroy it and recreate the trigger against the new project, without it will attempt to use the update endpoint which "succeeds" but the project doesn't actually change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, is this a known issue with the update API then?

Copy link
Collaborator

@kevjt kevjt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@tleed5 tleed5 merged commit 460fb08 into main Mar 27, 2024
37 checks passed
@tleed5 tleed5 deleted the tl/add-scheduled-project-trigger branch March 27, 2024 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for project triggers
2 participants