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
24 class CinderAPIVersionException(Exception):
25 def __init__(self
, errors
):
27 super(CinderAPIVersionException
, self
).__init
__("Multiple Exception Received")
30 return self
.__repr
__()
33 msg
= "{} : Following Exception(s) have occured during Cinder API discovery".format(self
.__class
__)
34 for n
,e
in enumerate(self
.errors
):
36 msg
+= " {}: {}".format(n
, str(e
))
39 class CinderEndpointException(Exception):
40 "Cinder Endpoint is absent"
43 class CinderDriver(object):
45 CinderDriver Class for image management
47 ### List of supported API versions in prioritized order
48 supported_versions
= ["2"]
52 region_name
= 'RegionOne',
53 service_type
= 'volume',
56 Constructor for CinderDriver class
58 sess_handle (instance of class SessionDriver)
59 region_name (string ): Region name
60 service_type(string) : Service type name
61 logger (instance of logging.Logger)
64 self
.log
= logging
.getLogger('rwcal.openstack.cinder')
65 self
.log
.setLevel(logging
.DEBUG
)
69 self
._sess
_handle
= sess_handle
70 #### Attempt to use API versions in prioritized order defined in
71 #### CinderDriver.supported_versions
72 def select_version(version
):
74 self
.log
.info("Attempting to use Cinder v%s APIs", version
)
75 cidrv
= ciclient
.Client(version
=version
,
76 region_name
= region_name
,
77 service_type
= service_type
,
78 session
=self
._sess
_handle
.session
)
79 except Exception as e
:
83 self
.log
.info("Cinder API v%s selected", version
)
84 return (version
, cidrv
)
87 for v
in CinderDriver
.supported_versions
:
89 (self
._version
, self
._ci
_drv
) = select_version(v
)
90 except Exception as e
:
95 raise CinderAPIVersionException(errors
)
98 self
._ci
_drv
.client
.get_endpoint()
99 except keystoneauth1
.exceptions
.catalog
.EndpointNotFound
:
100 self
.log
.info("Cinder endpoint not found")
101 raise CinderEndpointException()
104 def cinder_endpoint(self
):
105 return self
._ci
_drv
.client
.get_endpoint()
108 def project_id(self
):
109 return self
._sess
_handle
.project_id
114 Returns CinderDriver Quota (a dictionary) for project
117 quota
= self
._ci
_drv
.quotas
.get(self
.project_id
)
118 except Exception as e
:
119 self
.log
.exception("Get Cinder quota operation failed. Exception: %s", str(e
))
123 def _get_cinder_connection(self
):
125 Returns instance of object cinderclient.client.Client
130 def volume_list(self
):
132 Returns list of dictionaries. Each dictionary contains attributes associated with
137 Returns: List of dictionaries.
141 volume_info
= self
._ci
_drv
.volumes
.list()
142 except Exception as e
:
143 self
.log
.error("List volumes operation failed. Exception: %s", str(e
))
145 volumes
= [ volume
for volume
in volume_info
]
148 def volume_get(self
, volume_id
):
154 Returns: List of dictionaries.
157 vol
= self
._ci
_drv
.volumes
.get(volume_id
)
158 except Exception as e
:
159 self
.log
.error("Get volume operation failed. Exception: %s", str(e
))
163 def volume_set_metadata(self
, volume_id
, metadata
):
165 Set metadata for volume
166 Metadata is a dictionary of key-value pairs
170 Returns: List of dictionaries.
173 self
._ci
_drv
.volumes
.set_metadata(volume_id
, metadata
)
174 except Exception as e
:
175 self
.log
.error("Set metadata operation failed. Exception: %s", str(e
))
178 def volume_delete_metadata(self
, volume_id
, metadata
):
180 Delete metadata for volume
181 Metadata is a dictionary of key-value pairs
185 Returns: List of dictionaries.
188 self
._ci
_drv
.volumes
.delete_metadata(volume_id
, metadata
)
189 except Exception as e
:
190 self
.log
.error("Delete metadata operation failed. Exception: %s", str(e
))