summaryrefslogtreecommitdiff
path: root/novaclient/v2/flavor_access.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/flavor_access.py')
-rw-r--r--novaclient/v2/flavor_access.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/novaclient/v2/flavor_access.py b/novaclient/v2/flavor_access.py
new file mode 100644
index 0000000..ca78bdf
--- /dev/null
+++ b/novaclient/v2/flavor_access.py
@@ -0,0 +1,70 @@
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
16"""Flavor access interface."""
17
18from novaclient import base
19from novaclient.i18n import _
20
21
22class FlavorAccess(base.Resource):
23 def __repr__(self):
24 return ("<FlavorAccess flavor id: %s, tenant id: %s>" %
25 (self.flavor_id, self.tenant_id))
26
27
28class FlavorAccessManager(base.ManagerWithFind):
29 """
30 Manage :class:`FlavorAccess` resources.
31 """
32 resource_class = FlavorAccess
33
34 def list(self, **kwargs):
35 if kwargs.get('flavor'):
36 return self._list_by_flavor(kwargs['flavor'])
37 elif kwargs.get('tenant'):
38 return self._list_by_tenant(kwargs['tenant'])
39 else:
40 raise NotImplementedError(_('Unknown list options.'))
41
42 def _list_by_flavor(self, flavor):
43 return self._list('/flavors/%s/os-flavor-access' % base.getid(flavor),
44 'flavor_access')
45
46 def _list_by_tenant(self, tenant):
47 """Print flavor list shared with the given tenant."""
48 # TODO(uni): need to figure out a proper URI for list_by_tenant
49 # since current API already provided current tenant_id information
50 raise NotImplementedError(_('Sorry, query by tenant not supported.'))
51
52 def add_tenant_access(self, flavor, tenant):
53 """Add a tenant to the given flavor access list."""
54 info = {'tenant': tenant}
55 return self._action('addTenantAccess', flavor, info)
56
57 def remove_tenant_access(self, flavor, tenant):
58 """Remove a tenant from the given flavor access list."""
59 info = {'tenant': tenant}
60 return self._action('removeTenantAccess', flavor, info)
61
62 def _action(self, action, flavor, info, **kwargs):
63 """Perform a flavor action."""
64 body = {action: info}
65 self.run_hooks('modify_body_for_action', body, **kwargs)
66 url = '/flavors/%s/action' % base.getid(flavor)
67 _resp, body = self.api.client.post(url, body=body)
68
69 return [self.resource_class(self, res)
70 for res in body['flavor_access']]