Add utility functions for deleting/emptying swift containers

Change-Id: I5ae14eba2cc6707fa8e5273bdd93fcc10ac9d15a
This commit is contained in:
Ana Krivokapic 2016-12-09 20:52:06 +01:00
parent 5d977af0ac
commit cd87bc60f2
4 changed files with 134 additions and 27 deletions

View File

@ -10,6 +10,7 @@ features:
- CephMdsKey is now a generated Heat parameter. - CephMdsKey is now a generated Heat parameter.
- Add an new Action which generates environment parameters for configuring - Add an new Action which generates environment parameters for configuring
fencing. fencing.
- Add utility functions for deleting/emptying swift containers.
fixes: fixes:
- Fixes `bug 1644756 <https://bugs.launchpad.net/tripleo/+bug/1644756>`__ so - Fixes `bug 1644756 <https://bugs.launchpad.net/tripleo/+bug/1644756>`__ so
that flavour matching works as expected with the object-storage role. that flavour matching works as expected with the object-storage role.

View File

@ -19,11 +19,13 @@ import yaml
from heatclient import exc as heatexceptions from heatclient import exc as heatexceptions
from mistral.workflow import utils as mistral_workflow_utils from mistral.workflow import utils as mistral_workflow_utils
from mistralclient.api import base as mistralclient_base from mistralclient.api import base as mistralclient_base
import six
from swiftclient import exceptions as swiftexceptions from swiftclient import exceptions as swiftexceptions
from tripleo_common.actions import base from tripleo_common.actions import base
from tripleo_common import constants from tripleo_common import constants
from tripleo_common import exception from tripleo_common import exception
from tripleo_common.utils import swift as swiftutils
from tripleo_common.utils.validations import pattern_validator from tripleo_common.utils.validations import pattern_validator
@ -235,38 +237,20 @@ class DeletePlanAction(base.TripleOAction):
try: try:
swift = self.get_object_client() swift = self.get_object_client()
if self.container in [container["name"] for container in swiftutils.delete_container(swift, self.container)
swift.get_account()[1]]:
box = swift.get_container(self.container) # if mistral environment exists, delete it too
# ensure container is a plan mistral = self.get_workflow_client()
if box[0].get(constants.TRIPLEO_META_USAGE_KEY) != 'plan': if self.container in [env.name for env in
error_text = ("The {name} container does not contain a " mistral.environments.list()]:
"TripleO deployment plan and was not " # deletes environment
"deleted.".format(name=self.container)) mistral.environments.delete(self.container)
else:
# FIXME(rbrady): remove delete_object loop when
# LP#1615830 is fixed. See LP#1615825 for more info.
# delete files from plan
for data in box[1]:
swift.delete_object(self.container, data['name'])
# delete plan container
swift.delete_container(self.container)
# if mistral environment exists, delete it too
mistral = self.get_workflow_client()
if self.container in [env.name for env in
mistral.environments.list()]:
# deletes environment
mistral.environments.delete(self.container)
else:
# container does not exist
error_text = "The {name} container does not exist.".format(
name=self.container)
except swiftexceptions.ClientException as ce: except swiftexceptions.ClientException as ce:
LOG.exception("Swift error deleting plan.") LOG.exception("Swift error deleting plan.")
error_text = ce.msg error_text = ce.msg
except Exception as err: except Exception as err:
LOG.exception("Error deleting plan.") LOG.exception("Error deleting plan.")
error_text = err error_text = six.text_type(err)
if error_text: if error_text:
return mistral_workflow_utils.Result(error=error_text) return mistral_workflow_utils.Result(error=error_text)

View File

@ -0,0 +1,77 @@
# Copyright (c) 2017 Red Hat, Inc.
#
# Licensed 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.
import mock
from tripleo_common.tests import base
from tripleo_common.utils import swift as swift_utils
class SwiftTest(base.TestCase):
def setUp(self):
super(SwiftTest, self).setUp()
self.container_name = 'overcloud'
self.swiftclient = mock.MagicMock()
self.swiftclient.get_account.return_value = ({}, [
{'name': self.container_name},
{'name': 'test'},
])
self.swiftclient.get_container.return_value = (
{'x-container-meta-usage-tripleo': 'plan'}, [
{'name': 'some-name.yaml'},
{'name': 'some-other-name.yaml'},
{'name': 'yet-some-other-name.yaml'},
{'name': 'finally-another-name.yaml'}
]
)
def test_delete_container_success(self):
swift_utils.empty_container(self.swiftclient, self.container_name)
mock_calls = [
mock.call('overcloud', 'some-name.yaml'),
mock.call('overcloud', 'some-other-name.yaml'),
mock.call('overcloud', 'yet-some-other-name.yaml'),
mock.call('overcloud', 'finally-another-name.yaml')
]
self.swiftclient.delete_object.assert_has_calls(
mock_calls, any_order=True)
self.swiftclient.get_account.assert_called()
self.swiftclient.get_container.assert_called_with(self.container_name)
def test_delete_container_not_found(self):
self.assertRaises(ValueError,
swift_utils.empty_container,
self.swiftclient, 'idontexist')
self.swiftclient.get_account.assert_called()
self.swiftclient.get_container.assert_not_called()
self.swiftclient.delete_object.assert_not_called()
def test_delete_container_not_a_plan(self):
self.swiftclient.get_container.return_value = (
{'x-container-meta-usage-tripleo': 'not-a-plan'}, [
{'name': 'some-name.yaml'},
{'name': 'some-other-name.yaml'},
{'name': 'yet-some-other-name.yaml'},
{'name': 'finally-another-name.yaml'}
]
)
self.assertRaises(ValueError,
swift_utils.empty_container,
self.swiftclient, self.container_name)
self.swiftclient.get_account.assert_called()
self.swiftclient.get_container.assert_called()
self.swiftclient.delete_object.assert_not_called()

View File

@ -0,0 +1,45 @@
# Copyright 2016 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed 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.
from tripleo_common import constants
def empty_container(swiftclient, name):
container_names = [container["name"] for container
in swiftclient.get_account()[1]]
if name in container_names:
headers, objects = swiftclient.get_container(name)
# ensure container is a plan
if headers.get(constants.TRIPLEO_META_USAGE_KEY) != 'plan':
error_text = ("The {name} container does not contain a "
"TripleO deployment plan and was not "
"deleted.".format(name=name))
raise ValueError(error_text)
else:
# FIXME(rbrady): remove delete_object loop when
# LP#1615830 is fixed. See LP#1615825 for more info.
# delete files from plan
for o in objects:
swiftclient.delete_object(name, o['name'])
else:
error_text = "The {name} container does not exist.".format(name=name)
raise ValueError(error_text)
def delete_container(swiftclient, name):
empty_container(swiftclient, name)
swiftclient.delete_container(name)