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

Fixes #184: DM/runtime_config: refactoring #201

Merged
merged 1 commit into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions dm/templates/runtime_config/examples/runtime_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ resources:
- name: my-test-config
type: runtime_config.py
properties:
config: my-test-config
name: my-test-config
description: my config description
variables:
- variable: myapp/dev/sql/connection_string
- name: myapp/dev/sql/connection_string
text: super text value
- variable: myapp/dev/web/wildcardcert
- name: myapp/dev/web/wildcardcert
value: c3VwZXJhd2Vzb21ldGV4dAo=
waiters:
- waiter: my-test-waiter
- name: my-test-waiter
timeout: 3.5s
success:
cardinality:
Expand Down
17 changes: 13 additions & 4 deletions dm/templates/runtime_config/runtime_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@
This template creates a Runtime Configurator with the associated resources.
"""

from hashlib import sha1


def generate_config(context):
""" Entry point for the deployment resources. """

resources = []
properties = context.properties
project_id = properties.get('projectId', context.env['project'])
name = properties.get('config', context.env['name'])
name = properties.get('name', properties.get('config', context.env['name']))
parent = 'projects/{}/configs/{}'.format(project_id, name)

# The runtimeconfig resource.
runtime_config = {
'name': name,
'type': 'runtimeconfig.v1beta1.config',
# https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs
'type': 'gcp-types/runtimeconfig-v1beta1:projects.configs',
'properties': {
'config': name,
# TODO: uncomment after gcp type is fixed
# 'project': project_id,
'description': properties['description']
}
}
Expand All @@ -39,21 +44,25 @@ def generate_config(context):

# The runtimeconfig variable resources.
for variable in properties.get('variables', []):
suffix = sha1('{}-{}'.format(context.env['name'], variable.get('name', variable.get('variable')))).hexdigest()[:10]
variable['project'] = project_id
variable['parent'] = parent
variable['config'] = name
variable_res = {
'name': variable['variable'],
'name': '{}-{}'.format(context.env['name'], suffix),
'type': 'variable.py',
'properties': variable
}
resources.append(variable_res)

# The runtimeconfig waiter resources.
for waiter in properties.get('waiters', []):
suffix = sha1('{}-{}'.format(context.env['name'], waiter.get('name', waiter.get('waiter')))).hexdigest()[:10]
waiter['project'] = project_id
waiter['parent'] = parent
waiter['config'] = name
waiter_res = {
'name': waiter['waiter'],
'name': '{}-{}'.format(context.env['name'], suffix),
'type': 'waiter.py',
'properties': waiter
}
Expand Down
31 changes: 27 additions & 4 deletions dm/templates/runtime_config/runtime_config.py.schema
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,45 @@
info:
title: Runtime Configurator
author: Sourced Group Inc.
version: 1.0.0
description: |
Supports creation of a Runtime Configurator.

For more information on this resource, see
https://cloud.google.com/deployment-manager/runtime-configurator/.
https://cloud.google.com/deployment-manager/runtime-configurator/

APIs endpoints used by this template:
- gcp-types/runtimeconfig-v1beta1:projects.configs =>
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs

imports:
- path: variable.py
- path: waiter.py

additionalProperties: false

required:
- config
oneOf:
- required:
- config
- required:
- name

properties:
config:
type: string
description: The config resource name.
description: |
The config resource name. DEPRECATED, use "name"
Resource name would be used if omitted.
name:
type: string
description: |
The config resource name.
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.
projectId:
type: string
description: ProjectID of the project to create the config in.
Expand All @@ -41,13 +62,15 @@ properties:
description: The config resource description.
variables:
type: array
uniqItems: true
description: |
The list of variables as defined in the variable.py template.
Example:
- variableName: myappvariable
variableTextValue: "my variable value"
waiters:
type: array
uniqItems: true
description: |
The list of waiters as defined in the waiter.py template.
Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ resources:
- name: ${CONFIG_NAME}
type: runtime_config.py
properties:
config: ${CONFIG_NAME}
name: ${CONFIG_NAME}
description: my config description
variables:
- variable: ${VARIABLE_1}
- name: ${VARIABLE_1}
text: ${VARIABLE_1_VALUE}
- variable: ${VARIABLE_2}
- name: ${VARIABLE_2}
value: ${VARIABLE_2_VALUE}
waiters:
- waiter: ${WAITER_NAME}
- name: ${WAITER_NAME}
timeout: ${WAITER_TIMEOUT}
success:
cardinality:
Expand Down
29 changes: 18 additions & 11 deletions dm/templates/runtime_config/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,36 @@
def generate_config(context):
""" Entry point for the deployment resources. """

name = context.properties.get('variable', context.env['name'])
properties = context.properties
project_id = properties.get('project', context.env['project'])
config_name = context.properties.get('config')
required_properties = ['parent', 'variable']

props = {
'variable': properties.get('name', properties.get('variable')),
'parent': properties['parent'],
# TODO: uncomment after gcp type is fixed
# 'project': project_id,
}

optional_properties = ['text', 'value']
# Load the required properties, then the optional ones if specified.
properties = {p: context.properties[p] for p in required_properties}
properties.update({
p: context.properties[p]
for p in optional_properties if p in context.properties
props.update({
p: properties[p]
for p in optional_properties if p in properties
})

resources = [{
'name': name,
'type': 'runtimeconfig.v1beta1.variable',
'properties': properties,
'name': context.env['name'],
# https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables
'type': 'gcp-types/runtimeconfig-v1beta1:projects.configs.variables',
'properties': props,
'metadata': {
'dependsOn': [config_name]
}
}]

outputs = [{
'name': 'updateTime',
'value': '$(ref.{}.updateTime)'.format(name)
'value': '$(ref.{}.updateTime)'.format(context.env['name'])
}]

return {'resources': resources, 'outputs': outputs}
43 changes: 34 additions & 9 deletions dm/templates/runtime_config/variable.py.schema
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,60 @@
info:
title: Variable
author: Sourced Group Inc.
description: Creates a RuntimeConfig variable resource.
version: 1.0.0
description: |
Creates a RuntimeConfig variable resource.

For more information on this resource, see
https://cloud.google.com/deployment-manager/runtime-configurator/.
https://cloud.google.com/deployment-manager/runtime-configurator/

APIs endpoints used by this template:
- gcp-types/runtimeconfig-v1beta1:projects.configs.variables =>
https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.variables

additionalProperties: false

required:
- parent
- variable

oneOf:
- required:
- text
- required:
- value
allOf:
- oneOf:
- required:
- text
- required:
- value
- oneOf:
- required:
- variable
- required:
- name

properties:
project:
type: string
description: |
The project ID of the project containing resources. The
Google apps domain is prefixed if applicable.
parent:
type: string
description: |
The path to the configuration that will own the waiter.
The configuration must exist beforehand; the path must be in the
projects/[PROJECT_ID]/configs/[CONFIG_NAME] format.
variable:
name:
type: string
description: |
The key (name) of the variable. For example, status and
users/jane-smith/favorite_color are valid keys. Can contain digits,
letters, dashes, and slashes. The max length is 256 characters.
variable:
type: string
description: |
DEPRECATED, please use "name"
config:
type: string
description: |
Config resource name (for dependency)
text:
type: string
description: |
Expand Down
33 changes: 21 additions & 12 deletions dm/templates/runtime_config/waiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,38 @@
def generate_config(context):
""" Entry point for the deployment resources. """

name = context.properties.get('name', context.env['name'])
config_name = context.properties.get('config')
required_properties = ['waiter', 'parent', 'timeout', 'success']
properties = context.properties
project_id = properties.get('project', context.env['project'])
config_name = properties.get('config')

props = {
'waiter': properties.get('name', properties.get('waiter')),
'parent': properties['parent'],
'timeout': properties['timeout'],
'success': properties['success'],
# TODO: uncomment after gcp type is fixed
# 'project': project_id,
}

optional_properties = ['failure']
# Load the required properties, then the optional ones if specified.
properties = {p: context.properties[p] for p in required_properties}
properties.update({
p: context.properties[p]
for p in optional_properties if p in context.properties
props.update({
p: properties[p]
for p in optional_properties if p in properties
})

resources = [{
'name': name,
'type': 'runtimeconfig.v1beta1.waiter',
'properties': properties,
'name': context.env['name'],
# https://cloud.google.com/deployment-manager/runtime-configurator/reference/rest/v1beta1/projects.configs.waiters
'type': 'gcp-types/runtimeconfig-v1beta1:projects.configs.waiters',
'properties': props,
'metadata': {
'dependsOn': [config_name]
}
}]

outputs = [{
'name': 'createTime',
'value': '$(ref.{}.createTime)'.format(name)
'value': '$(ref.{}.createTime)'.format(context.env['name'])
}]

return {'resources': resources, 'outputs': outputs}
Loading