Skip to content

Commit

Permalink
Merge pull request #18 from wnkz/ft-show-multiple-ami-id
Browse files Browse the repository at this point in the history
show command now accepts multiple image id
  • Loading branch information
wnkz committed May 18, 2017
2 parents 345fed6 + 097498b commit c41eaeb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 60 deletions.
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

0 comments on commit c41eaeb

Please sign in to comment.