GoogleCloudPlatform / deploymentmanager-samples

Deployment Manager samples and templates.
Apache License 2.0
939 stars 718 forks source link

Unable to add instance in Unmanaged Instance group by Jinja file #528

Closed SprintCT closed 4 years ago

SprintCT commented 4 years ago

Hi,

I tried to add instance in unmanaged instance for my backend LB by below jinja files. However it is creating unmanaged Instance group without member(means no instance has added). Kindly assist me to add the instance in unmanaged instance group by jinja file. I could find some python file however we required Jinja file format to add the instance in unmanaged instance group

Example 1: {% set COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' %}

resources:

Example2:-

{% set COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' %}

resources:

ocsig commented 4 years ago

Unfortunately the instanceGroup API has no Instances property. It is an instanceGroup.addInstance action. This is not fully compatible with Deployment Manager on the type level. You can use DM action (instead of type) to add or remove the instance to your instancegroup.

Please take a look at the Cloud Foundation Toolkit implementation: https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/tree/master/dm/templates/unmanaged_instance_group

I would encourage you to use this template instead of writing your own. You can even use this template from your jinja template by importing it and using it as a type. However if you wish, you can refactor this python template to Jinja. The concept, how to use actions is the same. ( Note, because adding and removing is an action and not a decleration, the implementation needs to deal with this if you wish to do proper lifecycle management.)

The problem with your code is:

SprintCT commented 4 years ago

Hi ocisg,

Thank you. I tried convert python to jinja, However it is very difficult. Also as per your suggestion i tried with ACTION instead of TYPE as well. But no luck only Instance group is created without member. Kindly assist me on this for jinja code.

{% set COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' %}

resources:

ocsig commented 4 years ago

Hi @SprintCT,

Your best option is to use the already well crafted CFT template which solves your problem and hides the complexity either from the yaml or from your Jinja: (Please note, this YAML directly points to the file on Github, it is advised to clone the repo localy to freeze to a specific version and not rely on Github latest because it is an external dependency to your project. However it is good for trying this out.)


resources:
  - name: unmanaged-instance-group-example
    type: https://raw.githubusercontent.com/GoogleCloudPlatform/cloud-foundation-toolkit/master/dm/templates/unmanaged_instance_group/unmanaged_instance_group.py
    properties:
      project: <FIXME:projectID>  # Optional
      name: <FIXME:groupName>
      zone: <FIXME:zone>
      instances:
        add:
        - <FIXME:instance-1>
        - <FIXME:instance-2>
{% set COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' %}

resources:
- name: {{ properties['instancegpname'] }}
  type:  https://raw.githubusercontent.com/GoogleCloudPlatform/cloud-foundation-toolkit/master/dm/templates/unmanaged_instance_group/unmanaged_instance_group.py
  properties:
    zone: {{ properties['zone'] }}
    instances:
      - instance: [ "{{ COMPUTE_URL_BASE }}projects/{{ properties['project'] }}/zones/{{ properties['zone'] }}/instances/{{ properties['instancename'] }}" ]

If you really wish to reimplement it yourself the following code will create an unmanaged instance group and add one instance:

{% set COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/' %}

resources:
  - name: {{ properties['instancegpname'] }}
    type: compute.v1.instanceGroup
    properties:
      zone: {{ properties['zone'] }}
  - name: {{ properties['instancegpname'] }}-addInstance
    action: compute.v1.instanceGroup.addInstance
    properties:
      instanceGroup: {ref.{{ properties['instancegpname'] }}.selfLink}
      instances:
        - instance: [ "{{ COMPUTE_URL_BASE }}projects/{{ properties['project'] }}/zones/{{ properties['zone'] }}/instances/{{ properties['instancename'] }}" ]

NOTE: I did not tested the snipets above, it may contains typos, but it should point you to the right direction.

SprintCT commented 4 years ago

Hi, My issue resolved when i using the below action:

action: gcp-types/compute-v1:compute.instanceGroups.addInstances