#
# Copyright 2020 Google LLC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#

substitutions:
  _SERVERLESS_REGION: '__SERVERLESS_REGION__'
  _PROJECTS_DN: '__PROJECTS_DN__'
  _AD_DOMAIN: '__AD_DOMAIN__'
  _AD_NETBIOS_DOMAIN: '__AD_NETBIOS_DOMAIN__'
  _SERVICE_ACCOUNT_EMAIL: '__SERVICE_ACCOUNT_EMAIL__'
  _SERVERLESS_NETWORK: '__SERVERLESS_NETWORK__'
  _SERVERLESS_SUBNET: '__SERVERLESS_SUBNET__'

  _IMAGE_TAG: 'latest'
  _SECRET_NAME: 'ad-password'
  _SECRET_VERSION: 'latest'

steps:
# Build Docker image containing the Cloud Run app
- name: 'gcr.io/cloud-builders/docker'
  args: 
  - build
  - -t
  - gcr.io/$PROJECT_ID/register-computer:$_IMAGE_TAG
  - .

# Push Docker image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
  args: 
  - push
  - gcr.io/$PROJECT_ID/register-computer:$_IMAGE_TAG
  
# Deploy to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args: 
  - run
  - deploy
  - register-computer
  - --image=gcr.io/$PROJECT_ID/register-computer:$_IMAGE_TAG
  - --project=$PROJECT_ID 
  - --region=$_SERVERLESS_REGION
  - --platform=managed
  - --allow-unauthenticated
  - --network=$_SERVERLESS_NETWORK
  - --subnet=$_SERVERLESS_SUBNET
  - --vpc-egress=private-ranges-only
  - --service-account=$_SERVICE_ACCOUNT_EMAIL
  - --set-env-vars=AD_DOMAIN=$_AD_DOMAIN 
  - --set-env-vars=AD_USERNAME=$_AD_NETBIOS_DOMAIN\register-computer 
  - --set-env-vars=SM_PROJECT=$PROJECT_ID 
  - --set-env-vars=SM_NAME_ADPASSWORD=$_SECRET_NAME 
  - --set-env-vars=SM_VERSION_ADPASSWORD=$_SECRET_VERSION 
  - --set-env-vars=FUNCTION_IDENTITY=$_SERVICE_ACCOUNT_EMAIL
  - --set-env-vars=^:^PROJECTS_DN=$_PROJECTS_DN

images: ['gcr.io/$PROJECT_ID/register-computer:$_IMAGE_TAG']
