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

show command now accepts multiple image id #18

Merged
merged 1 commit into from
May 18, 2017
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
70 changes: 37 additions & 33 deletions shipami/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,46 +139,50 @@ def f(_):


@cli.command()
@click.argument('image-id')
@click.argument('image-id', nargs=-1)
@click.pass_obj
def show(shipami, image_id):
try:
image = shipami.show(image_id)
images = shipami.show(image_id)
except RuntimeError as e:
raise click.ClickException(str(e))

click.echo('id:\t{}'.format(image.get('ImageId')))
click.echo('name:\t{}'.format(image.get('Name')))
click.echo('state:\t{}'.format(click.style(image.get('State'), fg=state_colors.get(image.get('State')))))

if image.get('Tags'):
click.echo('tags:')
for tag in sorted(image['Tags'], key=lambda _: _['Key']):
key = tag.get('Key')
value = tag.get('Value')
color = 'blue' if 'shipami:' in key else 'white'
click.echo(' {}: {}'.format(key, click.style(value, fg=color, bold=True)))

if image.get('BlockDeviceMappings'):
click.echo('devices mappings:')
for block_device_mapping in image.get('BlockDeviceMappings', []):
click.echo(' {} {}Go type:{}'.format(
block_device_mapping['DeviceName'],
block_device_mapping['Ebs']['VolumeSize'],
block_device_mapping['Ebs']['VolumeType']
for i, image in enumerate(images):
click.echo('id:\t{}'.format(image.get('ImageId')))
click.echo('name:\t{}'.format(image.get('Name')))
click.echo('state:\t{}'.format(click.style(image.get('State'), fg=state_colors.get(image.get('State')))))

if image.get('Tags'):
click.echo('tags:')
for tag in sorted(image['Tags'], key=lambda _: _['Key']):
key = tag.get('Key')
value = tag.get('Value')
color = 'blue' if 'shipami:' in key else 'white'
click.echo(' {}: {}'.format(key, click.style(value, fg=color, bold=True)))

if image.get('BlockDeviceMappings'):
click.echo('devices mappings:')
for block_device_mapping in image.get('BlockDeviceMappings', []):
click.echo(' {} {}Go type:{}'.format(
block_device_mapping['DeviceName'],
block_device_mapping['Ebs']['VolumeSize'],
block_device_mapping['Ebs']['VolumeType']
)
)
)

if image.get('Shares'):
click.echo('shared with:')
for share in image.get('Shares'):
click.echo(' {}'.format(share['UserId']), nl=False)
if share.get('Marketplace') is not None:
click.echo(' (AWS MARKETPLACE)', nl=False)
if share.get('Marketplace') is True:
click.secho(' OK', fg='green', nl=False)
else:
click.secho(' PARTIAL', fg='yellow', nl=False)

if image.get('Shares'):
click.echo('shared with:')
for share in image.get('Shares'):
click.echo(' {}'.format(share['UserId']), nl=False)
if share.get('Marketplace') is not None:
click.echo(' (AWS MARKETPLACE)', nl=False)
if share.get('Marketplace') is True:
click.secho(' OK', fg='green', nl=False)
else:
click.secho(' PARTIAL', fg='yellow', nl=False)
click.echo()

if i < len(images) - 1:
click.echo()


Expand Down
58 changes: 31 additions & 27 deletions shipami/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,41 @@ def list(self):

return result

def show(self, image_id):
ec2 = self.__get_session().client('ec2')
image = self.__get_session().resource('ec2').Image(image_id)
def show(self, image_ids):
result_images = []
for image_id in image_ids:
ec2 = self.__get_session().client('ec2')
image = self.__get_session().resource('ec2').Image(image_id)

try:
r = ec2.describe_images(
Owners=[
'self'
],
ImageIds=[
image.id
]
)
except botocore.exceptions.ClientError as e:
message = e.response['Error']['Message']
logger.error(message)
raise RuntimeError(message)
try:
r = ec2.describe_images(
Owners=[
'self'
],
ImageIds=[
image.id
]
)
except botocore.exceptions.ClientError as e:
message = e.response['Error']['Message']
logger.error(message)
raise RuntimeError(message)

try:
result_image = r.get('Images', [])[0]
except IndexError:
message = 'Something went wrong'
logger.error(message)
raise RuntimeError(message)
try:
result_image = r.get('Images', [])[0]
except IndexError:
message = 'Something went wrong'
logger.error(message)
raise RuntimeError(message)

result_image['Shares'] = self.__get_image_permissions(image)
for share in result_image['Shares']:
if share.get('UserId') == self.MARKETPLACE_ACCOUNT_ID:
share['Marketplace'] = self.__is_ami_shared(image)

result_image['Shares'] = self.__get_image_permissions(image)
for share in result_image['Shares']:
if share.get('UserId') == self.MARKETPLACE_ACCOUNT_ID:
share['Marketplace'] = self.__is_ami_shared(image)
result_images.append(result_image)

return result_image
return result_images

def copy(self, image_id, **kwargs):
src_image = self.__get_session(kwargs.pop('source_region')).resource('ec2').Image(image_id)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ def test_show_managed(self, released_image):

assert r.exit_code == 0

def test_show_multiple(self, base_image, released_image):
r = runner.invoke(shipami, ['show', base_image.id, released_image.id])

assert r.exit_code == 0

def test_show_inexistant_id(self):
r = runner.invoke(shipami, ['show', 'ami-42424242'])

Expand Down