0

We have a nodejs application which creates VM on GCP using config file (.yaml) and templates. Now I want to update few properties in the yaml/template based on the user input from UI while creating the VM. How can we update the config properties dynamically? Thanks in advance for any suggestions.

user2106568
  • 93
  • 1
  • 3
  • 10

2 Answers2

2

It seems like you have two options:

1) The jinja template way

Instead of a config file, you define a jinja template: resources:

# my-template.jinja
resources:
- name: my-resource
  type: some-type
  properties:
    prop1: {{ properties['foo'] }}
    prop2: {{ properties['bar'] }}

Then, you can invoke it like so and the variables foo and bar will get mapped to the provided properties:

gcloud deployment-manager deployments create <my-deployment> \
  --template my-template.jinja \
  --properties foo:user-custom-value,bar:another-value

2) The old-schoold template way

We are replacing the custom values in the text itself, instead of using a render engine (like jinja2 is)

# my-template.yaml
resources:
- name: my-resource
  type: some-type
  properties:
    prop1: REPLACE-PROP-1
    prop2: REPLACE-PROP-2

Replace the text as you may, you can use sed if you are running a shell script, or from node/javascript itself

const replaces = [
  {name: 'REPLACE-PROP-1', value: 'user-custom-value'},
  {name: 'REPLACE-PROP-2', value: 'another-custom-value'},
];
const templateYaml = fs.readFileSync('my-template.yaml','utf-8');
const customYaml = replaces
  .map(r => templateYaml.replace(RegExp(r.name,'g'), r.value);

Or use sed

sed -ie 's/REPLACE-PROP-1/user-custom-value/g' my-template.yaml
sed -ie 's/REPLACE-PROP-2/another-cst-value/g' my-template.yaml

And finally deploy the config:

gcloud deployment-manager deployments create <my-deployment> \
  --config my-template.yaml
dinigo
  • 6,872
  • 4
  • 37
  • 48
0

GCP deployment Manager does not have a way to do this dynamically. You'd have to add an additional layer (like the click to deploy marketplace) which allows users to select variables before applying the config file. DM does not have something that does this.

Patrick W
  • 4,603
  • 1
  • 12
  • 26