def test_validate_okay(self): test_res = self._get_test_resource(self.kp_template) self.m.StubOutWithMock(nova_utils, 'get_keypair') nova_utils.get_keypair(mox.IgnoreArg(), 'key_pair').AndRaise( exception.UserKeyPairMissing(key_name='foo')) self.m.ReplayAll() self.assertIsNone(test_res.validate())
def get_keypair(self, key_name): """Get the public key specified by :key_name: :param key_name: the name of the key to look for :returns: the keypair (name, public_key) for :key_name: :raises: exception.UserKeyPairMissing """ try: return self.client().keypairs.get(key_name) except exceptions.NotFound: raise exception.UserKeyPairMissing(key_name=key_name)
def get_keypair(nova_client, key_name): ''' Get the public key specified by :key_name: :param nova_client: the nova client to use :param key_name: the name of the key to look for :returns: the keypair (name, public_key) for :key_name: :raises: exception.UserKeyPairMissing ''' try: return nova_client.keypairs.get(key_name) except nova_exceptions.NotFound: raise exception.UserKeyPairMissing(key_name=key_name)
def get_keypair(nova_client, key_name): ''' Get the public key specified by :key_name: :param nova_client: the nova client to use :param key_name: the name of the key to look for :returns: the keypair (name, public_key) for :key_name: :raises: exception.UserKeyPairMissing ''' for keypair in nova_client.keypairs.list(): if keypair.name == key_name: return keypair raise exception.UserKeyPairMissing(key_name=key_name)
def get_keypair(nova_client, key_name): warnings.warn('nova_utils.get_keypair is deprecated. ' 'Use self.client_plugin("nova").get_keypair') ''' Get the public key specified by :key_name: :param nova_client: the nova client to use :param key_name: the name of the key to look for :returns: the keypair (name, public_key) for :key_name: :raises: exception.UserKeyPairMissing ''' try: return nova_client.keypairs.get(key_name) except nova_exceptions.NotFound: raise exception.UserKeyPairMissing(key_name=key_name)
def validate(self): ''' Validate any of the provided params ''' res = super(Instance, self).validate() if res: return res # check validity of key key_name = self.properties.get('KeyName', None) if key_name: keypairs = self.nova().keypairs.list() if not any(k.name == key_name for k in keypairs): raise exception.UserKeyPairMissing(key_name=key_name) # check validity of security groups vs. network interfaces security_groups = self._get_security_groups() if security_groups and self.properties.get('NetworkInterfaces'): raise exception.ResourcePropertyConflict( 'SecurityGroups/SecurityGroupIds', 'NetworkInterfaces') # make sure the image exists. nova_utils.get_image_id(self.nova(), self.properties['ImageId'])
def handle_create(self): if self.properties.get('SecurityGroups') == None: security_groups = None else: security_groups = [self.physical_resource_name_find(sg) for sg in self.properties.get('SecurityGroups')] userdata = self.properties['UserData'] userdata += '\ntouch /var/lib/cloud/instance/provision-finished\n' flavor = self.properties['InstanceType'] key_name = self.properties['KeyName'] keypairs = [k.name for k in self.nova().keypairs.list()] if key_name not in keypairs: raise exception.UserKeyPairMissing(key_name=key_name) image_name = self.properties['ImageId'] image_id = None image_list = self.nova().images.list() for o in image_list: if o.name == image_name: image_id = o.id if image_id is None: logger.info("Image %s was not found in glance" % image_name) raise exception.ImageNotFound(image_name=image_name) flavor_list = self.nova().flavors.list() for o in flavor_list: if o.name == flavor: flavor_id = o.id tags = {} if self.properties['Tags']: for tm in self.properties['Tags']: tags[tm['Key']] = tm['Value'] else: tags = None scheduler_hints = {} if self.properties['NovaSchedulerHints']: for tm in self.properties['NovaSchedulerHints']: scheduler_hints[tm['Key']] = tm['Value'] else: scheduler_hints = None server_userdata = self._build_userdata(userdata) server = self.nova().servers.create(name=self.physical_resource_name(), image=image_id, flavor=flavor_id, key_name=key_name, security_groups=security_groups, userdata=server_userdata, meta=tags, scheduler_hints=scheduler_hints) while server.status == 'BUILD': server.get() eventlet.sleep(1) if server.status == 'ACTIVE': self.instance_id_set(server.id) self._set_ipaddress(server.networks) else: raise exception.Error('%s instance[%s] status[%s]' % ('nova reported unexpected', self.name, server.status))
def handle_create(self): if self.properties.get('SecurityGroups') is None: security_groups = None else: security_groups = [ self.physical_resource_name_find(sg) for sg in self.properties.get('SecurityGroups') ] userdata = self.properties['UserData'] or '' flavor = self.properties['InstanceType'] key_name = self.properties['KeyName'] availability_zone = self.properties['AvailabilityZone'] keypairs = [k.name for k in self.nova().keypairs.list()] if key_name not in keypairs and key_name is not None: raise exception.UserKeyPairMissing(key_name=key_name) image_name = self.properties['ImageId'] image_id = None image_list = self.nova().images.list() for o in image_list: if o.name == image_name: image_id = o.id break if image_id is None: logger.info("Image %s was not found in glance" % image_name) raise exception.ImageNotFound(image_name=image_name) flavor_id = None flavor_list = self.nova().flavors.list() for o in flavor_list: if o.name == flavor: flavor_id = o.id break if flavor_id is None: raise exception.FlavorMissing(flavor_id=flavor) tags = {} if self.properties['Tags']: for tm in self.properties['Tags']: tags[tm['Key']] = tm['Value'] else: tags = None scheduler_hints = {} if self.properties['NovaSchedulerHints']: for tm in self.properties['NovaSchedulerHints']: scheduler_hints[tm['Key']] = tm['Value'] else: scheduler_hints = None nics = self._build_nics(self.properties['NetworkInterfaces'], subnet_id=self.properties['SubnetId']) server_userdata = self._build_userdata(userdata) server = None try: server = self.nova().servers.create( name=self.physical_resource_name(), image=image_id, flavor=flavor_id, key_name=key_name, security_groups=security_groups, userdata=server_userdata, meta=tags, scheduler_hints=scheduler_hints, nics=nics, availability_zone=availability_zone) 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