OpenStack Compute (Nova) Client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

flavor_access.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright 2012 OpenStack Foundation
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """Flavor access interface."""
  16. from novaclient import base
  17. from novaclient.i18n import _
  18. class FlavorAccess(base.Resource):
  19. def __repr__(self):
  20. return ("<FlavorAccess flavor id: %s, tenant id: %s>" %
  21. (self.flavor_id, self.tenant_id))
  22. class FlavorAccessManager(base.ManagerWithFind):
  23. """
  24. Manage :class:`FlavorAccess` resources.
  25. """
  26. resource_class = FlavorAccess
  27. def list(self, **kwargs):
  28. if kwargs.get('flavor'):
  29. return self._list_by_flavor(kwargs['flavor'])
  30. elif kwargs.get('tenant'):
  31. return self._list_by_tenant(kwargs['tenant'])
  32. else:
  33. raise NotImplementedError(_('Unknown list options.'))
  34. def _list_by_flavor(self, flavor):
  35. return self._list('/flavors/%s/os-flavor-access' % base.getid(flavor),
  36. 'flavor_access')
  37. def _list_by_tenant(self, tenant):
  38. """Print flavor list shared with the given tenant."""
  39. # TODO(uni): need to figure out a proper URI for list_by_tenant
  40. # since current API already provided current tenant_id information
  41. raise NotImplementedError(_('Sorry, query by tenant not supported.'))
  42. def add_tenant_access(self, flavor, tenant):
  43. """Add a tenant to the given flavor access list."""
  44. info = {'tenant': tenant}
  45. return self._action('addTenantAccess', flavor, info)
  46. def remove_tenant_access(self, flavor, tenant):
  47. """Remove a tenant from the given flavor access list."""
  48. info = {'tenant': tenant}
  49. return self._action('removeTenantAccess', flavor, info)
  50. def _action(self, action, flavor, info, **kwargs):
  51. """Perform a flavor action."""
  52. body = {action: info}
  53. self.run_hooks('modify_body_for_action', body, **kwargs)
  54. url = '/flavors/%s/action' % base.getid(flavor)
  55. _resp, body = self.api.client.post(url, body=body)
  56. return [self.resource_class(self, res)
  57. for res in body['flavor_access']]