def _parse_create_args(client, args): """Converts CLI arguments to args for VSManager.create_instance. :param dict args: CLI arguments """ data = {} if args.get('quantity'): data['quantity'] = int(args.get('quantity')) if args.get('postinstall'): data['provisionScripts'] = [args.get('postinstall')] if args.get('complex_type'): data['complexType'] = args.get('complex_type') if args.get('fqdn'): servers = [] for name in args.get('fqdn'): fqdn = name.split(".", 1) servers.append({'hostname': fqdn[0], 'domain': fqdn[1]}) data['hardware'] = servers if args.get('image'): if args.get('image').isdigit(): image_mgr = ImageManager(client) image_details = image_mgr.get_image(args.get('image'), mask="id,globalIdentifier") data['imageTemplateGlobalIdentifier'] = image_details[ 'globalIdentifier'] else: data['imageTemplateGlobalIdentifier'] = args['image'] userdata = None if args.get('userdata'): userdata = args['userdata'] elif args.get('userfile'): with open(args['userfile'], 'r') as userfile: userdata = userfile.read() if userdata: for hardware in data['hardware']: hardware['userData'] = [{'value': userdata}] # Get the SSH keys if args.get('key'): keys = [] for key in args.get('key'): resolver = SshKeyManager(client).resolve_ids key_id = helpers.resolve_id(resolver, key, 'SshKey') keys.append(key_id) data['sshKeys'] = keys return data
def _parse_create_args(client, args): """Converts CLI arguments to args for VSManager.create_instance. :param dict args: CLI arguments """ data = {} if args.get('quantity'): data['quantity'] = int(args.get('quantity')) if args.get('postinstall'): data['provisionScripts'] = [args.get('postinstall')] if args.get('complex_type'): data['complexType'] = args.get('complex_type') if args.get('fqdn'): servers = [] for name in args.get('fqdn'): fqdn = name.split(".", 1) servers.append({'hostname': fqdn[0], 'domain': fqdn[1]}) data['hardware'] = servers if args.get('image'): if args.get('image').isdigit(): image_mgr = ImageManager(client) image_details = image_mgr.get_image(args.get('image'), mask="id,globalIdentifier") data['imageTemplateGlobalIdentifier'] = image_details['globalIdentifier'] else: data['imageTemplateGlobalIdentifier'] = args['image'] userdata = None if args.get('userdata'): userdata = args['userdata'] elif args.get('userfile'): with open(args['userfile'], 'r') as userfile: userdata = userfile.read() if userdata: for hardware in data['hardware']: hardware['userData'] = [{'value': userdata}] # Get the SSH keys if args.get('key'): keys = [] for key in args.get('key'): resolver = SshKeyManager(client).resolve_ids key_id = helpers.resolve_id(resolver, key, 'SshKey') keys.append(key_id) data['sshKeys'] = keys return data
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)
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
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)
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)
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)
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)
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)