def get_record_by_name(self, **kwargs): """Get record by name :param record: Record name :type record: str :param zone: Zone name :type zone: str """ required_args = set(["record", "zone"]) check_args(required_args, **kwargs) args = { 'record': kwargs.get('record'), 'zone': kwargs.get('zone'), } zone_id = self.get_zone_id(args['zone']) if not isinstance(zone_id, int): if "errors" in zone_id: for key_name in zone_id["errors"]: if key_name["code"] == "not_found": return resource_not_found() records = self.get_records(zone=args["zone"]) for record in records: if record["host"] == args["record"]: return record return resource_not_found()
def get_record_by_value(self, **kwargs): """Get record by value :param record: Record value :type record: str :param zone: Zone name :type zone: str """ args = set(["record", "zone"]) check_args(args, **kwargs) args = { 'record': kwargs.get('record'), 'zone': kwargs.get('zone'), } records = self.get_records(zone=args["zone"]) if "errors" in records: for key_name in records["errors"]: if key_name["code"] == "not_found": return resource_not_found() for record in records: if record["data"] == args["record"]: return record return resource_not_found()
def get_lb_listener_by_port(self, lb, port): """Retrieve specific listener from load balancer by port :param lb: Load balancer name or ID :type lb: str :param port: Listener port :type port: str :return: Listener information :rtype: dict """ # Retrieve load balancer information lb_info = self.get_lb(lb) if "errors" in lb_info: return lb_info try: # Retrieve listeners data = self.get_lb_listeners(lb_info["id"]) if "errors" in data: return data # Loop over listeners until filter match for listener in data["listeners"]: if listener["port"] == port: # Return data return listener # Return error if no listener is found return resource_not_found() except Exception as error: print("Error fetching listener with port {} from load balancer" " {}. {}".format(str(port), lb, error)) raise
def get_resource_group_by_name(self, name): """Retrieve specific resource group by name :param name: Resource group name :type name: str :return: Resource group information :rtype: dict """ try: # Retrieve resource groups data = self.get_resource_groups() if "errors" in data: return data # Loop over resources until filter match for resource in data['resources']: if resource["name"] == name: # Return data return resource # Return error if no resource is found return resource_not_found() except Exception as error: print("Error fetching resource group with name {}. {}".format( name, error)) raise
def get_network_by_name(self, instance, name): """Retrieve specific network by name :param instance: Cloud instance ID :type instance: str :param name: Network name :type name: 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 # Retrieve networks data = self.get_networks(ci_info["name"]) if "errors" in data: return data # Loop over networks until filter match for network in data['networks']: if network["name"] == name: # Return data return network # Return error if no network is found return resource_not_found() except Exception as error: print("Error fetching network with name {} for cloud instance {}." " {}".format(name, instance, error))
def get_security_group_by_name(self, name): """Retrieve specific security group by name :param name: Security group name :type name: str :return: Security group information :rtype: dict """ try: # Retrieve security groups data = self.get_security_groups() if "errors" in data: return data # Loop over security groups until filter match for sg in data['security_groups']: if sg["name"] == name: # Return data return sg # Return error if no security group is found return resource_not_found() except Exception as error: print("Error fetching security group with name {}. {}".format( name, error)) raise
def get_floating_ip_by_address(self, address): """Retrieve specific floating IP by address :param address: Floating IP address :type address: str :return: Floating IP information :rtype: dict """ try: # Retrieve floating IPs data = self.get_floating_ips() if "errors" in data: return data # Loop over instances until filter match for fip in data["floating_ips"]: if fip["address"] == address: # Return data return fip # Return error if no floating ips is found return resource_not_found() except Exception as error: print("Error fetching floating IP with address {}. {}".format( address, error)) raise
def get_route_by_name(self, vpc, name): """Retrieve specific route from VPC default routing table by name :param vpc: VPC name or ID :type vpc: str :param name: Routing table name :type name: 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: # Retrieve routes data = self.get_routes(vpc_info["id"]) if "errors" in data: return data # Loop over routes until filter match for prefix in data['routes']: if prefix["name"] == name: # Return data return prefix # Return error if no route is found return resource_not_found() except Exception as error: print("Error fetching route with name {} in VPC {}. {}".format( name, vpc, error)) raise
def get_key_by_name(self, name): """Retrieve specific key by name :param name: Key name :type name: str :return: Key information :rtype: dict """ try: # Retrieve keys data = self.get_keys() if "errors" in data: return data # Loop over keys until filter match for key in data["keys"]: if key["name"] == name: # Return data return key # Return error if no key is found return resource_not_found() except Exception as error: print("Error fetching key with name {}. {}".format(name, error)) raise
def get_instance_interface_by_name(self, instance, name): """Retrieve specific network interface for a specific instance by ID :param instance: Instance name or ID :type instance: str :param name: Interface name :type name: str :return: Instance's interface information :rtype: dict """ instance_info = self.get_instance(instance) if "errors" in instance_info: return instance_info try: data = self.get_instance_interfaces(instance_info["id"]) if "errors" in data: return data for interface in data['network_interfaces']: if interface["name"] == name: return interface return resource_not_found() except Exception as error: print("Error fetching network interface {} for instance" " {}. {}".format(instance, name, error)) raise
def get_image_by_name(self, name): """Retrieve specific image by name :param name: Image name :type name: str :return: Image information :rtype: dict """ try: # Retrieve images data = self.get_images() if "errors" in data: return data # Loop over images until filter match for image in data['images']: if image["name"] == name: # Return data return image # Return error if no image is found return resource_not_found() except Exception as error: print("Error fetching image with name {}. {}".format(name, error))
def get_volume_by_name(self, name): """Retrieve specific volume by name :param name: Volume name :type name: str :return: Volume information :rtype: dict """ try: # Retrieve volumes data = self.get_volumes() if "errors" in data: return data # Loop over volumes until filter match for volume in data["volumes"]: if volume["name"] == name: # Return response data return volume # Return error if no volume is found return resource_not_found() except Exception as error: print("Error fetching volume with name {}. {}".format(name, error)) raise
def get_resource_instance_by_guid(self, guid): """Retrieve specific resoure instance by GUID :param guid: Resource instance GUID :ẗype guid: str :return: Resource instance information :rtype: dict """ try: # Connect to api endpoint for resource instances path = ("/v2/resource_instances/{}".format(quote(guid))) result = qw("rg", "GET", path, headers())["data"] # Needed because API doens't return the same output as other API. if "status_code" in result: if result["status_code"] == 404: return resource_not_found() return result return result except Exception as error: print("Error fetching resource instance with ID {}. {}".format( guid, error)) raise
def get_lb_by_name(self, name): """Retrieve specific load balancer by name :param name: Load balancer name :type name: str :return: Load balancer information :rtype: dict """ try: # Retrieve load balancers data = self.get_lbs() if "errors" in data: return data # Loop over load balancers until filter match for lb in data["load_balancers"]: if lb["name"] == name: # Return data return lb # Return error if no load balancer is found return resource_not_found() except Exception as error: print("Error fetching load balancer with name {}. {}".format( name, error)) raise
def get_lb_pool_by_name(self, lb, name): """Retrieve specific pool from load balancer by name :param lb: Load balancer name or ID :type lb: str :param name: Pool ID :type name: str :return: Pool information :rtype: dict """ # Retrieve load balancer information lb_info = self.get_lb(lb) if "errors" in lb_info: return lb_info try: # Retrieve pools data = self.get_lb_pools(lb_info["id"]) if "errors" in data: return data # Loop over pools until filter match for pool in data["pools"]: if pool["name"] == name: # Return data return pool # Return error if no pool is found return resource_not_found() except Exception as error: print("Error fetching pool with name {} from load balancer" " {}. {}".format(name, lb, error)) raise
def get_snapshot_by_name(self, instance, name): """Retrieve specific snapshot by name :param instance: Cloud instance ID :type instance: str :param name: Snapshot name :type name: str :return: Snapshot information :rtype: dict """ try: # Retrieve snapshots data = self.get_snapshots(instance) if "errors" in data: return data # Loop over snapshots until filter match for snapshot in data['snapshots']: if snapshot["name"] == name: # Return data return snapshot # Return error if no snapshot is found return resource_not_found() except Exception as error: print("Error fetching snapshot with name {} for cloud instance {}." " {}".format(name, instance, error))
def get_address_prefix_by_cidr(self, vpc, cidr): """Retrieve specific VPC address prefix by cidr :param vpc: VPC name or ID :type vpc: str :param cidr: Address prefix CIDR :type cidr: 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: # Retrieve address prefixes data = self.get_address_prefixes(vpc_info["id"]) if "errors" in data: return data # Loop over address prefixes until filter match for prefix in data['address_prefixes']: if prefix["cidr"] == cidr: # Return data return prefix # Return error if no address prefix is found return resource_not_found() except Exception as error: print("Error fetching address prefix with CIDR {} in VPC {}." " {}".format(cidr, vpc, error)) raise
def get_operating_system(self, image, package=None): """Retrieve specific baremetal operating system :param image: Image name :type image: str :param package: Package name, defaults to `BARE_METAL_SERVER` :type package: str, optional :return: Baremetal operating system :rtype: dict """ # Package should have "os" category code. # By default BARE_METAL_SERVER will be used. pkg_name = "BARE_METAL_SERVER" if package: pkg_name = package try: # Retrieve operating systems data = self.get_operating_systems(pkg_name) # Loop over operating systems until filter match for operating_system in data["operating_systems"]: if operating_system["keyName"] == image: # Return data return operating_system # Return error if no operating system is found return resource_not_found() except sl.SoftLayer.SoftLayerAPIError as error: return resource_error(error.faultCode, error.faultString)
def get_vpc_by_name(self, name): """Retrieve specific VPC by name :param name: VPC name :type name: str :return: VPC information :rtype: dict """ try: # Retrieve VPCs data = self.get_vpcs() if "errors" in data: return data # Loop over VPCs until filter match for vpc in data['vpcs']: if vpc["name"] == name: # Return data return vpc # Return error if no VPC is found return resource_not_found() except Exception as error: print("Error fetching VPC with name {}. {}".format(name, error)) raise
def get_vpn_gateway_by_name(self, name): """Retrieve specific VPN gateway by name :param name: VPN gateway name :type name: str :return: Gateway information :rtype: dict """ try: # Retrieve gateways data = self.get_vpn_gateways() if "errors" in data: return data # Loop over gateways until filter match for gateway in data["vpn_gateways"]: if gateway["name"] == name: # Return data return gateway # Return error if no VPN gateway is found return resource_not_found() except Exception as error: print("Error fetching VPN gateway with name {}. {}".format( name, error)) raise
def get_floating_ip_by_name(self, name): """Retrieve specific floating IP by name :param name: Floating IP name :type name: str :return: Floating IP information :rtype: dict """ try: # Retrieve floating IPs data = self.get_floating_ips() if "errors" in data: return data # Loop over instances until filter match for fip in data["floating_ips"]: if fip["name"] == name: # Return data return fip # Return error if no floating ips is found return resource_not_found() except Exception as error: print("Error fetching floating IP with name {}. {}".format( name, error)) raise
def get_vpn_gateway_connection_by_name(self, gateway, name): """Retrieve specific connection for a VPN gateway by name :param gateway: VPN gateway name :type gateway: str :param name: Connection name :type name: str :return: Connection information :rtype: dict """ # Retrieve gateway information to get the ID # (mostly useful if a name is provided) gateway_info = self.get_vpn_gateway(gateway) if "errors" in gateway_info: return gateway_info try: # Retrieve gateway connections data = self.get_vpn_gateway_connections(gateway_info["id"]) if "errors" in data: return data # Loop over connections until filter match for connection in data["connections"]: if connection["name"] == name: # Return data return connection # Return error if no VPN gateway connection is found return resource_not_found() except Exception as error: print("Error fetching connection with name {} for VPN gateway" " with name {}. {}".format(name, gateway, error)) raise
def get_subnet_by_name(self, name): """Retrieve specific subnet by name :param name: Subnet name :type name: str :return: Subnet information :rtype: dict """ try: # Retrieve subnets data = self.get_subnets() if "errors" in data: return data # Loop over subnets until filter match for subnet in data["subnets"]: if subnet["name"] == name: # Return data return subnet # Return error if no subnet is found return resource_not_found() except Exception as error: print("Error fetching subnet with name {}. {}".format(name, error)) raise
def get_ike_policy_by_name(self, name): """Retrieve specific IKE policy by name :param name: IKE policy name :type name: str :return: IKE policy information :rtype: dict """ try: # Retrieve policies data = self.get_ike_policies() if "errors" in data: return data # Loop over policies until filter match for policy in data["ike_policies"]: if policy["name"] == name: # Return data return policy # Return error if no IKE policy is found return resource_not_found() except Exception as error: print("Error fetching IKE policy with name {}. {}".format( name, error)) raise
def get_quota_definition_by_name(self, name): """Retrieve specific quota definitnion by name :param name: Quota definition name :type name: str :return: Quota definition :rtype: dict """ try: # Retrieve quota definitions data = self.get_quota_definitions() if "errors" in data: return data # Loop over quota definitions until filter match for quota in data['resources']: if quota["name"] == name: # Return data return quota # Return error if no quota definition is found return resource_not_found() except Exception as error: print("Error fetching quota definition with name {}. {}".format( name, error)) raise
def get_network_acl_rule_by_name(self, acl, name): """Retrieve specific rule for a specific network ACL by name :param acl: Network ACL name or ID :type acl: str :param name: Rule name :type name: 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: # Retrieve network ACL rules data = self.get_network_acl_rules() if "errors" in data: return data # Loop over network ACL rules until filter match for rule in data['rules']: if rule["name"] == name: # Return data return rule # Return error if no network ACL is found return resource_not_found() except Exception as error: print("Error fetching rule with name {} for network ACL" "with ID {}. {}".format(name, acl_info["id"], error)) raise
def get_network_acl_by_name(self, name): """Retrieve specific network ACL by name :param name: Network ACL name :type name: str :return: Network ACL information :rtype: dict """ try: # Retrieve network ACLs data = self.get_network_acls() if "errors" in data: return data # Loop over network ACLs until filter match for acl in data['network_acls']: if acl["name"] == name: # Return data return acl # Return error if no network ACL is found return resource_not_found() except Exception as error: print("Error fetching network ACL with name {}. {}".format( name, error)) raise
def get_volume_by_name(self, instance, name): """Retrieve specific volume by name :param instance: Cloud instance ID :type instance: str :param name: Volume name :type name: str :return: 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 # Retrieve volumes data = self.get_volumes(ci_info["name"]) if "errors" in data: return data # Loop over volumes until filter match for volume in data['volumes']: if volume["name"] == name: # Return data return volume # Return error if no volume is found return resource_not_found() except Exception as error: print("Error fetching volume with name {} for cloud instance {}." " {}".format(name, instance, error))
def delete_record(self, **kwargs): """Delete a record :param record: Record name :type record: str :param zone: Zone name :type zone: str """ args = set(["record", "zone"]) check_args(args, **kwargs) args = { 'record': kwargs.get('record'), 'zone': kwargs.get('zone'), } record = self.get_record(record=args["record"], zone=args["zone"]) if "errors" in record: for key_name in record["errors"]: if key_name["code"] == "not_found": return resource_not_found() try: self.dns.delete_record(record["id"]) except Exception as error: print("Error while deleting record. {}".format(error)) raise
def get_pvm_by_name(self, instance, name): """Retrieve specific Power Virtual Instance by name :param instance: Cloud instance ID :type instance: str :param name: Power Virtual Instance name :type name: str :return: PVM 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 # Retrieve pvms data = self.get_pvms(ci_info["name"]) if "errors" in data: return data # Loop over pvms until filter match for pvm in data['pvmInstances']: if pvm["serverName"] == name: # Return data return pvm # Return error if no pvm is found return resource_not_found() except Exception as error: print("Error fetching Power Virtual Instance with name {} for" " cloud instance {}. {}".format(name, instance, error))