Merge "Move plan container creation to utils"

This commit is contained in:
Zuul 2020-02-26 00:14:37 +00:00 committed by Gerrit Code Review
commit 2a5a3590f4
3 changed files with 29 additions and 22 deletions

View File

@ -30,15 +30,10 @@ from tripleo_common import exception
from tripleo_common.utils import plan as plan_utils
from tripleo_common.utils import roles as roles_utils
from tripleo_common.utils import swift as swiftutils
from tripleo_common.utils.validations import pattern_validator
LOG = logging.getLogger(__name__)
default_container_headers = {
constants.TRIPLEO_META_USAGE_KEY: 'plan'
}
class CreateContainerAction(base.TripleOAction):
"""Creates an object container
@ -53,20 +48,13 @@ class CreateContainerAction(base.TripleOAction):
def run(self, context):
oc = self.get_object_client(context)
try:
plan_utils.create_plan_container(oc, self.container)
# checks to see if a container has a valid name
if not pattern_validator(constants.PLAN_NAME_PATTERN, self.container):
message = ("Unable to create plan. The plan name must "
"only contain letters, numbers or dashes")
return actions.Result(error=message)
# checks to see if a container with that name exists
if self.container in [container["name"] for container in
oc.get_account()[1]]:
result_string = ("A container with the name %s already"
" exists.") % self.container
return actions.Result(error=result_string)
oc.put_container(self.container, headers=default_container_headers)
except Exception as err:
err_msg = ("Container creation failed for plan:%s" % (err))
LOG.exception(err_msg)
return actions.Result(error=err_msg)
class ListPlansAction(base.TripleOAction):

View File

@ -167,7 +167,7 @@ class CreateContainerActionTest(base.TestCase):
# Verify
swift.put_container.assert_called_once_with(
self.container_name,
headers=plan.default_container_headers
headers={'x-container-meta-usage-tripleo': 'plan'}
)
@mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client')
@ -183,7 +183,8 @@ class CreateContainerActionTest(base.TestCase):
action = plan.CreateContainerAction(self.container_name)
result = action.run(self.ctx)
error_str = ('A container with the name %s already'
error_str = ('Container creation failed for plan:'
'A container with the name %s already'
' exists.') % self.container_name
self.assertEqual(result, actions.Result(
None, error_str))
@ -198,8 +199,9 @@ class CreateContainerActionTest(base.TestCase):
action = plan.CreateContainerAction("invalid_underscore")
result = action.run(self.ctx)
error_str = ("Unable to create plan. The plan name must only contain "
"letters, numbers or dashes")
error_str = ('Container creation failed for plan:'
'The plan name must only contain '
'letters, numbers or dashes')
self.assertEqual(result, actions.Result(
None, error_str))

View File

@ -26,6 +26,7 @@ from swiftclient import exceptions as swiftexceptions
from tripleo_common import constants
from tripleo_common.utils import swift as swiftutils
from tripleo_common.utils.validations import pattern_validator
def update_in_env(swift, env, key, value='', delete_key=False):
@ -235,6 +236,22 @@ def cache_delete(swift, plan_name, key):
pass
def create_plan_container(swift, plan_name):
if not pattern_validator(constants.PLAN_NAME_PATTERN, plan_name):
message = ("The plan name must "
"only contain letters, numbers or dashes")
raise RuntimeError(message)
# checks to see if a container with that name exists
if plan_name in [container["name"] for container in
swift.get_account()[1]]:
message = ("A container with the name %s already "
"exists.") % plan_name
raise RuntimeError(message)
default_container_headers = {constants.TRIPLEO_META_USAGE_KEY: 'plan'}
swift.put_container(plan_name, headers=default_container_headers)
def update_plan_environment(swift, environments,
container=constants.DEFAULT_CONTAINER_NAME):
env = get_env(swift, container)