GoogleCloudPlatform / deploymentmanager-samples

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

References between templates using python #525

Closed komasoftware closed 4 years ago

komasoftware commented 4 years ago

I am trying something very basic but struggling to get this to work. I have 2 templates :

I am trying to combine both templates together in a 3rd master template environment.py The front_end.py needs a folder Id. It expects the folder Id as one of the properties like this :

_(template front_end.py)_

    ....
    folder_id = context.properties["environment_folder"]
    resources.append({
        'name': project_id,
        'type': 'cloudresourcemanager.v1.project',
        'properties': {
            'name': project_name,
            'projectId': project_id,
            'parent': {
                'type': 'folder',
                'id': folder_id
            }
        }
    })
   ...

The code below defines an explicit depends on the folder creation before the project creation can happen, but passing the reference of the folder ID in the properties using its reference _"environment_folder": "$(ref.environment_folder.folder_id)"_ does not work. How can I reference the finalValue for the reference in python ?

(master template environment.py)

def generate_config(context):
    """Main entry point for Deployment Manager logic"""

    resources = []
    outputs = []

    # environment outputs
    outputs.append({
        "name": "environment_name",
        "value": context.properties["envName"]
    })

    # environment folder
    resources.append({
        "name": "environment_folder",
        "type": "environment_folder.py",
        "properties": context.properties
    })
    outputs.append({
        "name": "environment_folder_id",
        "value": "$(ref.environment_folder.folder_id)"
    })

    # front_end project
    resources.append({
        "name": "front_end",
        "type": "front_end.py",
        "properties": {
            "envName": context.properties["envName"],
            "environment_folder": "$(ref.environment_folder.folder_id)"
        },
        "metadata": {
            "dependsOn": ["environment_folder"]
        }
    })

    return {"resources": resources, "outputs": outputs}

Selection_999(564)

Any suggestions very welcome.

komasoftware commented 4 years ago

I seem not to be the only one struggling here

https://stackoverflow.com/questions/59460623/how-to-create-a-folder-a-project-under-it-with-deployment-manager-google-clou

komasoftware commented 4 years ago

see also https://github.com/GoogleCloudPlatform/deploymentmanager-samples/issues/353#issuecomment-579194769

komasoftware commented 4 years ago

Answered in a thread on #353