Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Fix - disk list size #660

Merged
merged 1 commit into from
May 20, 2020
Merged

Fix - disk list size #660

merged 1 commit into from
May 20, 2020

Conversation

azzamsa
Copy link
Contributor

@azzamsa azzamsa commented May 12, 2020

  • Fix listing disk: disk object has no size attribute, but sizeMb.

Related: vmware-archive/vcd-cli#527


This change is Reviewable

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r1.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @azzamsa)


pyvcloud/vcd/utils.py, line 552 at r1 (raw file):

    result['id'] = extract_id(disk.get('id'))
    result['status'] = disk.get('status')
    result['size'] = humanfriendly.format_size(int(disk.get('sizeMb')))

This change is introduced in API version 33.0. For older versions, you still need to get it from 'size' attribute. Please add a check to read correct attribute depending on API version.

Also requesting to test it manually with both API version 33.0 and 32.0.

@azzamsa
Copy link
Contributor Author

azzamsa commented May 19, 2020

Thanks for the input.

To get the api version, I need to do something like:

    size_key = 'size'
    if client.get_api_version() < ApiVersion.VERSION_33.value:
        size_key = 'sizeMb'

because disk object doesn't contain any information about api version. So now disk_to_dict will need two parameters client and disk.

Any cleaner solution?

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can first check 'sizeMb' if it doesn't have a valid value, read 'size'. Since this is a mandatory parameter, either of the two will have a valid value. This way, we can skip checking API version.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @azzamsa)

@azzamsa
Copy link
Contributor Author

azzamsa commented May 19, 2020

We can first check 'sizeMb' if it doesn't have a valid value, read 'size'. Since this is a mandatory parameter, either of the two will have a valid value. This way, we can skip checking API version.

Done.

@azzamsa azzamsa requested a review from shashim22 May 20, 2020 03:41
Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @azzamsa and @shashim22)


pyvcloud/vcd/utils.py, line 552 at r2 (raw file):

    result['id'] = extract_id(disk.get('id'))
    result['status'] = disk.get('status')
    if 'sizeMb' in disk.keys():

I think you missed to assign 'size' to size_key before the IF block. Also, in case when 'sizeMb' is set, you will have to multiple by 1024 to get the size in bytes.

@azzamsa
Copy link
Contributor Author

azzamsa commented May 20, 2020

the correct formula would be:

  size_in_bytes = int(disk.get('sizeMb')) * (1000 * 1000)

Tested and compared the size using vcloud director and this lib.

@azzamsa azzamsa requested a review from shashim22 May 20, 2020 05:47
Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @azzamsa and @shashim22)


pyvcloud/vcd/utils.py, line 552 at r2 (raw file):

Previously, shashim22 wrote…

I think you missed to assign 'size' to size_key before the IF block. Also, in case when 'sizeMb' is set, you will have to multiple by 1024 to get the size in bytes.

Thanks for pointing it out, that was a typo. We should use 1024*1024 to be precise.
In the IF block, size_in_bytes should be converted to int:
size_in_bytes = int(disk.get('size'))

@azzamsa
Copy link
Contributor Author

azzamsa commented May 20, 2020

In vcloud director

image

using 1000 * 1000 (API result)

"size": "953 MB",
"size_bytes": "953000000",

using 1024 * 1024 (API result)

"size": "999.29 MB",
"size_bytes": "999292928",

How?

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://pypi.org/project/humanfriendly/

humanfriendly.format_size(bytes, binary=True) converts the size accurately. If you don't provide binary=True, it uses 1000 in place of 1024.

Try to look at other methods in vdc.py, you can see that we have used 1024*1024 at multiple places.
vCloud director converts the same way. For example, if you give 2000 MB as disk size, it will show 1.86 GB.

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @azzamsa and @shashim22)

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r3.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @azzamsa)

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @azzamsa)

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mark open issues as resolved so that I can merge the code.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @azzamsa)

@azzamsa
Copy link
Contributor Author

azzamsa commented May 20, 2020

:lgtm:

@azzamsa
Copy link
Contributor Author

azzamsa commented May 20, 2020


pyvcloud/vcd/utils.py, line 552 at r2 (raw file):

Previously, shashim22 wrote…

Thanks for pointing it out, that was a typo. We should use 1024*1024 to be precise.
In the IF block, size_in_bytes should be converted to int:
size_in_bytes = int(disk.get('size'))

Done.

Copy link
Contributor

@shashim22 shashim22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@shashim22 shashim22 merged commit e7e853c into vmware-archive:master May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants