Merge "Use stack lookup for getting stack name/id"

This commit is contained in:
Jenkins
2015-12-24 17:24:02 +00:00
committed by Gerrit Code Review
2 changed files with 42 additions and 292 deletions

View File

@@ -804,19 +804,6 @@ class ShellTestUserPass(ShellBase):
},
]
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
stack_resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict))
def find_output(key):
for out in outputs:
if out['output_key'] == key:
@@ -829,16 +816,10 @@ class ShellTestUserPass(ShellBase):
jsonutils.dumps(find_output(output_key)))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1',
'GET').AndReturn(stack_resp)
self.client.request(
'/stacks/teststack/1/outputs/%s' % output_key,
'GET').AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((stack_resp,
stack_dict))
self.client.json_request(
'GET',
'/stacks/teststack/1/outputs/%s' % output_key).AndReturn(
@@ -858,19 +839,6 @@ class ShellTestUserPass(ShellBase):
}
}
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
stack_resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict))
resp = fakes.FakeHTTPResponse(
200,
'OK',
@@ -878,16 +846,10 @@ class ShellTestUserPass(ShellBase):
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1',
'GET').AndReturn(stack_resp)
self.client.request(
'/stacks/teststack/1/outputs/%s' % output_key,
'GET').AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((stack_resp,
stack_dict))
self.client.json_request(
'GET',
'/stacks/teststack/1/outputs/%s' % output_key).AndReturn(
@@ -1647,13 +1609,6 @@ class ShellTestUserPass(ShellBase):
def test_stack_abandon(self):
self.register_keystone_auth_fixture()
resp_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
abandoned_stack = {
"action": "CREATE",
"status": "COMPLETE",
@@ -1671,25 +1626,16 @@ class ShellTestUserPass(ShellBase):
}
}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
abandoned_resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(abandoned_stack))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1', 'GET').AndReturn(resp)
self.client.request(
'/stacks/teststack/1/abandon',
'DELETE').AndReturn(abandoned_resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((resp, resp_dict))
http.HTTPClient.raw_request(
'DELETE', '/stacks/teststack/1/abandon').AndReturn(
abandoned_resp)
@@ -1701,13 +1647,6 @@ class ShellTestUserPass(ShellBase):
def test_stack_abandon_with_outputfile(self):
self.register_keystone_auth_fixture()
resp_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
abandoned_stack = {
"action": "CREATE",
"status": "COMPLETE",
@@ -1725,25 +1664,16 @@ class ShellTestUserPass(ShellBase):
}
}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
abandoned_resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(abandoned_stack))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1', 'GET').AndReturn(resp)
self.client.request(
'/stacks/teststack/1/abandon',
'DELETE').AndReturn(abandoned_resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((resp, resp_dict))
http.HTTPClient.raw_request(
'DELETE', '/stacks/teststack/1/abandon').AndReturn(
abandoned_resp)
@@ -2314,24 +2244,6 @@ class ShellTestUserPass(ShellBase):
def _setup_stubs_update_dry_run(self, template_file, existing=False):
self.register_keystone_auth_fixture()
resp_dict = {"stack": {
"id": "2",
"stack_name": "teststack2",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack2/2', 'GET').AndReturn(resp)
else:
self.client.json_request(
'GET', '/stacks/teststack2/2').AndReturn((resp, resp_dict))
template_data = open(template_file).read()
replaced_res = {"resource_name": "my_res",
@@ -2527,13 +2439,6 @@ class ShellTestUserPass(ShellBase):
def test_stack_snapshot(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {"snapshot": {
"id": "1",
"creation_time": "2012-10-25T01:58:47Z"
@@ -2545,24 +2450,11 @@ class ShellTestUserPass(ShellBase):
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request('/stacks/teststack/1', 'GET').AndReturn(
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)))
self.client.request(
'/stacks/teststack/1/snapshots',
'POST',
data={}).AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn(
(fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)), stack_dict))
http.HTTPClient.json_request(
'POST',
'/stacks/teststack/1/snapshots',
@@ -2575,13 +2467,6 @@ class ShellTestUserPass(ShellBase):
def test_snapshot_list(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {"snapshots": [{
"id": "2",
"name": "snap1",
@@ -2590,27 +2475,16 @@ class ShellTestUserPass(ShellBase):
"creation_time": "2014-12-05T01:25:52Z"
}]}
stack_resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict))
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1',
'GET').AndReturn(stack_resp)
self.client.request(
'/stacks/teststack/1/snapshots',
'GET').AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((stack_resp,
stack_dict))
http.HTTPClient.json_request(
'GET',
'/stacks/teststack/1/snapshots').AndReturn((resp, resp_dict))
@@ -2634,13 +2508,6 @@ class ShellTestUserPass(ShellBase):
def test_snapshot_show(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {"snapshot": {
"id": "2",
"creation_time": "2012-10-25T01:58:47Z"
@@ -2652,23 +2519,10 @@ class ShellTestUserPass(ShellBase):
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request('/stacks/teststack/1', 'GET').AndReturn(
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)))
self.client.request(
'/stacks/teststack/1/snapshots/2',
'GET').AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)), stack_dict))
http.HTTPClient.json_request(
'GET',
'/stacks/teststack/1/snapshots/2').AndReturn((resp, resp_dict))
@@ -2680,37 +2534,21 @@ class ShellTestUserPass(ShellBase):
def test_snapshot_delete(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {"snapshot": {
"id": "2",
"creation_time": "2012-10-25T01:58:47Z"
}}
resp = fakes.FakeHTTPResponse(
204,
'No Content',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict))
second_resp = fakes.FakeHTTPResponse(
204,
'No Content',
{},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1', 'GET').AndReturn(resp)
self.client.request(
'/stacks/teststack/1/snapshots/2',
'DELETE').AndReturn(second_resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((resp, stack_dict))
http.HTTPClient.raw_request(
'DELETE',
'/stacks/teststack/1/snapshots/2').AndReturn(second_resp)
@@ -2722,33 +2560,16 @@ class ShellTestUserPass(ShellBase):
def test_stack_restore(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
stack_resp = fakes.FakeHTTPResponse(
204,
'No Content',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict))
no_resp = fakes.FakeHTTPResponse(
204,
'No Content',
{'content-type': 'application/json'},
jsonutils.dumps({}))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1', 'GET').AndReturn(stack_resp)
self.client.request(
'/stacks/teststack/1/snapshots/2/restore',
'POST').AndReturn(no_resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((stack_resp,
stack_dict))
http.HTTPClient.json_request(
'POST',
'/stacks/teststack/1/snapshots/2/restore').AndReturn((no_resp,
@@ -2761,13 +2582,6 @@ class ShellTestUserPass(ShellBase):
def test_output_list(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {"outputs": [{
"output_key": "key",
"description": "description"
@@ -2777,27 +2591,16 @@ class ShellTestUserPass(ShellBase):
"description": "description1"
}]}
stack_resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict))
resp = fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request(
'/stacks/teststack/1',
'GET').AndReturn(stack_resp)
self.client.request(
'/stacks/teststack/1/outputs',
'GET').AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((stack_resp,
stack_dict))
http.HTTPClient.json_request(
'GET',
'/stacks/teststack/1/outputs').AndReturn((resp, resp_dict))
@@ -2819,13 +2622,6 @@ class ShellTestUserPass(ShellBase):
def test_output_show_all(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {'outputs': [
{
'output_key': 'key',
@@ -2852,42 +2648,16 @@ class ShellTestUserPass(ShellBase):
jsonutils.dumps(resp_dict1))
if self.client == http.SessionClient:
self.client.request('/stacks/teststack/1', 'GET').AndReturn(
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)))
self.client.request(
'/stacks/teststack/1/outputs',
'GET').AndReturn(resp)
self.client.request('/stacks/teststack/1', 'GET').AndReturn(
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)))
self.client.request(
'/stacks/teststack/1/outputs/key',
'GET').AndReturn(resp1)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)), stack_dict))
http.HTTPClient.json_request(
'GET',
'/stacks/teststack/1/outputs').AndReturn((resp, resp_dict))
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)), stack_dict))
http.HTTPClient.json_request(
'GET',
'/stacks/teststack/1/outputs/key').AndReturn((resp1,
@@ -2909,13 +2679,6 @@ class ShellTestUserPass(ShellBase):
def test_output_show(self):
self.register_keystone_auth_fixture()
stack_dict = {"stack": {
"id": "1",
"stack_name": "teststack",
"stack_status": 'CREATE_COMPLETE',
"creation_time": "2012-10-25T01:58:47Z"
}}
resp_dict = {"output": {
"output_key": "key",
"output_value": "value",
@@ -2928,23 +2691,10 @@ class ShellTestUserPass(ShellBase):
{'content-type': 'application/json'},
jsonutils.dumps(resp_dict))
if self.client == http.SessionClient:
self.client.request('/stacks/teststack/1', 'GET').AndReturn(
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)))
self.client.request(
'/stacks/teststack/1/outputs/key',
'GET').AndReturn(resp)
else:
http.HTTPClient.json_request(
'GET', '/stacks/teststack/1').AndReturn((
fakes.FakeHTTPResponse(
200,
'OK',
{'content-type': 'application/json'},
jsonutils.dumps(stack_dict)), stack_dict))
http.HTTPClient.json_request(
'GET',
'/stacks/teststack/1/outputs/key').AndReturn((resp, resp_dict))

View File

@@ -87,7 +87,27 @@ class Stack(base.Resource):
return '%s/%s' % (self.stack_name, self.id)
class StackManager(base.BaseManager):
class StackChildManager(base.BaseManager):
@property
def api(self):
return self.client
def _resolve_stack_id(self, stack_id):
# if the id already has a slash in it,
# then it is already {stack_name}/{stack_id}
if stack_id.find('/') > 0:
return stack_id
# We want to capture the redirect, not actually get the stack,
# since all we want is the stacks:lookup response to get the
# fully qualified ID, and not all users are allowed to do the
# redirected stacks:show, so pass redirect=False
resp = self.client.get('/stacks/%s' % stack_id, redirect=False)
location = resp.headers.get('location')
path = self.client.strip_endpoint(location)
return path[len('/stacks/'):]
class StackManager(StackChildManager):
resource_class = Stack
def list(self, **kwargs):
@@ -155,15 +175,15 @@ class StackManager(base.BaseManager):
def preview_update(self, stack_id, **kwargs):
"""Preview a stack update."""
s = self.get(stack_id)
stack_identifier = self._resolve_stack_id(stack_id)
headers = self.client.credentials_headers()
if kwargs.pop('existing', None):
resp = self.client.patch('/stacks/%s/%s/preview' %
(s.stack_name, s.id),
resp = self.client.patch('/stacks/%s/preview' %
stack_identifier,
data=kwargs, headers=headers)
else:
resp = self.client.put('/stacks/%s/%s/preview' %
(s.stack_name, s.id),
resp = self.client.put('/stacks/%s/preview' %
stack_identifier,
data=kwargs, headers=headers)
body = utils.get_response_body(resp)
return body
@@ -174,59 +194,59 @@ class StackManager(base.BaseManager):
def abandon(self, stack_id):
"""Abandon a stack."""
stack = self.get(stack_id)
resp = self.client.delete('/stacks/%s/abandon' % stack.identifier)
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.delete('/stacks/%s/abandon' % stack_identifier)
body = utils.get_response_body(resp)
return body
def snapshot(self, stack_id, name=None):
"""Snapshot a stack."""
stack = self.get(stack_id)
stack_identifier = self._resolve_stack_id(stack_id)
data = {}
if name:
data['name'] = name
resp = self.client.post('/stacks/%s/snapshots' % stack.identifier,
resp = self.client.post('/stacks/%s/snapshots' % stack_identifier,
data=data)
body = utils.get_response_body(resp)
return body
def snapshot_show(self, stack_id, snapshot_id):
stack = self.get(stack_id)
resp = self.client.get('/stacks/%s/snapshots/%s' % (stack.identifier,
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.get('/stacks/%s/snapshots/%s' % (stack_identifier,
snapshot_id))
body = utils.get_response_body(resp)
return body
def snapshot_delete(self, stack_id, snapshot_id):
stack = self.get(stack_id)
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.delete('/stacks/%s/snapshots/%s' %
(stack.identifier, snapshot_id))
(stack_identifier, snapshot_id))
body = utils.get_response_body(resp)
return body
def restore(self, stack_id, snapshot_id):
stack = self.get(stack_id)
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.post('/stacks/%s/snapshots/%s/restore' %
(stack.identifier, snapshot_id))
(stack_identifier, snapshot_id))
body = utils.get_response_body(resp)
return body
def snapshot_list(self, stack_id):
stack = self.get(stack_id)
resp = self.client.get('/stacks/%s/snapshots' % stack.identifier)
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.get('/stacks/%s/snapshots' % stack_identifier)
body = utils.get_response_body(resp)
return body
def output_list(self, stack_id):
stack = self.get(stack_id)
resp = self.client.get('/stacks/%s/outputs' % stack.identifier)
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.get('/stacks/%s/outputs' % stack_identifier)
body = utils.get_response_body(resp)
return body
def output_show(self, stack_id, output_key):
stack = self.get(stack_id)
stack_identifier = self._resolve_stack_id(stack_id)
resp = self.client.get('/stacks/%(id)s/outputs/%(key)s' % {
'id': stack.identifier,
'id': stack_identifier,
'key': output_key
})
body = utils.get_response_body(resp)
@@ -259,23 +279,3 @@ class StackManager(base.BaseManager):
resp = self.client.post(url, data=kwargs)
body = utils.get_response_body(resp)
return body
class StackChildManager(base.BaseManager):
@property
def api(self):
return self.client
def _resolve_stack_id(self, stack_id):
# if the id already has a slash in it,
# then it is already {stack_name}/{stack_id}
if stack_id.find('/') > 0:
return stack_id
# We want to capture the redirect, not actually get the stack,
# since all we want is the stacks:lookup response to get the
# fully qualified ID, and not all users are allowed to do the
# redirected stacks:show, so pass redirect=False
resp = self.client.get('/stacks/%s' % stack_id, redirect=False)
location = resp.headers.get('location')
path = self.client.strip_endpoint(location)
return path[len('/stacks/'):]