4 # Copyright 2017 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
19 from cinderclient
import client
as ciclient
20 import cinderclient
.exceptions
as CinderException
23 class CinderAPIVersionException(Exception):
24 def __init__(self
, errors
):
26 super(CinderAPIVersionException
, self
).__init
__("Multiple Exception Received")
29 return self
.__repr
__()
32 msg
= "{} : Following Exception(s) have occured during Neutron API discovery".format(self
.__class
__)
33 for n
,e
in enumerate(self
.errors
):
35 msg
+= " {}: {}".format(n
, str(e
))
38 class CinderDriver(object):
40 CinderDriver Class for image management
42 ### List of supported API versions in prioritized order
43 supported_versions
= ["2"]
47 region_name
= 'RegionOne',
48 service_type
= 'volume',
51 Constructor for CinderDriver class
53 sess_handle (instance of class SessionDriver)
54 region_name (string ): Region name
55 service_type(string) : Service type name
56 logger (instance of logging.Logger)
59 self
.log
= logging
.getLogger('rwcal.openstack.cinder')
60 self
.log
.setLevel(logging
.DEBUG
)
64 self
._sess
_handle
= sess_handle
65 #### Attempt to use API versions in prioritized order defined in
66 #### CinderDriver.supported_versions
67 def select_version(version
):
69 self
.log
.info("Attempting to use Cinder v%s APIs", version
)
70 cidrv
= ciclient
.Client(version
=version
,
71 region_name
= region_name
,
72 service_type
= service_type
,
73 session
=self
._sess
_handle
.session
)
74 except Exception as e
:
78 self
.log
.info("Cinder API v%s selected", version
)
79 return (version
, cidrv
)
82 for v
in CinderDriver
.supported_versions
:
84 (self
._version
, self
._ci
_drv
) = select_version(v
)
85 except Exception as e
:
90 raise CinderAPIVersionException(errors
)
93 def cinder_endpoint(self
):
94 return self
._ci
_drv
.client
.get_endpoint()
98 return self
._sess
_handle
.project_id
103 Returns CinderDriver Quota (a dictionary) for project
106 quota
= self
._ci
_drv
.quotas
.get(self
.project_id
)
107 except Exception as e
:
108 self
.log
.exception("Get Cinder quota operation failed. Exception: %s", str(e
))
112 def _get_cinder_connection(self
):
114 Returns instance of object cinderclient.client.Client
119 def volume_list(self
):
121 Returns list of dictionaries. Each dictionary contains attributes associated with
126 Returns: List of dictionaries.
130 volume_info
= self
._ci
_drv
.volumes
.list()
131 except Exception as e
:
132 self
.log
.error("List volumes operation failed. Exception: %s", str(e
))
134 volumes
= [ volume
for volume
in volume_info
]
137 def volume_get(self
, volume_id
):
143 Returns: List of dictionaries.
146 vol
= self
._ci
_drv
.volumes
.get(volume_id
)
147 except Exception as e
:
148 self
.log
.error("Get volume operation failed. Exception: %s", str(e
))
152 def volume_set_metadata(self
, volume_id
, metadata
):
154 Set metadata for volume
155 Metadata is a dictionary of key-value pairs
159 Returns: List of dictionaries.
162 self
._ci
_drv
.volumes
.set_metadata(volume_id
, metadata
)
163 except Exception as e
:
164 self
.log
.error("Set metadata operation failed. Exception: %s", str(e
))
167 def volume_delete_metadata(self
, volume_id
, metadata
):
169 Delete metadata for volume
170 Metadata is a dictionary of key-value pairs
174 Returns: List of dictionaries.
177 self
._ci
_drv
.volumes
.delete_metadata(volume_id
, metadata
)
178 except Exception as e
:
179 self
.log
.error("Delete metadata operation failed. Exception: %s", str(e
))