Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate a single template from route DM template. #257

Merged
merged 5 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 8 additions & 73 deletions dm/templates/route/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,64 +32,27 @@ def generate_config(context):
name = route.get('name')
if not name:
name = '%s-%s'.format(context.env['name'], sha1(json.dumps(route)).hexdigest()[:10])

# Set the common route properties.
properties = {
route_properties = {
'name': name,
'network': network_name,
'project': project_id,
'tags': route['tags'],
'priority': route.get('priority', i),
'destRange': route['destRange']
}

# Check the route type and fill out the following fields:
if route.get('routeType') == 'instance':
instance_name = route.get('instanceName')
zone = route.get('zone', '')
properties['nextHopInstance'] = generate_instance_url(
project_id,
zone,
instance_name
)
elif route.get('routeType') == 'gateway':
gateway_name = route.get('gatewayName')
properties['nextHopGateway'] = generate_gateway_url(
project_id,
gateway_name
)
elif route.get('routeType') == 'vpntunnel':
vpn_tunnel_name = route.get('vpnTunnelName')
region = route.get('region', '')
properties['nextHopVpnTunnel'] = generate_vpn_tunnel_url(
project_id,
region,
vpn_tunnel_name
)

optional_properties = [
'nextHopIp',
'nextHopInstance',
'nextHopNetwork',
'nextHopGateway',
'nextHopVpnTunnel',
]

for prop in optional_properties:
if prop in route:
properties[prop] = route[prop]
for specified_properties in route:
route_properties[specified_properties] = route[specified_properties]

resources.append(
{
'name': name,
# https://cloud.google.com/compute/docs/reference/rest/v1/routes
'type': 'gcp-types/compute-v1:routes',
'properties': properties
'type': 'single_route.py',
'properties': route_properties
}
)

out[name] = {
'selfLink': '$(ref.' + name + '.selfLink)',
'nextHopNetwork': network_name
'nextHopNetwork': '$(ref.' + name + '.nextHopNetwork)',
}

outputs = [{'name': 'routes', 'value': out}]
Expand All @@ -109,31 +72,3 @@ def generate_network_url(properties):
network_url = 'global/networks/{}'.format(network_name)

return network_url


def generate_instance_url(project, zone, instance):
""" Format the resource name as a resource URI. """

is_self_link = '/' in instance or '.' in instance

if is_self_link:
instance_url = instance
else:
instance_url = 'projects/{}/zones/{}/instances/{}'
instance_url = instance_url.format(project, zone, instance)

return instance_url


def generate_gateway_url(project, gateway):
""" Format the resource name as a resource URI. """
return 'projects/{}/global/gateways/{}'.format(project, gateway)


def generate_vpn_tunnel_url(project, region, vpn_tunnel):
""" Format the resource name as a resource URI. """
return 'projects/{}/regions/{}/vpnTunnels/{}'.format(
project,
region,
vpn_tunnel
)
201 changes: 7 additions & 194 deletions dm/templates/route/route.py.schema
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ info:

imports:
- path: route.py
- path: ../route/single_route.py
name: single_route.py

additionalProperties: false

Expand All @@ -51,196 +53,12 @@ properties:
description: A list of routes.
items:
type: object
additionalProperties: false
description: |
Please check the properties in single_route.py.schema for details.
required:
- tags
- destRange
- priority
allOf:
- oneOf:
- required:
- nextHopInstance
- required:
- nextHopNetwork
- required:
- nextHopGateway
- required:
- nextHopVpnTunnel
- anyOf:
- required:
- nextHopIp
- required:
- routeType
- oneOf:
- allOf:
- not:
required:
- routeType
- not:
required:
- nextHopIp
- required:
- nextHopIp
- required:
- instanceName
- required:
- gatewayName
- required:
- vpnTunnelName
- oneOf:
- not:
required:
- gatewayName
- allOf:
- required:
- gatewayName
- routeType
- properties:
routeType:
enum: ["gateway"]
- oneOf:
- not:
required:
- vpnTunnelName
- allOf:
- required:
- vpnTunnelName
- routeType
- region
- properties:
routeType:
enum: ["vpntunnel"]
- oneOf:
- not:
required:
- instanceName
- allOf:
- required:
- instanceName
- routeType
- zone
- properties:
routeType:
enum: ["instance"]
- oneOf:
- not:
required:
- nextHopIp
- allOf:
- required:
- nextHopIp
- routeType
- properties:
routeType:
enum: ["ipaddress"]
- allOf:
- required:
- nextHopIp
- not:
required:
- routeType
properties:
name:
type: string
description: |
Name of the resource. Provided by the client when the resource is created. The name must be 1-63 characters long,
and comply with RFC1035. Specifically, the name must be 1-63 characters long and match the regular expression
[a-z]([-a-z0-9]*[a-z0-9])?. The first character must be a lowercase letter, and all following characters
(except for the last character) must be a dash, lowercase letter, or digit.
The last character must be a lowercase letter or digit.
Resource name would be used if omitted.
description:
type: string
description: |
An optional description of this resource. Provide this property when you create the resource.
routeType:
type: string
description: |
The resource type that will handle the matching packets.
Optionally you can use nextHop* attributes without specifying this field
enum:
- ipaddress
- instance
- gateway
- vpntunnel
tags:
type: array
uniqueItems: True
minItems: 1
description: |
A list of instance tags to which the route applies.
items:
type: string
description: An instance tag for the route.
priority:
type: number
description: |
The priority of this route. Priority is used to break ties in cases where there is more than one
matching route of equal prefix length. In cases where multiple routes have equal prefix length, the one
with the lowest-numbered priority value wins. The default value is 1000.
The priority value must be from 0 to 65535, inclusive.
default: 1000
minimum: 0
maximum: 65535
destRange:
type: string
description: |
The destination range of outgoing packets the route applies
to. Example: 192.168.0.1/10. Only IPv4 is supported.
pattern: ^([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}$
nextHopInstance:
type: string
description: |
The URL to an instance that should handle matching packets. You can specify this as a full or partial URL.
For example:
https://www.googleapis.com/compute/v1/projects/project/zones/zone/instances/
nextHopIp:
type: string
description: |
Used when routeType is 'ipaddress'.
The network IP address of the instance that should handle the matching
packets. Example: 192.168.0.1. Only IPv4 is supported.
pattern: ^([0-9]{1,3}\.){3}[0-9]{1,3}$
nextHopNetwork:
type: string
description: |
The URL of the local network if it should handle matching packets.
nextHopGateway:
type: string
description: |
The URL to a gateway that should handle matching packets. You can only specify the internet gateway using
a full or partial valid URL:
projects/project/global/gateways/default-internet-gateway
nextHopVpnTunnel:
type: string
description: |
The URL to a VpnTunnel that should handle matching packets.
instanceName:
type: string
description: |
Used when routeType is 'instance'.
The name of the instance that should handle the matching packets.
zone:
type: string
description: |
Used when routeType is 'instance'.
The zone where the instance resides.
gatewayName:
type: string
description: |
Used when routeType is 'gateway'.
The name of the gateway that will handle the matching packets. Only the
'default-internet-gateway' value is supported.
vpnTunnelName:
type: string
description: |
Used when routeType is 'vpntunnel'.
The name of the VPN tunnel that should handle the matching packets.
region:
type: string
description: |
Used when routeType is 'vpntunnel'.
The region where the VPN tunnel resides.

outputs:
properties:
Expand All @@ -254,14 +72,9 @@ outputs:
patternProperties:
".*":
type: object
description: Details for a route resource.
properties:
selfLink:
type: string
description: The URI (SelfLink) of the firewall rule resource.
nextHopNetwork:
type: string
description: URL to a Network that should handle matching packets.
description: |
Details for a route resource. Please check the outputs in
single_route.py.schema for details.

documentation:
- templates/route/README.md
Expand Down
Loading