Skip to content

Commit 6da1a79

Browse files
authored
RD-2141 Add deployments display name (#1292)
* Adding display name to deployments create. * Adding --generate-id to deployments create. * Adding --search-name to deployments list.
1 parent 95e54de commit 6da1a79

File tree

4 files changed

+108
-8
lines changed

4 files changed

+108
-8
lines changed

cloudify_cli/cli/cfy.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,17 @@ def parse_and_validate_label_to_delete(ctx, param, value):
250250
return get_formatted_labels_list(value, allow_only_key=True)
251251

252252

253+
def validate_value_not_empty(ctx, param, value):
254+
if value is None or ctx.resilient_parsing:
255+
return
256+
257+
if not value:
258+
raise CloudifyValidationError(
259+
'ERROR: The `{0}` argument is empty'.format(param.name))
260+
261+
return value
262+
263+
253264
def get_formatted_labels_list(raw_labels_string, allow_only_key=False):
254265
labels_list = []
255266
if any(unicodedata.category(char)[0] == 'C' or char == '"'
@@ -1734,6 +1745,26 @@ def __init__(self):
17341745
help=helptexts.FILTER_ID
17351746
)
17361747

1748+
self.generate_id = click.option(
1749+
'--generate-id',
1750+
is_flag=True,
1751+
default=False,
1752+
help=helptexts.GENERATE_ID
1753+
)
1754+
1755+
self.display_name = click.option(
1756+
'-n',
1757+
'--display-name',
1758+
callback=validate_value_not_empty,
1759+
help=helptexts.DISPLAY_NAME
1760+
)
1761+
1762+
self.search_name = click.option(
1763+
'--search-name',
1764+
callback=validate_value_not_empty,
1765+
help=helptexts.SEARCH_NAME
1766+
)
1767+
17371768
def common_options(self, f):
17381769
"""A shorthand for applying commonly used arguments.
17391770

cloudify_cli/cli/helptexts.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209

210210
SORT_BY = "Key for sorting the list"
211211
DESCENDING = "Sort list in descending order [default: False]"
212-
SEARCH = 'Search resources by name/id. The returned list will include only ' \
212+
SEARCH = 'Search resources by id. The returned list will include only ' \
213213
'resources that contain the given search pattern'
214214

215215
TENANT = 'The name of the tenant'
@@ -543,3 +543,13 @@
543543
'already existing in this group.'
544544
GROUP_ID_FILTER = 'Show only results belonging to this group'
545545
DELETE_GROUP_DEPLOYMENTS = 'Delete all deployments belonging to this group'
546+
547+
GENERATE_ID = 'Generate a UUID to serve as the deployment ID. This flag ' \
548+
'cannot be provided if a deployment ID is specified'
549+
550+
DISPLAY_NAME = 'The display name of the deployment. If not specified, ' \
551+
'the deployment ID will be used instead.'
552+
553+
SEARCH_NAME = 'Search deployments by their display name. The returned list ' \
554+
'will include only deployments that contain the given search ' \
555+
'pattern'

cloudify_cli/commands/deployments.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
############
1616

1717
import os
18+
import uuid
1819
import json
1920
import shutil
2021
from datetime import datetime
@@ -65,8 +66,8 @@
6566

6667

6768
DEPLOYMENT_COLUMNS = [
68-
'id', 'blueprint_id', 'created_at', 'updated_at', 'visibility',
69-
'tenant_name', 'created_by', 'site_name', 'labels',
69+
'id', 'display_name', 'blueprint_id', 'created_at', 'updated_at',
70+
'visibility', 'tenant_name', 'created_by', 'site_name', 'labels',
7071
'deployment_status', 'installation_status'
7172
]
7273
DEPLOYMENT_UPDATE_COLUMNS = [
@@ -205,6 +206,7 @@ def _print_single_update(deployment_update_dict,
205206
required=False, resource_name_for_help='deployment')
206207
@cfy.options.all_tenants
207208
@cfy.options.search
209+
@cfy.options.search_name
208210
@cfy.options.pagination_offset
209211
@cfy.options.pagination_size
210212
@cfy.options.common_options
@@ -219,6 +221,7 @@ def manager_list(blueprint_id,
219221
descending,
220222
all_tenants,
221223
search,
224+
search_name,
222225
pagination_offset,
223226
pagination_size,
224227
logger,
@@ -245,7 +248,8 @@ def manager_list(blueprint_id,
245248
_offset=pagination_offset,
246249
_size=pagination_size,
247250
_group_id=group_id,
248-
blueprint_id=blueprint_id)
251+
blueprint_id=blueprint_id,
252+
_search_name=search_name)
249253
modify_resource_labels(deployments)
250254
total = deployments.metadata.pagination.total
251255
print_data(DEPLOYMENT_COLUMNS, deployments, 'Deployments:')
@@ -522,6 +526,8 @@ def manager_update(ctx,
522526
@cfy.options.visibility()
523527
@cfy.options.site_name
524528
@cfy.options.labels
529+
@cfy.options.generate_id
530+
@cfy.options.display_name
525531
@cfy.options.common_options
526532
@cfy.options.tenant_name(required=False, resource_name_for_help='deployment')
527533
@cfy.options.runtime_only_evaluation
@@ -536,6 +542,8 @@ def manager_create(blueprint_id,
536542
visibility,
537543
site_name,
538544
labels,
545+
generate_id,
546+
display_name,
539547
logger,
540548
client,
541549
tenant_name,
@@ -549,8 +557,18 @@ def manager_create(blueprint_id,
549557
utils.explicit_tenant_name_message(tenant_name, logger)
550558
logger.info('Creating new deployment from blueprint {0}...'.format(
551559
blueprint_id))
552-
deployment_id = deployment_id or blueprint_id
553560
visibility = get_visibility(private_resource, visibility, logger)
561+
if deployment_id:
562+
if generate_id:
563+
raise CloudifyCliError('`--generate-id` cannot be provided if a '
564+
'deployment ID is specified')
565+
else:
566+
if generate_id:
567+
deployment_id = str(uuid.uuid4())
568+
else:
569+
deployment_id = blueprint_id
570+
571+
display_name = display_name or deployment_id
554572

555573
try:
556574
deployment = client.deployments.create(
@@ -561,7 +579,8 @@ def manager_create(blueprint_id,
561579
skip_plugins_validation=skip_plugins_validation,
562580
site_name=site_name,
563581
runtime_only_evaluation=runtime_only_evaluation,
564-
labels=labels
582+
labels=labels,
583+
display_name=display_name
565584
)
566585
except (MissingRequiredDeploymentInputError,
567586
UnknownDeploymentInputError) as e:
@@ -580,8 +599,8 @@ def manager_create(blueprint_id,
580599
logger.info('Unable to create deployment due to invalid secret')
581600
raise CloudifyCliError(str(e))
582601

583-
logger.info("Deployment created. The deployment's id is {0}".format(
584-
deployment.id))
602+
logger.info("Deployment `{0}` created. The deployment's id is "
603+
"{1}".format(deployment.display_name, deployment.id))
585604

586605

587606
@cfy.command(name='delete',

cloudify_cli/tests/commands/test_deployments.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414
# limitations under the License.
1515
############
1616

17+
from __future__ import unicode_literals
18+
1719

1820
import json
1921
import inspect
2022
import datetime
2123
import warnings
24+
from uuid import UUID
2225

2326
from mock import patch, MagicMock, PropertyMock, Mock
2427

@@ -724,6 +727,43 @@ def raise_error(*args, **kwargs):
724727
self.assertTrue(found, 'String ''{0}'' not found in outcome {1}'
725728
.format(output, outcome))
726729

730+
def test_create_deployment_with_display_name(self):
731+
dep_display_name = 'Depl\xf3yment'
732+
self.client.deployments.create = Mock()
733+
self.invoke('cfy deployments create -b bp1 -n {0} '
734+
'dep1'.format(dep_display_name))
735+
call_args = list(self.client.deployments.create.call_args)
736+
self.assertEqual(call_args[1]['display_name'], dep_display_name)
737+
738+
def test_create_deployment_display_name_defaults_to_id(self):
739+
dep_id = 'dep1'
740+
self.client.deployments.create = Mock()
741+
self.invoke('cfy deployments create -b bp1 {0}'.format(dep_id))
742+
call_args = list(self.client.deployments.create.call_args)
743+
self.assertEqual(call_args[1]['display_name'], dep_id)
744+
745+
def test_create_deployment_with_generated_id(self):
746+
self.client.deployments.create = Mock()
747+
self.invoke('cfy deployments create -b bp1 --generate-id')
748+
call_args = list(self.client.deployments.create.call_args)
749+
try:
750+
UUID(call_args[0][1], version=4)
751+
except ValueError:
752+
raise Exception('The deployment was not created with a valid UUID')
753+
754+
def test_create_deployment_with_id_and_generate_id_fails(self):
755+
self.invoke('cfy deployments create -b bp1 --generate-id dep1',
756+
err_str_segment='cannot be provided',
757+
exception=CloudifyCliError)
758+
759+
def test_list_deployments_with_search_name(self):
760+
search_name_pattern = 'De#pl\xf3yment 1'
761+
self.client.deployments.list = Mock(return_value=MockListResponse())
762+
self.invoke('cfy deployments list --search-name '
763+
'"{0}"'.format(search_name_pattern))
764+
call_args = list(self.client.deployments.list.call_args)
765+
self.assertEqual(call_args[1].get('_search_name'), search_name_pattern)
766+
727767

728768
class DeploymentModificationsTest(CliCommandTest):
729769
def _mock_wait_for_executions(self, value):

0 commit comments

Comments
 (0)