Refactor the TemplateGenerator class

This commit is contained in:
George Peristerakis 2014-09-11 15:32:02 -04:00
parent 4b2158401e
commit ddc2c802f1
7 changed files with 537 additions and 459 deletions

View File

@ -22,11 +22,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from flameclient.flame import TemplateGenerator
from flameclient.flame import TemplateGenerator # noqa
class Client(object):
def __init__(self, version, **kwargs):
def __init__(self, api_version, **kwargs):
username = kwargs.get('username')
password = kwargs.get('password')
@ -34,6 +34,11 @@ class Client(object):
auth_url = kwargs.get('auth_url')
insecure = kwargs.get('insecure')
self.template_factory = TemplateGenerator(username, password,
tenant_name, auth_url,
insecure)
self.template_generator = TemplateGenerator(username, password,
tenant_name, auth_url,
insecure)
def generate(self, include_networks, include_instances, include_volumes):
return self.template_generator.generate(include_networks,
include_instances,
include_volumes)

View File

@ -22,6 +22,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import print_function
import argparse
import os
@ -65,9 +67,13 @@ def main(args=None):
"stack data file.")
args = parser.parse_args()
arguments = (args.username, args.password, args.project, args.auth_url,
args.insecure)
TemplateGenerator(args.exclude_servers,
args.exclude_volumes,
args.generate_stack_data,
*arguments).run()
template = TemplateGenerator(args.username, args.password, args.project,
args.auth_url, args.insecure)
template.extract_server_details(args.exclude_servers, args.exclude_volumes,
args.generate_stack_data)
template.extract_data()
print("### Heat Template ###")
print(template.heat_template())
if args.generate_stack_data:
print("### Stack Data ###")
print(template.stack_data())

View File

@ -23,7 +23,6 @@
# SOFTWARE.
import logging
import sys
import ipaddr
import yaml
@ -48,15 +47,38 @@ resources:
class TemplateGenerator(object):
neutron_manager = managers.NeutronManager
nova_manager = managers.NovaManager
cinder_manager = managers.CinderManager
template = None
stack_data = None
def _setup_managers(self, arguments):
self.neutron = self.neutron_manager(*arguments)
def __init__(self, username, password, tenant_name, auth_url, insecure):
self.generate_data = False
self._setup_templates()
self._setup_managers(username, password, tenant_name, auth_url,
insecure)
def _setup_templates(self):
self.template = yaml.load(template_skeleton)
self.template['resources'] = {}
self.template['parameters'] = {}
self.stack_data = yaml.load(stack_data_skeleton)
self.stack_data['resources'] = {}
def _setup_managers(self, username, password, tenant_name, auth_url,
insecure):
self.neutron = managers.NeutronManager(username, password, tenant_name,
auth_url, insecure)
self.nova = managers.NovaManager(username, password, tenant_name,
auth_url, insecure)
self.cinder = managers.CinderManager(username, password, tenant_name,
auth_url, insecure)
def extract_vm_details(self, exclude_servers, exclude_volumes,
generate_data):
self.exclude_servers = exclude_servers
self.exclude_volumes = exclude_volumes
self.generate_data = generate_data
self.subnets = self.build_data(self.neutron.subnet_list())
self.networks = self.build_data(self.neutron.network_list())
self.routers = self.neutron.router_list()
@ -65,38 +87,19 @@ class TemplateGenerator(object):
self.ports = self.build_data(self.neutron.port_list())
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:
if not exclude_servers:
self.flavors = self.build_data(self.nova.flavor_list())
self.servers = self.build_data(self.nova.server_list())
if (not self.exclude_volumes or
(self.exclude_volumes and not self.exclude_servers)):
self.cinder = self.cinder_manager(*arguments)
if (not exclude_volumes or
(exclude_volumes and not exclude_servers)):
self.volumes = self.build_data(self.cinder.volume_list())
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):
def build_data(self, data):
if not data:
return {}
@ -600,9 +603,8 @@ class TemplateGenerator(object):
if not self.exclude_volumes:
self._extract_volumes()
def run(self):
self.extract_data()
self.print_generated(self.template, "Heat Template")
def heat_template(self):
return self.print_generated(self.template)
if self.generate_data:
self.print_generated(self.stack_data, "Heat Stack Data")
def stack_data(self):
return self.print_generated(self.stack_data)

View File

@ -30,6 +30,7 @@ from novaclient.v1_1 import client as nova_client
class KeystoneManager(object):
"""Manages Keystone queries."""
client = None
def __init__(self, username, password, project, auth_url, insecure):
self.client = keystone_client.Client(

View File

@ -50,4 +50,4 @@ class TestCase(testtools.TestCase):
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
self.log_fixture = self.useFixture(fixtures.FakeLogger())
self.log_fixture = self.useFixture(fixtures.FakeLogger())

File diff suppressed because it is too large Load Diff

View File

@ -2,10 +2,11 @@ hacking>=0.9.2,<0.10
coverage>=3.6
discover
mock>=1.0
fixtures>=0.3.14
python-subunit
sphinx>=1.1.2
oslosphinx
testrepository>=0.0.18
testscenarios>=0.4
testtools>=0.9.34
testtools>=0.9.34