GoogleCloudPlatform / deploymentmanager-samples

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

Is there any already defined internal API for custom creation of Cloud NAT? #550

Closed Priyankasaggu11929 closed 4 years ago

Priyankasaggu11929 commented 4 years ago

I'm looking for a discovery document for creating custom type-provider for creation of Cloud NAT using deployment manager.

ocsig commented 4 years ago

Hi @Priyankasaggu11929, CloudNAT is part of the Compute API under Cloud Router resource. You don't need a custom resource. https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/master/dm/templates/cloud_router/examples/cloud_nat_router.yaml I also recommend you to look throught the available templates under the CloudFoundation Toolkit.

Priyankasaggu11929 commented 4 years ago

Thank you @ocsig, I am checking it.

But there is one doubt,

All the existing templates I've are written in jinja.

The network field, I'm supposed to write a value for, is created inside a jinja template. I can integrate the python template in the config file but how can I pass the network created inside the jinja template as a property to this cloud_nat_router resource which is being created through python template.

For example:

imports:
- path: vpc-network.jinja
- path: cloud_router.py

resources:

- name: vpc-network
  type: vpc-network.jinja
  properties:
    subnets:
    - vpc-network-sub-0
    region: us-west1
    zone: us-west1-a

resources:
  - name: test-cloud-nat-router
    type: cloud_router.py
    properties:
      name: cloud-nat-router
      network: vpc-network
      region: us-east1
      nats:
        - name: cloud-nat
          sourceSubnetworkIpRangesToNat: LIST_OF_SUBNETWORKS
          natIpAllocateOption: AUTO_ONLY
          subnetworks:
            - name: vpc-network-sub-0
      dependsOn:
      -   vpc-network

As pointed by you earlier, the inter template dependsOn is currently not supported. And you pointed a way out as well. But I couldn't reproduce a similar thing here.

What could be done here to achieve this?

ocsig commented 4 years ago

I would extend the network jinja template and schema simmilar to the cloud_router.py. Basically you need to pass the nats optional property to the cloud router resource. https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/35dd7c692b087134861b3878caf5f98df0884ef1/dm/templates/cloud_router/cloud_router.py#L60 And the schema for input validation: https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/35dd7c692b087134861b3878caf5f98df0884ef1/dm/templates/cloud_router/cloud_router.py.schema#L258

Priyankasaggu11929 commented 4 years ago

Extending here means adding nats property to the network jinja template itself?

Apologies, I'm trying to understand this part

Basically you need to pass the nats optional property to the cloud router resource. https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/35dd7c692b087134861b3878caf5f98df0884ef1/dm/templates/cloud_router/cloud_router.py#L60 And the schema for input validation: https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/35dd7c692b087134861b3878caf5f98df0884ef1/dm/templates/cloud_router/cloud_router.py.schema#L258

As I see it is already there in the cloud_router.py.

Extending essentially means what here?

ocsig commented 4 years ago

Appologies, I wasn't reading you question well.

If you go on a route you showed above, having a vpc-network.jinja and a cloud_router.py resource in the main yaml, then you need to make is a list under the same resources node:

resources:

- name: vpc-network
  type: vpc-network.jinja
  properties:
    subnets:
    - vpc-network-sub-0
    region: us-west1
    zone: us-west1-a
- name: test-cloud-nat-router
  type: cloud_router.py
  properties:
    name: cloud-nat-router
[...]

About your question on referencing: references are working properly. What doesn't work is dependsOn all the resources of an other template.

In your case you need a reference for the VPC network: $(ref.a-new-network.selfLink) What you need to know is how you name the VPC network resource within vpc-network.jinja. That is what you need to use instead of a-new-network. This reference will be the input for cloud_router.py

Priyankasaggu11929 commented 4 years ago

Thank you @ocsig.

I think it clarified my doubt quite nicely. I am testing it now.

Will close the issue once it runs properly.