# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

main:
  params: [event]
  steps:
    - init:
        assign:
          - project_id: ${sys.get_env("GOOGLE_CLOUD_PROJECT")}
          - event_bucket: ${event.data.bucket}
          - event_file: ${event.data.name}
          - target_bucket: ${sys.get_env("EZTF_CONFIG_BUCKET")}
          - job_name: ezytf-job
          - job_location: ${sys.get_env("EZTF_LOCATION")}
    - check_input_file:
        switch:
          - condition: ${event_bucket == target_bucket}
            next: run_job
          - condition: true
            next: end
    - run_job:
        call: googleapis.run.v1.namespaces.jobs.run
        args:
          name: ${"namespaces/" + project_id + "/jobs/" + job_name}
          location: ${job_location}
          body:
            overrides:
              containerOverrides:
                env:
                  - name: EZTF_CONFIG_BUCKET
                    value: ${event_bucket}
                  - name: EZTF_INPUT_CONFIG
                    value: ${event_file}
        result: job_execution
    - finish:
        return: ${job_execution}