Example #1
0
    def destroy_node(self, node_id):
        session = get_session()
        with session.begin():
            query = model_query(models.Node, session=session)
            query = add_identity_filter(query, node_id)

            try:
                node_ref = query.one()
            except NoResultFound:
                raise exception.NodeNotFound(node=node_id)
            if node_ref['reservation'] is not None:
                raise exception.NodeLocked(node=node_id)
            if node_ref['instance_uuid'] is not None:
                raise exception.NodeAssociated(node=node_id,
                                            instance=node_ref['instance_uuid'])

            # Get node ID, if an UUID was supplied. The ID is
            # required for deleting all ports, attached to the node.
            if utils.is_uuid_like(node_id):
                node_id = node_ref['id']

            port_query = model_query(models.Port, session=session)
            port_query = add_port_filter_by_node(port_query, node_id)
            port_query.delete()

            query.delete()
Example #2
0
 def test_create_port_generate_uuid(self):
     pdict = post_get_test_port()
     del pdict["uuid"]
     response = self.post_json("/ports", pdict)
     result = self.get_json("/ports/%s" % response.json["uuid"])
     self.assertEqual(pdict["address"], result["address"])
     self.assertTrue(utils.is_uuid_like(result["uuid"]))
Example #3
0
 def test_create_port_generate_uuid(self):
     pdict = post_get_test_port()
     del pdict['uuid']
     response = self.post_json('/ports', pdict)
     result = self.get_json('/ports/%s' % response.json['uuid'])
     self.assertEqual(pdict['address'], result['address'])
     self.assertTrue(utils.is_uuid_like(result['uuid']))
Example #4
0
 def test_create_port_generate_uuid(self):
     pdict = post_get_test_port()
     del pdict['uuid']
     response = self.post_json('/ports', pdict)
     result = self.get_json('/ports/%s' % response.json['uuid'])
     self.assertEqual(pdict['address'], result['address'])
     self.assertTrue(utils.is_uuid_like(result['uuid']))
Example #5
0
 def test_create_chassis_generate_uuid(self):
     cdict = apiutils.chassis_post_data()
     del cdict['uuid']
     self.post_json('/chassis', cdict)
     result = self.get_json('/chassis')
     self.assertEqual(cdict['description'],
                      result['chassis'][0]['description'])
     self.assertTrue(utils.is_uuid_like(result['chassis'][0]['uuid']))
Example #6
0
 def test_create_chassis_generate_uuid(self):
     cdict = apiutils.chassis_post_data()
     del cdict['uuid']
     self.post_json('/chassis', cdict)
     result = self.get_json('/chassis')
     self.assertEqual(cdict['description'],
                      result['chassis'][0]['description'])
     self.assertTrue(utils.is_uuid_like(result['chassis'][0]['uuid']))
Example #7
0
    def get(cls, context, chassis_id):
        """Find a chassis based on its id or uuid and return a Chassis object.

        :param chassis_id: the id *or* uuid of a chassis.
        :returns: a :class:`Chassis` object.
        """
        if utils.is_int_like(chassis_id):
            return cls.get_by_id(context, chassis_id)
        elif utils.is_uuid_like(chassis_id):
            return cls.get_by_uuid(context, chassis_id)
        else:
            raise exception.InvalidIdentity(identity=chassis_id)
Example #8
0
    def get(cls, context, node_id):
        """Find a node based on its id or uuid and return a Node object.

        :param node_id: the id *or* uuid of a node.
        :returns: a :class:`Node` object.
        """
        if utils.is_int_like(node_id):
            return cls.get_by_id(context, node_id)
        elif utils.is_uuid_like(node_id):
            return cls.get_by_uuid(context, node_id)
        else:
            raise exception.InvalidIdentity(identity=node_id)
Example #9
0
    def get_node_by_instance(self, instance):
        if not utils.is_uuid_like(instance):
            raise exception.InvalidUUID(uuid=instance)

        query = (model_query(models.Node).filter_by(instance_uuid=instance))

        try:
            result = query.one()
        except NoResultFound:
            raise exception.InstanceNotFound(instance=instance)

        return result
Example #10
0
    def get(cls, context, node_id):
        """Find a node based on its id or uuid and return a Node object.

        :param node_id: the id *or* uuid of a node.
        :returns: a :class:`Node` object.
        """
        if utils.is_int_like(node_id):
            return cls.get_by_id(context, node_id)
        elif utils.is_uuid_like(node_id):
            return cls.get_by_uuid(context, node_id)
        else:
            raise exception.InvalidIdentity(identity=node_id)
Example #11
0
    def get(cls, context, chassis_id):
        """Find a chassis based on its id or uuid and return a Chassis object.

        :param chassis_id: the id *or* uuid of a chassis.
        :returns: a :class:`Chassis` object.
        """
        if utils.is_int_like(chassis_id):
            return cls.get_by_id(context, chassis_id)
        elif utils.is_uuid_like(chassis_id):
            return cls.get_by_uuid(context, chassis_id)
        else:
            raise exception.InvalidIdentity(identity=chassis_id)
Example #12
0
File: api.py Project: nkaul/ironic
    def get_node_by_instance(self, instance):
        if not utils.is_uuid_like(instance):
            raise exception.InvalidUUID(uuid=instance)

        query = model_query(models.Node).filter_by(instance_uuid=instance)

        try:
            result = query.one()
        except NoResultFound:
            raise exception.InstanceNotFound(instance=instance)

        return result
Example #13
0
    def get(cls, context, port_id):
        """Find a port based on its id or uuid and return a Port object.

        :param port_id: the id *or* uuid of a port.
        :returns: a :class:`Port` object.
        """
        if utils.is_int_like(port_id):
            return cls.get_by_id(context, port_id)
        elif utils.is_uuid_like(port_id):
            return cls.get_by_uuid(context, port_id)
        elif utils.is_valid_mac(port_id):
            return cls.get_by_address(context, port_id)
        else:
            raise exception.InvalidIdentity(identity=port_id)
Example #14
0
    def swift_temp_url(self, image_info):
        """Generate a no-auth Swift temporary URL.

        This function will generate the temporary Swift URL using the image
        id from Glance and the config options: 'swift_endpoint_url',
        'swift_api_version', 'swift_account' and 'swift_container'.
        The temporary URL will be valid for 'swift_temp_url_duration' seconds.
        This allows Ironic to download a Glance image without passing around
        an auth_token.

        :param image_info: The return from a GET request to Glance for a
            certain image_id. Should be a dictionary, with keys like 'name' and
            'checksum'. See
            http://docs.openstack.org/developer/glance/glanceapi.html for
            examples.
        :returns: A signed Swift URL from which an image can be downloaded,
            without authentication.

        :raises: InvalidParameterValue if Swift config options are not set
            correctly.
        :raises: MissingParameterValue if a required parameter is not set.
        :raises: ImageUnacceptable if the image info from Glance does not
            have a image ID.
        """
        self._validate_temp_url_config()

        if ('id' not in image_info or not
                utils.is_uuid_like(image_info['id'])):
            raise exc.ImageUnacceptable(_(
                'The given image info does not have a valid image id: %s')
                % image_info)

        url_fragments = {
            'endpoint_url': CONF.glance.swift_endpoint_url,
            'api_version': CONF.glance.swift_api_version,
            'account': CONF.glance.swift_account,
            'container': self._get_swift_container(image_info['id']),
            'object_id': image_info['id']
        }

        template = '/{api_version}/{account}/{container}/{object_id}'
        url_path = template.format(**url_fragments)
        path = swift_utils.generate_temp_url(
            path=url_path,
            seconds=CONF.glance.swift_temp_url_duration,
            key=CONF.glance.swift_temp_url_key,
            method='GET')

        return '{endpoint_url}{url_path}'.format(
            endpoint_url=url_fragments['endpoint_url'], url_path=path)
Example #15
0
    def swift_temp_url(self, image_info):
        """Generate a no-auth Swift temporary URL.

        This function will generate the temporary Swift URL using the image
        id from Glance and the config options: 'swift_endpoint_url',
        'swift_api_version', 'swift_account' and 'swift_container'.
        The temporary URL will be valid for 'swift_temp_url_duration' seconds.
        This allows Ironic to download a Glance image without passing around
        an auth_token.

        :param image_info: The return from a GET request to Glance for a
            certain image_id. Should be a dictionary, with keys like 'name' and
            'checksum'. See
            http://docs.openstack.org/developer/glance/glanceapi.html for
            examples.
        :returns: A signed Swift URL from which an image can be downloaded,
            without authentication.

        :raises: InvalidParameterValue if Swift config options are not set
            correctly.
        :raises: MissingParameterValue if a required parameter is not set.
        :raises: ImageUnacceptable if the image info from Glance does not
            have a image ID.
        """
        self._validate_temp_url_config()

        if ('id' not in image_info or not
                utils.is_uuid_like(image_info['id'])):
            raise exc.ImageUnacceptable(_(
                'The given image info does not have a valid image id: %s')
                % image_info)

        url_fragments = {
            'endpoint_url': CONF.glance.swift_endpoint_url,
            'api_version': CONF.glance.swift_api_version,
            'account': CONF.glance.swift_account,
            'container': self._get_swift_container(image_info['id']),
            'object_id': image_info['id']
        }

        template = '/{api_version}/{account}/{container}/{object_id}'
        url_path = template.format(**url_fragments)
        path = swift_utils.generate_temp_url(
            path=url_path,
            seconds=CONF.glance.swift_temp_url_duration,
            key=CONF.glance.swift_temp_url_key,
            method='GET')

        return '{endpoint_url}{url_path}'.format(
            endpoint_url=url_fragments['endpoint_url'], url_path=path)
Example #16
0
    def get(cls, context, port_id):
        """Find a port based on its id or uuid and return a Port object.

        :param port_id: the id *or* uuid of a port.
        :returns: a :class:`Port` object.
        """
        if utils.is_int_like(port_id):
            return cls.get_by_id(context, port_id)
        elif utils.is_uuid_like(port_id):
            return cls.get_by_uuid(context, port_id)
        elif utils.is_valid_mac(port_id):
            return cls.get_by_address(context, port_id)
        else:
            raise exception.InvalidIdentity(identity=port_id)
Example #17
0
def add_identity_filter(query, value):
    """Adds an identity filter to a query.

    Filters results by ID, if supplied value is a valid integer.
    Otherwise attempts to filter results by UUID.

    :param query: Initial query to add filter to.
    :param value: Value for filtering results by.
    :return: Modified query.
    """
    if utils.is_int_like(value):
        return query.filter_by(id=value)
    elif utils.is_uuid_like(value):
        return query.filter_by(uuid=value)
    else:
        raise exception.InvalidIdentity(identity=value)
Example #18
0
def add_identity_filter(query, value):
    """Adds an identity filter to a query.

    Filters results by ID, if supplied value is a valid integer.
    Otherwise attempts to filter results by UUID.

    :param query: Initial query to add filter to.
    :param value: Value for filtering results by.
    :return: Modified query.
    """
    if utils.is_int_like(value):
        return query.filter_by(id=value)
    elif utils.is_uuid_like(value):
        return query.filter_by(uuid=value)
    else:
        raise exception.InvalidIdentity(identity=value)
Example #19
0
def add_filter_by_many_identities(query, model, values):
    """Adds an identity filter to a query for values list.

    Filters results by ID, if supplied values contain a valid integer.
    Otherwise attempts to filter results by UUID.

    :param query: Initial query to add filter to.
    :param model: Model for filter.
    :param values: Values for filtering results by.
    :return: tuple (Modified query, filter field name).
    """
    if not values:
        raise exception.InvalidIdentity(identity=values)
    value = values[0]
    if utils.is_int_like(value):
        return query.filter(getattr(model, 'id').in_(values)), 'id'
    elif utils.is_uuid_like(value):
        return query.filter(getattr(model, 'uuid').in_(values)), 'uuid'
    else:
        raise exception.InvalidIdentity(identity=value)
Example #20
0
    def validate(self, node=None, node_uuid=None):
        """Validate the driver interfaces, using the node's UUID or name.

        Note that the 'node_uuid' interface is deprecated in favour
        of the 'node' interface

        :param node: UUID or name of a node.
        :param node_uuid: UUID of a node.
        """
        if node:
            # We're invoking this interface using positional notation, or
            # explicitly using 'node'.  Try and determine which one.
            if not allow_logical_names() and not utils.is_uuid_like(node):
                raise exception.NotAcceptable()

        rpc_node = _get_rpc_node(node_uuid or node)

        topic = pecan.request.rpcapi.get_topic_for(rpc_node)
        return pecan.request.rpcapi.validate_driver_interfaces(
                pecan.request.context, rpc_node.uuid, topic)
Example #21
0
def add_filter_by_many_identities(query, model, values):
    """Adds an identity filter to a query for values list.

    Filters results by ID, if supplied values contain a valid integer.
    Otherwise attempts to filter results by UUID.

    :param query: Initial query to add filter to.
    :param model: Model for filter.
    :param values: Values for filtering results by.
    :return: tuple (Modified query, filter field name).
    """
    if not values:
        raise exception.InvalidIdentity(identity=values)
    value = values[0]
    if utils.is_int_like(value):
        return query.filter(getattr(model, 'id').in_(values)), 'id'
    elif utils.is_uuid_like(value):
        return query.filter(getattr(model, 'uuid').in_(values)), 'uuid'
    else:
        raise exception.InvalidIdentity(identity=value)
Example #22
0
    def destroy_node(self, node_id):
        session = get_session()
        with session.begin():
            query = model_query(models.Node, session=session)
            query = add_identity_filter(query, node_id)

            try:
                node_ref = query.one()
            except NoResultFound:
                raise exception.NodeNotFound(node=node_id)

            # Get node ID, if an UUID was supplied. The ID is
            # required for deleting all ports, attached to the node.
            if utils.is_uuid_like(node_id):
                node_id = node_ref['id']

            port_query = model_query(models.Port, session=session)
            port_query = add_port_filter_by_node(port_query, node_id)
            port_query.delete()

            query.delete()
Example #23
0
def _get_rpc_node(node_ident):
    """Get the RPC node from the node uuid or logical name.

    :param node_ident: the UUID or logical name of a node.

    :returns: The RPC Node.
    :raises: InvalidUuidOrName if the name or uuid provided is not valid.
    :raises: NodeNotFound if the node is not found.

    """
    # Check to see if the node_ident is a valid UUID.  If it is, treat it
    # as a UUID.
    if utils.is_uuid_like(node_ident):
        return objects.Node.get_by_uuid(pecan.request.context, node_ident)

    # If it was not UUID-like, but it is name-like, and we allow names,
    # check for nodes by that name
    if allow_logical_names() and utils.is_hostname_safe(node_ident):
        return objects.Node.get_by_name(pecan.request.context, node_ident)

    # It's not a valid uuid, or it's not a valid name, or we don't allow names
    raise exception.InvalidUuidOrName(name=node_ident)
Example #24
0
 def validate(value):
     if not utils.is_uuid_like(value):
         raise exception.InvalidUUID(uuid=value)
     return value
Example #25
0
 def test_name_is_uuid_like(self):
     self.assertFalse(utils.is_uuid_like('zhongyueluo'))
Example #26
0
 def validate(value):
     if not utils.is_uuid_like(value):
         raise exception.InvalidUUID(uuid=value)
     return value
Example #27
0
 def test_is_uuid_like(self):
     self.assertTrue(utils.is_uuid_like(str(uuid.uuid4())))
Example #28
0
 def test_id_is_uuid_like(self):
     self.assertFalse(utils.is_uuid_like(1234567))
Example #29
0
 def test_is_uuid_like(self):
     self.assertTrue(utils.is_uuid_like(str(uuid.uuid4())))
Example #30
0
 def test_id_is_uuid_like(self):
     self.assertFalse(utils.is_uuid_like(1234567))
Example #31
0
 def validate(value):
     if not (utils.is_uuid_like(value) or utils.is_hostname_safe(value)):
         raise exception.InvalidUuidOrName(name=value)
     return value
Example #32
0
def is_valid_name(name):
    return utils.is_hostname_safe(name) and (not utils.is_uuid_like(name))
Example #33
0
 def test_name_is_uuid_like(self):
     self.assertFalse(utils.is_uuid_like('zhongyueluo'))