CAL refactoring
[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
22
23 class CinderAPIVersionException(Exception):
24 def __init__(self, errors):
25 self.errors = errors
26 super(CinderAPIVersionException, self).__init__("Multiple Exception Received")
27
28 def __str__(self):
29 return self.__repr__()
30
31 def __repr__(self):
32 msg = "{} : Following Exception(s) have occured during Neutron API discovery".format(self.__class__)
33 for n,e in enumerate(self.errors):
34 msg += "\n"
35 msg += " {}: {}".format(n, str(e))
36 return msg
37
38 class CinderDriver(object):
39 """
40 CinderDriver Class for image management
41 """
42 ### List of supported API versions in prioritized order
43 supported_versions = ["2"]
44
45 def __init__(self,
46 sess_handle,
47 region_name = 'RegionOne',
48 service_type = 'volume',
49 logger = None):
50 """
51 Constructor for CinderDriver class
52 Arguments:
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)
57 """
58 if logger is None:
59 self.log = logging.getLogger('rwcal.openstack.cinder')
60 self.log.setLevel(logging.DEBUG)
61 else:
62 self.log = logger
63
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):
68 try:
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:
75 self.log.info(str(e))
76 raise
77 else:
78 self.log.info("Cinder API v%s selected", version)
79 return (version, cidrv)
80
81 errors = []
82 for v in CinderDriver.supported_versions:
83 try:
84 (self._version, self._ci_drv) = select_version(v)
85 except Exception as e:
86 errors.append(e)
87 else:
88 break
89 else:
90 raise CinderAPIVersionException(errors)
91
92 @property
93 def cinder_endpoint(self):
94 return self._ci_drv.client.get_endpoint()
95
96 @property
97 def project_id(self):
98 return self._sess_handle.project_id
99
100 @property
101 def quota(self):
102 """
103 Returns CinderDriver Quota (a dictionary) for project
104 """
105 try:
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))
109 raise
110 return quota
111
112 def _get_cinder_connection(self):
113 """
114 Returns instance of object cinderclient.client.Client
115 Use for DEBUG ONLY
116 """
117 return self._ci_drv
118
119 def volume_list(self):
120 """
121 Returns list of dictionaries. Each dictionary contains attributes associated with
122 volumes
123
124 Arguments: None
125
126 Returns: List of dictionaries.
127 """
128 volumes = []
129 try:
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))
133 raise
134 volumes = [ volume for volume in volume_info ]
135 return volumes
136
137 def volume_get(self, volume_id):
138 """
139 Get details volume
140
141 Arguments: None
142
143 Returns: List of dictionaries.
144 """
145 try:
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))
149 raise
150 return vol
151
152 def volume_set_metadata(self, volume_id, metadata):
153 """
154 Set metadata for volume
155 Metadata is a dictionary of key-value pairs
156
157 Arguments: None
158
159 Returns: List of dictionaries.
160 """
161 try:
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))
165 raise
166
167 def volume_delete_metadata(self, volume_id, metadata):
168 """
169 Delete metadata for volume
170 Metadata is a dictionary of key-value pairs
171
172 Arguments: None
173
174 Returns: List of dictionaries.
175 """
176 try:
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))
180 raise