# Copyright 2022 The Kubeflow Authors
#
# 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: tensorboard_experiment_creator
description: |
  Create a TensorboardExperiment for Pipeline usage.

  Args:
      tensorboad_resource_name (str):
          Required. Resource name used to retrieve tensorboard instances. Format example: projects/{project_number}/locations/{location}/tensorboards/{tensorboard_id}
      tensorboard_experiment_id (Optional[str]):
          The tensorboard experiment id.
          If not set, default to task name.
      tensorboard_experiment_display_name (Optional[str]):
          The display name of the tensorboard experiment. If not set, default to None.
      tensorboard_experiment_description (Optional[str]):
          The description of the tensorboard experiment. If not set, default to None.
      tensorboard_experiment_labels (Optional[dict]):
          The labels of the tensorboard experiment. If not set, default to None.
  Returns:
      gcp_resources (str):
          Gcp_resource with Full resource name of the TensorboardExperiment as uri. Format example: projects/{project_number}/locations/{location}/tensorboards/{tensorboard_id}/experiments/{experiment}

inputs:
  - { name: tensorboard_resource_name, type: String }
  - {
      name: tensorboard_experiment_id,
      type: String,
      default: "{{$.pipeline_task_name}}",
    }
  - { name: tensorboard_experiment_display_name, type: String, optional: true }
  - { name: tensorboard_experiment_description, type: String, optional: true }
  - { name: tensorboard_experiment_labels, type: Dict, optional: true }
#TODO(b/244597396)
outputs:
  - { name: gcp_resources, type: String }
implementation:
  container:
    image: gcr.io/ml-pipeline/google-cloud-pipeline-components:2.0.0b1
    command:
      [
        python3,
        -u,
        -m,
        google_cloud_pipeline_components.container.experimental.tensorboard.tensorboard_experiment_creator,
      ]
    args:
      - --task
      - "tensorboard_experiment_creator"
      - --display_name
      - "tensorboard_experiment_creator-run"
      - --tensorboard_resource_name
      - { inputValue: tensorboard_resource_name }
      - --tensorboard_experiment_id
      - { inputValue: tensorboard_experiment_id }
      - if:
          cond: { isPresent: tensorboard_experiment_display_name }
          then:
            - --tensorboard_experiment_display_name
            - { inputValue: tensorboard_experiment_display_name }
      - if:
          cond: { isPresent: tensorboard_experiment_description }
          then:
            - --tensorboard_experiment_description
            - { inputValue: tensorboard_experiment_description }
      - if:
          cond: { isPresent: tensorboard_experiment_labels }
          then:
            - --tensorboard_experiment_labels
            - { inputValue: tensorboard_experiment_labels }
      - --gcp_resources
      - { outputPath: gcp_resources }
