Refactor the TemplateGenerator class
This commit is contained in:
parent
4348089f90
commit
4b2158401e
@ -22,6 +22,18 @@
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
|
||||
def template_configuration(allow_insecure=False, exclude_servers=False,
|
||||
exclude_volumes=False, generate_stack_data=False):
|
||||
raise NotImplementedError()
|
||||
from flameclient.flame import TemplateGenerator
|
||||
|
||||
|
||||
class Client(object):
|
||||
def __init__(self, version, **kwargs):
|
||||
|
||||
username = kwargs.get('username')
|
||||
password = kwargs.get('password')
|
||||
tenant_name = kwargs.get('tenant_name')
|
||||
auth_url = kwargs.get('auth_url')
|
||||
|
||||
insecure = kwargs.get('insecure')
|
||||
self.template_factory = TemplateGenerator(username, password,
|
||||
tenant_name, auth_url,
|
||||
insecure)
|
||||
|
@ -28,7 +28,7 @@ import os
|
||||
from flame import TemplateGenerator
|
||||
|
||||
|
||||
def main():
|
||||
def main(args=None):
|
||||
desc = "Heat template and data file generator"
|
||||
parser = argparse.ArgumentParser(description=desc)
|
||||
parser.add_argument("--username", type=str,
|
||||
|
@ -23,6 +23,7 @@
|
||||
# SOFTWARE.
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import ipaddr
|
||||
import yaml
|
||||
@ -30,7 +31,6 @@ import yaml
|
||||
from flameclient import managers
|
||||
|
||||
|
||||
|
||||
logging.basicConfig(level=logging.ERROR)
|
||||
|
||||
template_skeleton = '''
|
||||
@ -52,21 +52,10 @@ class TemplateGenerator(object):
|
||||
neutron_manager = managers.NeutronManager
|
||||
nova_manager = managers.NovaManager
|
||||
cinder_manager = managers.CinderManager
|
||||
template = None
|
||||
stack_data = None
|
||||
|
||||
def __init__(self, exclude_servers, exclude_volumes,
|
||||
generate_data, *arguments):
|
||||
self.exclude_servers = exclude_servers
|
||||
self.exclude_volumes = exclude_volumes
|
||||
self.generate_data = generate_data
|
||||
|
||||
self.template = yaml.load(template_skeleton)
|
||||
self.template['resources'] = {}
|
||||
self.template['parameters'] = {}
|
||||
|
||||
if self.generate_data:
|
||||
self.stack_data = yaml.load(stack_data_skeleton)
|
||||
self.stack_data['resources'] = {}
|
||||
|
||||
def _setup_managers(self, arguments):
|
||||
self.neutron = self.neutron_manager(*arguments)
|
||||
self.subnets = self.build_data(self.neutron.subnet_list())
|
||||
self.networks = self.build_data(self.neutron.network_list())
|
||||
@ -77,9 +66,8 @@ class TemplateGenerator(object):
|
||||
self.external_networks = []
|
||||
|
||||
self.nova = self.nova_manager(*arguments)
|
||||
self.keys = dict(
|
||||
(key.name, (index, key))
|
||||
for index, key in enumerate(self.nova.keypair_list()))
|
||||
self.keys = dict((key.name, (index, key)) for index, key
|
||||
in enumerate(self.nova.keypair_list()))
|
||||
|
||||
if not self.exclude_servers:
|
||||
self.flavors = self.build_data(self.nova.flavor_list())
|
||||
@ -90,7 +78,25 @@ class TemplateGenerator(object):
|
||||
self.cinder = self.cinder_manager(*arguments)
|
||||
self.volumes = self.build_data(self.cinder.volume_list())
|
||||
|
||||
def build_data(self, data):
|
||||
def setup_templates(self):
|
||||
self.template = yaml.load(template_skeleton)
|
||||
self.template['resources'] = {}
|
||||
self.template['parameters'] = {}
|
||||
if self.generate_data:
|
||||
self.stack_data = yaml.load(stack_data_skeleton)
|
||||
self.stack_data['resources'] = {}
|
||||
|
||||
def __init__(self, exclude_servers, exclude_volumes,
|
||||
generate_data, *arguments):
|
||||
self.exclude_servers = exclude_servers
|
||||
self.exclude_volumes = exclude_volumes
|
||||
self.generate_data = generate_data
|
||||
|
||||
self.setup_templates()
|
||||
self._setup_managers(arguments)
|
||||
|
||||
@staticmethod
|
||||
def build_data(data):
|
||||
if not data:
|
||||
return {}
|
||||
|
||||
@ -101,9 +107,9 @@ class TemplateGenerator(object):
|
||||
return dict((element.id, (index, element))
|
||||
for index, element in enumerate(data))
|
||||
|
||||
def print_generated(self, file, message):
|
||||
print("########## %s ##########" % message)
|
||||
print(yaml.safe_dump(file, default_flow_style=False))
|
||||
@staticmethod
|
||||
def print_generated(filename, message):
|
||||
print(yaml.safe_dump(filename, default_flow_style=False))
|
||||
|
||||
def add_resource(self, name, status, resource_id, resource_type):
|
||||
resource = {
|
||||
@ -191,7 +197,7 @@ class TemplateGenerator(object):
|
||||
}
|
||||
self.template['resources'].update(resource)
|
||||
|
||||
def extract_routers(self):
|
||||
def _extract_routers(self):
|
||||
for n, router in enumerate(self.routers):
|
||||
router_resource_name = "router_%d" % n
|
||||
resource_type = 'OS::Neutron::Router'
|
||||
@ -219,7 +225,7 @@ class TemplateGenerator(object):
|
||||
self.add_router_gateway_resource(router_resource_name,
|
||||
router)
|
||||
|
||||
def extract_networks(self):
|
||||
def _extract_networks(self):
|
||||
for n, network in self.networks.itervalues():
|
||||
if network['router:external']:
|
||||
self.external_networks.append(network['id'])
|
||||
@ -245,13 +251,13 @@ class TemplateGenerator(object):
|
||||
}
|
||||
self.template['resources'].update(resource)
|
||||
|
||||
def get_network_resource_name(self, id):
|
||||
return "network_%d" % self.networks[id][0]
|
||||
def get_network_resource_name(self, network_id):
|
||||
return "network_%d" % self.networks[network_id][0]
|
||||
|
||||
def get_subnet_resource_name(self, id):
|
||||
return "subnet_%d" % self.subnets[id][0]
|
||||
def get_subnet_resource_name(self, subnet_id):
|
||||
return "subnet_%d" % self.subnets[subnet_id][0]
|
||||
|
||||
def extract_subnets(self):
|
||||
def _extract_subnets(self):
|
||||
for n, subnet in self.subnets.itervalues():
|
||||
if subnet['network_id'] in self.external_networks:
|
||||
continue
|
||||
@ -301,7 +307,7 @@ class TemplateGenerator(object):
|
||||
brules.append(rule)
|
||||
return brules
|
||||
|
||||
def extract_secgroups(self):
|
||||
def _extract_secgroups(self):
|
||||
for n, secgroup in self.secgroups.itervalues():
|
||||
|
||||
resource_name = "security_group_%d" % n
|
||||
@ -330,7 +336,7 @@ class TemplateGenerator(object):
|
||||
}
|
||||
self.template['resources'].update(resource)
|
||||
|
||||
def extract_keys(self):
|
||||
def _extract_keys(self):
|
||||
for n, key in self.keys.itervalues():
|
||||
key_resource_name = "key_%d" % n
|
||||
resource_type = 'OS::Nova::KeyPair'
|
||||
@ -390,7 +396,7 @@ class TemplateGenerator(object):
|
||||
networks.append({'network': {'get_resource': net}})
|
||||
return networks
|
||||
|
||||
def extract_servers(self):
|
||||
def _extract_servers(self):
|
||||
for n, server in self.servers.itervalues():
|
||||
resource_name = "server_%d" % n
|
||||
resource_type = 'OS::Nova::Server'
|
||||
@ -475,7 +481,7 @@ class TemplateGenerator(object):
|
||||
}
|
||||
self.template['resources'].update(resource)
|
||||
|
||||
def extract_floating(self):
|
||||
def _extract_floating(self):
|
||||
for n, ip in enumerate(self.floatingips):
|
||||
ip_resource_name = "floatingip_%d" % n
|
||||
net_param_name = "external_network_for_floating_ip_%d" % n
|
||||
@ -523,7 +529,7 @@ class TemplateGenerator(object):
|
||||
self.template['resources'].update(resource)
|
||||
self.template['resources'].update(floating_resource)
|
||||
|
||||
def extract_volumes(self):
|
||||
def _extract_volumes(self):
|
||||
for n, volume in self.volumes.itervalues():
|
||||
resource_name = "volume_%d" % n
|
||||
resource_type = 'OS::Cinder::Volume'
|
||||
@ -582,20 +588,20 @@ class TemplateGenerator(object):
|
||||
}
|
||||
self.template['resources'].update(resource)
|
||||
|
||||
def run(self):
|
||||
self.extract_routers()
|
||||
self.extract_networks()
|
||||
self.extract_subnets()
|
||||
self.extract_secgroups()
|
||||
self.extract_floating()
|
||||
self.extract_keys()
|
||||
|
||||
def extract_data(self):
|
||||
self._extract_routers()
|
||||
self._extract_networks()
|
||||
self._extract_subnets()
|
||||
self._extract_secgroups()
|
||||
self._extract_floating()
|
||||
self._extract_keys()
|
||||
if not self.exclude_servers:
|
||||
self.extract_servers()
|
||||
|
||||
self._extract_servers()
|
||||
if not self.exclude_volumes:
|
||||
self.extract_volumes()
|
||||
self._extract_volumes()
|
||||
|
||||
def run(self):
|
||||
self.extract_data()
|
||||
self.print_generated(self.template, "Heat Template")
|
||||
|
||||
if self.generate_data:
|
||||
|
@ -166,7 +166,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_keys()
|
||||
generator._extract_keys()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_router(self):
|
||||
@ -193,7 +193,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_routers()
|
||||
generator._extract_routers()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_router_with_external_gateway(self):
|
||||
@ -233,7 +233,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
|
||||
generator.extract_routers()
|
||||
generator._extract_routers()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_router_with_ports(self):
|
||||
@ -301,7 +301,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_routers()
|
||||
generator._extract_routers()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_network(self):
|
||||
@ -331,7 +331,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_networks()
|
||||
generator._extract_networks()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_external_network(self):
|
||||
@ -351,7 +351,7 @@ class StackDataTests(base.TestCase):
|
||||
'status': 'COMPLETE',
|
||||
'resources': {}
|
||||
}
|
||||
generator.extract_networks()
|
||||
generator._extract_networks()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_subnet(self):
|
||||
@ -397,7 +397,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
|
||||
}
|
||||
generator.extract_subnets()
|
||||
generator._extract_subnets()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_floatingip(self):
|
||||
@ -428,7 +428,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
|
||||
}
|
||||
generator.extract_floating()
|
||||
generator._extract_floating()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_security_group(self):
|
||||
@ -471,7 +471,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_secgroups()
|
||||
generator._extract_secgroups()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_default_security_group(self):
|
||||
@ -504,7 +504,7 @@ class StackDataTests(base.TestCase):
|
||||
'status': 'COMPLETE',
|
||||
'resources': {}
|
||||
}
|
||||
generator.extract_secgroups()
|
||||
generator._extract_secgroups()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_volume(self):
|
||||
@ -526,7 +526,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_server(self):
|
||||
@ -548,7 +548,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
|
||||
def test_server_with_default_security_group(self):
|
||||
@ -617,7 +617,7 @@ class StackDataTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.stack_data)
|
||||
self.assertEqual(template_expected, generator.template)
|
||||
|
||||
@ -649,7 +649,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_keys()
|
||||
generator._extract_keys()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_router(self):
|
||||
@ -675,7 +675,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_routers()
|
||||
generator._extract_routers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_router_with_external_gateway(self):
|
||||
@ -719,7 +719,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_routers()
|
||||
generator._extract_routers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_router_with_ports(self):
|
||||
@ -784,7 +784,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_routers()
|
||||
generator._extract_routers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_network(self):
|
||||
@ -814,7 +814,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_networks()
|
||||
generator._extract_networks()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_external_network(self):
|
||||
@ -835,7 +835,7 @@ class NetworkTests(base.TestCase):
|
||||
'parameters': {},
|
||||
'resources': {}
|
||||
}
|
||||
generator.extract_networks()
|
||||
generator._extract_networks()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_subnet(self):
|
||||
@ -886,7 +886,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_subnets()
|
||||
generator._extract_subnets()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_floatingip(self):
|
||||
@ -922,7 +922,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_floating()
|
||||
generator._extract_floating()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_security_group(self):
|
||||
@ -1026,7 +1026,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_secgroups()
|
||||
generator._extract_secgroups()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_security_group_default(self):
|
||||
@ -1144,7 +1144,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_secgroups()
|
||||
generator._extract_secgroups()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_security_groups(self):
|
||||
@ -1345,7 +1345,7 @@ class NetworkTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_secgroups()
|
||||
generator._extract_secgroups()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
|
||||
@ -1378,7 +1378,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_source_volid_external(self):
|
||||
@ -1405,7 +1405,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_source_volid_included(self):
|
||||
@ -1436,7 +1436,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_image(self):
|
||||
@ -1478,7 +1478,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_snapshot_id(self):
|
||||
@ -1506,7 +1506,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_volume_type(self):
|
||||
@ -1534,7 +1534,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_metadata(self):
|
||||
@ -1556,7 +1556,7 @@ class VolumeTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_volumes()
|
||||
generator._extract_volumes()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
|
||||
@ -1601,7 +1601,7 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_keypair(self):
|
||||
@ -1635,7 +1635,7 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_boot_from_volume(self):
|
||||
@ -1678,7 +1678,7 @@ class ServerTests(base.TestCase):
|
||||
},
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_volume_attached(self):
|
||||
@ -1726,7 +1726,7 @@ class ServerTests(base.TestCase):
|
||||
},
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_security_groups(self):
|
||||
@ -1777,8 +1777,8 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_secgroups()
|
||||
generator.extract_servers()
|
||||
generator._extract_secgroups()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_config_drive(self):
|
||||
@ -1812,7 +1812,7 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_metadata(self):
|
||||
@ -1846,7 +1846,7 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_networks(self):
|
||||
@ -1897,7 +1897,7 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
||||
def test_excluded_volume_attached(self):
|
||||
@ -1951,5 +1951,5 @@ class ServerTests(base.TestCase):
|
||||
}
|
||||
}
|
||||
}
|
||||
generator.extract_servers()
|
||||
generator._extract_servers()
|
||||
self.assertEqual(expected, generator.template)
|
||||
|
16
setup.cfg
16
setup.cfg
@ -3,9 +3,9 @@ name = python-flameclient
|
||||
summary = Automatic Heat template generation
|
||||
description-file =
|
||||
README.rst
|
||||
author = OpenStack
|
||||
author-email = openstack-dev@lists.openstack.org
|
||||
home-page = http://www.openstack.org/
|
||||
author = CloudWatt
|
||||
author-email = info@cloudwatt.com
|
||||
home-page = http://www.cloudwatt.com/
|
||||
classifier =
|
||||
Environment :: OpenStack
|
||||
Intended Audience :: Information Technology
|
||||
@ -16,8 +16,6 @@ classifier =
|
||||
Programming Language :: Python :: 2
|
||||
Programming Language :: Python :: 2.7
|
||||
Programming Language :: Python :: 2.6
|
||||
Programming Language :: Python :: 3
|
||||
Programming Language :: Python :: 3.3
|
||||
|
||||
[files]
|
||||
packages =
|
||||
@ -27,6 +25,10 @@ packages =
|
||||
setup-hooks =
|
||||
pbr.hooks.setup_hook
|
||||
|
||||
[entry_points]
|
||||
console_scripts =
|
||||
flame = flameclient.cmd:main
|
||||
|
||||
[build_sphinx]
|
||||
source-dir = doc/source
|
||||
build-dir = doc/build
|
||||
@ -49,9 +51,5 @@ keywords = _ gettext ngettext l_ lazy_gettext
|
||||
mapping_file = babel.cfg
|
||||
output_file = flameclient/locale/python-flameclient.pot
|
||||
|
||||
[entry_points]
|
||||
console_scripts =
|
||||
flame = flameclient.cmd:main
|
||||
|
||||
[wheel]
|
||||
universal = 1
|
Loading…
Reference in New Issue
Block a user