Skip to content

Commit

Permalink
edit to match upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijs Schnitger committed May 5, 2016
2 parents 71f5408 + a2a73d2 commit 3f71ad7
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- 2.7
- pypy
install: "pip install -r requirements.txt"
script: py.test
script: py.test
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ Or in a playbook:
## Releases

`terraform.py` is with matching versions of
[microservices-infrastructure](https://github.com/CiscoCloud/microservices-infrastructure).
Run `terraform.py --version` to report the current version.
[mantl](https://github.com/CiscoCloud/mantl). Run `terraform.py --version` to
report the current version.

## Adding a new provider

Expand All @@ -73,7 +73,7 @@ calls them.) A parser is a function that takes a resource (a dictionary
processed from JSON) and outputs a tuple of `(name, attributes, groups)`. The
parser should be decorated with the `parses` decorator, which takes the name of
the resource (EG `aws_instance`). It should also be decorated with
`calculate_mi_vars`.
`calculate_mantl_vars`.

As a guideline, `terraform.py` should require no resources outside the Python
standard distribution so it can just be copied in wherever it's needed.
Expand Down Expand Up @@ -121,13 +121,13 @@ a list. Given keys like this and the prefix "keys":

...
"tags.#": "2",
"tags.2783239913": "mi",
"tags.2783239913": "mantl",
"tags.3990563915": "control",
...

`parse_list` would return this:

["mi", "control"]
["mantl", "control"]

## License

Expand Down
103 changes: 86 additions & 17 deletions terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def inner(func):
return inner


def calculate_mi_vars(func):
def calculate_mantl_vars(func):
"""calculate Mantl vars"""

@wraps(func)
Expand Down Expand Up @@ -138,8 +138,77 @@ def parse_bool(string_form):
raise ValueError('could not convert %r to a bool' % string_form)


@parses('triton_machine')
@calculate_mantl_vars
def triton_machine(resource, module_name):
raw_attrs = resource['primary']['attributes']
name = raw_attrs.get('name')
groups = []

attrs = {
'id': raw_attrs['id'],
'dataset': raw_attrs['dataset'],
'disk': raw_attrs['disk'],
'firewall_enabled': parse_bool(raw_attrs['firewall_enabled']),
'image': raw_attrs['image'],
'ips': parse_list(raw_attrs, 'ips'),
'memory': raw_attrs['memory'],
'name': raw_attrs['name'],
'networks': parse_list(raw_attrs, 'networks'),
'package': raw_attrs['package'],
'primary_ip': raw_attrs['primaryip'],
'root_authorized_keys': raw_attrs['root_authorized_keys'],
'state': raw_attrs['state'],
'tags': parse_dict(raw_attrs, 'tags'),
'type': raw_attrs['type'],
'user_data': raw_attrs['user_data'],
'user_script': raw_attrs['user_script'],

# ansible
'ansible_ssh_host': raw_attrs['primaryip'],
'ansible_ssh_port': 22,
'ansible_ssh_user': 'root', # it's "root" on Triton by default

# generic
'public_ipv4': raw_attrs['primaryip'],
'provider': 'triton',
}

# private IPv4
for ip in attrs['ips']:
if ip.startswith('10') or ip.startswith('192.168'): # private IPs
attrs['private_ipv4'] = ip
break

if 'private_ipv4' not in attrs:
attrs['private_ipv4'] = attrs['public_ipv4']

# attrs specific to Mantl
attrs.update({
'consul_dc': _clean_dc(attrs['tags'].get('dc', 'none')),
'role': attrs['tags'].get('role', 'none'),
'ansible_python_interpreter': attrs['tags'].get('python_bin', 'python')
})

# add groups based on attrs
groups.append('triton_image=' + attrs['image'])
groups.append('triton_package=' + attrs['package'])
groups.append('triton_state=' + attrs['state'])
groups.append('triton_firewall_enabled=%s' % attrs['firewall_enabled'])
groups.extend('triton_tags_%s=%s' % item
for item in attrs['tags'].items())
groups.extend('triton_network=' + network
for network in attrs['networks'])

# groups specific to Mantl
groups.append('role=' + attrs['role'])
groups.append('dc=' + attrs['consul_dc'])

return name, attrs, groups


@parses('digitalocean_droplet')
@calculate_mi_vars
@calculate_mantl_vars
def digitalocean_host(resource, tfvars=None):
raw_attrs = resource['primary']['attributes']
name = raw_attrs['name']
Expand All @@ -150,7 +219,7 @@ def digitalocean_host(resource, tfvars=None):
'image': raw_attrs['image'],
'ipv4_address': raw_attrs['ipv4_address'],
'locked': parse_bool(raw_attrs['locked']),
'metadata': json.loads(raw_attrs['user_data']),
'metadata': json.loads(raw_attrs.get('user_data', '{}')),
'region': raw_attrs['region'],
'size': raw_attrs['size'],
'ssh_keys': parse_list(raw_attrs, 'ssh_keys'),
Expand All @@ -161,7 +230,8 @@ def digitalocean_host(resource, tfvars=None):
'ansible_ssh_user': 'root', # it's always "root" on DO
# generic
'public_ipv4': raw_attrs['ipv4_address'],
'private_ipv4': raw_attrs['ipv4_address'],
'private_ipv4': raw_attrs.get('ipv4_address_private',
raw_attrs['ipv4_address']),
'provider': 'digitalocean',
}

Expand Down Expand Up @@ -189,7 +259,7 @@ def digitalocean_host(resource, tfvars=None):


@parses('softlayer_virtualserver')
@calculate_mi_vars
@calculate_mantl_vars
def softlayer_host(resource, module_name):
raw_attrs = resource['primary']['attributes']
name = raw_attrs['name']
Expand Down Expand Up @@ -227,7 +297,7 @@ def softlayer_host(resource, module_name):


@parses('openstack_compute_instance_v2')
@calculate_mi_vars
@calculate_mantl_vars
def openstack_host(resource, module_name):
raw_attrs = resource['primary']['attributes']
name = raw_attrs['name']
Expand Down Expand Up @@ -295,7 +365,7 @@ def openstack_host(resource, module_name):


@parses('aws_instance')
@calculate_mi_vars
@calculate_mantl_vars
def aws_host(resource, module_name):
name = resource['primary']['attributes']['tags.Name']
raw_attrs = resource['primary']['attributes']
Expand Down Expand Up @@ -364,7 +434,7 @@ def aws_host(resource, module_name):


@parses('google_compute_instance')
@calculate_mi_vars
@calculate_mantl_vars
def gce_host(resource, module_name):
name = resource['primary']['id']
raw_attrs = resource['primary']['attributes']
Expand Down Expand Up @@ -439,7 +509,7 @@ def gce_host(resource, module_name):


@parses('vsphere_virtual_machine')
@calculate_mi_vars
@calculate_mantl_vars
def vsphere_host(resource, module_name):
raw_attrs = resource['primary']['attributes']
network_attrs = parse_dict(raw_attrs, 'network_interface')
Expand Down Expand Up @@ -481,7 +551,7 @@ def vsphere_host(resource, module_name):
return name, attrs, groups

@parses('azure_instance')
@calculate_mi_vars
@calculate_mantl_vars
def azure_host(resource, module_name):
name = resource['primary']['attributes']['name']
raw_attrs = resource['primary']['attributes']
Expand Down Expand Up @@ -512,27 +582,27 @@ def azure_host(resource, module_name):
'ansible_ssh_host': raw_attrs['vip_address'],
}

# attrs specific to microservices-infrastructure
# attrs specific to mantl
attrs.update({
'consul_dc': attrs['location'].lower().replace(" ", "-"),
'role': attrs['description']
})

# groups specific to microservices-infrastructure
# groups specific to mantl
groups.extend(['azure_image=' + attrs['image'],
'azure_location=' + attrs['location'].lower().replace(" ", "-"),
'azure_username=' + attrs['username'],
'azure_security_group=' + attrs['security_group']])

# groups specific to microservices-infrastructure
# groups specific to mantl
groups.append('role=' + attrs['role'])
groups.append('dc=' + attrs['consul_dc'])

return name, attrs, groups


@parses('clc_server')
@calculate_mi_vars
@calculate_mantl_vars
def clc_server(resource, module_name):
raw_attrs = resource['primary']['attributes']
name = raw_attrs.get('id')
Expand Down Expand Up @@ -570,7 +640,7 @@ def clc_server(resource, module_name):


@parses('ucs_service_profile')
@calculate_mi_vars
@calculate_mantl_vars
def ucs_host(resource, module_name):
name = resource['primary']['id']
raw_attrs = resource['primary']['attributes']
Expand All @@ -582,7 +652,7 @@ def ucs_host(resource, module_name):
'provider': 'ucs',
}

# attrs specific to microservices-infrastructure
# attrs specific to mantl
attrs.update({
'consul_dc': _clean_dc(attrs['metadata'].get('dc', module_name)),
'role': attrs['metadata'].get('role', 'none'),
Expand All @@ -605,7 +675,6 @@ def ucs_host(resource, module_name):

return name, attrs, groups


## QUERY TYPES
def query_host(hosts, target):
for name, attrs, _ in hosts:
Expand Down
14 changes: 7 additions & 7 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


@pytest.fixture
def calculate_mi_vars():
from terraform import calculate_mi_vars
return calculate_mi_vars
def calculate_mantl_vars():
from terraform import calculate_mantl_vars
return calculate_mantl_vars


def mirror(item):
Expand All @@ -18,22 +18,22 @@ def inner(*args, **kwargs):
@pytest.mark.parametrize('role,serverstate',
[('control', True), ('worker', False),
('none', False)])
def test_attrs(calculate_mi_vars, role, serverstate):
def test_attrs(calculate_mantl_vars, role, serverstate):
mirrorer = mirror(('', {'role': role}, []))
func = calculate_mi_vars(mirrorer)
func = calculate_mantl_vars(mirrorer)
_, attrs, _ = func()
assert 'consul_is_server' in attrs
assert attrs['consul_is_server'] == serverstate


@pytest.mark.parametrize('routable', [True, False])
def test_publicly_routable(calculate_mi_vars, routable):
def test_publicly_routable(calculate_mantl_vars, routable):
attrs = {}
if routable:
attrs['publicly_routable'] = True

mirrorer = mirror(('', attrs, []))
func = calculate_mi_vars(mirrorer)
func = calculate_mantl_vars(mirrorer)
_, _, groups = func()
if routable:
assert 'publicly_routable' in groups
Expand Down
6 changes: 3 additions & 3 deletions tests/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def gce_resource():
"id": "mi-control-01",
"attributes": {
"can_ip_forward": "false",
"description": "microservices-infrastructure control node #01",
"description": "mantl control node #01",
"disk.#": "1",
"disk.0.auto_delete": "true",
"disk.0.device_name": "",
Expand All @@ -40,7 +40,7 @@ def gce_resource():
"network_interface.0.access_config.0.nat_ip": "104.197.63.156",
"network_interface.0.address": "10.0.237.130",
"network_interface.0.name": "nic0",
"network_interface.0.network": "microservices-infrastructure",
"network_interface.0.network": "mantl",
"self_link":
"https://www.googleapis.com/compute/v1/projects/test-project/zones/us-central1-a/instances/mi-control-01",
"service_account.#": "0",
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_name(gce_resource, gce_host):
'sshKeys': 'fake ssh key', },
'network': [],
'network_interface': [{
'network': 'microservices-infrastructure',
'network': 'mantl',
'access_config': [{'nat_ip': '104.197.63.156'}],
'address': '10.0.237.130',
'name': 'nic0'
Expand Down
Loading

0 comments on commit 3f71ad7

Please sign in to comment.