Skip to content

Commit

Permalink
Add sorting entries in the inventory file (hitachienergy#1076)
Browse files Browse the repository at this point in the history
* Add sorting hostnames for `Azure` and `Any` provider in the inventory
  file
  • Loading branch information
sbbroot committed Nov 8, 2021
1 parent cab6718 commit 3368de5
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 8 deletions.
7 changes: 5 additions & 2 deletions cli/engine/providers/any/APIProxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cli.helpers.doc_list_helpers import select_first
from cli.helpers.Log import Log
from cli.models.AnsibleHostModel import AnsibleHostModel
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel


class APIProxy:
Expand All @@ -22,5 +22,8 @@ def get_ips_for_feature(self, component_key):
for machine in component_config.machines:
machine_doc = select_first(self.config_docs,
lambda x: x.kind == 'infrastructure/machine' and x.name == machine)
result.append(AnsibleHostModel(machine_doc.specification.hostname, machine_doc.specification.ip))
result.append(AnsibleOrderedHostModel(machine_doc.specification.hostname, machine_doc.specification.ip))

result.sort()

return result
9 changes: 6 additions & 3 deletions cli/engine/providers/azure/APIProxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cli.helpers.Log import LogPipe, Log
from cli.helpers.doc_list_helpers import select_first
from cli.helpers.naming_helpers import resource_name, cluster_tag
from cli.models.AnsibleHostModel import AnsibleHostModel
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel

class APIProxy:
def __init__(self, cluster_model, config_docs):
Expand Down Expand Up @@ -55,7 +55,7 @@ def get_ips_for_feature(self, component_key):
look_for_public_ip = self.cluster_model.specification.cloud.use_public_ips
cluster = cluster_tag(self.cluster_prefix, self.cluster_name)
running_instances = self.run(self, f'az vm list-ip-addresses --ids $(az resource list --query "[?type==\'Microsoft.Compute/virtualMachines\' && tags.{component_key} == \'\' && tags.cluster == \'{cluster}\'].id" --output tsv)')
result = []
result: List[AnsibleOrderedHostModel] = []
for instance in running_instances:
if isinstance(instance, list):
instance = instance[0]
Expand All @@ -64,7 +64,10 @@ def get_ips_for_feature(self, component_key):
ip = instance['virtualMachine']['network']['publicIpAddresses'][0]['ipAddress']
else:
ip = instance['virtualMachine']['network']['privateIpAddresses'][0]
result.append(AnsibleHostModel(name, ip))
result.append(AnsibleOrderedHostModel(name, ip))

result.sort()

return result

def get_storage_account_primary_key(self, storage_account_name):
Expand Down
22 changes: 19 additions & 3 deletions cli/models/AnsibleHostModel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
class AnsibleHostModel:
def __init__(self, name, ip):
self.name = name
self.ip = ip
def __init__(self, name: str, ip: str):
self.name: str = name
self.ip: str = ip

def __eq__(self, other) -> bool:
return (self.name == other.name and
self.ip == other.ip)

def __lt__(self, other) -> bool:
pass


class AnsibleOrderedHostModel(AnsibleHostModel):
"""
Sortable variant of AnsibleHostModel
"""

def __lt__(self, other) -> bool:
return self.name < other.name
1 change: 1 addition & 0 deletions docs/changelogs/CHANGELOG-1.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [#2547](https://github.com/epiphany-platform/epiphany/issues/2547) - Refactoring and removal of old code from Ansible inventory creator and upgrade
- [#2644](https://github.com/epiphany-platform/epiphany/issues/2644) - Add validation to check hostnames for on-prem deployment
- [#2703](https://github.com/epiphany-platform/epiphany/issues/2703) - Add tests for docker and kubelet cgroup driver
- [#1076](https://github.com/epiphany-platform/epiphany/issues/1076) - Add sorting entries in the inventory file

### Fixed

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/engine/providers/any/test_APIProxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest import mock, TestCase

from cli.engine.providers.any.APIProxy import APIProxy
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel
from tests.unit.engine.providers.data.APIProxy_data import CLUSTER_MODEL, CONFIG_DOC


class APIProxyTest(TestCase):
""" Tests for `any` provider """

def setUp(self):
self.maxDiff = None


def test_get_ips_for_feature(self):
"""
Make sure that hostnames in inventory are sorted.
"""

with mock.patch('cli.engine.providers.any.APIProxy.Log', return_value='mock_Log') as mock_Log:
proxy = APIProxy(CLUSTER_MODEL('any'), CONFIG_DOC())

EXPECTED_RESULT = [
AnsibleOrderedHostModel('service-vm-0', '20.73.105.240'),
AnsibleOrderedHostModel('service-vm-1', '20.73.105.188'),
AnsibleOrderedHostModel('service-vm-2', '20.73.105.18'),
AnsibleOrderedHostModel('service-vm-3', '20.73.105.33'),
AnsibleOrderedHostModel('service-vm-4', '20.73.105.54')
]

result = proxy.get_ips_for_feature('service')

self.assertListEqual(EXPECTED_RESULT, result)
33 changes: 33 additions & 0 deletions tests/unit/engine/providers/azure/test_APIProxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest import mock, TestCase

from cli.engine.providers.azure.APIProxy import APIProxy
from cli.models.AnsibleHostModel import AnsibleOrderedHostModel
from tests.unit.engine.providers.data.APIProxy_data import CLUSTER_MODEL, RUNNING_INSTANCES


class APIProxyTest(TestCase):
""" Tests for `azure` provider """

def setUp(self):
self.maxDiff = None

def test_get_ips_for_feature(self):
"""
Make sure that hostnames in inventory are sorted.
"""

with mock.patch('cli.engine.providers.azure.APIProxy.Log', return_value='mock_Log') as mock_Log:
proxy = APIProxy(CLUSTER_MODEL('azure'), RUNNING_INSTANCES)
proxy.run = (lambda *args: RUNNING_INSTANCES) # mock run with prepared data

EXPECTED_RESULT = [
AnsibleOrderedHostModel('prefix-cluster-service-vm-0', '20.73.105.240'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-1', '20.73.105.188'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-2', '20.73.105.18'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-3', '20.73.105.33'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-4', '20.73.105.54')
]

result = proxy.get_ips_for_feature('service')

self.assertListEqual(EXPECTED_RESULT, result)
193 changes: 193 additions & 0 deletions tests/unit/engine/providers/data/APIProxy_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
from typing import Dict, List

from cli.helpers.ObjDict import ObjDict
from cli.helpers.objdict_helpers import dict_to_objdict


def CONFIG_DOC() -> ObjDict:
return dict_to_objdict([
{
'kind': 'infrastructure/machine',
'title': 'Virtual Machine Infra',
'provider': 'any',
'name': 'service-0',
'specification': {
'ip': '20.73.105.18',
'hostname': 'service-vm-2'
},
'version': '1.3.0dev'
},
{
'kind': 'infrastructure/machine',
'title': 'Virtual Machine Infra',
'provider': 'any',
'name': 'service-1',
'specification': {
'ip': '20.73.105.54',
'hostname': 'service-vm-4'
},
'version': '1.3.0dev'
},
{
'kind': 'infrastructure/machine',
'title': 'Virtual Machine Infra',
'provider': 'any',
'name': 'service-2',
'specification': {
'ip': '20.73.105.188',
'hostname': 'service-vm-1'
},
'version': '1.3.0dev'
},
{
'kind': 'infrastructure/machine',
'title': 'Virtual Machine Infra',
'provider': 'any',
'name': 'service-3',
'specification': {
'ip': '20.73.105.240',
'hostname': 'service-vm-0'
},
'version': '1.3.0dev'
},
{
'kind': 'infrastructure/machine',
'title': 'Virtual Machine Infra',
'provider': 'any',
'name': 'service-4',
'specification': {
'ip': '20.73.105.33',
'hostname': 'service-vm-3'
},
'version': '1.3.0dev'
},
])


def CLUSTER_MODEL(provider: str) -> ObjDict:
return dict_to_objdict({
'kind': 'epiphany-cluster',
'title': 'Epiphany cluster Config',
'provider': f'{provider}',
'name': 'default',
'specification': {
'prefix': 'prefix',
'name': 'cluster',
'admin_user': {
'name': 'username',
'key_path': '/path/to/key'
},
'cloud': {
'k8s_as_cloud_service': False,
'subscription_name': 'Subscription Name',
'vnet_address_pool': '10.1.0.0/20',
'use_public_ips': True,
'use_service_principal': False,
'region': 'West Europe',
'network': {'use_network_security_groups': True},
'default_os_image': 'default'
},
'components': {
'service': {
'count': 5,
'machine': 'service-machine',
'configuration': 'default',
'subnets': [{'address_pool': '10.1.8.0/24'}],
'machines': ['service-0',
'service-1',
'service-2',
'service-3',
'service-4']
}
}
},
'version': '1.3.0dev'
})

RUNNING_INSTANCES: List[List[Dict]] = [
[
{'virtualMachine': {
'name': 'prefix-cluster-service-vm-0',
'network': {
'privateIpAddresses': ['10.1.8.6'],
'publicIpAddresses': [
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-0',
'ipAddress': '20.73.105.240',
'ipAllocationMethod': 'Static',
'name': 'prefix-cluster-service-pubip-0',
'resourceGroup': 'prefix-cluster-rg',
'zone': '1'}
]
},
'resourceGroup': 'prefix-cluster-rg'}
}
],
[
{'virtualMachine': {
'name': 'prefix-cluster-service-vm-2',
'network': {
'privateIpAddresses': ['10.1.8.5'],
'publicIpAddresses': [
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2',
'ipAddress': '20.73.105.18',
'ipAllocationMethod': 'Static',
'name': 'prefix-cluster-service-pubip-2',
'resourceGroup': 'prefix-cluster-rg',
'zone': '1'}
]
},
'resourceGroup': 'prefix-cluster-rg'}
}
],
[
{'virtualMachine': {
'name': 'prefix-cluster-service-vm-1',
'network': {
'privateIpAddresses': ['10.1.8.4'],
'publicIpAddresses': [
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2',
'ipAddress': '20.73.105.188',
'ipAllocationMethod': 'Static',
'name': 'prefix-cluster-service-pubip-1',
'resourceGroup': 'prefix-cluster-rg',
'zone': '1'}
]
},
'resourceGroup': 'prefix-cluster-rg'}
}
],
[
{'virtualMachine': {
'name': 'prefix-cluster-service-vm-4',
'network': {
'privateIpAddresses': ['10.1.8.3'],
'publicIpAddresses': [
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2',
'ipAddress': '20.73.105.54',
'ipAllocationMethod': 'Static',
'name': 'prefix-cluster-service-pubip-4',
'resourceGroup': 'prefix-cluster-rg',
'zone': '1'}
]
},
'resourceGroup': 'prefix-cluster-rg'}
}
],
[
{'virtualMachine': {
'name': 'prefix-cluster-service-vm-3',
'network': {
'privateIpAddresses': ['10.1.8.2'],
'publicIpAddresses': [
{'id': '/subscriptions/subscription_hash/resourceGroups/prefix-cluster-rg/providers/Microsoft.Network/publicIPAddresses/prefix-cluster-service-pubip-2',
'ipAddress': '20.73.105.33',
'ipAllocationMethod': 'Static',
'name': 'prefix-cluster-service-pubip-3',
'resourceGroup': 'prefix-cluster-rg',
'zone': '1'}
]
},
'resourceGroup': 'prefix-cluster-rg'}
}
]
]
35 changes: 35 additions & 0 deletions tests/unit/models/test_AnsibleHostModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import List
from unittest import TestCase

from cli.models.AnsibleHostModel import AnsibleOrderedHostModel


class AnsibleHostModelTest(TestCase):

def setUp(self):
self.maxDiff = None

def test_sort(self):
"""
Test the `less` operator
"""

EXPECTED_HOSTS: List[AnsibleOrderedHostModel] = [
AnsibleOrderedHostModel('prefix-cluster-service-vm-0', '20.82.14.10'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-1', '20.82.14.34'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-2', '20.82.14.101'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-3', '20.82.14.67'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-4', '20.82.14.11'),
]

unordered_hosts: List[AnsibleOrderedHostModel] = [
AnsibleOrderedHostModel('prefix-cluster-service-vm-4', '20.82.14.11'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-1', '20.82.14.34'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-3', '20.82.14.67'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-0', '20.82.14.10'),
AnsibleOrderedHostModel('prefix-cluster-service-vm-2', '20.82.14.101')
]

unordered_hosts.sort()

self.assertListEqual(EXPECTED_HOSTS, unordered_hosts)

0 comments on commit 3368de5

Please sign in to comment.