def test_validate_no_script_okay(self): stack_name = "srv_val" (t, stack) = self._setup_test_stack(stack_name) # create an server with non exist image Id t["Resources"]["WebServer"]["Properties"]["image"] = "1" server = cloud_server.CloudServer("server_create_image_err", t["Resources"]["WebServer"], stack) self.m.StubOutWithMock(server, "nova") server.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(clients.OpenStackClients, "nova") clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(server.__class__, "script") server.script = None self.m.StubOutWithMock(server.__class__, "has_userdata") server.has_userdata = False self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like("1").MultipleTimes().AndReturn(True) self.m.ReplayAll() self.assertIsNone(server.validate()) self.m.VerifyAll()
def test_server_create_with_image_id(self): return_server = self.fc.servers.list()[1] server = self._setup_test_server(return_server, 'test_server_create_image_id', image_id='1', override_name=True) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() scheduler.TaskRunner(server.create)() # this makes sure the auto increment worked on server creation self.assertTrue(server.id > 0) public_ip = return_server.networks['public'][0] self.assertEqual( server.FnGetAtt('addresses')['public'][0]['addr'], public_ip) self.assertEqual(server.FnGetAtt('networks')['public'][0], public_ip) private_ip = return_server.networks['private'][0] self.assertEqual( server.FnGetAtt('addresses')['private'][0]['addr'], private_ip) self.assertEqual(server.FnGetAtt('networks')['private'][0], private_ip) self.assertIn(server.FnGetAtt('first_address'), (private_ip, public_ip)) self.m.VerifyAll()
def test_validate_no_script_okay(self): stack_name = 'srv_val' (t, stack) = self._setup_test_stack(stack_name) # create an server with non exist image Id t['Resources']['WebServer']['Properties']['image'] = '1' server = cloud_server.CloudServer('server_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(server, 'nova') server.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(clients.OpenStackClients, 'nova') clients.OpenStackClients.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(server.__class__, 'script') server.script = None self.m.StubOutWithMock(server.__class__, 'has_userdata') server.has_userdata = False self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').MultipleTimes().AndReturn(True) self.m.ReplayAll() self.assertIsNone(server.validate()) self.m.VerifyAll()
def test_server_create_with_image_id(self): return_server = self.fc.servers.list()[1] server = self._setup_test_server(return_server, 'test_server_create_image_id', image_id='1', override_name=True) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() scheduler.TaskRunner(server.create)() # this makes sure the auto increment worked on server creation self.assertTrue(server.id > 0) public_ip = return_server.networks['public'][0] self.assertEqual( server.FnGetAtt('addresses')['public'][0]['addr'], public_ip) self.assertEqual( server.FnGetAtt('networks')['public'][0], public_ip) private_ip = return_server.networks['private'][0] self.assertEqual( server.FnGetAtt('addresses')['private'][0]['addr'], private_ip) self.assertEqual( server.FnGetAtt('networks')['private'][0], private_ip) self.assertIn( server.FnGetAtt('first_address'), (private_ip, public_ip)) self.m.VerifyAll()
def get_secgroup_uuids(self, security_groups): '''Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs ''' seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = self.client().list_security_groups() all_groups = response['security_groups'] same_name_groups = [g for g in all_groups if g['name'] == sg] groups = [g['id'] for g in same_name_groups] if len(groups) == 0: raise exception.PhysicalResourceNotFound(resource_id=sg) elif len(groups) == 1: seclist.append(groups[0]) else: # for admin roles, can get the other users' # securityGroups, so we should match the tenant_id with # the groups, and return the own one own_groups = [g['id'] for g in same_name_groups if g['tenant_id'] == self.context.tenant_id] if len(own_groups) == 1: seclist.append(own_groups[0]) else: raise exception.PhysicalResourceNameAmbiguity(name=sg) return seclist
def _get_image_id(self, image_identifier): image_id = None if uuidutils.is_uuid_like(image_identifier): try: image_id = self.nova().images.get(image_identifier).id except clients.novaclient.exceptions.NotFound: logger.info("Image %s was not found in glance" % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) else: try: image_list = self.nova().images.list() except clients.novaclient.exceptions.ClientException as ex: raise exception.ServerError(message=str(ex)) image_names = dict( (o.id, o.name) for o in image_list if o.name == image_identifier) if len(image_names) == 0: logger.info("Image %s was not found in glance" % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif len(image_names) > 1: logger.info("Mulitple images %s were found in glance with name" % image_identifier) raise exception.NoUniqueImageFound(image_name=image_identifier) image_id = image_names.popitem()[0] return image_id
def get_image_id(nova_client, image_identifier): ''' Return an id for the specified image name or identifier. :param nova_client: the nova client to use :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.NoUniqueImageFound ''' image_id = None if uuidutils.is_uuid_like(image_identifier): try: image_id = nova_client.images.get(image_identifier).id except clients.novaclient.exceptions.NotFound: logger.info("Image %s was not found in glance" % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) else: try: image_list = nova_client.images.list() except clients.novaclient.exceptions.ClientException as ex: raise exception.ServerError(message=str(ex)) image_names = dict( (o.id, o.name) for o in image_list if o.name == image_identifier) if len(image_names) == 0: logger.info("Image %s was not found in glance" % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif len(image_names) > 1: logger.info("Mulitple images %s were found in glance with name" % image_identifier) raise exception.NoUniqueImageFound(image_name=image_identifier) image_id = image_names.popitem()[0] return image_id
def get_secgroup_uuids(self, security_groups): '''Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs ''' seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = self.client().list_security_groups() all_groups = response['security_groups'] same_name_groups = [g for g in all_groups if g['name'] == sg] groups = [g['id'] for g in same_name_groups] if len(groups) == 0: raise exception.PhysicalResourceNotFound(resource_id=sg) elif len(groups) == 1: seclist.append(groups[0]) else: # for admin roles, can get the other users' # securityGroups, so we should match the tenant_id with # the groups, and return the own one own_groups = [ g['id'] for g in same_name_groups if g['tenant_id'] == self.context.tenant_id ] if len(own_groups) == 1: seclist.append(own_groups[0]) else: raise exception.PhysicalResourceNameAmbiguity(name=sg) return seclist
def get_image_id(nova_client, image_identifier): ''' Return an id for the specified image name or identifier. :param nova_client: the nova client to use :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.NoUniqueImageFound ''' image_id = None if uuidutils.is_uuid_like(image_identifier): try: image_id = nova_client.images.get(image_identifier).id except clients.novaclient.exceptions.NotFound: logger.info("Image %s was not found in glance" % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) else: try: image_list = nova_client.images.list() except clients.novaclient.exceptions.ClientException as ex: raise exception.Error( message="Error retrieving image list from nova: %s" % str(ex)) image_names = dict( (o.id, o.name) for o in image_list if o.name == image_identifier) if len(image_names) == 0: logger.info("Image %s was not found in glance" % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif len(image_names) > 1: logger.info("Mulitple images %s were found in glance with name" % image_identifier) raise exception.NoUniqueImageFound(image_name=image_identifier) image_id = image_names.popitem()[0] return image_id
def get_image_id(nova_client, image_identifier): """ Return an id for the specified image name or identifier. :param nova_client: the nova client to use :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.PhysicalResourceNameAmbiguity """ image_id = None if uuidutils.is_uuid_like(image_identifier): try: image_id = nova_client.images.get(image_identifier).id except clients.novaclient.exceptions.NotFound: logger.info(_("Image %s was not found in glance") % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) else: try: image_list = nova_client.images.list() except clients.novaclient.exceptions.ClientException as ex: raise exception.Error(message=(_("Error retrieving image list from nova: %s") % str(ex))) image_names = dict((o.id, o.name) for o in image_list if o.name == image_identifier) if len(image_names) == 0: logger.info(_("Image %s was not found in glance") % image_identifier) raise exception.ImageNotFound(image_name=image_identifier) elif len(image_names) > 1: logger.info(_("Mulitple images %s were found in glance with name") % image_identifier) raise exception.PhysicalResourceNameAmbiguity(name=image_identifier) image_id = image_names.popitem()[0] return image_id
def test_server_validate(self): stack_name = "srv_val" (t, stack) = self._setup_test_stack(stack_name) # create an server with non exist image Id t["Resources"]["WebServer"]["Properties"]["image"] = "1" server = servers.Server("server_create_image_err", t["Resources"]["WebServer"], stack) self.m.StubOutWithMock(server, "nova") server.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like("1").AndReturn(True) self.m.ReplayAll() self.assertEqual(server.validate(), None) self.m.VerifyAll()
def test_instance_validate(self): stack_name = "test_instance_validate_stack" (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t["Resources"]["WebServer"]["Properties"]["ImageId"] = "1" instance = instances.Instance("instance_create_image_err", t["Resources"]["WebServer"], stack) self.m.StubOutWithMock(instance, "nova") instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like("1").AndReturn(True) self.m.ReplayAll() self.assertEqual(instance.validate(), None) self.m.VerifyAll()
def test_server_validate(self): stack_name = 'srv_val' (t, stack) = self._setup_test_stack(stack_name) # create an server with non exist image Id t['Resources']['WebServer']['Properties']['image'] = '1' server = servers.Server('server_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(server, 'nova') server.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() self.assertEqual(server.validate(), None) self.m.VerifyAll()
def test_server_validate(self): stack_name = 'test_server_validate_stack' (t, stack) = self._setup_test_stack(stack_name) # create an server with non exist image Id t['Resources']['WebServer']['Properties']['image'] = '1' server = servers.Server('server_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(server, 'nova') server.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() self.assertEqual(server.validate(), None) self.m.VerifyAll()
def test_server_create_image_id_err(self): stack_name = "img_id_err" (t, stack) = self._setup_test_stack(stack_name) # create an server with non exist image Id t["Resources"]["WebServer"]["Properties"]["image"] = "1" server = servers.Server("server_create_image_err", t["Resources"]["WebServer"], stack) self.m.StubOutWithMock(server, "nova") server.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like("1").AndReturn(True) self.m.StubOutWithMock(self.fc.client, "get_images_1") self.fc.client.get_images_1().AndRaise(servers.clients.novaclient.exceptions.NotFound(404)) self.m.ReplayAll() self.assertRaises(exception.ImageNotFound, server.handle_create) self.m.VerifyAll()
def test_instance_validate(self): stack_name = 'test_instance_validate_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() self.assertIsNone(instance.validate()) self.m.VerifyAll()
def test_instance_validate(self): stack_name = 'test_instance_validate_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() self.assertEqual(instance.validate(), None) self.m.VerifyAll()
def test_instance_create_image_id_err(self): stack_name = 'test_instance_create_image_id_err_stack' (t, stack) = self._setup_test_stack(stack_name) # create an instance with non exist image Id t['Resources']['WebServer']['Properties']['ImageId'] = '1' instance = instances.Instance('instance_create_image_err', t['Resources']['WebServer'], stack) self.m.StubOutWithMock(instance, 'nova') instance.nova().MultipleTimes().AndReturn(self.fc) self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.StubOutWithMock(self.fc.client, "get_images_1") self.fc.client.get_images_1().AndRaise( instances.clients.novaclient.exceptions.NotFound(404)) self.m.ReplayAll() self.assertRaises(exception.ImageNotFound, instance.handle_create) self.m.VerifyAll()
def test_instance_create_with_image_id(self): return_server = self.fc.servers.list()[1] instance = self._setup_test_instance(return_server, 'in_create_imgid', image_id='1') self.m.StubOutWithMock(uuidutils, "is_uuid_like") uuidutils.is_uuid_like('1').AndReturn(True) self.m.ReplayAll() scheduler.TaskRunner(instance.create)() # this makes sure the auto increment worked on instance creation self.assertTrue(instance.id > 0) expected_ip = return_server.networks['public'][0] self.assertEqual(expected_ip, instance.FnGetAtt('PublicIp')) self.assertEqual(expected_ip, instance.FnGetAtt('PrivateIp')) self.assertEqual(expected_ip, instance.FnGetAtt('PublicDnsName')) self.assertEqual(expected_ip, instance.FnGetAtt('PrivateDnsName')) self.m.VerifyAll()
def identify_stack(self, cnxt, stack_name): """ The identify_stack method returns the full stack identifier for a single, live stack given the stack name. arg1 -> RPC context. arg2 -> Name or UUID of the stack to look up. """ if uuidutils.is_uuid_like(stack_name): s = db_api.stack_get(cnxt, stack_name, show_deleted=True) else: s = db_api.stack_get_by_name(cnxt, stack_name) if s: stack = parser.Stack.load(cnxt, stack=s) return dict(stack.identifier()) else: raise exception.StackNotFound(stack_name=stack_name)
def get_image_id(nova_client, image_identifier): ''' Return an id for the specified image name or identifier. :param nova_client: the nova client to use :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.PhysicalResourceNameAmbiguity ''' if uuidutils.is_uuid_like(image_identifier): try: image_id = nova_client.images.get(image_identifier).id except clients.novaclient.exceptions.NotFound: image_id = get_image_id_by_name(nova_client, image_identifier) else: image_id = get_image_id_by_name(nova_client, image_identifier) return image_id
def get_image_id(self, image_identifier): ''' Return an id for the specified image name or identifier. :param image_identifier: image name or a UUID-like identifier :returns: the id of the requested :image_identifier: :raises: exception.ImageNotFound, exception.PhysicalResourceNameAmbiguity ''' if uuidutils.is_uuid_like(image_identifier): try: image_id = self.client().images.get(image_identifier).id except exc.HTTPNotFound: image_id = self.get_image_id_by_name(image_identifier) else: image_id = self.get_image_id_by_name(image_identifier) return image_id
def identify_stack(self, cnxt, stack_name): """ The identify_stack method returns the full stack identifier for a single, live stack given the stack name. :param cnxt: RPC context. :param stack_name: Name or UUID of the stack to look up. """ if uuidutils.is_uuid_like(stack_name): s = db_api.stack_get(cnxt, stack_name, show_deleted=True) # may be the name is in uuid format, so if get by id returns None, # we should get the info by name again if not s: s = db_api.stack_get_by_name(cnxt, stack_name) else: s = db_api.stack_get_by_name(cnxt, stack_name) if s: stack = parser.Stack.load(cnxt, stack=s) return dict(stack.identifier()) else: raise exception.StackNotFound(stack_name=stack_name)
def get_secgroup_uuids(security_groups, client, tenant_id): ''' Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs client: reference to neutronclient tenant_id: the tenant id to match the security_groups ''' warnings.warn('neutron.NeutronResource.get_secgroup_uuids is ' 'deprecated. Use ' 'self.client_plugin("neutron").get_secgroup_uuids') seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = client.list_security_groups() all_groups = response['security_groups'] same_name_groups = [g for g in all_groups if g['name'] == sg] groups = [g['id'] for g in same_name_groups] if len(groups) == 0: raise exception.PhysicalResourceNotFound(resource_id=sg) elif len(groups) == 1: seclist.append(groups[0]) else: # for admin roles, can get the other users' # securityGroups, so we should match the tenant_id with # the groups, and return the own one own_groups = [ g['id'] for g in same_name_groups if g['tenant_id'] == tenant_id ] if len(own_groups) == 1: seclist.append(own_groups[0]) else: raise exception.PhysicalResourceNameAmbiguity(name=sg) return seclist
def _build_nics(self, networks): if not networks: return None nics = [] for net_data in networks: nic_info = {} if net_data.get(self.NETWORK_UUID): nic_info['net-id'] = net_data[self.NETWORK_UUID] label_or_uuid = net_data.get(self.NETWORK_ID) if label_or_uuid: if uuidutils.is_uuid_like(label_or_uuid): nic_info['net-id'] = label_or_uuid else: network = self.nova().networks.find(label=label_or_uuid) nic_info['net-id'] = network.id if net_data.get(self.NETWORK_FIXED_IP): nic_info['v4-fixed-ip'] = net_data[self.NETWORK_FIXED_IP] if net_data.get(self.NETWORK_PORT): nic_info['port-id'] = net_data[self.NETWORK_PORT] nics.append(nic_info) return nics
def get_secgroup_uuids(security_groups, client): ''' Returns a list of security group UUIDs. Args: security_groups: List of security group names or UUIDs client: reference to neutronclient ''' seclist = [] all_groups = None for sg in security_groups: if uuidutils.is_uuid_like(sg): seclist.append(sg) else: if not all_groups: response = client.list_security_groups() all_groups = response['security_groups'] groups = [g['id'] for g in all_groups if g['name'] == sg] if len(groups) == 0: raise exception.PhysicalResourceNotFound(resource_id=sg) if len(groups) > 1: raise exception.PhysicalResourceNameAmbiguity(name=sg) seclist.append(groups[0]) return seclist
def handle_create(self): ''' Create cloud database instance. ''' self.flavor = self.client_plugin().get_flavor_id( self.properties[self.FLAVOR]) self.volume = {'size': self.properties[self.SIZE]} self.databases = self.properties.get(self.DATABASES) self.users = self.properties.get(self.USERS) restore_point = self.properties.get(self.RESTORE_POINT) if restore_point: restore_point = {"backupRef": restore_point} zone = self.properties.get(self.AVAILABILITY_ZONE) self.datastore_type = self.properties.get(self.DATASTORE_TYPE) self.datastore_version = self.properties.get(self.DATASTORE_VERSION) # convert user databases to format required for troveclient. # that is, list of database dictionaries for user in self.users: dbs = [{'name': db} for db in user.get(self.USER_DATABASES, [])] user[self.USER_DATABASES] = dbs # convert networks to format required by troveclient nics = [] for nic in self.properties.get(self.NICS): nic_dict = {} net = nic.get(self.NET) if net: if uuidutils.is_uuid_like(net): net_id = net else: # using Nova for lookup to cover both neutron and # nova-network cases nova = self.client('nova') net_id = nova.networks.find(label=net).id nic_dict['net-id'] = net_id port = nic.get(self.PORT) if port: neutron = self.client_plugin('neutron') nic_dict['port-id'] = neutron.find_neutron_resource( self.properties, self.PORT, 'port') ip = nic.get(self.V4_FIXED_IP) if ip: nic_dict['v4-fixed-ip'] = ip nics.append(nic_dict) # create db instance instance = self.trove().instances.create( self._dbinstance_name(), self.flavor, volume=self.volume, databases=self.databases, users=self.users, restorePoint=restore_point, availability_zone=zone, datastore=self.datastore_type, datastore_version=self.datastore_version, nics=nics) self.resource_id_set(instance.id) return instance
def handle_create(self): security_groups = self.properties.get(self.SECURITY_GROUPS) user_data_format = self.properties.get(self.USER_DATA_FORMAT) ud_content = self.properties.get(self.USER_DATA) if self.user_data_software_config() or self.user_data_raw(): if uuidutils.is_uuid_like(ud_content): # attempt to load the userdata from software config try: ud_content = sc.SoftwareConfig.get_software_config( self.heat(), ud_content) except exception.SoftwareConfigMissing: # no config was found, so do not modify the user_data pass if self.user_data_software_config(): self._create_transport_credentials() if self.properties[self.ADMIN_USER]: instance_user = self.properties[self.ADMIN_USER] elif cfg.CONF.instance_user: instance_user = cfg.CONF.instance_user else: instance_user = None userdata = nova_utils.build_userdata(self, ud_content, instance_user=instance_user, user_data_format=user_data_format) flavor = self.properties[self.FLAVOR] availability_zone = self.properties[self.AVAILABILITY_ZONE] image = self.properties.get(self.IMAGE) if image: image = nova_utils.get_image_id(self.nova(), image) flavor_id = nova_utils.get_flavor_id(self.nova(), flavor) instance_meta = self.properties.get(self.METADATA) if instance_meta is not None: instance_meta = nova_utils.meta_serialize(instance_meta) scheduler_hints = self.properties.get(self.SCHEDULER_HINTS) nics = self._build_nics(self.properties.get(self.NETWORKS)) block_device_mapping = self._build_block_device_mapping( self.properties.get(self.BLOCK_DEVICE_MAPPING)) reservation_id = self.properties.get(self.RESERVATION_ID) config_drive = self.properties.get(self.CONFIG_DRIVE) disk_config = self.properties.get(self.DISK_CONFIG) admin_pass = self.properties.get(self.ADMIN_PASS) or None server = None try: server = self.nova().servers.create( name=self.physical_resource_name(), image=image, flavor=flavor_id, key_name=self._key_name(), security_groups=security_groups, userdata=userdata, meta=instance_meta, scheduler_hints=scheduler_hints, nics=nics, availability_zone=availability_zone, block_device_mapping=block_device_mapping, reservation_id=reservation_id, config_drive=config_drive, disk_config=disk_config, files=self._personality(), admin_pass=admin_pass) finally: # Avoid a race condition where the thread could be cancelled # before the ID is stored if server is not None: self.resource_id_set(server.id) return server
def handle_create(self): security_groups = self.properties.get(self.SECURITY_GROUPS) user_data_format = self.properties.get(self.USER_DATA_FORMAT) ud_content = self.properties.get(self.USER_DATA) if self.user_data_software_config() or self.user_data_raw(): if uuidutils.is_uuid_like(ud_content): # attempt to load the userdata from software config try: ud_content = self.heat().software_configs.get( ud_content).config except Exception as ex: self.client_plugin('heat').ignore_not_found(ex) metadata = self.metadata_get(True) or {} if self.user_data_software_config(): self._create_transport_credentials() self._populate_deployments_metadata(metadata) if self.properties[self.ADMIN_USER]: instance_user = self.properties[self.ADMIN_USER] elif cfg.CONF.instance_user: instance_user = cfg.CONF.instance_user else: instance_user = None userdata = self.client_plugin().build_userdata( metadata, ud_content, instance_user=instance_user, user_data_format=user_data_format) flavor = self.properties[self.FLAVOR] availability_zone = self.properties[self.AVAILABILITY_ZONE] image = self.properties.get(self.IMAGE) if image: image = self.client_plugin('glance').get_image_id(image) flavor_id = self.client_plugin().get_flavor_id(flavor) instance_meta = self.properties.get(self.METADATA) if instance_meta is not None: instance_meta = self.client_plugin().meta_serialize(instance_meta) scheduler_hints = self.properties.get(self.SCHEDULER_HINTS) nics = self._build_nics(self.properties.get(self.NETWORKS)) block_device_mapping = self._build_block_device_mapping( self.properties.get(self.BLOCK_DEVICE_MAPPING)) reservation_id = self.properties.get(self.RESERVATION_ID) disk_config = self.properties.get(self.DISK_CONFIG) admin_pass = self.properties.get(self.ADMIN_PASS) or None personality_files = self.properties.get(self.PERSONALITY) key_name = self.properties.get(self.KEY_NAME) server = None try: server = self.nova().servers.create( name=self._server_name(), image=image, flavor=flavor_id, key_name=key_name, security_groups=security_groups, userdata=userdata, meta=instance_meta, scheduler_hints=scheduler_hints, nics=nics, availability_zone=availability_zone, block_device_mapping=block_device_mapping, reservation_id=reservation_id, config_drive=self._config_drive(), disk_config=disk_config, files=personality_files, admin_pass=admin_pass) finally: # Avoid a race condition where the thread could be canceled # before the ID is stored if server is not None: self.resource_id_set(server.id) return server