blob: dcac2fc838a56856c4a7d34ff96c5a5960d203d4 [file] [log] [blame]
Jiri Broulika2c79292017-02-05 21:01:38 +01001# -*- coding: utf-8 -*-
2from __future__ import absolute_import
3from pprint import pprint
4
5# Import python libs
6import logging
7
8# Import salt libs
9import salt.utils.openstack.nova as suon
10
11# Get logging started
12log = logging.getLogger(__name__)
13
14# Function alias to not shadow built-ins
15__func_alias__ = {
16 'list_': 'list'
17}
18
19# Define the module's virtual name
20__virtualname__ = 'novang'
21
22
23def __virtual__():
24 '''
25 Only load this module if nova
26 is installed on this minion.
27 '''
28 if suon.check_nova():
29 return __virtualname__
30 return (False, 'The nova execution module failed to load: '
31 'only available if nova is installed.')
32
33
34__opts__ = {}
35
36
Richard Felkl55d1f572017-02-15 16:41:53 +010037def _auth(profile=None, tenant_name=None):
Jiri Broulika2c79292017-02-05 21:01:38 +010038 '''
39 Set up nova credentials
40 '''
41 if profile:
42 credentials = __salt__['config.option'](profile)
43 user = credentials['keystone.user']
44 password = credentials['keystone.password']
Richard Felkl55d1f572017-02-15 16:41:53 +010045 if tenant_name:
46 tenant = tenant_name
47 else:
48 tenant = credentials['keystone.tenant']
Jiri Broulika2c79292017-02-05 21:01:38 +010049 auth_url = credentials['keystone.auth_url']
50 region_name = credentials.get('keystone.region_name', None)
51 api_key = credentials.get('keystone.api_key', None)
52 os_auth_system = credentials.get('keystone.os_auth_system', None)
53 else:
54 user = __salt__['config.option']('keystone.user')
55 password = __salt__['config.option']('keystone.password')
56 tenant = __salt__['config.option']('keystone.tenant')
57 auth_url = __salt__['config.option']('keystone.auth_url')
58 region_name = __salt__['config.option']('keystone.region_name')
59 api_key = __salt__['config.option']('keystone.api_key')
60 os_auth_system = __salt__['config.option']('keystone.os_auth_system')
61 kwargs = {
62 'username': user,
63 'password': password,
64 'api_key': api_key,
65 'project_id': tenant,
66 'auth_url': auth_url,
67 'region_name': region_name,
68 'os_auth_plugin': os_auth_system
69 }
Jiri Broulika2c79292017-02-05 21:01:38 +010070 return suon.SaltNova(**kwargs)
71
72
Jiri Broulik70d9e3f2017-02-15 18:37:13 +010073def server_list(profile=None, tenant_name=None):
74 '''
75 Return list of active servers
76 CLI Example:
77 .. code-block:: bash
78 salt '*' nova.server_list
79 '''
80 conn = _auth(profile, tenant_name)
81 return conn.server_list()
82
83
84def server_get(name, tenant_name=None, profile=None):
85 '''
86 Return information about a server
87 '''
88 items = server_list(profile, tenant_name)
89 instance_id = None
90 for key, value in items.iteritems():
91 if key == name:
92 instance_id = value['id']
93 return instance_id
94
95
Jiri Broulika2c79292017-02-05 21:01:38 +010096def get_connection_args(profile=None):
97 '''
98 Set up profile credentials
99 '''
100 if profile:
101 credentials = __salt__['config.option'](profile)
102 user = credentials['keystone.user']
103 password = credentials['keystone.password']
104 tenant = credentials['keystone.tenant']
105 auth_url = credentials['keystone.auth_url']
106
107 kwargs = {
108 'username': user,
109 'password': password,
110 'tenant': tenant,
111 'auth_url': auth_url
112 }
113 return kwargs
114
115
116def quota_list(tenant_name, profile=None):
117 '''
118 list quotas of a tenant
119 '''
120 connection_args = get_connection_args(profile)
121 tenant = __salt__['keystone.tenant_get'](name=tenant_name, profile=profile, **connection_args)
122 tenant_id = tenant[tenant_name]['id']
123 conn = _auth(profile)
124 nt_ks = conn.compute_conn
125 item = nt_ks.quotas.get(tenant_id).__dict__
126 return item
127
128
129def quota_get(name, tenant_name, profile=None, quota_value=None):
130 '''
131 get specific quota value of a tenant
132 '''
133 item = quota_list(tenant_name, profile)
134 quota_value = item[name]
135 return quota_value
136
137
138def quota_update(tenant_name, profile=None, **quota_argument):
139 '''
140 update quota of specified tenant
141 '''
142 connection_args = get_connection_args(profile)
143 tenant = __salt__['keystone.tenant_get'](name=tenant_name, profile=profile, **connection_args)
144 tenant_id = tenant[tenant_name]['id']
145 conn = _auth(profile)
146 nt_ks = conn.compute_conn
147 item = nt_ks.quotas.update(tenant_id, **quota_argument)
148 return item
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100149
150
Richard Felkl55d1f572017-02-15 16:41:53 +0100151def server_list(profile=None, tenant_name=None):
152 '''
153 Return list of active servers
154 CLI Example:
155 .. code-block:: bash
156 salt '*' nova.server_list
157 '''
158 conn = _auth(profile, tenant_name)
159 return conn.server_list()
Jiri Broulika2c79292017-02-05 21:01:38 +0100160
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100161
Richard Felkl55d1f572017-02-15 16:41:53 +0100162def secgroup_list(profile=None, tenant_name=None):
163 '''
164 Return a list of available security groups (nova items-list)
165 CLI Example:
166 .. code-block:: bash
167 salt '*' nova.secgroup_list
168 '''
169 conn = _auth(profile, tenant_name)
170 return conn.secgroup_list()
Jiri Broulika2c79292017-02-05 21:01:38 +0100171
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100172
Richard Felkl55d1f572017-02-15 16:41:53 +0100173def boot(name, flavor_id=0, image_id=0, profile=None, tenant_name=None, timeout=300, **kwargs):
174 '''
175 Boot (create) a new instance
176 name
177 Name of the new instance (must be first)
178 flavor_id
179 Unique integer ID for the flavor
180 image_id
181 Unique integer ID for the image
182 timeout
183 How long to wait, after creating the instance, for the provider to
184 return information about it (default 300 seconds).
185 .. versionadded:: 2014.1.0
186 CLI Example:
187 .. code-block:: bash
188 salt '*' nova.boot myinstance flavor_id=4596 image_id=2
189 The flavor_id and image_id are obtained from nova.flavor_list and
190 nova.image_list
191 .. code-block:: bash
192 salt '*' nova.flavor_list
193 salt '*' nova.image_list
194 '''
195 #kwargs = {'nics': nics}
196 conn = _auth(profile, tenant_name)
197 return conn.boot(name, flavor_id, image_id, timeout, **kwargs)
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100198
199
Richard Felkl55d1f572017-02-15 16:41:53 +0100200def network_show(name, profile=None):
201 conn = _auth(profile)
202 return conn.network_show(name)
Jiri Broulik70d9e3f2017-02-15 18:37:13 +0100203
204
205def availability_zone_list(profile=None):
206 '''
207 list existing availability zones
208 '''
209 connection_args = get_connection_args(profile)
210 conn = _auth(profile)
211 nt_ks = conn.compute_conn
212 ret = nt_ks.aggregates.list()
213 return ret
214
215
216def availability_zone_get(name, profile=None):
217 '''
218 list existing availability zones
219 '''
220 connection_args = get_connection_args(profile)
221 conn = _auth(profile)
222 nt_ks = conn.compute_conn
223 zone_exists=False
224 items = availability_zone_list(profile)
225 for p in items:
226 item = nt_ks.aggregates.get(p).__getattr__('name')
227 if item == name:
228 zone_exists = True
229 return zone_exists
230
231
232def availability_zone_create(name, availability_zone, profile=None):
233 '''
234 create availability zone
235 '''
236 connection_args = get_connection_args(profile)
237 conn = _auth(profile)
238 nt_ks = conn.compute_conn
239 item = nt_ks.aggregates.create(name, availability_zone)
240 ret = {
241 'Id': item.__getattr__('id'),
242 'Aggregate Name': item.__getattr__('name'),
243 'Availability Zone': item.__getattr__('availability_zone'),
244 }
245 return ret
Damian Szeluga5dca0f02017-04-13 17:27:15 +0200246
247def aggregate_list(profile=None):
248 '''
249 list existing aggregates
250 '''
251 connection_args = get_connection_args(profile)
252 conn = _auth(profile)
253 nt_ks = conn.compute_conn
254 ret = nt_ks.aggregates.list()
255 return ret
256
257
258def aggregate_get(name, profile=None):
259 '''
260 list existing aggregates
261 '''
262 connection_args = get_connection_args(profile)
263 conn = _auth(profile)
264 nt_ks = conn.compute_conn
265 aggregate_exists=False
266 items = aggregate_list(profile)
267 for p in items:
268 item = nt_ks.aggregates.get(p).__getattr__('name')
269 if item == name:
270 aggregate_exists = True
271 return aggregate_exists
272
273
274def aggregate_create(name, aggregate, profile=None):
275 '''
276 create aggregate
277 '''
278 connection_args = get_connection_args(profile)
279 conn = _auth(profile)
280 nt_ks = conn.compute_conn
281 item = nt_ks.aggregates.create(name, aggregate)
282 ret = {
283 'Id': item.__getattr__('id'),
284 'Aggregate Name': item.__getattr__('name'),
285 }
286 return ret