예제 #1
0
    def get_image_by_name(self, image_name, create_date=None):
        """
        Get image on the basis of image name.
        :param image_name: Name of the image.
        :param create_date: Creation Date of image
        :return: Return the filtered image on the basis of above params.
        """
        try:
            image_manager = ImageManager(self.client)
            images = self.retry.call(image_manager.list_private_images,
                                     name=image_name)
            if not images:
                return
            if not create_date or len(images) == 1:
                return images[0]
            else:
                try:
                    time_list = [
                        abs((parser.parse(image["createDate"]) -
                             parser.parse(create_date)).total_seconds())
                        for image in images
                    ]
                    minimum_index = time_list.index(min(time_list))
                    return images[minimum_index]
                except (parser.ParserError, KeyError, IndexError):
                    return images[0]

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
예제 #2
0
 def list_virtual_servers(self,
                          address=None,
                          subnets=None,
                          ssh_keys_required=True,
                          security_groups_required=True) -> List:
     """Retrieve a list of all virtual servers on the Account."""
     details = {
         'address': address,
         'subnets': subnets,
         'ssh_keys_required': ssh_keys_required,
         'security_groups_required': security_groups_required
     }
     instances_list = []
     self.vs_manager = VSManager(self.client)
     try:
         instances = self.retry.call(self.vs_manager.list_instances,
                                     mask=VIRTUAL_SERVER_MASK)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
     else:
         for instance in instances:
             instance_obj = self.__parse_to_softlayer(instance, **details)
             if instance_obj:
                 instances_list.append(instance_obj)
         return instances_list
예제 #3
0
    def list_private_images_name(self):
        """List all private and public images on the Account as Dict."""
        image_list = list()
        try:
            # TODO we should ask region from the user and send the exact name rather then first element
            self.image_manager = ImageManager(self.client)
            images = self.retry.call(self.image_manager.list_private_images,
                                     mask=IMAGE_MASK)
            for image in images:
                if not image.get("children"):
                    continue

                image_child = image["children"][0]
                if image_child.get('transactionId'):
                    continue

                if not image_child.get("blockDevices"):
                    continue

                block_device = image_child["blockDevices"][0]
                if not block_device.get("diskImage"):
                    continue

                disk_image = block_device.get("diskImage")
                if not disk_image.get('softwareReferences'):
                    continue

                software_reference = disk_image["softwareReferences"][0]
                if not (software_reference.get("softwareDescription")
                        and software_reference.get("softwareDescription").get(
                            "longDescription")):
                    continue

                image_name = software_reference.get("softwareDescription").get(
                    "longDescription")
                instance_vpc_image = classical_vpc_image_dictionary.get(
                    image_name)
                instance_vpc_image = instance_vpc_image[
                    0] if instance_vpc_image else "-"
                if instance_vpc_image == "-":
                    continue

                image_list.append({
                    "id": image.get('id'),
                    "name": image.get('name'),
                    "vpc_image_name": instance_vpc_image,
                    "operating_systems": {
                        "architecture": "amd64"
                    }
                })

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
        else:
            return image_list
예제 #4
0
 def list_ssh_keys(self) -> list:
     """Lists all SSH keys on the Account."""
     try:
         self.ssh_manager = SshKeyManager(self.client)
         return self.retry.call(self.ssh_manager.list_keys)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
예제 #5
0
 def list_ssl_certs(self) -> dict:
     """A list of dictionaries representing the requested SSL certs."""
     try:
         self.ssl_manager = SSLManager(self.client)
         return self.retry.call(self.ssl_manager.list_certs)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
예제 #6
0
 def authenticate_sl_account(self):
     """
     Authenticate SL account with provided credentials
     :return:
     """
     try:
         return self.retry.call(self.client.call, "Account", "getObject")
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
예제 #7
0
    def list_private_subnets(self,
                             vlan_no=None,
                             network_identifier=None) -> list:
        """List all the private Subnetworks associated with an Account."""

        subnets_list = list()
        try:
            self.network_manager = NetworkManager(self.client)
            vlans = self.retry.call(
                self.network_manager.list_vlans,
                filter={
                    "networkVlans": {
                        "subnets": {
                            "addressSpace": {
                                "operation": "PRIVATE"
                            }
                        }
                    }
                },
                mask="mask{subnets}".format(subnets=SUBNET_MASK))

            for vlan in vlans:
                if vlan_no and vlan_no != vlan.get("vlanNumber"):
                    continue

                vlan_name = "{}-{{}}".format(
                    vlan.get("name")
                    or "subnet-{}".format(vlan.get("vlanNumber")))
                count = 1
                for subnet in vlan.get("subnets", []):
                    sl_subnet = SoftLayerSubnet(
                        name=vlan_name.format(count),
                        vif_id=vlan.get("vlanNumber"),
                        address="{}/{}".format(subnet.get("gateway"),
                                               subnet.get("cidr")),
                        network_id=subnet["networkIdentifier"])
                    count = count + 1
                    if not (network_identifier
                            and network_identifier != sl_subnet.network_id):
                        subnets_list.append(sl_subnet)

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)

        return subnets_list
예제 #8
0
 def export_image(self, image_id, cos_url, api_key):
     """
     Export Image template from Classic to specified COS
     :return:
     """
     try:
         self.image_manager = ImageManager(self.client)
         return self.retry.call(self.image_manager.export_image_to_uri,
                                image_id, cos_url, api_key)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
예제 #9
0
 def create_instance(self, instance_body):
     """
     Create Sofltlayer Instance
     return:
     """
     try:
         self.vs_manager = VSManager(self.client)
         return self.retry.call(self.vs_manager.create_instance,
                                **instance_body)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
예제 #10
0
 def capture_image(self, instance_id, image_name, additional_disks=False):
     """
     Create and capture image template of an image belonging to classical VSI
     :return:
     """
     try:
         self.vs_manager = VSManager(self.client)
         return self.retry.call(self.vs_manager.capture, instance_id,
                                image_name, additional_disks)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
예제 #11
0
    def delete_image(self, image_id):
        """
        Delete image template from Classical Infrastructure
        :param image_id: Classical Image ID for the image
        :return:
        """
        try:
            self.image_manager = ImageManager(self.client)
            return self.retry.call(self.image_manager.delete_image, image_id)

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
예제 #12
0
    def get_instance_by_id(self, instance_id):
        """
        Get a softlayer instance, with provided ID
        :return:
        """
        try:
            self.vs_manager = VSManager(self.client)
            instances = self.retry.call(self.vs_manager.get_instance,
                                        instance_id=instance_id)

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
        else:
            return instances
예제 #13
0
    def get_image_by_id(self, image_id):
        """
        Get image on the basis of image id.
        :param image_id: Classical ID of the image.
        :return: Return the filtered image on the basis of above params.
        """
        try:
            image_manager = ImageManager(self.client)
            image = self.retry.call(image_manager.get_image, image_id=image_id)
            if image:
                return image

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
예제 #14
0
    def list_images(self) -> dict:
        """List all private and public images on the Account as Dict."""
        try:

            self.image_manager = ImageManager(self.client)
            return {
                "private_images":
                self.retry.call(self.image_manager.list_private_images),
                "public_images":
                self.retry.call(self.image_manager.list_public_images),
            }

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
예제 #15
0
 def list_vsi_hostnames(self) -> List[Dict]:
     """List all private and public images on the Account as Dict."""
     instances_list = list()
     try:
         self.vs_manager = VSManager(self.client)
         instances = self.retry.call(self.vs_manager.list_instances,
                                     mask=VSI_ID_HOSTNAME_ONLY_MASK)
         for instance in instances:
             instances_list.append({
                 "id": instance.get('id'),
                 "hostname": instance.get('hostname')
             })
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
     else:
         return instances_list
예제 #16
0
    def list_security_groups(self) -> list:
        """List all the Security Groups within an Account."""
        sl_security_groups_list = list()
        try:
            security_groups = self.retry.call(
                NetworkManager(self.client).list_securitygroups,
                mask="mask{mask}".format(
                    mask="[networkComponentBindings, rules]"))
            for security_group in security_groups:
                sl_security_group = SoftLayerSecurityGroup(
                    name=security_group["name"])
                for rule in security_group.get("rules", []):
                    if rule["ethertype"] == "IPv6":
                        continue

                    sl_security_group_rule = SoftLayerSecurityGroupRule(
                        direction=rule["direction"],
                        protocol=rule.get("protocol", "all"),
                        port_max=rule.get("portRangeMax"),
                        port_min=rule.get("portRangeMin"))

                    if rule.get("remoteIp") and "/" in rule.get("remoteIp"):
                        sl_security_group_rule.rule_type = "cidr_block"
                        sl_security_group_rule.cidr_block = rule.get(
                            "remoteIp")

                    elif rule.get("remoteIp"):
                        sl_security_group_rule.rule_type = "address"
                        sl_security_group_rule.address = rule.get("remoteIp")

                    sl_security_group.rules.append(sl_security_group_rule)
                sl_security_groups_list.append(sl_security_group)

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
        else:
            return sl_security_groups_list
예제 #17
0
    def get_classic_image_name(self, image_name):
        """concatenate an integer value if this image already exists"""
        try:
            img_manager = ImageManager(self.client)
            private_images = self.retry.call(img_manager.list_private_images,
                                             name=image_name)
            num = 1
            while True:
                if image_name not in [
                        image.get("name") for image in private_images
                ]:
                    return image_name
                image_name = "-".join([image_name, str(num)])
                num += 1

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
예제 #18
0
 def get_instance_details(self,
                          instance_id,
                          ssh_keys_required=False,
                          security_groups_required=False):
     details = {
         'ssh_keys_required': ssh_keys_required,
         'security_groups_required': security_groups_required
     }
     self.vs_manager = VSManager(self.client)
     try:
         instance = self.retry.call(self.vs_manager.get_instance,
                                    mask=VIRTUAL_SERVER_MASK,
                                    instance_id=instance_id)
     except SoftLayerAPIError as ex:
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
     else:
         instance = self.__parse_to_softlayer(instance, **details)
         return instance
예제 #19
0
 def list_dedicated_hosts(self, instances=False, raw=False) -> List[Dict]:
     """List all Dedicated Hosts on the Account as Dict."""
     try:
         self.dedicated_host_manager = DedicatedHostManager(self.client)
         dedicated_hosts = self.retry.call(
             self.dedicated_host_manager.list_instances,
             mask=DEDICATED_HOST_W_INSTANCES_MASK
             if instances else DEDICATED_HOST_WO_INSTANCES_MASK)
         dedicated_hosts_list = dedicated_hosts
         if not raw:
             dedicated_hosts_list = [
                 SoftLayerDedicatedHost.from_softlayer_json(dedicated_host)
                 for dedicated_host in dedicated_hosts
             ]
     except SoftLayerAPIError as ex:
         LOGGER.info(f"error_message => {ex.reason}")
         if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
             raise SLRateLimitExceededError(ex)
         elif ex.faultCode == INVALID_API_KEY_CODE:
             raise SLAuthError(self.username)
         raise SLExecuteError(ex)
     else:
         return dedicated_hosts_list
예제 #20
0
    def list_load_balancers(self, vs_instances=None) -> list:
        """Returns a list of IBM Cloud Loadbalancers"""
        load_balancers_list = list()
        try:
            self.load_balancer_manager = LoadBalancerManager(self.client)
            load_balancers = self.retry.call(
                self.load_balancer_manager.get_lbaas, mask=LOAD_BALANCER_MASK)
            for lb in load_balancers:
                pools_list, subnets_list = list(), list()
                sl_load_balancer = SoftLayerLoadBalancer(
                    lb["name"], lb["isPublic"], lb["address"])
                for listener in lb.get("listeners", []):
                    sl_listener = SoftLayerListener(
                        protocol=listener.get("protocol"),
                        port=listener.get("protocolPort"),
                        connection_limit=listener.get("connectionLimit"))

                    if listener.get("defaultPool"):
                        sl_pool = SoftLayerBackendPool(
                            port=listener["defaultPool"].get("protocolPort"),
                            protocol=listener["defaultPool"].get("protocol"),
                            algorithm=listener["defaultPool"].get(
                                "loadBalancingAlgorithm"))

                        if listener["defaultPool"].get("sessionAffinity"):
                            sl_pool.session_persistence = listener[
                                "defaultPool"]["sessionAffinity"].get("type")
                        if listener["defaultPool"].get("healthMonitor"):
                            sl_health_monitor = SoftLayerPoolHealthMonitor(
                                listener["defaultPool"]["healthMonitor"].get(
                                    "maxRetries"), listener["defaultPool"]
                                ["healthMonitor"].get("timeout"),
                                listener["defaultPool"]["healthMonitor"].get(
                                    "monitorType"), listener["defaultPool"]
                                ["healthMonitor"].get("urlPath"),
                                listener["defaultPool"]["healthMonitor"].get(
                                    "interval"))
                            sl_pool.health_monitor = sl_health_monitor

                        for member in listener["defaultPool"].get(
                                "members", []):
                            sl_pool_mem = SoftLayerPoolMember(
                                weight=member.get("weight"),
                                port=listener.get("protocolPort"),
                                ip=member.get("address"))

                            if not vs_instances:
                                instance = self.list_virtual_servers(
                                    address=member.get("address"))
                            else:
                                instance = [
                                    instance for instance in vs_instances
                                    if member.get("address") in [
                                        interf.private_ip for interf in
                                        instance.network_interfaces
                                    ]
                                ]

                            if not instance:
                                continue

                            sl_pool_mem.instance = instance[0]
                            for network_interface in sl_pool_mem.instance.network_interfaces:
                                if not network_interface.subnet:
                                    continue

                                if network_interface.subnet.name not in [
                                        subnet.name for subnet in subnets_list
                                ]:
                                    subnets_list.append(
                                        network_interface.subnet)

                            sl_pool.pool_members.append(sl_pool_mem)
                        sl_listener.backend_pool = sl_pool
                        pools_list.append(sl_pool)
                    sl_load_balancer.listeners.append(sl_listener)

                sl_load_balancer.pools = pools_list
                sl_load_balancer.subnets = subnets_list
                load_balancers_list.append(sl_load_balancer)

        except SoftLayerAPIError as ex:
            if ex.faultCode == SL_RATE_LIMIT_FAULT_CODE:
                raise SLRateLimitExceededError(ex)
            elif ex.faultCode == INVALID_API_KEY_CODE:
                raise SLAuthError(self.username)
            raise SLExecuteError(ex)
        else:
            return load_balancers_list