Beispiel #1
0
class SubnetHandler:

    def __init__(self):
        self.db = ProviderDb()
        self.SOUTHBOUND_DIRECTORY = 'overlay_south'

    def get_all_subnets(self, args) -> list:
        customer_id = args["customerId"]

        vpc_id_list = self.db.get_all_vpcs(customer_id)
        ret_list = []

        for vpc_id in vpc_id_list:
            vpc = self.db.get_vpc_by_id(vpc_id, customer_id)
            subnets = self.db.get_all_subnets(vpc_id)
            sub_list = []

            for subnet in subnets:
                s = {}
                s["subnetId"] = subnet.subnetId
                s["vpcId"] = subnet.vpcId
                s["ipAddress"] = subnet.ipAddress
                s["subnetName"] = subnet.subnetName

                sub_list.append(s)

            ret_list.append(
                {"vpcId": vpc_id, "vpcName": vpc.vpcName, "host": vpc.host, "subnets": sub_list})

        return {"message": ret_list, "success": True}

    def create_subnet(self, args):
        global bgp_vpc_map
        customer_id = args["customerId"]
        vpc_id = args["vpcId"]
        subnet_str = args["subnet"]

        if (self.db.get_customer_by_id(customer_id) == None):
            return {"message": "No customer found with id '{0}'".format(customer_id), "success": False}

        # get vpc
        vpc = self.db.get_vpc_by_id(vpc_id, customer_id)
        if(vpc == None):
            return {"message": "No vpc found with id '{0}' for customer '{1}'".format(vpc_id, customer_id), "success": False}

        # convert str to dict
        subnet_str = subnet_str.replace("\'", "\"")
        subnet = json.loads(subnet_str)
        ip_address = subnet["ipAddress"]
        subnet_name = subnet["subnetName"]

        # verify valid ip NETWORK address
        ip_ok, ip_error = self.ensure_valid_ip_network(ip_address)
        if not ip_ok and ip_error == None:
            return {"message": "Invalid ip given! Fix this --> '{0}' ".format(ip_address), "success": False}
        if not ip_ok and ip_error != None:
            return {"message": "Invalid ip NETWORK address given! The network address is: '{0}'! Fix this --> '{1}' ".format(ip_error, ip_address), "success": False}

        # check db to see if subnet exists
        if(self.db.get_subnet(ip_address, vpc_id) != None):
            return {
                "message": "subnet '{0}' for vpc {1} (id '{2}') already exists".format(
                    ip_address, vpc.vpcName, vpc_id),
                "success": False
            }
        else:
            # save in db if successful
            if self.create_physically(ip_address, vpc_id, vpc.host, vpc.scaleFactor):
                subnetId = self.create_in_db(vpc_id, ip_address, subnet_name)
                # restart bgp service in the edges
                bgp_configurations = bgp_vpc_map[vpc_id]
                # for all configs, update it with new network and restart vpc
                for i in range(len(bgp_configurations)):
                    config = bgp_configurations[i]
                    config.autonomus_systems[0].add_network(ip_address)
                    filename = "edge-%s-%s.conf" % (vpc_id, i)
                    config.write_config_to(filename)
                
                # restart the edge
                for i in vpc.scaleFactor:
                    ns = "V%sE%s" % (vpc_id, i)
class CustomerHandler:
    def __init__(self):
        self.SOUTHBOUND_DIRECTORY = 'overlay_south'
        self.db = ProviderDb()

    def create_customer(self, args):
        customerName = args["customerName"]

        db_result = self.create_in_db(customerName)
        return db_result

    def create_in_db(self, customer_name):
        # check db to see if vpc exists...
        if (self.db.get_customer(customer_name) != None):
            return {
                "message":
                "customer '{0}' already exists".format(customer_name),
                "success": False
            }

        # create customer in db
        customerId_str = self.db.create_customer(customer_name)
        if (customerId_str != None):
            return {
                "message":
                "customer created. Id is '{0}'".format(customerId_str),
                "success": True
            }
        else:
            return {
                "message":
                "failed to create customer '{0}'".format(customer_name),
                "success": False
            }

    def get_all_customers(self):
        # No args required...
        return self.db.get_all_customers()

    def delete_customer(self, args):
        customer_name = args["customerName"]
        customer = self.db.get_customer(customer_name)

        if (customer == None):
            return {
                "message":
                "No customer with name '{0}' found".format(customer_name),
                "success":
                False
            }

        errors = {}

        # This should delete VPCs, Subnets, VMs, the whole works!
        # get ALL the vpc for customer

        vpc_list = self.db.get_all_vpcs(customer.customerId)
        if len(vpc_list) > 0:
            return {
                "message":
                "Please delete these VPCs first! --> '{0}'".format(vpc_list),
                "success":
                False
            }

        # delete customer
        if not self.db.delete_customer(customer.customerId):
            mess = {
                "message":
                "Failed to delete customer with id '{0}'".format(
                    customer.customerId)
            }
            errors.append(mess)

        if len(errors) == 0:
            return {
                "message":
                "Successfully deleted customer '{0}'".format(
                    customer.customerName),
                "success":
                True,
                "notice":
                "Customer '{0}' does not exist anymore!".format(
                    customer.customerName)
            }
        else:
            return {
                "message":
                "Failed to delete ALL of customer '{0}'".format(
                    customer.customerName),
                "success":
                False,
                "errors":
                errors
            }
Beispiel #3
0
class VPCHandler():
    def __init__(self):
        self.db = ProviderDb()
        self.SOUTHBOUND_DIRECTORY = 'overlay_south'

        self.bgpConfig = []

    def get_vpc_by_hosts(self, args):
        customerId = args["customerId"]
        print(args)
        print("\r\nThe customer Id is: {0}\r\n".format(customerId))
        output = {}
        vpc_list = self.db.get_all_vpcs(customerId)
        print("\r\n THe vpc_list is: {0}\r\n".format(vpc_list))
        output["message"] = "The list of VPC IDs are: '{0}'".format(vpc_list)
        output["success"] = True
        return output

    def create_vpc(self, args):
        vpcName = args["vpcName"]
        customerId = args["customerId"]
        host = args["host"]
        scaleFactor = args["scaleFactor"]

        if (self.db.get_customer_by_id(customerId) == None):
            return {
                "message":
                "No customer found with id '{0}'".format(customerId),
                "success": False
            }

        if (self.db.get_vpc(vpcName, customerId, host) != None):
            return {
                "message":
                "vpc '{0}' already exists for host '{1}'".format(
                    vpcName, host),
                "success":
                False
            }

        vpc_id = self.db.get_next_vpc_id()
        if vpc_id < 0:
            return {
                "message":
                "A SQL error occured in creating vpc '{0}'".format(vpcName),
                "success":
                False
            }

        self.bgp_config = [cfg.config_utils()] * scaleFactor
        for i in range(scaleFactor):
            config_obj = cfg.config_utils()
            config_as = cfg.AS(int("1{1}{2}".format(vpc_id, i)))
            config_obj.add_as(config_as)
            self.bgp_config.append(config_obj)
            config_obj.write_config_to("edge-{1}-{2}.conf".format(vpc_id, i))

        if self.create_physically(vpc_id, host, scaleFactor):
            # save in db if successful
            db_result = self.create_in_db(vpcName, customerId, host,
                                          scaleFactor)
            return db_result

        # TODO: Remove this below!
        # db_result = self.create_in_db(
        #     vpcName, customerId, host, scaleFactor)
        # return db_result

        return {
            "message": "failed to create vpc {0}".format(vpcName),
            "success": False
        }

    def create_in_db(self, vpcName, customerId, host, scaleFactor):
        # create vpc in db
        vpcId_str = self.db.create_vpc(vpcName, customerId, host, scaleFactor)
        if (vpcId_str != None):
            return {
                "message": "vpc created. Id is '{0}'".format(vpcId_str),
                "success": True
            }
        else:
            return {
                "message": "failed to create vpc '{0}'".format(vpcName),
                "success": False
            }

    def delete_vpc(self, args):
        vpc_id = args["vpcId"]
        customer_id = args["customerId"]

        # ensure customer is valid
        if (self.db.get_customer_by_id(customer_id) == None):
            return {
                "message":
                "No customer found with id '{0}'".format(customer_id),
                "success": False
            }

        # get vpc id
        vpc = self.db.get_vpc_by_id(vpc_id, customer_id)
        if (vpc == None):
            return {
                "message":
                "No vpc found with id '{0}' for customer '{1}'".format(
                    vpc_id, customer_id),
                "success":
                False
            }

        # get a list of subnets
        subnet_ip_list = self.db.get_all_subnets_ip(vpc_id)

        if len(subnet_ip_list) > 0:
            return {
                "message":
                "Please delete these subnets from vpc '{0}'".format(vpc_id),
                "ipList":
                subnet_ip_list,
                "success":
                False
            }
        else:
            # delete vpc
            vpc_del_ok = self.delete_physically(
                vpc.vpcId, vpc.scaleFactor, vpc.host) and self.db.delete_vpc(
                    vpc.vpcId)

            if not vpc_del_ok:
                return {
                    "message":
                    "Failed to delete vpc '{0}'. Does it exist?".format(
                        vpc_id),
                    "success":
                    False
                }
            else:
                return {
                    "message":
                    "Successfully deleted vpc '{0}'.".format(vpc_id),
                    "success": True
                }

    # might have to change the names of the south -bound scripts

    def create_physically(self, vpc_id, host, scale_factor):
        scale_factor = int(scale_factor)
        for i in range(scale_factor):
            json_obj = {}
            json_obj["creation_vars"] = [{"vpc_id": vpc_id, "edge_number": i}]
            vars_json = json.dumps(json_obj)
            commands = [
                "sudo", "ansible-playbook", "create-edges.yml", "-i",
                "hosts.ini", "-e", vars_json, "-l {0}".format(host.lower())
            ]
            p = subprocess.Popen(commands, cwd=self.SOUTHBOUND_DIRECTORY)
            p.wait()

            if p.returncode != 0:
                print(
                    "\r\nUnable to run ansible 'create-edges.yml' in vpc_handler.create_physically\r\n"
                )
                return False

            other_host = 'host2' if host == 'Host1' else 'host1'

            commands = [
                "sudo", "ansible-playbook", "edge-route-to-host.yml", "-i",
                "hosts.ini", "-e", vars_json, "-l {0}".format(other_host)
            ]
            p = subprocess.Popen(commands, cwd=self.SOUTHBOUND_DIRECTORY)
            p.wait()

            if p.returncode != 0:
                print(
                    "\r\nUnable to run ansible 'edge-route-to-host.yml' in vpc_handler.create_physically\r\n"
                )
                return False

        return True

    def delete_physically(self, vpc_id, scaleFactor, host):
        for i in range(scaleFactor):
            json_obj = {}
            json_obj["creation_vars"] = [{"vpc_id": vpc_id, "edge_number": i}]
            vars_json = json.dumps(json_obj)
            commands = [
                "sudo", "ansible-playbook", "delete-edges.yml", "-i",
                "hosts.ini", "-e", vars_json, "-l {0}".format(host.lower())
            ]
            p = subprocess.Popen(commands, cwd=self.SOUTHBOUND_DIRECTORY)
            p.wait()

            if p.returncode != 0:
                print(
                    "\r\nUnable to run ansible 'delete-edges.yml' in vpc_handler.delete_physically\r\n"
                )
                return False

            other_host = 'host2' if host == 'Host1' else 'host1'

            commands = [
                "sudo", "ansible-playbook", "delete-routes-to-edges.yml", "-i",
                "hosts.ini", "-e", vars_json, "-l {0}".format(other_host)
            ]
            p = subprocess.Popen(commands, cwd=self.SOUTHBOUND_DIRECTORY)
            p.wait()

            if p.returncode != 0:
                print(
                    "\r\nUnable to run ansible 'delete-routes-to-edges.yml' in vpc_handler.delete_physically\r\n"
                )
                return False

        return True
class SubnetHandler:

    def __init__(self):
        self.db = ProviderDb()
        self.SOUTHBOUND_DIRECTORY = 'overlay_south'

    def get_all_subnets(self, args) -> list:
        customer_id = args["customerId"]

        vpc_id_list = self.db.get_all_vpcs(customer_id)
        ret_list = []

        for vpc_id in vpc_id_list:
            vpc = self.db.get_vpc_by_id(vpc_id, customer_id)
            subnets = self.db.get_all_subnets(vpc_id)
            sub_list = []

            for subnet in subnets:
                s = {}
                s["subnetId"] = subnet.subnetId
                s["vpcId"] = subnet.vpcId
                s["ipAddress"] = subnet.ipAddress
                s["subnetName"] = subnet.subnetName

                sub_list.append(s)

            ret_list.append(
                {"vpcId": vpc_id, "vpcName": vpc.vpcName, "host": vpc.host, "subnets": sub_list})

        return {"message": ret_list, "success": True}

    def create_subnet(self, args):
        customer_id = args["customerId"]
        vpc_id = args["vpcId"]
        subnet_str = args["subnet"]

        if (self.db.get_customer_by_id(customer_id) == None):
            return {"message": "No customer found with id '{0}'".format(customer_id), "success": False}

        # get vpc
        vpc = self.db.get_vpc_by_id(vpc_id, customer_id)
        if(vpc == None):
            return {"message": "No vpc found with id '{0}' for customer '{1}'".format(vpc_id, customer_id), "success": False}

        # convert str to dict
        subnet_str = subnet_str.replace("\'", "\"")
        subnet = json.loads(subnet_str)
        ip_address = subnet["ipAddress"]
        subnet_name = subnet["subnetName"]

        # verify valid ip NETWORK address
        ip_ok, ip_error = self.ensure_valid_ip_network(ip_address)
        if not ip_ok and ip_error == None:
            return {"message": "Invalid ip given! Fix this --> '{0}' ".format(ip_address), "success": False}
        if not ip_ok and ip_error != None:
            return {"message": "Invalid ip NETWORK address given! The network address is: '{0}'! Fix this --> '{1}' ".format(ip_error, ip_address), "success": False}

        # check db to see if subnet exists
        if(self.db.get_subnet(ip_address, vpc_id) != None):
            return {
                "message": "subnet '{0}' for vpc {1} (id '{2}') already exists".format(
                    ip_address, vpc.vpcName, vpc_id),
                "success": False
            }
        else:
            # save in db if successful
            if self.create_physically(ip_address, vpc_id, vpc.host, vpc.scaleFactor):
                subnetId = self.create_in_db(vpc_id, ip_address, subnet_name)
                return {
                    "subnet_id":subnetId,
                    "message": "Successfully created a physical subnet '{0}'for vpc '{1}' in host '{2}'".format(ip_address, vpc_id, vpc.host),
                    #"dbMessage": db_result,
                    "success": True
                }
            else:
                return {
                    "message": "Unable to physically create subnet '{0}' for vpc {1} (id is {2})".format(ip_address, vpc.vpcName, vpc_id),
                    "success": False
                }

    def ensure_valid_ip_network(self, subnet_str):
        try:
            valid_ip_network = ipaddress.IPv4Interface(subnet_str).network
            print(valid_ip_network)
            print(subnet_str)
            if str(valid_ip_network) != subnet_str:
                return False, valid_ip_network
            else:
                return True, None
        except Exception as e:
            traceback.print_exception(type(e), e, e.__traceback__)
            return False, None

    def create_physically(self, ip_address, vpc_id, host, scaleFactor):
        result = False
        host = host.lower()

        subnet_id = self.db.get_next_subnet_id()
        if subnet_id < 0:
            return {"message": "A SQL error occured in creating subnet '{0}'".format(ip_address), "success": False}

        try:
            json_obj = {}
            json_obj["subnet_id"] = subnet_id
            json_obj["vpc_id"] = vpc_id
            json_obj["edge_count"] = scaleFactor
            
            if ip_address:  
                json_obj["ip_address"] = ip_address

            vars_json = json.dumps(json_obj)
            commands = [
                "sudo",
                "ansible-playbook",
                "-i",
                "hosts.ini",
                "create-subnet.yml",
                "-l {0}".format(host),
                "-e",
                vars_json
            ]
            p = subprocess.Popen(commands, cwd=self.SOUTHBOUND_DIRECTORY)
            p.wait()
            # print("stderr", stderr)
            # print("retcode =", p.returncode)
            if p.returncode != 0:
                print(
                    "\r\nUnable to run ansible 'create-subnet.yml' for subnet '{0}' in VPC '{1}'\r\n".format(ip_address, vpc_id))
                return False

            result = True
        except Exception as e:
            print(
                "\r\nUnable to physically create the subnet '{0}' for vpc id '{1}'\r\nException below:\r\n{2}".format(ip_address, vpc_id, e))
            result = False

        return result

    def create_in_db(self, vpc_id, ip_address, subnet_name) -> int:

        # create vpc in db
        subnetId_str = self.db.create_subnet(ip_address, subnet_name, vpc_id)
        if(subnetId_str != None):
            return int(subnetId_str)
        else:
            return -1

    def delete_subnet(self, args):
        customer_id = args["customerId"]
        vpc_id = args["vpcId"]
        subnet_id = args["subnetId"]

        # ensure valid customer
        if (self.db.get_customer_by_id(customer_id) == None):
            return {"message": "No customer found with id '{0}'".format(customer_id), "success": False}

        # ensure valid vpc
        vpc = self.db.get_vpc_by_id(vpc_id, customer_id)
        if(vpc == None):
            return {"message": "No vpc found with id '{0}' for customer '{1}'".format(vpc_id, customer_id), "success": False}

        # get subnet
        subnet = self.db.get_subnet_by_id(subnet_id, vpc_id)
        if subnet == None:
            return {"message": "No subnet found with id '{0}' for vpc '{1}'".format(subnet_id, vpc_id), "success": False}

        # Check for no VMs ...and then delete subnets
        vm_list = self.db.get_all_vms(subnet_id)
        if len(vm_list) > 0:
            return {
                "vmList": vm_list,
                "message": "Please delete these VMs (id list given) first!",
                "success": False
            }

        # ...NO VMs were found...

        # delete subnet physically
        phy_result = self.delete_physically(subnet.subnetId, vpc_id, vpc.host)

        # save in db if successful
        if phy_result:
            return self.delete_in_db(subnet_id, subnet.ipAddress, vpc_id, vpc.host)
        else:
            return {
                "message": "Unable to physically delete subnet '{0}' for vpc '{1}' in host '{2}'".format(subnet.ipAddress, vpc_id, vpc.host),
                "success": False
            }

    def delete_physically(self, subnet_id, vpc_id, host):
        # call southbound to delete subnet
        result = False
        host = host.lower()

        try:
            json_obj = {}
            json_obj["subnet_id"] = subnet_id
            json_obj["vpc_id"] = vpc_id

            vars_json = json.dumps(json_obj)
            commands = [
                "sudo",
                "ansible-playbook",
                "-i",
                "hosts.ini",
                "delete-subnet.yml",
                "-l {0}".format(host),
                "-e",
                vars_json
            ]
            p = subprocess.Popen(commands, cwd=self.SOUTHBOUND_DIRECTORY)
            p.wait()
            # print("stderr", stderr)
            # print("retcode =", p.returncode)
            if p.returncode != 0:
                print(
                    "\r\nUnable to run ansible 'delete-subnet.yml' for subnet '{0}' in VPC '{1}'\r\n".format(subnet_id, vpc_id))
                return False

            result = True
        except Exception as e:
            print(
                "\r\nUnable to physically create the subnet '{0}' for vpc id '{1}'\r\nException below:\r\n{2}".format(subnet_id, vpc_id, e))
            result = False

        return result

    def delete_in_db(self, subnet_id, ip_address, vpc_id, host):
        # delete subnet in db
        delete_ok = self.db.delete_subnet(subnet_id)
        if(delete_ok):
            return {
                "message": "subnet '{0}' is deleted from host '{1}'".format(
                    ip_address, host),
                "success": True
            }
        else:
            return {
                "message": "failed to delete subnet '{0}' from host '{1}'".format(
                    ip_address, host),
                "success": False
            }