Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete --attribute option #120

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions mcmd/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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:
Expand All @@ -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))):
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/commands/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down