This repository was archived by the owner on Apr 24, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
113 lines (90 loc) · 3.41 KB
/
Copy path__init__.py
File metadata and controls
113 lines (90 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# encoding: utf-8
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
"""This module contains helper functions used across the package namespace.
"""
from ec2stack import helpers
from ec2stack.providers.cloudstack import requester
def describe_item(args, keyname, not_found, prefix):
"""
Describe a specific item or all items.
@param args: Arguments involved in the request.
@param keyname: Keyname of the Cloudstack response.
@param not_found: Function to if the item is not found.
@param prefix: Parameter prefix.
@return: Response.
"""
if helpers.contains_parameter_with_keyword(prefix):
response = _describe_specific_item(args, keyname, not_found, prefix)
else:
response = describe_items_request(args, {})
return response
def _describe_specific_item(args, keyname, not_found, prefix):
"""
Describe a specific item based on args['id'] or args['name'].
@param args: Arguments involved in the request.
@param keyname: Keyname of the Cloudstack response.
@param not_found: Function to call if the item is not found.
@param prefix: Parameter prefix.
@return: Response.
"""
keys = helpers.get_request_parameter_keys(prefix)
response = {keyname: []}
for key in keys:
name = helpers.get(key)
if 'Id' in key:
args['id'] = name
elif 'Name' in key:
args['name'] = name
request = describe_item_request(args, keyname, not_found)
response[keyname].append(request)
return response
def describe_item_request(args, keyname, not_found):
"""
Executes the request and searches the Cloudstack response for the item.
@param args: Arguments involved in the request.
@param keyname: Keyname of the Cloudstack response.
@param not_found: Function to call if the item is not found.
@return: Response.
"""
request = describe_items_request(args, not_found)
request = request[keyname]
for item in request:
if 'id' in args and args['id'].lower() == item['id'].lower():
return item
elif 'name' in args and args['name'].lower() == item['name'].lower():
return item
return not_found()
def describe_items_request(args, not_found):
"""
Executes the request.
@param args: Request payload.
@param not_found: Function to call on empty response from Cloudstack.
@return: Response.
"""
args['listAll'] = 'true'
response = requester.make_request(args)
response = response[response.keys()[0]]
if 'count' in response:
return response
elif callable(not_found):
return not_found()
else:
return not_found