def get_network_by_id(self, instance, id): """Retrieve specific network by ID :param instance: Cloud instance ID :type instance: str :param id: Network ID :type id: str :return: Network information :rtype: dict """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/networks/{}".format( ci_info["name"], id)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching network with ID {} for cloud instance {}." " {}".format(id, instance, error))
def get_pvm_volume_by_id(self, instance, pvm, id): """Retrieve specific volume from Power Virtual Instance by ID :param instance: Cloud instance ID :type instance: str :param pvm: Power Virtual Instance name or ID :type pvm: str :param id: Volume ID :type id: str :return: PVM volume information :rtype: dict """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if pvm exists and retrieve information pvm_info = self.pvm.get_pvm(instance, pvm) if "errors" in pvm_info: return pvm_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/pvm-instances/{}" "/networks/{}".format(ci_info["name"], pvm_info["pvmInstanceID"], id)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching volume with ID {} from Power Virtual" " Instance {} for cloud instance {}. {}".format( id, pvm, instance, error))
def get_ports(self, instance, network): """Retrieve port list from from network :param instance: Cloud instance ID :type instance: str :param network: Network name or ID :type netowkr: str :return: Port list :rtype: list """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if network exists and retrieve information net_info = self.get_network(ci_info["name"], network) if "errors" in net_info: return net_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/networks/{}/ports".format( ci_info["name"], net_info["networkID"])) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching port list from network {} for cloud instance" " {}. {}".format(network, instance, error))
def export_instance_image(self, **kwargs): """Export an image for a cloud instance :param instance: Cloud instance ID :type instance: str :param image: Image ID of existing source image :type image: str :param region: Cloud Storage Region :type region: str, optional :param bucket: Cloud Storage bucket name :type bucket: str, optional :param access_key: Cloud Storage access key :type access_key: str, optional :param secret_key: Cloud Storage secret key :type secret_key: str, optional :return: Export image information :rtype: dict """ args = ["instance", "image"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'instance': kwargs.get('source'), 'image': kwargs.get('image'), 'region': kwargs.get('region'), 'bucketName': kwargs.get('bucket'), 'accessKey': kwargs.get('access_key'), 'secretKey': kwargs.get('secret_key'), } # Construct payload payload = {} for key, value in args.items(): if key != "instance" and key != "image" and value is not None: payload[key] = value try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(args['instance']) if "errors" in ci_info: return ci_info # Check if image exists and retrieve information image_info = self.get_instance_image(ci_info["name"], args["image"]) if "errors" in image_info: return image_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/images/{}/export".format( ci_info["name"], image_info["imageID"])) # Return data return qw("power", "POST", path, headers(), json.dumps(payload))["data"] except Exception as error: print("Error exporting image {} for cloud instance {}. {}".format( args['image'], args['instance'], error))
def delete_volume(self, instance, volume): """Delete volume from cloud instance :param instance: Instance name or ID :type instance: str :param volume: Volume name or ID :type volume: str :return: Deletion status :rtype: dict """ try: ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if volume exists and retrieve information vol_info = self.get_volume(ci_info["name"], volume) if "errors" in vol_info: return vol_info path = ("/pcloud/v1/cloud-instances/{}/volumes/{}".format( ci_info["name"], vol_info["volumeID"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting volume {} from cloud instance {}. {}".format( volume, instance, error))
def delete_key(self, tenant, key): """Delete key :param tenant: Tenant ID (Account ID) :type tenant: str :param key: Key name :type key: str :return: Deletion status :rtype: dict """ try: # Check if key exists key_info = self.get_key(tenant, key) if "errors" in key_info: return key_info # Connect to api endpoint for sshkeys path = ("/pcloud/v1/tenants/{}/sshkeys/{}".format( tenant, key_info["name"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting key {}. {}".format(key, error))
def delete_instance(self, instance): """Delete cloud instance :param instance: Cloud instance ID :type instance: str :return: Deletion status :rtype: dict """ try: # Check if cloud instance exists and retrieve information ci_info = self.get_instance(instance) if "errors" in ci_info: return ci_info # Connect to api endpoint for sshkeys path = ("/pcloud/v1/cloud-instances/{}".format(ci_info["name"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting cloud instance {}. {}".format( instance, error))
def get_pvm_volumes(self, instance, pvm): """Retrieve volumes list for Power Virtual Instance :param instance: Cloud instance ID :type instance: str :param pvm: Power Virtual Instance name or ID :type pvm: str :return: PVM volume list :rtype: list """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if pvm exists and retrieve information pvm_info = self.pvm.get_pvm(instance, pvm) if "errors" in pvm_info: return pvm_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/pvm-instances/{}" "/volumes".format(ci_info["name"], pvm_info["pvmInstanceID"])) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching volume list for Power Virtual Instance list" " for cloud instance {}. {}".format(instance, error))
def get_port(self, instance, network, port): """Retrieve specific port by ID :param instance: Cloud instance ID :type instance: str :param network: Network name or ID :type netowkr: str :param port: Port ID :type port: str :return: Port information :rtype: dict """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if network exists and retrieve information net_info = self.get_network(ci_info["name"], network) if "errors" in net_info: return net_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/networks/{}/" "ports/{}".format(ci_info["name"], net_info["networkID"], id)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching port with ID {} from network {} for cloud" " instance {}. {}".format(id, network, instance, error))
def delete_network(self, instance, network): """Delete network from cloud instance :param instance: Cloud instance ID :type instance: str :param network: Network name or ID :type network: str :return: Deletion status :rtype: dict """ try: ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if network exists and retrieve information net_info = self.get_network(ci_info["name"], network) if "errors" in net_info: return net_info path = ("/pcloud/v1/cloud-instances/{}/networks/{}".format( ci_info["name"], net_info["networkID"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting network {} from cloud instance {}." " {}".format(network, instance, error))
def delete_task(self, task): """Delete task :param task: Task ID :type task: str :return: Deletion status :rtype: dict """ # Check if task exists and get information task_info = self.get_task(task) if "errors" in task_info: return task_info try: # Connect to api endpoint for tasks path = ("/pcloud/v1/tasks/{}".format(task_info["taskID"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting task {}. {}".format(task, error))
def attach_volume(self, **kwargs): """Attach volume to a Power Virtual Instance :param instance: Instance name or ID :type instance: str :param pvm: Power Virtual Instance name or ID :type pvm: str :param volume: Volume name or ID :type volume: str :return: Attachment status :rtype: dict """ args = ["instance", "pvm", "volume"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'instance': kwargs.get('instance'), 'pvm': kwargs.get('pvm'), 'volume': kwargs.get('volume'), } try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(args['instance']) if "errors" in ci_info: return ci_info # Check if pvm exists and retrieve information pvm_info = self.pvm.get_pvm(args['instance'], args['pvm']) if "errors" in pvm_info: return pvm_info # Check if volume exists and retrieve information vol_info = self.get_volume(ci_info["name"], args["volume"]) if "errors" in vol_info: return vol_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/pvm-instances/{}" "/volumes/{}".format(ci_info["name"], pvm_info["pvmInstanceID"], vol_info["volumeID"])) data = qw("power", "POST", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status payload = {"status": "attached"} return resource_created(payload) except Exception as error: print("Error attaching volume {} to Power Virtual Instance {}" " from cloud instance {}. {}".format(args["volume"], args['pvm'], args['instance'], error))
def clone_volume(self, **kwargs): """Create a clone from a volume :param instance: Instance name or ID :type instance: str :param volumes: List of volumes to be cloned :type volumes: list :param name: Display name for the new cloned volumes. Cloned Volume names will be prefixed with 'clone-'. If multiple volumes cloned they will be suffix with a '-' and an incremental number starting with 1. :type name: str :param prefix_name: Prefix to use when naming the new cloned volumes :type prefix_name: str, optional :return: Volume clone information :rtype: dict """ args = ["instance", "volumes", "name"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'instance': kwargs.get('instance'), 'volumeIDs': kwargs.get('volumes'), 'displayName': kwargs.get('name'), 'namingPrefix': kwargs.get('prefix_name'), } # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(args['instance']) if "errors" in ci_info: return ci_info # Construct payload payload = {} for key, value in args.items(): if key != "instance" and value is not None: if key == "volumeIDs": vl = [] for volume in args["volumeIDs"]: vol_info = self.get_volume(ci_info["name"], volume) if "errors" in vol_info: return vol_info vl.append(vol_info["volumeID"]) payload["volumeIDs"] = vl payload[key] = value try: # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/volumes/clone".format( ci_info["name"])) # Return data return qw("power", "POST", path, headers(), json.dumps(payload))["data"] except Exception as error: print("Error creating clone from volume(s) {} for cloud instance" " {}. {}".format(args["volumeIDs"], args['instance'], error))
def create_port(self, **kwargs): """Create network :param instance: Instance name or ID :type instance: str :param network: Network name or ID :type network: str :param description: Description of the port :type description: str, optional :param ip_address: The requested ip address of this port :type ip_address: str, optional :return: Port information :rtype: dict """ args = ["instance", "network"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'instance': kwargs.get('instance'), 'network': kwargs.get('network'), 'description': kwargs.get('description'), 'ip_address': kwargs.get('ip_address'), } # Construct payload payload = {} for key, value in args.items(): if key != "instance" and key != "network" and value is not None: if key == "ip_address": payload["ipAddress"] = args['ip_address'] else: payload[key] = value try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if network exists and retrieve information net_info = self.get_network(ci_info["name"], args['network']) if "errors" in net_info: return net_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/networks/{}/ports".format( ci_info["name"], net_info["networkID"])) # Return data return qw("power", "POST", path, headers(), json.dumps(payload))["data"] except Exception as error: print("Error creating port in network {} for cloud instance {}." " {}".format(args['network'], args['instance'], error))
def perform_action(self, **kwargs): """Perform an action on Power Virtual Machine :param instance: Cloud instance ID :type instance: str :param pvm: Power Virtual Instance name or ID :type pvm: str :param action: Name of the action to take :type action: str :return: Action information :rtype: dict """ args = ["instance", "pvm", "action"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'instance': kwargs.get('instance'), 'pvm': kwargs.get('pvm'), 'action': kwargs.get('action'), } # Construct payload payload = {} for key, value in args.items(): if key != "instance" and key != "pvm" and value is not None: payload[key] = value try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(args['instance']) if "errors" in ci_info: return ci_info # Check if pvm exists and retrieve information pvm_info = self.get_pvm(ci_info["name"], args['pvm']) if "errors" in pvm_info: return pvm_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/pvm-instances/{}" "/action".format(ci_info["name"], pvm_info["pvmInstanceID"])) # Return data return qw("power", "POST", path, headers(), json.dumps(payload))["data"] except Exception as error: print("Error performing action {} on Power Virtual Machine {} for" " cloud instance {}. {}".format(args['action'], args['network'], args['instance'], error))
def get_images(self): """Retrieve image list :return: List of images :rtype: list """ try: # Connect to api endpoint for images path = ("/pcloud/v1/images") # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching images. {}".format(error))
def get_task(self, task): """Retrieve specific task :param task: Task ID :type task: str :return: Task information :rtype: dict """ try: # Connect to api endpoint for tasks path = ("/pcloud/v1/tasks/{}".format(task)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching task {}. {}".format(task, error))
def get_image_by_id(self, id): """Retrieve specific image by ID :param id: Image ID :type id: str :return: Image information :rtype: dict """ try: # Connect to api endpoint for images path = ("/pcloud/v1/images/{}".format(id)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching image with ID {}. {}".format(id, error))
def get_snapshots(self, instance): """Retrieve snapshot list for a specific cloud instance :param instance: Cloud instance ID :type instance: str :return: List of snapshots :rtype: list """ try: # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/snapshots".format(instance)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching snapshot list for cloud instance {}." " {}".format(instance, error))
def get_keys(self, tenant): """Retrieve keys for a specific tenant :param tenant: Tenant ID (Account ID) :type tenant: str :return: List of keys :rtype: list """ try: # Connect to api endpoint for sshkeys path = ("/pcloud/v1/tenants/{}/sshkeys".format(tenant)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching keys for tenant {}. {}".format( tenant, error))
def get_instance(self, instance): """Retrieve information about cloud instance :param instance: Cloud instance ID :type instance: str :return: Cloud instance information :rtype: dict """ try: # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}".format(instance)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching cloud instance {}. {}".format( instance, error))
def get_state(self, tenant): """Retrieve tenant state :param tenant: Tenant ID (Account ID) :type tenant: str :return: Tenant information :rtype: dict """ try: # Connect to api endpoint for tenants path = ("/pcloud/v1/tenants/{}".format(tenant)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching information for tenant {}. {}".format( tenant, error))
def get_key(self, tenant, key): """Retrieve specific key for a specific tenant :param tenant: Tenant ID (Account ID) :type tenant: str :param key: Key name :type key: str :return: Key information :rtype: dict """ try: # Connect to api endpoint for sshkeys path = ("/pcloud/v1/tenants/{}/sshkeys/{}".format(tenant, key)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching key {} for tenant {}. {}".format( key, tenant, error))
def create_key(self, **kwargs): """Create key :param tenant: Tenant ID (Account ID) :type tenant: str :param name: User defined name for the SSH key :type name: str :param public_key: A unique public SSH key to import :type public_key: str :return: Key information :rtype: dict """ args = ["tenant", "name", "public_key"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'tenant': kwargs.get('tenant'), 'name': kwargs.get('name'), 'public_key': kwargs.get('public_key'), } # Construct payload payload = {} for key, value in args.items(): if key != "tenant" and value is not None: if key == "public_key": payload["sshKey"] = args['public_key'] else: payload[key] = value try: # Connect to api endpoint for sshkeys path = ("/pcloud/v1/tenants/{}/sshkeys".format(args['tenant'])) # Return data return qw("power", "POST", path, headers(), json.dumps(payload))["data"] except Exception as error: print("Error creating key. {}".format(error))
def get_snapshot_by_id(self, instance, id): """Retrieve specific snapshot by ID :param instance: Cloud instance ID :type instance: str :param id: Snapshot ID :type id: str :return: Snapshot information :rtype: dict """ try: # Connect to api endpoint for images path = ("/pcloud/v1/cloud-instances/{}/snapshots/{}".format( instance, id)) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching snapshot with ID {} for cloud instance {}." " {}".format(id, instance, error))
def get_instance_images(self, instance): """Retrieve images for a cloud instance :param instance: Cloud instance ID :type instance: str :return: List of images per instance :rtype: list """ try: ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Connect to api endpoint for images path = ("/pcloud/v1/cloud-instances/{}/images".format( ci_info["name"])) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching images for cloud instance {}. {}".format( instance, error))
def delete_snapshot(self, instance, snapshot): """Delete cloud instance :param instance: Cloud instance ID :type instance: str :param snapshot: Snapshot name or ID :type snapshot: str :return: Deletion status :rtype: dict """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if snapshot exists and retrieve information snap_info = self.get_snapshot(ci_info["name"], snapshot) if "errors" in snap_info: return snap_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/snapshot{}".format( ci_info["name"], snap_info["snapshotID"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting snapshot {} from cloud instance {}." " {}".format(snapshot, instance, error))
def get_pools(self, instance): """Retrieve system pools for a specific cloud instance :param instance: Cloud instance ID :type instance: str :return: List of system pools :rtype: list """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Connect to api endpoint for system-pools path = ("/pcloud/v1/cloud-instances/{}/system-pools".format( ci_info["name"])) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching system pools for cloud instance {}." " {}".format(instance, error))
def delete_pvm(self, instance, pvm): """Delete Power Virtual Instance :param instance: Cloud instance ID :type instance: str :param pvm: Power Virtual Instance name or ID :type pvm: str :return: Deletion status :rtype: dict """ try: ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Check if pvm exists and retrieve information pvm_info = self.get_pvm(instance, pvm) if "errors" in pvm_info: return pvm_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/pvm-instances/{}".format( ci_info["name"], pvm_info["pvmInstanceID"])) data = qw("power", "DELETE", path, headers()) # Return data if data["response"].status != 200: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting Power Virtual Instance {} from cloud" " instance {}. {}".format(pvm, instance, error))
def get_networks(self, instance): """Retrieve network list from cloud instance :param instance: Cloud instance ID :type instance: str :return: Network list :rtype: list """ try: # Check if cloud instance exists and retrieve information ci_info = self.instance.get_instance(instance) if "errors" in ci_info: return ci_info # Connect to api endpoint for cloud-instances path = ("/pcloud/v1/cloud-instances/{}/networks".format( ci_info["name"])) # Return data return qw("power", "GET", path, headers())["data"] except Exception as error: print("Error fetching Power Virtual Instance list for cloud" " instance {}. {}".format(instance, error))