~ubuntu-branches/ubuntu/trusty/horizon/trusty-updates

« back to all changes in this revision

Viewing changes to openstack_dashboard/api/cinder.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-06 16:53:28 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20140306165328-w2vgmtfriqlhp27m
Tags: 1:2014.1~b3-0ubuntu1
* New upstream milestone release.
* d/static/*: Refreshed assets for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from django.conf import settings
28
28
from django.utils.translation import ugettext_lazy as _
29
29
 
30
 
from cinderclient.v1 import client as cinder_client
31
30
from cinderclient.v1.contrib import list_extensions as cinder_list_extensions
32
31
 
33
32
from horizon import exceptions
44
43
DEFAULT_QUOTA_NAME = 'default'
45
44
 
46
45
 
 
46
VERSIONS = base.APIVersionManager("volume", preferred_version=1)
 
47
 
 
48
try:
 
49
    from cinderclient.v1 import client as cinder_client_v1
 
50
    VERSIONS.load_supported_version(1, {"client": cinder_client_v1,
 
51
                                        "version": 1})
 
52
except ImportError:
 
53
    pass
 
54
 
 
55
try:
 
56
    from cinderclient.v2 import client as cinder_client_v2
 
57
    VERSIONS.load_supported_version(2, {"client": cinder_client_v2,
 
58
                                        "version": 2})
 
59
except ImportError:
 
60
    pass
 
61
 
 
62
 
 
63
class BaseCinderAPIResourceWrapper(base.APIResourceWrapper):
 
64
 
 
65
    @property
 
66
    def name(self):
 
67
        # If a volume doesn't have a name, use its id.
 
68
        return (getattr(self._apiresource, 'name', None) or
 
69
                getattr(self._apiresource, 'display_name', None) or
 
70
                getattr(self._apiresource, 'id', None))
 
71
 
 
72
    @property
 
73
    def description(self):
 
74
        return (getattr(self._apiresource, 'description', None) or
 
75
                getattr(self._apiresource, 'display_description', None))
 
76
 
 
77
 
 
78
class Volume(BaseCinderAPIResourceWrapper):
 
79
 
 
80
    _attrs = ['id', 'name', 'description', 'size', 'status', 'created_at',
 
81
              'volume_type', 'availability_zone', 'imageRef', 'bootable'
 
82
              'snapshot_id', 'source_volid', 'attachments', 'tenant_name',
 
83
              'os-vol-host-attr:host', 'os-vol-tenant-attr:tenant_id',
 
84
              'metadata']
 
85
 
 
86
 
 
87
class VolumeSnapshot(BaseCinderAPIResourceWrapper):
 
88
 
 
89
    _attrs = ['id', 'name', 'description', 'size', 'status',
 
90
              'created_at', 'volume_id',
 
91
              'os-extended-snapshot-attributes:project_id']
 
92
 
 
93
 
47
94
def cinderclient(request):
 
95
    api_version = VERSIONS.get_active_version()
 
96
 
48
97
    insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
49
98
    cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
50
99
    cinder_url = ""
51
100
    try:
52
 
        cinder_url = base.url_for(request, 'volume')
 
101
        # The cinder client assumes that the v2 endpoint type will be
 
102
        # 'volumev2'. However it also allows 'volume' type as a
 
103
        # fallback if the requested version is 2 and there is no
 
104
        # 'volumev2' endpoint.
 
105
        if api_version['version'] == 2:
 
106
            try:
 
107
                cinder_url = base.url_for(request, 'volumev2')
 
108
            except exceptions.ServiceCatalogException:
 
109
                LOG.warning("Cinder v2 requested but no 'volumev2' service "
 
110
                            "type available in Keystone catalog. Falling back "
 
111
                            "to 'volume'.")
 
112
        if cinder_url == "":
 
113
            cinder_url = base.url_for(request, 'volume')
53
114
    except exceptions.ServiceCatalogException:
54
115
        LOG.debug('no volume service configured.')
55
116
        return None
56
117
    LOG.debug('cinderclient connection created using token "%s" and url "%s"' %
57
118
              (request.user.token.id, cinder_url))
58
 
    c = cinder_client.Client(request.user.username,
59
 
                             request.user.token.id,
60
 
                             project_id=request.user.tenant_id,
61
 
                             auth_url=cinder_url,
62
 
                             insecure=insecure,
63
 
                             cacert=cacert,
64
 
                             http_log_debug=settings.DEBUG)
 
119
    c = api_version['client'].Client(request.user.username,
 
120
                                     request.user.token.id,
 
121
                                     project_id=request.user.tenant_id,
 
122
                                     auth_url=cinder_url,
 
123
                                     insecure=insecure,
 
124
                                     cacert=cacert,
 
125
                                     http_log_debug=settings.DEBUG)
65
126
    c.client.auth_token = request.user.token.id
66
127
    c.client.management_url = cinder_url
67
128
    return c
68
129
 
69
130
 
 
131
def _replace_v2_parameters(data):
 
132
    if VERSIONS.active < 2:
 
133
        data['display_name'] = data['name']
 
134
        data['display_description'] = data['description']
 
135
        del data['name']
 
136
        del data['description']
 
137
    return data
 
138
 
 
139
 
70
140
def volume_list(request, search_opts=None):
71
141
    """To see all volumes in the cloud as an admin you can pass in a special
72
142
    search option: {'all_tenants': 1}
74
144
    c_client = cinderclient(request)
75
145
    if c_client is None:
76
146
        return []
77
 
    return c_client.volumes.list(search_opts=search_opts)
 
147
    return [Volume(v) for v in c_client.volumes.list(search_opts=search_opts)]
78
148
 
79
149
 
80
150
def volume_get(request, volume_id):
89
159
            # the lack a server_id property; to work around that we'll
90
160
            # give the attached instance a generic name.
91
161
            attachment['instance_name'] = _("Unknown instance")
92
 
    return volume_data
 
162
    return Volume(volume_data)
93
163
 
94
164
 
95
165
def volume_create(request, size, name, description, volume_type,
96
166
                  snapshot_id=None, metadata=None, image_id=None,
97
 
                  availability_zone=None):
98
 
    return cinderclient(request).volumes.create(size, display_name=name,
99
 
            display_description=description, volume_type=volume_type,
100
 
            snapshot_id=snapshot_id, metadata=metadata, imageRef=image_id,
101
 
            availability_zone=availability_zone)
 
167
                  availability_zone=None, source_volid=None):
 
168
    data = {'name': name,
 
169
            'description': description,
 
170
            'volume_type': volume_type,
 
171
            'snapshot_id': snapshot_id,
 
172
            'metadata': metadata,
 
173
            'imageRef': image_id,
 
174
            'availability_zone': availability_zone,
 
175
            'source_volid': source_volid}
 
176
    data = _replace_v2_parameters(data)
 
177
 
 
178
    volume = cinderclient(request).volumes.create(size, **data)
 
179
    return Volume(volume)
 
180
 
 
181
 
 
182
def volume_extend(request, volume_id, new_size):
 
183
    return cinderclient(request).volumes.extend(volume_id, new_size)
102
184
 
103
185
 
104
186
def volume_delete(request, volume_id):
106
188
 
107
189
 
108
190
def volume_update(request, volume_id, name, description):
109
 
    vol_data = {'display_name': name,
110
 
                'display_description': description}
 
191
    vol_data = {'name': name,
 
192
                'description': description}
 
193
    vol_data = _replace_v2_parameters(vol_data)
111
194
    return cinderclient(request).volumes.update(volume_id,
112
195
                                                **vol_data)
113
196
 
114
197
 
115
198
def volume_snapshot_get(request, snapshot_id):
116
 
    return cinderclient(request).volume_snapshots.get(snapshot_id)
 
199
    snapshot = cinderclient(request).volume_snapshots.get(snapshot_id)
 
200
    return VolumeSnapshot(snapshot)
117
201
 
118
202
 
119
203
def volume_snapshot_list(request):
120
204
    c_client = cinderclient(request)
121
205
    if c_client is None:
122
206
        return []
123
 
    return c_client.volume_snapshots.list()
 
207
    return [VolumeSnapshot(s) for s in c_client.volume_snapshots.list()]
124
208
 
125
209
 
126
210
def volume_snapshot_create(request, volume_id, name,
127
211
                           description=None, force=False):
128
 
    return cinderclient(request).volume_snapshots.create(
129
 
        volume_id, force=force, display_name=name,
130
 
        display_description=description)
 
212
    data = {'name': name,
 
213
            'description': description,
 
214
            'force': force}
 
215
    data = _replace_v2_parameters(data)
 
216
 
 
217
    return VolumeSnapshot(cinderclient(request).volume_snapshots.create(
 
218
        volume_id, **data))
131
219
 
132
220
 
133
221
def volume_snapshot_delete(request, snapshot_id):