def delete_network_acl(self, acl): """Delete network ACL :param acl: Network ACL name or ID :type acl: str :return: Delete status :rtype: dict """ try: # Check if network ACL exists acl_info = self.get_network_acl(acl) if "errors" in acl_info: return acl_info # Connect to api endpoint for network_acls path = ("/v1/network_acls/{}?version={}&generation={}".format( acl_info["id"], self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting network ACL with {}. {}".format(acl, error)) raise
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 create_group(self, **kwargs): """Create resource group :param name: Name of the resource group :type name: str :param account_id: The account ID of the resource group :type account_id: str """ args = ["name", "account_id"] check_args(args, **kwargs) # Build dict of argument and assign default value when needed args = { 'name': kwargs.get('name'), 'account_id': kwargs.get('account_id'), } # Construct payload payload = {} for key, value in args.items(): if value is not None: payload[key] = value try: # Connect to api endpoint for resource_groups path = ("/v2/resource_groups") # Return data return qw("rg", "POST", path, headers(), json.dumps(payload))["data"] except Exception as error: print("Error create resource group. {}".format(error)) raise
def detach_public_gateway(self, subnet): """Detach public gateway from a subnet :param subnet: Subnet name or ID :type subnet: str :return: Detach status :rtype: resource_deleted() """ # Retrieve subnet and public gateway information to get the ID # (mostly useful if a name is provided) subnet_info = self.get_subnet(subnet) if "errors" in subnet_info: return subnet_info try: # Connect to api endpoint for subnets path = ("/v1/subnets/{}/public_gateway?version={}" "&generation={}".format(subnet_info["id"], self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error detaching subnet's public gateway for" "subnet {}. {}".format(subnet, error)) raise
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 delete_security_group(self, security_group): """Delete security group :param security_group: Security group name or ID :type security_group: str :return: Delete status :rtype: dict """ try: # Check if security group exists sg_info = self.get_security_group_by_name(security_group) if "errors" in sg_info: return sg_info # Connect to api endpoint for security_groups path = ("/v1/security_groups/{}?version={}&generation={}".format( sg_info["id"], self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting security group {}. {}".format( security_group, error)) raise
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 get_default_security_group(self, vpc): """Retrieve VPC's default security group :param vpc: VPC name or ID :type vpc: str :return: Default security group information :rtype: dict """ # Check if VPC exists and get information vpc_info = self.get_vpc(vpc) if "errors" in vpc_info: return vpc_info try: # Connect to api endpoint for vpcs path = ("/v1/vpcs/{}/default_security_group?version={}" "&generation={}".format(vpc_info["id"], self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching default security group for VPC" " {}. {}".format(vpc, error)) raise
def get_address_prefixes(self, vpc): """Retrieve VPC address pool prefix list :param vpc: VPC name or ID :type vpc: str :return: List of adress prefixes :rtype: list """ # Check if VPC exists and get information vpc_info = self.get_vpc(vpc) if "errors" in vpc_info: return vpc_info try: # Connect to api endpoint for vpcs path = ("/v1/vpcs/{}/address_prefixes?version={}" "&generation={}".format(vpc_info["id"], self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching address prefixes in VPC {}. {}".format( vpc, error)) raise
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 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 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_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 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_security_group_rules(self, security_group): """Retrieve rules from a security group :param security_group: Security group name or ID :type security_group: str :return: List of rules :rtype: list """ # Retrieve security group information to get the ID # (mostly useful if a name is provided) sg_info = self.get_security_group(security_group) if "errors" in sg_info: return sg_info try: # Connect to api endpoint for security_groups path = ("/v1/security_groups/{}/rules?version={}" "&generation={}".format(sg_info["id"], self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching rules for security group with" " ID {}. {}".format(id, error)) raise
def get_address_prefix_by_id(self, vpc, id): """Retrieve specific VPC address prefix by ID :param vpc: VPC name or ID :type vpc: str :param id: Address prefix ID :type id: str :return: Address prefix information :rtype: dict """ # Check if VPC exists and get information vpc_info = self.get_vpc(vpc) if "errors" in vpc_info: return vpc_info try: # Connect to api endpoint for vpcs path = ("/v1/vpcs/{}/address_prefixes/{}?version={}" "&generation={}".format(vpc_info["id"], id, self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching address prefix with ID {} in VPC {}." " {}".format(id, vpc, error)) raise
def get_security_group_rule_by_id(self, security_group, id): """Retrieve specific rule from a security group by ID :param security_group: Security group name or ID :type security_group: str :param id: Rule ID :type id: str :return: Rule information :rtype: dict """ # Retrieve security group information to get the ID # (mostly useful if a name is provided) sg_info = self.get_security_group(security_group) if "errors" in sg_info: return sg_info try: # Connect to api endpoint for security_groups path = ("/v1/security_groups/{}/rules/{}?version={}" "&generation={}".format(sg_info["id"], id, self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching rule with ID {} for security group" " {}. {}".format(id, security_group, error)) raise
def get_routes(self, vpc): """Retrieve route list from VPC default routing table :param vpc: VPC name or ID :type vpc: str :return: List of routing tables :rtype: dict """ # Check if VPC exists and get information vpc_info = self.get_vpc(vpc) if "errors" in vpc_info: return vpc_info try: # Connect to api endpoint for vpcs path = ("/v1/vpcs/{}/routes?version={}" "&generation={}".format(vpc_info["id"], self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching routes from VPC {}. {}".format(vpc, error)) raise
def remove_interface_security_group(self, security_group, interface): """Remove network interface from a security group :param security_group: Security group name or ID :type security_group: str :parem interface: Interface ID :type interface: str :return: Delete status :rtype: dict """ try: # Check if security group exists sg_info = self.get_security_group(security_group) if "errors" in sg_info: return sg_info # Connect to api endpoint for security_groups path = ("/v1/security_groups/{}/network_interfaces/{}?version={}" "&generation={}".format(sg_info["id"], interface, self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error removing network interface {} from security group" " {}. {}".format(interface, security_group, error)) raise
def get_route_by_id(self, vpc, id): """Retrieve specific route from VPC default routing table by ID :param vpc: VPC name or ID :type vpc: str :param id: Routing table ID :type id: str :return: Routing table information :rtype: dict """ # Check if VPC exists and get information vpc_info = self.get_vpc(vpc) if "errors" in vpc_info: return vpc_info try: # Connect to api endpoint for vpcs path = ("/v1/vpcs/{}/routes/{}?version={}" "&generation={}".format(vpc_info["id"], id, self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching route with ID {} in VPC {}. {}".format( id, vpc, error)) raise
def get_subnet_public_gateway(self, subnet): """Retrieve public gateway for a specific subnet :param subnet: Subnet name or ID :type subnet: str :return: Public gateway information :rtype: dict """ try: # Check if subnet exists and get information subnet_info = self.get_subnet(subnet) if "errors" in subnet_info: return subnet_info # Connect to api endpoint for subnets path = ("/v1/subnets/{}/public_gateway?version={}" "&generation={}".format(subnet_info["id"], self.cfg["version"], self.cfg["generation"])) return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching public gateway for subnet {}. {}".format( subnet, error)) raise
def delete_vpc(self, vpc): """Delete VPC :param vpc: VPC name or ID :type vpc: str :return: Delete status :rtype: resource_deleted() """ # Check if VPC exists and get information vpc_info = self.get_vpc(vpc) if "errors" in vpc_info: return vpc_info try: # Connect to api endpoint for vpcs path = ("/v1/vpcs/{}?version={}&generation={}".format( vpc_info["id"], self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting VPC {}. {}".format(vpc, error)) raise
def delete_subnet(self, subnet): """Delete subnet :param subnet: Subnet name or ID :type subnet: str :return: Delete status :rtype: resource_deleted() """ try: # Check if subnet exists subnet_info = self.get_subnet(subnet) if "errors" in subnet_info: return subnet_info # Connect to api endpoint for subnets path = ("/v1/subnets/{}?version={}&generation={}".format( subnet_info["id"], self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting subnet with name {}. {}".format( subnet, error)) raise
def release_floating_ip(self, fip): """Release floating IP :param fip: Floating IP name, ID or address :type fip: str """ try: # Check if floating IP exists fip_info = self.get_floating_ip(fip) if "errors" in fip_info: return fip_info # Connect to api endpoint for floating_ips path = ("/v1/floating_ips/{}?version={}&generation={}".format( fip_info["id"], self.cfg["version"], self.cfg["generation"])) data = qw("iaas", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting floating IP {}. {}".format(fip, error)) raise
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_policy(self, policy): """Delete policy :param policy: Policy ID :type policy: str :return: Deletion status :rtype: dict """ # Check if policy exists and get information policy_info = self.get_policy(policy) if "errors" in policy_info: return policy_info try: # Connect to api endpoint for policies path = ("/v1/policies/{}".format(policy_info["id"])) data = qw("auth", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting policy {}. {}".format(policy, error))
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 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 delete_group(self, group): """Delete resource group :param group: Resource group name or ID :type group: :return: Delete status :rtype: resource_deleted() """ try: # Check if group exists group_info = self.get_resource_group(group) if "errors" in group_info: return group_info # Connect to api endpoint resource_groups path = ("/v2/resource_groups/{}".format(group_info["id"])) data = qw("rg", "DELETE", path, headers()) # Return data if data["response"].status != 204: return data["data"] # Return status return resource_deleted() except Exception as error: print("Error deleting resource group {}. {}".format(group, error)) raise
def get_network_acl_rule_by_id(self, acl, id): """Retrieve specific rule for a specific network ACL by ID :param acl: Network ACL name or ID :type acl: str :param id: Rule ID :type id: str :return: Network ACL rule information :rtype: dict """ # Retrieve network ACL to get the ID # (mostly useful if a name is provided) acl_info = self.get_network_acl(acl) if "errors" in acl_info: return acl_info try: # Connect to api endpoint for network_acls path = ("/v1/network_acls/{}/rules/{}?version={}" "&generation={}".format(acl_info["id"], id, self.cfg["version"], self.cfg["generation"])) # Return data return qw("iaas", "GET", path, headers())["data"] except Exception as error: print("Error fetching rule with ID {} for network ACL" "with ID {}. {}".format(id, acl_info["id"], error)) raise