Utilizzo di più modelli

In questo passaggio, esplorerai un modello che importa altri modelli.

Dopo aver incorporato questi modelli, la configurazione deve chiamare un solo modello per creare un deployment con tutte queste risorse.

Apertura del modello per una rete

Apri il modello denominato compute-engine-template.py:

cd deploymentmanager-samples/examples/v2/step_by_step_guide/step6_use_multiple_templates/python

nano compute-engine-template.py  # use your preferred text editor

Questo modello importa altri modelli per tutte le risorse nel deployment: vm-template.py, vm-template-2.py per le macchine virtuali (VM), network-template.py per una rete e firewall-template.py per una regola firewall.

# 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.

"""Creates the Compute Engine."""

def GenerateConfig(context):
  """Creates the Compute Engine with network and firewall."""

  resources = [{
      'name': 'vm-1',
      'type': 'vm-template.py'
  }, {
      'name': 'vm-2',
      'type': 'vm-template-2.py'
  }, {
      'name': 'network-1',
      'type': 'network-template.py'
  }, {
      'name': 'firewall-1',
      'type': 'firewall-template.py'
  }]
  return {'resources': resources}

Visualizzazione della configurazione

Apri il file di configurazione per il deployment:

cd deploymentmanager-samples/examples/v2/step_by_step_guide/step6_use_multiple_templates/python

nano config-with-many-templates.yaml  # use your preferred text editor

Nota che la configurazione non ha chiamato direttamente gli altri modelli. Tuttavia, gli altri modelli vengono importati perché compute-engine-template.py dipende dagli altri modelli.

# 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: vm-template.py
- path: vm-template-2.py
- path: network-template.py
- path: firewall-template.py
- path: compute-engine-template.py

resources:
- name: compute-engine-setup
  type: compute-engine-template.py

È in corso il salvataggio della configurazione e il relativo deployment

Esegui questo comando per eseguire il deployment della configurazione:

gcloud deployment-manager deployments create deployment-with-many-templates \
  --config config-with-many-templates.yaml

Per visualizzare il deployment, esegui:

gcloud deployment-manager deployments describe deployment-with-many-templates

Prospettive future: proprietà del modello e variabili di ambiente

Poi, sostituirai alcune parti hardcoded dei modelli con pattern riutilizzabili, come le variabili di ambiente e i modelli personalizzati.

Eliminazione del deployment

Ti consigliamo di eliminare il deployment per evitare addebiti. Questo deployment non ti serve per il passaggio successivo. Esegui questo comando per eliminare il deployment:

gcloud deployment-manager deployments delete deployment-with-many-templates