Mengekspos Informasi Menggunakan Output

Saat membuat deployment, Anda mungkin ingin mengekspos properti utama konfigurasi atau template agar dapat digunakan oleh template atau pengguna lain. Misalnya, Anda mungkin ingin mengekspos alamat IP database yang dibuat dalam template sehingga pengguna dapat dengan mudah mereferensikan IP saat mengonfigurasi template mereka sendiri.

Anda dapat menggunakan bagian outputs dalam template atau konfigurasi untuk menentukan daftar pasangan nilai/kunci yang dapat dipanggil pengguna. Di bagian output, Anda menentukan kunci arbitrer dan menetapkan nilai kunci ke referensi, properti template, atau variabel lingkungan. Pengguna dapat menggunakan output untuk mengakses informasi penting tentang resource yang dibuat oleh template. Misalnya, Anda dapat mendeklarasikan output bernama databaseIP yang mereferensikan alamat IP instance yang menghosting database dan pengguna dapat mereferensikan output tersebut dalam template lain di deployment yang sama.

Sebelum memulai

Contoh

Berikut adalah contoh template dengan output:

mongodb.jinja
{% set MASTER = env["name"] + "-" + env["deployment"] + "-mongodb" %}
resources:
- name: {{ MASTER }}
  type: instance
  ...
outputs:
- name: databaseIp
  value: $(ref.{{ MASTER }}.network[0].ip)  # Treated as a string during expansion
- name: databasePort
  value: 88

Bagian output mendeklarasikan dua properti: databaseIp dan databasePort. databaseIp menggunakan referensi yang di-resolve ke alamat IP jaringan resource master, sedangkan databasePort adalah nilai statis. Dalam template lain, Anda dapat mengimpor mongodb.jinja, menggunakan template sebagai jenis, dan memanggil output. Contoh:

imports:
- path: example/path/to/mongodb.jinja
  name: mongodb.jinja

resources:
- name: my_mongo
  type: mongodb.jinja
  properties:
    size: 100

- name: my_instance
  type: compute.v1.instance
  properties:
    
    databaseIp: $(ref.my_mongo.databaseIp)
    databasePort: $(ref.my_mongo.databasePort)

Mendeklarasikan output

Nyatakan output dalam template atau konfigurasi dengan menentukan bagian outputs: di tingkat yang sama dengan bagian resources:. Kunci output harus unik dalam template atau konfigurasi.

Misalnya, contoh bagian outputs: mungkin terlihat seperti ini:

...
outputs:
- name: databaseIp
  value: $(ref.my-first-vm.networkInterfaces[0].accessConfigs[0].natIP)
- name: machineType
  value: {{ properties['machineType'] }}
- name: databasePort
  value: 88

Berikut tampilan output dalam template lengkap:

{#
Copyright 2016 Google Inc. All rights reserved.
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.
#}

resources:
- name: my-first-vm
  type: compute.v1.instance
  properties:
    zone: us-central1-a
    machineType: zones/us-central1-a/machineTypes/{{ properties['machineType'] }}
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    - network: global/networks/default
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT

# Declare outputs here
outputs:
- name: databaseIp
  value: $(ref.my-first-vm.networkInterfaces[0].accessConfigs[0].natIP)
- name: machineType
  value: {{ properties['machineType'] }}
- name: databasePort
  value: 88


Nilai output dapat berupa:

Menggunakan output dari template

Untuk menggunakan output yang telah ditentukan dalam template, impor dan gunakan template yang berisi output sebagai jenis. Misalnya, untuk menggunakan output yang ditentukan dalam template bernama template_with_outputs.jinja, output tersebut harus diimpor dan digunakan untuk membuat resource:

# Copyright 2016 Google Inc. All rights reserved.
#
# 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.
imports:
- path: template_with_outputs.jinja
  name: template.jinja

resources:
- name: my-first-vm
  type: template.jinja
  properties:
    machineType: n1-standard-1

outputs:
- name: databaseIp
  value: $(ref.my-first-vm.databaseIp)
- name: machineType
  value: $(ref.my-first-vm.machineType)
- name: databasePort
  value: $(ref.my-first-vm.databasePort)


Untuk memanggil output, gunakan format berikut:

$(ref.RESOURCE.OUTPUT)
  • RESOURCE adalah nama resource yang dibuat oleh template. Dalam contoh di atas, ini adalah my-first-vm.

  • OUTPUT adalah output yang dideklarasikan dalam template. Dalam contoh di atas, ini adalah databaseIp dan databasePort. Sintaksis ini sama dengan yang Anda gunakan untuk mendeklarasikan referensi. Anda juga dapat mereferensikan item daftar, misalnya: $ref.template.property[0].

Saat Anda men-deploy konfigurasi, Deployment Manager akan memperluas konfigurasi, lalu mengganti referensi ke output dengan nilai output.

Mendeskripsikan output dalam skema

Untuk template yang memiliki skema pendamping, Anda dapat menjelaskan properti output dengan lebih mendetail. Deployment Manager tidak menerapkan atau memvalidasi informasi apa pun di bagian output, tetapi bagian ini berpotensi berguna untuk memberikan informasi selengkapnya tentang output yang relevan, untuk kepentingan pengguna yang menggunakan template Anda.

Dalam file skema, berikan bagian output yang cocok dengan output dalam template Anda. Contoh:

...
outputs:
  databaseIp:
    description: Reference to ip address of your new cluster
    type: string
  databasePort:
    description: Port to talk on
    type: integer

Pengguna dapat merujuk ke file skema Anda untuk memahami penggunaan dan jenis output Anda.

Mencari nilai output akhir

Setelah men-deploy template yang menggunakan output, lihat nilai output akhir dengan melihat tata letak konfigurasi deployment. Nilai output akhir ditunjukkan oleh properti finalValue. Semua nilai output disertakan dalam kolom ini, termasuk nilai output dari template bertingkat. Contoh:

layout: |
  resources:
  - name: vm_template
    outputs:
    - finalValue: 104.197.69.69
      name: databaseIp
      value: $(ref.vm-test.networkInterfaces[0].accessConfigs[0].natIP)
    properties:
      zone: us-central1-a
    resources:
    - name: datadisk-example-instance
      type: compute.v1.disk
    - name: vm-test
      type: compute.v1.instance
    type: vm_template.jinja
name: manifest-1455057116997

Menghindari dependensi sirkular

Berhati-hatilah saat membuat template yang memiliki dua atau beberapa resource yang saling bergantung pada output masing-masing. Deployment Manager tidak mencegah struktur ini, tetapi jika output menyebabkan dependensi melingkar, deployment tidak akan berhasil di-deploy. Misalnya, cuplikan berikut diterima oleh Deployment Manager, tetapi jika konten template menyebabkan dependensi melingkar, deployment akan gagal:

resources:
- name: frontend
  type: frontend.jinja
  properties:
    ip: $(ref.backend.ip)
- name: backend
  type: backend.jinja
  properties:
    ip: $(ref.frontend.ip)

Sebagai contoh dependensi sirkular yang menyebabkan deployment gagal, asumsikan frontend.jinja dan backend.jinja terlihat seperti ini:

resources:
- name: {{ env['name'] }}
  type: compute.v1.instance
  properties:
    zone: us-central1-f
    ...
    networkInterfaces:
    - network: global/networks/default
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
    metadata:
      items:
      - key: startup-script
        value: |
          #!/bin/bash
          export IP={{ properties["ip"] }}
      ...

outputs:
- name: ip
  value: $(ref.{{ env['name'] }}.networkInterfaces[0].accessConfigs[0].natIP)

Ingatlah bahwa kedua resource menggunakan properti output IP dari resource yang berlawanan:

resources:
- name: frontend
  type: frontend.jinja
  properties:
    ip: $(ref.backend.ip)
- name: backend
  type: backend.jinja
  properties:
    ip: $(ref.frontend.ip)

Namun, nilai IP tidak dapat diisi karena kedua properti bergantung pada keberadaan resource lain, sehingga menciptakan dependensi melingkar. Berikut adalah template yang sama, yang diperluas sepenuhnya:

resources:
- name: frontend
  type: compute.v1.instance
  properties:
    zone: us-central1-f
    ...
    networkInterfaces:
    - network: global/networks/default
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
    metadata:
      items:
      - key: startup-script
        value: |
          #!/bin/bash
          export IP=$(ref.backend.networkInterfaces[0].accessConfigs[0].natIP)
- name: backend
  type: compute.v1.instance
  properties:
    zone: us-central1-f
    ...
    networkInterfaces:
    - network: global/networks/default
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
    metadata:
      items:
      - key: startup-script
        value: |
          #!/bin/bash
          export IP=$(ref.frontend.networkInterfaces[0].accessConfigs[0].natIP)

Deployment Manager akan menampilkan error jika Anda mencoba menjalankan konfigurasi:

 code: u'CONDITION_NOT_MET'
 message: u'A dependency cycle was found amongst backend, frontend.'>]>

Namun, template ini akan berfungsi jika:

  1. frontend.jinja membuat dua instance virtual machine, vm-1 dan vm-2.
  2. backend.jinja membuat vm-3 dan vm-4.
  3. vm-1 mengekspos IP eksternalnya sebagai output dan vm-4 menggunakan output tersebut.
  4. vm-3 mengekspos IP eksternal sebagai output, vm-2 menggunakan output tersebut.

Langkah berikutnya