update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rift / rwcal / openstack / cinder / cinder_drv.py
1 #!/usr/bin/python
2
3 #
4 # Copyright 2017 RIFT.IO Inc
5 #
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
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17 #
18 import logging
19 from cinderclient import client as ciclient
20 import cinderclient.exceptions as CinderException
21 import keystoneauth1
22
23
24 class CinderAPIVersionException(Exception):
25 def __init__(self, errors):
26 self.errors = errors
27 super(CinderAPIVersionException, self).__init__("Multiple Exception Received")
28
29 def __str__(self):
30 return self.__repr__()
31
32 def __repr__(self):
33 msg = "{} : Following Exception(s) have occured during Cinder API discovery".format(self.__class__)
34 for n,e in enumerate(self.errors):
35 msg += "\n"
36 msg += " {}: {}".format(n, str(e))
37 return msg
38
39 class CinderEndpointException(Exception):
40 "Cinder Endpoint is absent"
41 pass
42
43 class CinderDriver(object):
44 """
45 CinderDriver Class for image management
46 """
47 ### List of supported API versions in prioritized order
48 supported_versions = ["2"]
49
50 def __init__(self,
51 sess_handle,
52 region_name = 'RegionOne',
53 service_type = 'volume',
54 logger = None):
55 """
56 Constructor for CinderDriver class
57 Arguments:
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)
62 """
63 if logger is None:
64 self.log = logging.getLogger('rwcal.openstack.cinder')
65 self.log.setLevel(logging.DEBUG)
66 else:
67 self.log = logger
68
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):
73 try:
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:
80 self.log.info(str(e))
81 raise
82 else:
83 self.log.info("Cinder API v%s selected", version)
84 return (version, cidrv)
85
86 errors = []
87 for v in CinderDriver.supported_versions:
88 try:
89 (self._version, self._ci_drv) = select_version(v)
90 except Exception as e:
91 errors.append(e)
92 else:
93 break
94 else:
95 raise CinderAPIVersionException(errors)
96
97 try:
98 self._ci_drv.client.get_endpoint()
99 except keystoneauth1.exceptions.catalog.EndpointNotFound:
100 self.log.info("Cinder endpoint not found")
101 raise CinderEndpointException()
102
103 @property
104 def cinder_endpoint(self):
105 return self._ci_drv.client.get_endpoint()
106
107 @property
108 def project_id(self):
109 return self._sess_handle.project_id
110
111 @property
112 def quota(self):
113 """
114 Returns CinderDriver Quota (a dictionary) for project
115 """
116 try:
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))
120 raise
121 return quota
122
123 def _get_cinder_connection(self):
124 """
125 Returns instance of object cinderclient.client.Client
126 Use for DEBUG ONLY
127 """
128 return self._ci_drv
129
130 def volume_list(self):
131 """
132 Returns list of dictionaries. Each dictionary contains attributes associated with
133 volumes
134
135 Arguments: None
136
137 Returns: List of dictionaries.
138 """
139 volumes = []
140 try:
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))
144 raise
145 volumes = [ volume for volume in volume_info ]
146 return volumes
147
148 def volume_get(self, volume_id):
149 """
150 Get details volume
151
152 Arguments: None
153
154 Returns: List of dictionaries.
155 """
156 try:
157 vol = self._ci_drv.volumes.get(volume_id)
158 except ciclient.exceptions.NotFound:
159 return None
160 except Exception as e:
161 self.log.error("Get volume operation failed. Exception: %s", str(e))
162 raise
163 return vol
164
165 def volume_set_metadata(self, volume_id, metadata):
166 """
167 Set metadata for volume
168 Metadata is a dictionary of key-value pairs
169
170 Arguments: None
171
172 Returns: List of dictionaries.
173 """
174 try:
175 self._ci_drv.volumes.set_metadata(volume_id, metadata)
176 except Exception as e:
177 self.log.error("Set metadata operation failed. Exception: %s", str(e))
178 raise
179
180 def volume_delete_metadata(self, volume_id, metadata):
181 """
182 Delete metadata for volume
183 Metadata is a dictionary of key-value pairs
184
185 Arguments: None
186
187 Returns: List of dictionaries.
188 """
189 try:
190 self._ci_drv.volumes.delete_metadata(volume_id, metadata)
191 except Exception as e:
192 self.log.error("Delete metadata operation failed. Exception: %s", str(e))
193 raise