|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +data "google_client_config" "current" { |
| 18 | +} |
| 19 | + |
| 20 | +locals { |
| 21 | + is_active = var.input_workflow_state == null || var.input_workflow_state == "FAILED" ? "no" : "yes" |
| 22 | +} |
| 23 | + |
| 24 | +## Trigger the execution of the setup workflow with an API call |
| 25 | +data "http" "call_workflows_setup" { |
| 26 | + url = "https://workflowexecutions.googleapis.com/v1/${var.workflow_id}/executions" |
| 27 | + method = local.is_active == "no" ? "POST" : "GET" |
| 28 | + # method = "POST" |
| 29 | + request_headers = { |
| 30 | + Accept = "application/json" |
| 31 | + Authorization = "Bearer ${data.google_client_config.current.access_token}" } |
| 32 | +} |
| 33 | + |
| 34 | +resource "time_sleep" "workflow_execution_wait" { |
| 35 | + create_duration = "120s" |
| 36 | + depends_on = [ |
| 37 | + data.http.call_workflows_setup |
| 38 | + ] |
| 39 | +} |
| 40 | + |
| 41 | +# locals { |
| 42 | +# is_active = var.input_workflow_state == null || var.input_workflow_state == "FAILED" ? "no" : "yes" |
| 43 | +# } |
| 44 | + |
| 45 | +# ## Trigger the execution of the setup workflow with an API call |
| 46 | +# data "http" "call_workflows_setup" { |
| 47 | +# url = "https://workflowexecutions.googleapis.com/v1/${var.workflow_id}/executions" |
| 48 | +# method = local.is_active == "no" ? "POST" : "GET" |
| 49 | +# request_headers = { |
| 50 | +# Accept = "application/json" |
| 51 | +# Authorization = "Bearer ${data.google_client_config.current.access_token}" } |
| 52 | +# } |
| 53 | + |
| 54 | +# ## If the workflow last failed, sleep for 30 seconds before checking the workflow execution status. |
| 55 | +# ## If last execution did not fail, exit as quickly as possible (1 second) |
| 56 | +# resource "time_sleep" "workflow_execution_wait" { |
| 57 | +# create_duration = local.is_active == "no" ? "30s" : "1s" |
| 58 | +# depends_on = [ |
| 59 | +# data.http.call_workflows_setup, |
| 60 | +# ] |
| 61 | +# } |
| 62 | + |
| 63 | +## Check the state of the setup workflow execution with an API call |
| 64 | +data "http" "call_workflows_state" { |
| 65 | + url = "https://workflowexecutions.googleapis.com/v1/${var.workflow_id}/executions" |
| 66 | + method = "GET" |
| 67 | + request_headers = { |
| 68 | + Accept = "application/json" |
| 69 | + Authorization = "Bearer ${data.google_client_config.current.access_token}" } |
| 70 | + depends_on = [ |
| 71 | + data.http.call_workflows_setup, |
| 72 | + time_sleep.workflow_execution_wait |
| 73 | + ] |
| 74 | +} |
| 75 | + |
| 76 | +## Parse out the workflow execution state from the API call response |
| 77 | +locals { |
| 78 | + response_body = jsondecode(data.http.call_workflows_state.response_body) |
| 79 | + workflow_state = local.response_body.executions[0].state |
| 80 | +} |
| 81 | + |
| 82 | +## Output the workflow state to use as input for subsequent invocations |
| 83 | +output "workflow_state" { |
| 84 | + description = "State of the most recent workflow execution. Used to determine how to proceed with next polling run." |
| 85 | + value = local.workflow_state |
| 86 | +} |
| 87 | + |
| 88 | +# ## If workflow execution is actively running, sleep for 90 seconds to allow it to finish |
| 89 | +# ## If not, exit as quickly as possible (1 second) |
| 90 | +# resource "time_sleep" "complete_workflow" { |
| 91 | +# create_duration = local.workflow_state == "ACTIVE" ? "90s" : "1s" |
| 92 | +# depends_on = [ |
| 93 | +# data.http.call_workflows_setup, |
| 94 | +# time_sleep.workflow_execution_wait, |
| 95 | +# data.http.call_workflows_state, |
| 96 | +# local.workflow_state |
| 97 | +# ] |
| 98 | +# } |
0 commit comments