Bottlenecks stack config parser.
[bottlenecks.git] / utils / parser.py
index c871585..7b1f4e2 100644 (file)
@@ -82,3 +82,204 @@ class Parser():
         # TO-DO add cli parameters to stack_config.
         return test_cfg, stack_cfg
 
+
+class HeatTemplate_Parser():
+    """parser a Heat template and a method to deploy template to a stack"""
+
+    def __init__(self):
+        self.heat_date = {}
+        self.heat_date["resources"] = {}
+        self.heat_date["outputs"] = {}
+        self.heat_date["heat_template_version"] = "2013-05-23"
+        self.heat_date["description"] = {"Stack built by the bottlenecks"
+                                         " framework for root."}
+
+    def add_security_group(self, name):
+        """add to the template a Neutron SecurityGroup"""
+        security_name = name + "-security_group"
+        self.heat_date["resources"][security_name] = {
+            "type": "OS::Neutron::SecurityGroup",
+            "properties": {
+                "name": security_name,
+                "description": "Group allowing icmp and upd/tcp on all ports",
+                "rules": [
+                    {"remote_ip_prefix": "0.0.0.0/0",
+                     "protocol": "tcp",
+                     "port_range_min": "1",
+                     "port_range_max": "65535"},
+                    {"remote_ip_prefix": "0.0.0.0/0",
+                     "protocol": "udp",
+                     "port_range_min": "1",
+                     "port_range_max": "65535"},
+                    {"remote_ip_prefix": "0.0.0.0/0",
+                     "protocol": "icmp"}
+                ]
+            }
+        }
+
+        self.heat_date["outputs"][security_name] = {
+            "description": "ID of Security Group",
+            "value": {"get_resource": security_name}
+        }
+
+    def add_keypair(self, name):
+        """add to the template a Nova KeyPair"""
+        key_name = name + "key"
+        with open(Parser.root_dir +
+                  "utils/infra_setup/"
+                  "bottlenecks_key/bottlenecks_key.pub") as f:
+            key_content = f.read()
+            self.heat_date["resources"][key_name] = {
+                "type": "OS::Nova::KeyPair",
+                "properties": {
+                    "name": key_name,
+                    "public_key": key_content
+                }
+            }
+
+    def add_network(self, name):
+        """add to the template a Neutron Net"""
+        network_name = name + "-net"
+        self.heat_date["resources"][network_name] = {
+            "type": "OS::Neutron::Net",
+            "properties": {"name": network_name}
+        }
+
+    def add_subnet(self, name, cidr):
+        """add to the template a Neutron Subnet"""
+        network_name = name + "-net"
+        subnet_name = name + "-subnet"
+        self.heat_date["resources"][subnet_name] = {
+            "type": "OS::Neutron::Subnet",
+            "depends_on": network_name,
+            "properties": {
+                "name": subnet_name,
+                "cidr": cidr,
+                "network_id": {"get_resource": network_name}
+            }
+        }
+
+        self.heat_date["outputs"][subnet_name] = {
+            "description": "subnet %s ID" % subnet_name,
+            "value": {"get_resource": subnet_name}
+        }
+
+    def add_router(self, name, ext_gw_net):
+        """add to the template a Neutron Router and interface"""
+        router_name = name + "-route"
+        subnet_name = name + "-subnet"
+
+        self.heat_date["resources"][router_name] = {
+            "type": "OS::Neutron::Router",
+            "depends_on": [subnet_name],
+            "properties": {
+                "name": router_name,
+                "external_gateway_info": {
+                    "network": ext_gw_net
+                }
+            }
+        }
+
+    def add_router_interface(self, name):
+        """add to the template a Neutron RouterInterface and interface"""
+        router_name = name + "-route"
+        subnet_name = name + "-subnet"
+        router_if_name = name + "-interface"
+
+        self.heat_date["resources"][router_if_name] = {
+            "type": "OS::Neutron::RouterInterface",
+            "depends_on": [router_name, subnet_name],
+            "properties": {
+                "router_id": {"get_resource": router_name},
+                "subnet_id": {"get_resource": subnet_name}
+            }
+        }
+
+    def add_server(self, name, image, flavor, user, ports=None):
+        """add to the template a Nova Server"""
+
+        key_name = "bottlenecks-poscakey"
+        port_name = name + "-port"
+        self.heat_date["resources"][name] = {
+            "type": "OS::Nova::Server",
+            "properties": {
+                "name": name,
+                "flavor": flavor,
+                "image": image,
+                "key_name": {"get_resource": key_name},
+                "admin_user": user,
+                "networks": [{
+                    "port": {"get_resource": port_name}
+                }]
+            }
+        }
+
+        self.heat_date["outputs"][name] = {
+            "description": "VM UUID",
+            "value": {"get_resource": name}
+        }
+
+    def add_port(self, name, stack_name=None):
+        """add to the template a named Neutron Port"""
+        network_name = stack_name + "-net"
+        subnet_name = stack_name + "-subnet"
+        port_name = name + "-port"
+        security_name = stack_name + "-security_group"
+
+        self.heat_date["resources"][port_name] = {
+            "type": "OS::Neutron::Port",
+            "depends_on": [subnet_name],
+            "properties": {
+                "name": port_name,
+                "fixed_ips": [{"subnet": {"get_resource": subnet_name}}],
+                "network_id": {"get_resource": network_name},
+                "replacement_policy": "AUTO",
+                "security_groups": [{"get_resource": security_name}]
+            }
+        }
+
+        self.heat_date["outputs"][port_name] = {
+            "description": "Address for interface %s" % port_name,
+            "value": {"get_attr": [port_name, "fixed_ips", 0, "ip_address"]}
+        }
+
+    def add_floating_ip(self, name, stack_name, network_ext):
+        """add to the template a Nova FloatingIP resource
+        see: https://bugs.launchpad.net/heat/+bug/1299259
+        """
+        port_name = name + "-port"
+        floating_ip_name = name + "-floating"
+        router_if_name = stack_name + "-interface"
+
+        self.heat_date["resources"][floating_ip_name] = {
+            "depends_on": [router_if_name, port_name],
+            "type": "OS::Nova::FloatingIP",
+            "properties": {
+                "pool": network_ext,
+            }
+        }
+        self.heat_date['outputs'][floating_ip_name] = {
+            'description': 'floating ip %s' % name,
+            'value': {'get_attr': [name, 'ip']}
+        }
+
+    def add_floating_ip_ass(self, name):
+        """add to the template a Nova FloatingIP resource
+        see: https://bugs.launchpad.net/heat/+bug/1299259
+        """
+        port_name = name + "-port"
+        floating_ip_name = name + "-floating"
+        floating_ass = name + "-floating_ass"
+
+        self.heat_date["resources"][floating_ass] = {
+            "type": 'OS::Neutron::FloatingIPAssociation',
+            "depends_on": [port_name, floating_ip_name],
+            "properties": {
+                "floatingip_id": {'get_resource': floating_ip_name},
+                "port_id": {"get_resource": port_name}
+            }
+        }
+
+    def get_template_date(self):
+        return self.heat_date
+