Skip to content

Commit dc9a713

Browse files
author
Clark Perkins
committed
Added a few methods that the CLI was expecting to be there
1 parent 40136ab commit dc9a713

File tree

8 files changed

+85
-52
lines changed

8 files changed

+85
-52
lines changed

stackdio/client/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ def get_root(self):
4949
def get_version(self):
5050
return self._get(endpoint, jsonify=True)['version']
5151

52-
@endpoint("blueprints/{blueprint_id}/")
53-
def delete_blueprint(self, blueprint_id):
54-
result = self._delete(endpoint, jsonify=True)
55-
if result is None:
56-
raise BlueprintException("Blueprint %s not found" % blueprint_id)
57-
else:
58-
return result
59-
6052
@use_admin_auth
6153
@endpoint("security_groups/")
6254
def create_security_group(self, name, description, cloud_provider, is_default=True):

stackdio/client/blueprint.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,40 @@ def create_blueprint(self, blueprint, provider="ec2"):
1313

1414
# check the provided blueprint to see if we need to look up any ids
1515
for host in blueprint["hosts"]:
16-
if isinstance(host["size"], str):
16+
if isinstance(host["size"], basestring):
1717
host["size"] = self.get_instance_id(host["size"], provider)
1818

19-
if isinstance(host["zone"], str):
20-
host["zone"] = self.get_zone(host["zone"], provider)
19+
if isinstance(host["zone"], basestring):
20+
host["zone"] = self.get_zone_id(host["zone"], provider)
2121

22-
if isinstance(host["cloud_profile"], str):
22+
if isinstance(host["cloud_profile"], basestring):
2323
host["cloud_profile"] = self.get_profile_id(host["cloud_profile"])
2424

2525
for component in host["formula_components"]:
2626
if isinstance(component["id"], (tuple, list)):
27+
formula_id = self.get_formula_id(component["id"][0])
28+
2729
component["id"] = self.get_component_id(
28-
self.get_formula(component["id"][0]),
30+
self.get_formula(formula_id),
2931
component["id"][1])
3032

3133
return self._post(endpoint, data=json.dumps(blueprint), jsonify=True)
3234

33-
34-
3535
@endpoint("blueprints/")
3636
def list_blueprints(self):
3737
"""Return info for a specific blueprint_id"""
3838
return self._get(endpoint, jsonify=True)['results']
3939

40-
4140
@endpoint("blueprints/{blueprint_id}/")
4241
def get_blueprint(self, blueprint_id):
4342
"""Return info for a specific blueprint_id"""
4443
return self._get(endpoint, jsonify=True)
4544

46-
4745
@endpoint("blueprints/")
4846
def search_blueprints(self, **kwargs):
4947
"""Return info for a specific blueprint_id"""
5048
return self._get(endpoint, params=kwargs, jsonify=True)['results']
5149

52-
5350
@deprecated
5451
@accepted_versions("<0.7")
5552
def get_blueprint_id(self, title):

stackdio/client/formula.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
class FormulaMixin(HttpMixin):
8-
98
@endpoint("formulas/")
109
def import_formula(self, formula_uri, public=True):
1110
"""Import a formula"""
@@ -15,36 +14,40 @@ def import_formula(self, formula_uri, public=True):
1514
}
1615
return self._post(endpoint, data=json.dumps(data), jsonify=True)
1716

18-
1917
@endpoint("formulas/")
2018
def list_formulas(self):
2119
"""Return all formulas"""
2220
return self._get(endpoint, jsonify=True)['results']
2321

24-
2522
@endpoint("formulas/{formula_id}/")
2623
def get_formula(self, formula_id):
2724
"""Get a formula with matching id"""
2825
return self._get(endpoint, jsonify=True)
2926

30-
3127
@endpoint("formulas/")
3228
def search_formulas(self, **kwargs):
3329
"""Get a formula with matching id"""
3430
return self._get(endpoint, params=kwargs, jsonify=True)['results']
3531

36-
3732
@endpoint("formulas/{formula_id}/")
3833
def delete_formula(self, formula_id):
3934
"""Delete formula with matching id"""
40-
return self._delete(endpoint, jsonify=True)
41-
35+
return self._delete(endpoint)
4236

4337
@endpoint("formulas/{formula_id}/action/")
4438
def update_formula(self, formula_id):
4539
"""Delete formula with matching id"""
4640
return self._post(endpoint, json.dumps({"action": "update"}), jsonify=True)
4741

42+
def get_formula_id(self, title):
43+
"""Find a stack id"""
44+
45+
formulas = self.list_formulas()
46+
for formula in formulas:
47+
if formula.get("title") == title:
48+
return formula.get("id")
49+
50+
raise StackException("Formula %s not found" % title)
4851

4952
def get_component_id(self, formula, component_title):
5053
"""Get the id for a component from formula_id that matches title"""

stackdio/client/http.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ class HttpMixin(object):
8383
}
8484

8585
def __init__(self, auth=None, verify=True):
86-
self._http_options = {}
87-
self._http_options['auth'] = auth
88-
self._http_options['verify'] = verify
86+
self._http_options = {
87+
'auth': auth,
88+
'verify': verify,
89+
}
8990
self._http_log = logging.getLogger(__name__)
9091

9192
def _request(self, verb, url, quiet=False,
@@ -120,26 +121,20 @@ def _request(self, verb, url, quiet=False,
120121
else:
121122
return result
122123

123-
124124
def _head(self, url, *args, **kwargs):
125125
return self._request("HEAD", url, *args, **kwargs)
126126

127-
128127
def _get(self, url, *args, **kwargs):
129128
return self._request("GET", url, *args, **kwargs)
130129

131-
132130
def _delete(self, url, *args, **kwargs):
133131
return self._request("DELETE", url, *args, **kwargs)
134132

135-
136133
def _post(self, url, data=None, *args, **kwargs):
137134
return self._request("POST", url, data=data, *args, **kwargs)
138135

139-
140136
def _put(self, url, data=None, *args, **kwargs):
141137
return self._request("PUT", url, data=data, *args, **kwargs)
142138

143-
144139
def _patch(self, url, data=None, *args, **kwargs):
145140
return self._request("PATCH", url, data=data, *args, **kwargs)

stackdio/client/profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ def get_profile_id(self, slug, title=False):
5858
if profile.get("slug" if not title else "title") == slug:
5959
return profile.get("id")
6060

61-
return StackException("Provider %s not found" % slug)
61+
return StackException("Profile %s not found" % slug)

stackdio/client/provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def create_provider(self, **kwargs):
5959

6060

6161
@endpoint("providers/")
62-
def list_providers(self, provider_id):
62+
def list_providers(self):
6363
"""List all providers"""
6464
return self._get(endpoint, jsonify=True)['results']
6565

stackdio/client/region.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def get_zone_id(self, title, type_name="ec2"):
5656
"""Get a zone id for title"""
5757

5858
result = self._get(endpoint, jsonify=True)
59+
60+
type_id = self.get_provider_type_id(type_name)
5961
for zone in result['results']:
60-
if zone.get("title") == title and \
61-
zone.get("provider_type") == type_name:
62-
return zone.get("id")
62+
if zone.get("title") == title:
63+
region = self._get(zone.get("region"), jsonify=True)
64+
if region.get("provider_type") == type_id:
65+
return zone.get("id")
6366

6467
raise StackException("Zone %s not found for %s" % (title, type_name))

stackdio/client/stack.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,46 @@
66

77

88
class StackMixin(HttpMixin):
9+
VALID_LOG_TYPES = {
10+
"provisioning": ['log', 'err'],
11+
"global-orchestration": ['log', 'err'],
12+
"orchestration": ['log', 'err'],
13+
"launch": ['log'],
14+
}
915

1016
@endpoint("stacks/")
1117
def create_stack(self, stack_data):
1218
"""Launch a stack as described by stack_data"""
1319
return self._post(endpoint, data=json.dumps(stack_data), jsonify=True)
1420

15-
1621
@endpoint("stacks/")
1722
def list_stacks(self):
1823
"""Return a list of all stacks"""
1924
return self._get(endpoint, jsonify=True)['results']
2025

21-
2226
@endpoint("stacks/{stack_id}/")
2327
def get_stack(self, stack_id):
2428
"""Get stack info"""
2529
return self._get(endpoint, jsonify=True)
2630

27-
2831
@endpoint("stacks/")
2932
def search_stacks(self, *kwargs):
3033
"""Search for stacks that match the given criteria"""
3134
return self._get(endpoint, params=kwargs, jsonify=True)['results']
3235

33-
3436
@endpoint("stacks/{stack_id}/")
3537
def delete_stack(self, stack_id):
3638
"""Destructively delete a stack forever."""
3739
return self._delete(endpoint, jsonify=True)
3840

39-
4041
@endpoint("stacks/{stack_id}/action/")
41-
def get_valid_stack_actions(self):
42+
def get_valid_stack_actions(self, stack_id):
4243
return self._get(endpoint, jsonify=True)['available_actions']
4344

44-
4545
@endpoint("stacks/{stack_id}/action/")
4646
def do_stack_action(self, stack_id, action):
4747
"""Execute an action on a stack"""
48-
valid_actions = self.get_valid_actions(stack_id)
48+
valid_actions = self.get_valid_stack_actions(stack_id)
4949

5050
if action not in valid_actions:
5151
raise StackException("Invalid action, must be one of %s" %
@@ -55,7 +55,6 @@ def do_stack_action(self, stack_id, action):
5555

5656
return self._post(endpoint, data=json.dumps(data), jsonify=True)
5757

58-
5958
@endpoint("stacks/{stack_id}/history/")
6059
def get_stack_history(self, stack_id):
6160
"""Get stack info"""
@@ -65,7 +64,6 @@ def get_stack_history(self, stack_id):
6564
else:
6665
return result
6766

68-
6967
@deprecated
7068
@accepted_versions("<0.7")
7169
def get_stack_id(self, title):
@@ -78,13 +76,11 @@ def get_stack_id(self, title):
7876

7977
raise StackException("Stack %s not found" % title)
8078

81-
8279
@endpoint("stacks/{stack_id}/hosts/")
8380
def get_stack_hosts(self, stack_id):
8481
"""Get a list of all stack hosts"""
8582
return self._get(endpoint, jsonify=True)['results']
8683

87-
8884
@endpoint("stacks/{stack_id}/hosts/")
8985
def describe_hosts(self, stack_id, key="fqdn", ec2=False):
9086
"""Retrieve a list of info about a stack. Defaults to the id for each
@@ -102,10 +98,57 @@ def describe_hosts(self, stack_id, key="fqdn", ec2=False):
10298
else:
10399
host_details = host.get(EC2).get(key)
104100

105-
if host_details:
101+
if host_details is not None:
106102
stack_details.append(host_details)
107103

108104
if stack_details:
109105
return stack_details
110106

111107
raise StackException("Key %s for stack %s not available" % (key, stack_id))
108+
109+
@endpoint("stacks/{stack_id}/logs/{log_type}.{level}.{date}")
110+
def get_logs(self, stack_id, log_type, level='log', date='latest', tail=None):
111+
"""Get logs for a stack"""
112+
113+
if log_type and log_type not in self.VALID_LOG_TYPES:
114+
raise StackException("Invalid log type, must be one of %s" %
115+
", ".join(self.VALID_LOG_TYPES.keys()))
116+
117+
if level not in self.VALID_LOG_TYPES[log_type]:
118+
raise StackException("Invalid log level, must be one of %s" %
119+
", ".join(self.VALID_LOG_TYPES[log_type]))
120+
121+
return self._get(endpoint, params={'tail': tail}).text
122+
123+
@endpoint("stacks/{stack_id}/security_groups/")
124+
def list_access_rules(self, stack_id):
125+
"""Get Access rules for a stack"""
126+
127+
return self._get(endpoint, jsonify=True)['results']
128+
129+
@deprecated
130+
@accepted_versions("<0.7")
131+
def get_access_rule_id(self, stack_id, title):
132+
"""Find an access rule id"""
133+
134+
rules = self.list_access_rules(stack_id)
135+
136+
try:
137+
for group in rules:
138+
if group.get("blueprint_host_definition").get("title") == title:
139+
return group.get("id")
140+
except TypeError, e:
141+
pass
142+
143+
raise StackException("Access Rule %s not found" % title)
144+
145+
@endpoint("security_groups/{group_id}/rules/")
146+
def list_rules_for_group(self, group_id):
147+
return self._get(endpoint, jsonify=True)
148+
149+
@endpoint("security_groups/{group_id}/rules/")
150+
def edit_access_rule(self, group_id, data=None):
151+
"""Add an access rule to a group"""
152+
153+
return self._put(endpoint, jsonify=True, data=json.dumps(data))
154+

0 commit comments

Comments
 (0)