diff --git a/mcmd/commands/delete.py b/mcmd/commands/delete.py index 8386a552..16f98478 100644 --- a/mcmd/commands/delete.py +++ b/mcmd/commands/delete.py @@ -30,12 +30,21 @@ def add_arguments(subparsers): p_delete_resource.add_argument('--group', '-g', action='store_true', help='Flag to specify that the resource is a group') - p_delete.add_argument('--data', - action='store_true', - help='Use in conjunction with --entity-type to only delete the rows of the entity type') - p_delete.add_argument('--contents', - action='store_true', - help='Use in conjunction with --package to only delete the contents of the package') + + p_delete_options = p_delete.add_mutually_exclusive_group() + p_delete_options.add_argument('--data', + action='store_true', + help='Use in conjunction with --entity-type to only delete the rows of the entity ' + 'type') + p_delete_options.add_argument('--attribute', + metavar='NAME', + type=str, + help='Use in conjunction with --entity-type to only delete an attribute of the ' + 'entity type') + p_delete_options.add_argument('--contents', + action='store_true', + help='Use in conjunction with --package to only delete the contents of the package') + p_delete.add_argument('--force', '-f', action='store_true', help='Forces the delete action without asking for confirmation') @@ -55,6 +64,8 @@ def delete(args): if resource_type is ResourceType.ENTITY_TYPE: if args.data: _delete_entity_type_data(args) + elif args.attribute: + _delete_entity_type_attribute(args) else: _delete_entity_type(args) elif resource_type is ResourceType.PACKAGE: @@ -80,6 +91,19 @@ def _delete_entity_type_data(args): client.delete(api.rest1(args.resource)) +def _delete_entity_type_attribute(args): + if args.force or (not args.force and io.confirm( + 'Are you sure you want to delete attribute {} of entity type {}?'.format(args.attribute, args.resource))): + io.start('Deleting attribute {} of entity {}'.format(highlight(args.attribute), highlight(args.resource))) + response = client.get(api.rest2('sys_md_Attribute'), + params={ + 'q': 'entity=={};name=={}'.format(args.resource, + args.attribute) + }) + attribute_id = response.json()['items'][0]['id'] + client.delete(api.rest2('sys_md_Attribute/{}'.format(attribute_id))) + + def _delete_package(args): if args.force or (not args.force and io.confirm( 'Are you sure you want to delete package {} and all of its contents?'.format(args.resource))): diff --git a/tests/integration/commands/test_delete.py b/tests/integration/commands/test_delete.py index c383ec61..606d1cf5 100644 --- a/tests/integration/commands/test_delete.py +++ b/tests/integration/commands/test_delete.py @@ -39,6 +39,15 @@ def test_delete_entity_data(session, entity_type): assert entity_is_empty(session, entity_type) +@pytest.mark.integration +@patch('mcmd.io.confirm') +def test_delete_entity_attribute(are_you_sure, session, entity_type): + are_you_sure.return_value = True + run_commander('delete --entity-type {} --attribute firstName'.format(entity_type)) + + assert len(session.get_entity_meta_data(entity_type)['attributes']) == 2 + + @pytest.mark.integration def test_delete_package_contents(session, entity_type): package = entity_type.split('_')[0]