# Copyright 2024 Google Inc.
# 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.

---
name: 'Automation'
description: |
  An `Automation` enables the automation of manually driven actions for a Delivery Pipeline, which includes Release promotion amongst Targets, Rollout repair and Rollout deployment strategy advancement.
references:
  guides:
    'Automate your deployment': 'https://cloud.google.com/deploy/docs/automation'
  api: 'https://cloud.google.com/deploy/docs/api/reference/rest/v1/projects.locations.deliveryPipelines.automations'
docs:
id_format: 'projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}'
base_url: 'projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations'
self_link: 'projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}'
create_url: 'projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations?automationId={{name}}'
update_verb: 'PATCH'
update_mask: true
import_format:
  - 'projects/{{project}}/locations/{{location}}/deliveryPipelines/{{delivery_pipeline}}/automations/{{name}}'
timeouts:
  insert_minutes: 20
  update_minutes: 20
  delete_minutes: 20
autogen_async: true
async:
  actions: ['create', 'delete', 'update']
  type: 'OpAsync'
  operation:
    base_url: '{{op_id}}'
  result:
    resource_inside_response: false
custom_code:
examples:
  - name: 'clouddeploy_automation_basic'
    primary_resource_id: 'b-automation'
    vars:
      automation: 'cd-automation'
      delivery_pipeline: 'cd-pipeline'
    test_env_vars:
      service_account: 'SERVICE_ACCT'
  - name: 'clouddeploy_automation_full'
    primary_resource_id: 'f-automation'
    vars:
      automation: 'cd-automation'
      delivery_pipeline: 'cd-pipeline'
    test_env_vars:
      service_account: 'SERVICE_ACCT'
parameters:
  - name: 'location'
    type: String
    description: "The location for the resource"
    url_param_only: true
    required: true
    immutable: true
  - name: 'delivery_pipeline'
    type: String
    description: "The delivery_pipeline for the resource"
    url_param_only: true
    required: true
    immutable: true
properties:
  - name: 'name'
    type: String
    description: "Name of the `Automation`."
    url_param_only: true
    required: true
    immutable: true
  - name: 'uid'
    type: String
    description: "Output only. Unique identifier of the `Automation`."
    output: true
  - name: 'description'
    type: String
    description: "Optional. Description of the `Automation`. Max length is 255 characters."
  - name: 'createTime'
    type: String
    description: "Output only. Time at which the automation was created."
    output: true
  - name: 'updateTime'
    type: String
    description: "Output only. Time at which the automation was updated."
    output: true
  - name: 'annotations'
    type: KeyValueAnnotations
    description: "Optional. User annotations. These attributes can only be set and used by the user, and not by Cloud Deploy. Annotations must meet the following constraints: * Annotations are key/value pairs. * Valid annotation keys have two segments: an optional prefix and name, separated by a slash (`/`). * The name segment is required and must be 63 characters or less, beginning and ending with an alphanumeric character (`[a-z0-9A-Z]`) with dashes (`-`), underscores (`_`), dots (`.`), and alphanumerics between. * The prefix is optional. If specified, the prefix must be a DNS subdomain: a series of DNS labels separated by dots(`.`), not longer than 253 characters in total, followed by a slash (`/`). See https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set for more details."
  - name: 'labels'
    type: KeyValueLabels
    description: "Optional. Labels are attributes that can be set and used by both the user and by Cloud Deploy. Labels must meet the following constraints: * Keys and values can contain only lowercase letters, numeric characters, underscores, and dashes. * All characters must use UTF-8 encoding, and international characters are allowed. * Keys must start with a lowercase letter or international character. * Each resource is limited to a maximum of 64 labels. Both keys and values are additionally constrained to be <= 63 characters."
  - name: 'etag'
    type: String
    description: "Optional. The weak etag of the `Automation` resource. This checksum is computed by the server based on the value of other fields, and may be sent on update and delete requests to ensure the client has an up-to-date value before proceeding."
    output: true
  - name: 'suspended'
    type: Boolean
    description: "Optional. When Suspended, automation is deactivated from execution."
    send_empty_value: true
  - name: 'serviceAccount'
    type: String
    description: "Required. Email address of the user-managed IAM service account that creates Cloud Deploy release and rollout resources."
    required: true
  - name: 'selector'
    type: NestedObject
    description: "Required. Selected resources to which the automation will be applied."
    required: true
    properties:
      - name: 'targets'
        type: Array
        description: "Contains attributes about a target."
        required: true
        item_type:
          type: NestedObject
          properties:
            - name: 'id'
              type: String
              description: "ID of the `Target`. The value of this field could be one of the following: * The last segment of a target name. It only needs the ID to determine which target is being referred to * \"*\", all targets in a location."
            - name: 'labels'
              type: KeyValuePairs
              description: "Target labels."
              default_from_api: true
  - name: 'rules'
    type: Array
    description: "Required. List of Automation rules associated with the Automation resource. Must have at least one rule and limited to 250 rules per Delivery Pipeline. Note: the order of the rules here is not the same as the order of execution."
    required: true
    item_type:
      type: NestedObject
      properties:
        - name: 'promoteReleaseRule'
          type: NestedObject
          description: "Optional. `PromoteReleaseRule` will automatically promote a release from the current target to a specified target."
          exactly_one_of:
            - 'promoteReleaseRule'
            - 'advanceRolloutRule'
            - 'repairRolloutRule'
            - 'timedPromoteReleaseRule'
          properties:
            - name: 'id'
              type: String
              description: "Required. ID of the rule. This id must be unique in the `Automation` resource to which this rule belongs. The format is `a-z{0,62}`."
              required: true
            - name: 'wait'
              type: String
              description: "Optional. How long the release need to be paused until being promoted to the next target."
            - name: 'destinationTargetId'
              type: String
              description: "Optional. The ID of the stage in the pipeline to which this `Release` is deploying. If unspecified, default it to the next stage in the promotion flow. The value of this field could be one of the following: * The last segment of a target name. It only needs the ID to determine if the target is one of the stages in the promotion sequence defined in the pipeline. * \"@next\", the next target in the promotion sequence."
            - name: 'destinationPhase'
              type: String
              description: "Optional. The starting phase of the rollout created by this operation. Default to the first phase."
        - name: 'advanceRolloutRule'
          type: NestedObject
          description: "Optional. The `AdvanceRolloutRule` will automatically advance a successful Rollout."
          exactly_one_of:
            - 'promoteReleaseRule'
            - 'advanceRolloutRule'
            - 'repairRolloutRule'
            - 'timedPromoteReleaseRule'
          properties:
            - name: 'id'
              type: String
              description: "Required. ID of the rule. This id must be unique in the `Automation` resource to which this rule belongs. The format is `a-z{0,62}`."
              required: true
            - name: 'wait'
              type: String
              description: "Optional. How long to wait after a rollout is finished."
            - name: 'sourcePhases'
              type: Array
              description: "Optional. Proceeds only after phase name matched any one in the list. This value must consist of lower-case letters, numbers, and hyphens, start with a letter and end with a letter or a number, and have a max length of 63 characters. In other words, it must match the following regex: `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$`."
              item_type:
                type: String
        - name: 'repairRolloutRule'
          type: NestedObject
          description: "Optional. The RepairRolloutRule will automatically repair a failed rollout."
          exactly_one_of:
            - 'promoteReleaseRule'
            - 'advanceRolloutRule'
            - 'repairRolloutRule'
            - 'timedPromoteReleaseRule'
          properties:
            - name: 'id'
              type: String
              description: "Required. ID of the rule. This id must be unique in the `Automation` resource to which this rule belongs. The format is `a-z{0,62}`."
              required: true
            - name: 'phases'
              type: Array
              description: "Optional. Phases within which jobs are subject to automatic repair actions on failure. Proceeds only after phase name matched any one in the list, or for all phases if unspecified. This value must consist of lower-case letters, numbers, and hyphens, start with a letter and end with a letter or a number, and have a max length of 63 characters. In other words, it must match the following regex: ^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$."
              item_type:
                type: String
            - name: 'jobs'
              type: Array
              description: "Optional. Jobs to repair. Proceeds only after job name matched any one in the list, or for all jobs if unspecified or empty. The phase that includes the job must match the phase ID specified in sourcePhase. This value must consist of lower-case letters, numbers, and hyphens, start with a letter and end with a letter or a number, and have a max length of 63 characters. In other words, it must match the following regex: ^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$."
              item_type:
                type: String
            - name: 'repairPhases'
              type: Array
              description: "Optional. Proceeds only after phase name matched any one in the list. This value must consist of lower-case letters, numbers, and hyphens, start with a letter and end with a letter or a number, and have a max length of 63 characters. In other words, it must match the following regex: `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$`."
              item_type:
                type: NestedObject
                properties:
                  - name: 'retry'
                    description: "Optional. Retries a failed job."
                    exactly_one_of:
                      - 'retry'
                      - 'rollback'
                    type: NestedObject
                    properties:
                      - name: 'attempts'
                        type: String
                        description: "Required. Total number of retries. Retry is skipped if set to 0; The minimum value is 1, and the maximum value is 10."
                        required: true
                      - name: 'wait'
                        type: String
                        description: "Optional. How long to wait for the first retry. Default is 0, and the maximum value is 14d. A duration in seconds with up to nine fractional digits, ending with 's'. Example: `3.5s`."
                      - name: 'backoffMode'
                        type: Enum
                        description: "Optional. The pattern of how wait time will be increased. Default is linear. Backoff mode will be ignored if wait is 0."
                        enum_values:
                          - 'BACKOFF_MODE_UNSPECIFIED'
                          - 'BACKOFF_MODE_LINEAR'
                          - 'BACKOFF_MODE_EXPONENTIAL'
                  - name: 'rollback'
                    description: "Optional. Rolls back a Rollout."
                    exactly_one_of:
                      - 'retry'
                      - 'rollback'
                    type: NestedObject
                    send_empty_value: true
                    allow_empty_object: true
                    properties:
                      - name: 'destinationPhase'
                        type: String
                        description: "Optional. The starting phase ID for the Rollout. If unspecified, the Rollout will start in the stable phase."
                      - name: 'disableRollbackIfRolloutPending'
                        type: Boolean
                        send_empty_value: true
                        description: "Optional. If pending rollout exists on the target, the rollback operation will be aborted."
        - name: 'timedPromoteReleaseRule'
          type: NestedObject
          description: "Optional. The `TimedPromoteReleaseRule` will automatically promote a release from the current target(s) to the specified target(s) on a configured schedule."
          exactly_one_of:
            - 'promoteReleaseRule'
            - 'advanceRolloutRule'
            - 'repairRolloutRule'
            - 'timedPromoteReleaseRule'
          properties:
            - name: 'id'
              type: String
              description: "Required. ID of the rule. This id must be unique in the `Automation` resource to which this rule belongs. The format is `a-z{0,62}`."
              required: true
            - name: 'destinationTargetId'
              type: String
              description: |-
                Optional. The ID of the stage in the pipeline to which this Release is deploying. If unspecified, default it to the next stage in the promotion flow. The value of this field could be one of the following:
                  - The last segment of a target name
                  - "@next", the next target in the promotion sequence"
            - name: 'schedule'
              type: String
              description: "Required. Schedule in crontab format. e.g. `0 9 * * 1` for every Monday at 9am."
              required: true
            - name: 'timeZone'
              type: String
              description: "Required. The time zone in IANA format IANA Time Zone Database (e.g. America/New_York)."
              required: true
            - name: 'destinationPhase'
              type: String
              description: "Optional. The starting phase of the rollout created by this rule. Default to the first phase."
