Skip to content

Commit

Permalink
[DM] instance_template refactoring (GoogleCloudPlatform#89)
Browse files Browse the repository at this point in the history
GoogleCloudPlatform#68

- Added version, links to docs
- Switched to using type provider
- Added cross-project creation support
- Added additionalProperties: false for nested objects
- Added support for "networkInterfaces[].accessConfigs[]", "disks",
"scheduling", "minCpuPlatform", "guestAccelerators",
"shieldedInstanceConfig", "sourceInstance", "sourceInstanceParams"
- Fixed resource name
  • Loading branch information
bohdanyurov-gl authored and ocsig committed Jun 5, 2019
1 parent 0359ac4 commit 8007e64
Show file tree
Hide file tree
Showing 6 changed files with 615 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ resources:
properties:
diskImage: projects/ubuntu-os-cloud/global/images/family/ubuntu-1804-lts
networks:
- default
- network: default
accessConfigs:
- type: ONE_TO_ONE_NAT
machineType: f1-micro
tags:
items:
Expand Down
73 changes: 45 additions & 28 deletions dm/templates/instance_template/instance_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,32 @@ def get_network_interfaces(properties):
"""
network_interfaces = []

networks = properties.get('networks', [{
"name": properties.get('network'),
"hasExternalIp": properties.get('hasExternalIp'),
"natIP": properties.get('natIP'),
"subnetwork": properties.get('subnetwork'),
"networkIP": properties.get('networkIP'),
}])
networks = properties.get('networks', [])
if len(networks) == 0 and properties.get('network'):
network = {
"network": properties.get('network'),
"subnetwork": properties.get('subnetwork'),
"networkIP": properties.get('networkIP'),
}
networks.append(network)
if (properties.get('hasExternalIp')):
network['accessConfigs'] = [{
"type": "ONE_TO_ONE_NAT",
}]
if properties.get('natIP'):
network['accessConfigs'][0]["natIp"] = properties.get('natIP')

for network in networks:
if not '.' in network['name'] and not '/' in network['name']:
network_name = 'global/networks/{}'.format(network['name'])
if not '.' in network['network'] and not '/' in network['network']:
network_name = 'global/networks/{}'.format(network['network'])
else:
network_name = network['name']
network_name = network['network']

network_interface = {
'network': network_name,
}

if network['hasExternalIp']:
access_configs = {
'name': 'External NAT',
'type': 'ONE_TO_ONE_NAT'
}

if network.get('natIP'):
access_configs['natIP'] = network['natIP']

network_interface['accessConfigs'] = [access_configs]

netif_optional_props = ['subnetwork', 'networkIP']
netif_optional_props = ['subnetwork', 'networkIP', 'aliasIpRanges', 'accessConfigs']
for prop in netif_optional_props:
if network.get(prop):
network_interface[prop] = network[prop]
Expand All @@ -93,17 +89,19 @@ def generate_config(context):
properties = context.properties
name = properties.get('name', context.env['name'])
machine_type = properties['machineType']
boot_disk = create_boot_disk(properties)
network_interfaces = get_network_interfaces(context.properties)
project_id = properties.get('project', context.env['project'])
instance_template = {
'name': name,
'type': 'compute.v1.instanceTemplate',
'name': context.env['name'],
# https://cloud.google.com/compute/docs/reference/rest/v1/instanceTemplates
'type': 'gcp-types/compute-v1:instanceTemplates',
'properties':
{
'name': name,
'project': project_id,
'properties':
{
'machineType': machine_type,
'disks': [boot_disk],
'networkInterfaces': network_interfaces
}
}
Expand All @@ -113,15 +111,22 @@ def generate_config(context):

optional_props = [
'metadata',
'disks',
'scheduling',
'tags',
'canIpForward',
'labels',
'serviceAccounts',
'scheduling'
'scheduling',
'shieldedInstanceConfig',
'minCpuPlatform',
'guestAccelerators',
]

for prop in optional_props:
set_optional_property(template_spec, properties, prop)
if not template_spec.get('disks'):
template_spec['disks'] = [create_boot_disk(properties)]

set_optional_property(
template_spec,
Expand All @@ -137,6 +142,18 @@ def generate_config(context):
'description'
)

set_optional_property(
instance_template['properties'],
properties,
'sourceInstance'
)

set_optional_property(
instance_template['properties'],
properties,
'sourceInstanceParams'
)

return {
'resources': [instance_template],
'outputs':
Expand All @@ -147,7 +164,7 @@ def generate_config(context):
},
{
'name': 'selfLink',
'value': '$(ref.{}.selfLink)'.format(name)
'value': '$(ref.{}.selfLink)'.format(context.env['name'])
}
]
}
Loading

0 comments on commit 8007e64

Please sign in to comment.