Skip to content

Commit

Permalink
Fixes GoogleCloudPlatform#143: DM/internal_load_balancer: refactoring
Browse files Browse the repository at this point in the history
GoogleCloudPlatform#143

- Added version, links to docs
- Switched to using type provider
- Added support for cross-project resource creation
- Fixed resource names
  • Loading branch information
nick4fake committed Jun 28, 2019
1 parent 43cd422 commit 3ae6217
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
39 changes: 22 additions & 17 deletions dm/templates/internal_load_balancer/internal_load_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ def set_optional_property(destination, source, prop_name):
destination[prop_name] = source[prop_name]


def get_backend_service(properties, name):
def get_backend_service(properties, project_id, res_name):
""" Creates the backend service. """

backend_spec = properties['backendService']
name = '{}-bs'.format(res_name)
backend_properties = {
'name': backend_spec.get('name', properties.get('name', name)),
'project': project_id,
'loadBalancingScheme': 'INTERNAL',
'protocol': properties['protocol'],
'region': properties['region']
'region': properties['region'],
}

backend_spec = properties['backendService']
backend_name = backend_spec.get('name', name + '-bs')
backend_resource = {
'name': backend_name,
'name': name,
'type': 'backend_service.py',
'properties': backend_properties
}
Expand All @@ -54,29 +56,31 @@ def get_backend_service(properties, name):
return [backend_resource], [
{
'name': 'backendServiceName',
'value': backend_name,
'value': backend_resource['properties']['name'],
},
{
'name': 'backendServiceSelfLink',
'value': '$(ref.{}.selfLink)'.format(backend_name),
'value': '$(ref.{}.selfLink)'.format(name),
},
]


def get_forwarding_rule(properties, backend, name):
def get_forwarding_rule(properties, backend, project_id, res_name):
""" Creates the forwarding rule. """

rule_properties = {
'name': properties.get('name', res_name),
'project': project_id,
'loadBalancingScheme': 'INTERNAL',
'IPProtocol': properties['protocol'],
'backendService': '$(ref.{}.selfLink)'.format(backend['name']),
'region': properties['region']
'region': properties['region'],
}

rule_resource = {
'name': name,
'name': res_name,
'type': 'forwarding_rule.py',
'properties': rule_properties
'properties': rule_properties,
}

optional_properties = [
Expand All @@ -94,15 +98,15 @@ def get_forwarding_rule(properties, backend, name):
return [rule_resource], [
{
'name': 'forwardingRuleName',
'value': name,
'value': res_name,
},
{
'name': 'forwardingRuleSelfLink',
'value': '$(ref.{}.selfLink)'.format(name),
'value': '$(ref.{}.selfLink)'.format(res_name),
},
{
'name': 'IPAddress',
'value': '$(ref.{}.IPAddress)'.format(name),
'value': '$(ref.{}.IPAddress)'.format(res_name),
},
{
'name': 'region',
Expand All @@ -115,13 +119,14 @@ def generate_config(context):
""" Entry point for the deployment resources. """

properties = context.properties
name = properties.get('name', context.env['name'])
project_id = properties.get('project', context.env['project'])

backend_resources, backend_outputs = get_backend_service(properties, name)
backend_resources, backend_outputs = get_backend_service(properties, project_id, context.env['name'])
rule_resources, rule_outputs = get_forwarding_rule(
properties,
backend_resources[0],
name
project_id,
context.env['name']
)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ properties:
description: |
The internal load balancer name. This name is assigned to the
underlying forwarding rule resource.
Resource name would be used if omitted.
project:
type: string
description: |
The project ID of the project containing resources. The
Google apps domain is prefixed if applicable.
description:
type: string
description: |
Expand All @@ -49,6 +55,7 @@ properties:
The name of the region where the internal load balancer resides.
ports:
type: array
uniqItems: true
description: |
The list of ports; only packets addressed to these ports are forwarded
to the backends configured with the load balancer.
Expand Down Expand Up @@ -84,6 +91,7 @@ properties:
backendService:
type: object
description: The backend service configuration.
additionalProperties: false
required:
- healthCheck
- backends
Expand All @@ -96,11 +104,13 @@ properties:
description: An optional description of the backend service resource.
backends:
type: array
uniqItems: true
description: |
The list of backends (instance groups) to which the backend service
distributes traffic.
items:
type: object
additionalProperties: false
required:
- group
properties:
Expand Down Expand Up @@ -137,6 +147,7 @@ properties:
- CLIENT_IP_PORT_PROTO
connectionDraining:
type: object
additionalProperties: false
description: The connection draining settings.
properties:
drainingTimeoutSec:
Expand Down

0 comments on commit 3ae6217

Please sign in to comment.