Tasks

The /tasks endpoints interact with the tasks that Consul-Terraform-Sync (CTS) is responsible for running.

If you run CTS with high availability enabled, you can send requests to the /tasks endpoint on CTS leader or follower instances. Requests to a follower instance, however, return a 400 Bad Request and error message. The error message depends on what information the follower instance is able to obtain about the leader. Refer to Error Messages for more information.

Get Tasks

This endpoint returns information about all existing tasks.

MethodPathProduces
GET/tasksapplication/json

Response Statuses

StatusReason
200Successfully retrieved and returned a list of tasks

Response Fields

NameTypeDescription
request_idstringThe ID of the request. Used for auditing and debugging purposes.
taskslist[Task]A list of Tasks

Example: Retrieve all tasks

In this example, CTS contains a single task

Request:

  1. $ curl --request GET \
  2. localhost:8558/v1/tasks

Response:

  1. {
  2. "request_id": "0e0290f9-94df-3a4a-fd17-72467a16083c",
  3. "tasks": [
  4. {
  5. "buffer_period": {
  6. "enabled": true,
  7. "max": "25s",
  8. "min": "5s"
  9. },
  10. "condition": {
  11. "services": {
  12. "cts_user_defined_meta": {},
  13. "datacenter": "",
  14. "filter": "",
  15. "names": ["api", "web"],
  16. "namespace": "",
  17. "use_as_module_input": true
  18. }
  19. },
  20. "description": "Writes the service name, id, and IP address to a file",
  21. "enabled": true,
  22. "module": "path/to/module",
  23. "module_input": {},
  24. "name": "task_a",
  25. "providers": ["my-provider"],
  26. "variables": {},
  27. "version": ""
  28. }
  29. ]
  30. }

Get Task

This endpoint returns information about an existing task.

MethodPathProduces
GET/tasks/:task_nameapplication/json

Request Parameters

NameRequiredTypeDescriptionDefault
:task_nameRequiredstringSpecifies the name of the task to retrievenone

Response Statuses

StatusReason
200Successfully retrieved and returned task information
404Task with the given name not found

Response Fields

NameTypeDescription
request_idstringThe ID of the request. Used for auditing and debugging purposes.
taskobjectThe task’s configuration information. See task object for details.

Example: Retrieve a task

Request:

  1. $ curl --request GET \
  2. localhost:8558/v1/tasks/task_a

Response:

  1. {
  2. "request_id": "b7559ab0-5111-381b-367a-0dfb7e216d41",
  3. "task": {
  4. "buffer_period": {
  5. "enabled": true,
  6. "max": "20s",
  7. "min": "5s"
  8. },
  9. "condition": {
  10. "consul_kv": {
  11. "datacenter": "",
  12. "namespace": "",
  13. "path": "my_key",
  14. "recurse": false,
  15. "use_as_module_input": true
  16. }
  17. },
  18. "description": "task triggering on consul-kv",
  19. "enabled": true,
  20. "module": "path/to/module",
  21. "module_input": {
  22. "services": {
  23. "cts_user_defined_meta": {},
  24. "datacenter": "",
  25. "filter": "",
  26. "names": ["api"],
  27. "namespace": ""
  28. }
  29. },
  30. "name": "task_a",
  31. "providers": [],
  32. "variables": {},
  33. "version": ""
  34. }
  35. }

Create Task

This endpoint allows for creation of a task. It only supports creation of a new task, and does not support updating a task.

MethodPathProduces
POST/tasksapplication/json

Request Parameters

NameRequiredTypeDescriptionDefault
runOptionalstringValues can only be “inspect” and “now”.
  • “inspect”: Does not create the task but returns information on if/how resources would be changed for the proposed task creation.
  • “now”: Creates the task accordingly and immediately runs the task, rather than allowing the task to run at the natural daemon cadence
none

Request Body

NameRequiredTypeDescription
taskRequiredobjectThe task’s configuration information. See task object for details.

Response Statuses

StatusReason
201Successfully created the task

Response Fields

NameTypeDescription
request_idstringThe ID of the request. Used for auditing and debugging purposes.
taskobjectThe task’s configuration information after creation. See task object for details.

Example: Create a task

Payload:

  1. {
  2. "task": {
  3. "description": "Writes the service name, id, and IP address to a file",
  4. "enabled": true,
  5. "name": "task_a",
  6. "providers": ["my-provider"],
  7. "condition": {
  8. "services": {
  9. "names": ["web", "api"]
  10. }
  11. },
  12. "module": "path/to/module"
  13. }
  14. }

Request:

  1. $ curl --header "Content-Type: application/json" \
  2. --request POST \
  3. --data @payload.json \
  4. localhost:8558/v1/tasks

Response:

  1. {
  2. "request_id": "0428ed18-1359-874c-8b27-742e43ebd7e7",
  3. "task": {
  4. "buffer_period": {
  5. "enabled": true,
  6. "max": "25s",
  7. "min": "5s"
  8. },
  9. "condition": {
  10. "services": {
  11. "cts_user_defined_meta": {},
  12. "datacenter": "",
  13. "filter": "",
  14. "names": ["api", "web"],
  15. "namespace": "",
  16. "use_as_module_input": true
  17. }
  18. },
  19. "description": "Writes the service name, id, and IP address to a file",
  20. "enabled": true,
  21. "module": "path/to/module",
  22. "module_input": {},
  23. "name": "task_a",
  24. "providers": ["my-provider"],
  25. "variables": {},
  26. "version": ""
  27. }
  28. }

Update Task

This endpoint allows patch updates to specifically supported fields for existing tasks. Currently only supports updating a task’s enabled value.

MethodPathProduces
PATCH/tasks/:task_nameapplication/json

Request Parameters

NameRequiredTypeDescriptionDefault
:task_nameRequiredstringSpecifies the name of the task to updatenone
runOptionalstringValues can only be “inspect” and “now”.
  • “inspect”: Does not update the task but returns information on if/how resources would be changed for the proposed task update.
  • “now”: Updates the task accordingly and immediately runs the task, rather than allowing the task to run at the natural daemon cadence
none

Request Body

NameRequiredTypeDescription
enabledRequiredbooleanWhether the task is enabled to run and manage resources.

Response Statuses

StatusReason
200Successfully updated the task
404Task with the given name not found

Response Fields

NameTypeDescription
inspectobjectInformation on how resources would be changed given a proposed task update, similar to inspect-mode. This is only returned when passed the run=inspect request parameter.
inspect.changes_presentbooleanWhether or not changes were detected for running the proposed task update.
inspect.planstringThe Terraform plan generated for the proposed task update. Note: a non-empty string does not necessarily indicate changes were detected.

Example: Disable a task

Request:

  1. $ curl --header "Content-Type: application/json" \
  2. --request PATCH \
  3. --data '{"enabled":false}' \
  4. localhost:8558/v1/tasks/task_a

Response:

  1. {}

Example: Inspect enabling a task

Request:

  1. $ curl --header "Content-Type: application/json" \
  2. --request PATCH \
  3. --data '{"enabled":true}' \
  4. localhost:8558/v1/tasks/task_a?run=inspect

Response if no changes present:

  1. {
  2. "inspect": {
  3. "changes_present": false,
  4. "plan": "No changes. Infrastructure is up-to-date. <truncated>"
  5. }
  6. }

Response if changes present:

  1. {
  2. "inspect": {
  3. "changes_present": true,
  4. "plan": "<terraform plan truncated> Plan: 3 to add, 0 to change, 0 to destroy."
  5. }
  6. }

Delete Task

This endpoint allows for deletion of existing tasks. It marks a task for deletion based on the name provided. If the task is not running, it will be deleted immediately. Otherwise, it will be deleted once the task is completed.

Note: Deleting a task will not destroy the infrastructure managed by the task.

MethodPathProduces
DELETE/tasks/:task_nameapplication/json

Request Parameters

NameRequiredTypeDescriptionDefault
:task_nameRequiredstringSpecifies the name of the task to retrievenone

Response Statuses

StatusReason
202Successfully marked the task for deletion
404Task with the given name not found

Response Fields

NameTypeDescription
request_idstringThe ID of the request. Used for auditing and debugging purposes.

Sample Request

  1. $ curl --request DELETE \
  2. localhost:8558/v1/tasks/task_a

Sample Response

  1. {
  2. "request_id": "9b23eea7-a435-2797-c71e-10c15766cd73"
  3. }

Task Object

The task object is used by the Task APIs as part of a request or response. It represents the task’s configuration information.

NameRequiredTypeDescriptionDefault
buffer_periodobjectOptionalThe buffer period for triggering task execution.The global buffer period configured for CTS.
buffer_period.enabledboolOptionalWhether the buffer period is enabled or disabled.The global buffer period’s enabled value configured for CTS.
buffer_period.minstringOptionalThe minimum period of time to wait after changes are detected before triggering the task.The global buffer period’s min value configured for CTS.
buffer_period.maxstringOptionalThe maximum period of time to wait after changes are detected before triggering the task.The global buffer period’s max value configured for CTS.
conditionobjectRequiredThe condition on which to trigger the task to execute.

If the task has the deprecated services field configured as a module input, it is represented here as condition.services.
none
descriptionstringOptionalThe human readable text to describe the task.none
enabledboolOptionalWhether the task is enabled or disabled from executing.true
modulestringRequiredThe location of the Terraform module.none
module_inputobjectOptionalThe additional module input(s) that the tasks provides to the Terraform module on execution.

If the task has the deprecated services field configured as a module input, it is represented here as module_input.services.
none
namestringRequiredThe unique name of the task.none
providerslist[string]OptionalThe list of provider names that the task’s module uses.none
variablesmap[string]OptionalThe map of variables that are provided to the task’s module.none
versionstringOptionalThe version of the configured module that the task uses.The latest version.
terraform_versionstringOptionalEnterprise Deprecated in CTS 0.6.0 and will be removed in 0.8.0. Review terraform_version in terraform_cloud_workspace instead. The version of Terraform to use for the Terraform Cloud workspace associated with the task. This is only available when used with the Terraform Cloud driver.The latest compatible version supported by the organization.
terraform_cloud_workspaceobjectOptionalEnterprise The configurable attributes of the Terraform Cloud workspace associated with the task. This option is only available when used with the Terraform Cloud driver.none