blob: cd71348ef8ece8d2db59fedfaea83413b9720d62 [file] [log] [blame]
Jiri Broulik47aa6b32017-07-10 18:39:15 +02001# -*- coding: utf-8 -*-
2import logging
3from functools import wraps
4LOG = logging.getLogger(__name__)
5
6# Import third party libs
7HAS_CINDER = False
8try:
9 from cinderclient.v3 import client
10 HAS_CINDER = True
11except ImportError:
12 pass
13
14__opts__ = {}
15
16
17def __virtual__():
18 '''
19 Only load this module if cinder
20 is installed on this minion.
21 '''
22 if HAS_CINDER:
23 return 'cinderng'
24 return False
25
26def _authng(profile=None):
27 '''
28 Set up cinder credentials
29 '''
30 credentials = {
31 'username': profile['user'],
32 'password': profile['password'],
33 'project_id': profile['project_id'],
34 'auth_url': profile['protocol'] + "://" + profile['host'] + ":" + str(profile['port']) + "/v3",
35 'endpoint_type': profile['endpoint_type'],
36 'certificate': profile['certificate'],
37 'region_name': profile['region_name']
38 }
39 return credentials
40
41def create_conn(cred=None):
42 '''
43 create connection
44 '''
45 nt = client.Client(username=cred['username'], api_key=cred['password'], project_id=cred['project_id'], auth_url=cred['auth_url'], endpoint_type=cred['endpoint_type'], cacert=cred['certificate'], region_name=cred['region_name'])
46 return nt
47
48def list_volumes(profile=None, **kwargs):
49 '''
50 Return list of cinder volumes.
51 '''
52 cred = _authng(profile)
53 nt = create_conn(cred)
54 return nt.volumes.list()
55
56def list_volume_type(profile=None, **kwargs):
57 '''
58 Return list of volume types
59 '''
60 cred = _authng(profile)
61 nt = create_conn(cred)
62 return nt.volume_types.list()
63
64def get_volume_type(type_name, profile=None, **kwargs):
65 '''
66 Returns id of the specified volume type name
67 '''
68 vt_id = None
69 vt_list = list_volume_type(profile);
70 for vt in vt_list:
71 if vt.name == type_name:
72 vt_id = vt.id
73
74 if vt_id:
75 cred = _authng(profile)
76 nt = create_conn(cred)
77 try:
78 vt = nt.volume_types.get(vt_id)
79 return vt
80 except:
81 return
82 else:
83 return
84
85def create_volume_type(type_name, profile=None, **kwargs):
86 '''
87 Create cinder volume type
88 '''
89 vt = get_volume_type(type_name, profile)
90 if not vt:
91 cred = _authng(profile)
92 nt = create_conn(cred)
93 try:
94 nt.volume_types.create(type_name)
95 return 'created'
96 except:
97 return 'failed'
98 else:
99 return 'exists'
100
101
102def get_keys_volume_type(type_name, profile=None, **kwargs):
103 '''
104 Return extra specs of the specified volume type.
105 '''
106
107 vt = get_volume_type(type_name, profile)
108 if vt:
109 try:
110 return vt.get_keys()
111 except:
112 return 'failed'
113 else:
114 return
115
116def set_keys_volume_type(type_name, keys={}, profile=None, **kwargs):
117 '''
118 Set extra specs of the specified volume type.
119 '''
120 set_keys = False
121 vt = get_volume_type(type_name, profile)
122 if vt:
123 k = get_keys_volume_type(type_name, profile)
124 if not k:
125 set_keys = True
126 elif k:
127 for key in keys:
128 if k.get(key) != keys[key]:
129 set_keys = True
130 elif len(k) != len(keys):
131 set_keys = True
132 else:
133 return
134
135 if set_keys:
136 try:
137 vt.set_keys(keys)
138 return 'updated'
139 except:
140 return 'failed'
141 else:
142 return 'exist'
143 else:
144 return 'not found'