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

766 fixing azure rm virtualmachine top issue #767

Conversation

pgmgb
Copy link
Contributor

@pgmgb pgmgb commented Feb 21, 2022

SUMMARY

Fixes #766

  • Removed top=1 and adjusted orderby for azure_rm_virtualmachine / get_marketplace_image_version
  • Added tests for azure_rm_virtualmachine to cover latest and specific images
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

azure_rm_virtualmachine

ADDITIONAL INFORMATION

@Fred-sun
Copy link
Collaborator

@pgmgb I think your idea is correct, but it should not be changed in this way. My suggestions are as follows,

First: Resolve conflicts in PR.

Second: if image.version = "lastest", use top=1 and return the latest

Third: If image.version! ="latest", no top=1, returns the list to iterate over the specified image version.

@Fred-sun Fred-sun added medium_priority Medium priority work in In trying to solve, or in working with contributors labels Feb 27, 2022
@pgmgb
Copy link
Contributor Author

pgmgb commented Feb 28, 2022

@Fred-sun thank you for the review.
I'm not sure of how to implement your suggestion in a way that does not include code redundancy and more complexity.
Therefore, I would keep it that way because I only removed changes from the following pull request (#606) and did no logical/structural changes to the code.

@Fred-sun
Copy link
Collaborator

Fred-sun commented Mar 1, 2022

@pgmgb Thank you for your reply, your change is not very reasonable. I still recommend following the comments above to change.

First, please put the "import" statement back to its original position, in accordance with code design specifications.

Second, wrap your code. This is not necessary, but 160 characters per line is fine.

Third, do not delete "top=1", otherwise the first version returned is not "latest",

@pgmgb
Copy link
Contributor Author

pgmgb commented Mar 1, 2022

@Fred-sun
https://github.com/ansible-collections/azure/blob/dev/plugins/modules/azure_rm_virtualmachine.py#L2024
top=1 will add a query parameter to the API call to only return one result. The combination of top=1 and orderby='name desc' does return the latest image. But these two query parameters disable the code, which is already implemented further below. My understanding would be that removing top=1 and the sorting (desc) would fix the issue. From my point of view and understanding, no code rewrite is necessary. Please see my comments inside the code.

    def get_marketplace_image_version(self):
        try:
            versions = self.compute_client.virtual_machine_images.list(self.location,
                                                                       self.image['publisher'],
                                                                       self.image['offer'],
                                                                       self.image['sku'],
                                                                       top=1, # -> Not needed because the list would return all the time one result and disables the followed code
                                                                       orderby='name desc') # -> "desc" not needed because of the followed code (return versions[len(versions) - 1]) will not work
        except Exception as exc:
            self.fail("Error fetching image {0} {1} {2} - {3}".format(self.image['publisher'],
                                                                      self.image['offer'],
                                                                      self.image['sku'],
                                                                      str(exc)))
        if versions and len(versions) > 0:
            if self.image['version'] == 'latest':
                return versions[len(versions) - 1] # -> Will return the latest image which resists on the last position inside the array
            for version in versions:
                if version.name == self.image['version']:
                    return version

        self.fail("Error could not find image {0} {1} {2} {3}".format(self.image['publisher'],
                                                                      self.image['offer'],
                                                                      self.image['sku'],
                                                                      self.image['version']))
        return None

If my understanding is wrong or the code needs still some rewriting, I would suggest that I close this pull request and only provide the test cases to the issue #766. I'm also fine if someone else fixes the issue.

@Fred-sun
Copy link
Collaborator

Fred-sun commented Mar 3, 2022

@pgmgb Thank you very much for your contribution, other changes are ok, please put the import statement back. Thank you very much!

import-before-documentation: Import found before documentation variables. All imports must appear below DOCUMENTATION/EXAMPLES/RETURN.

@Fred-sun Fred-sun added ready_for_review The PR has been modified and can be reviewed and merged and removed work in In trying to solve, or in working with contributors labels Mar 4, 2022
@xuzhang3
Copy link
Collaborator

xuzhang3 commented Mar 7, 2022

@pgmgb can you revert the format changes? Too many changes. Also please help resolve the file conflicts.

@xuzhang3 xuzhang3 removed the ready_for_review The PR has been modified and can be reviewed and merged label Mar 7, 2022
@Fred-sun
Copy link
Collaborator

Fred-sun commented Mar 9, 2022

@pgmgb Small change requests.

plugins/modules/azure_rm_virtualmachine.py:2342:0: missing-final-newline: Final newline missing

@Fred-sun Fred-sun added the ready_for_review The PR has been modified and can be reviewed and merged label Mar 22, 2022
@Fred-sun
Copy link
Collaborator

Fred-sun commented Apr 7, 2022

ready_for_review

1 similar comment
@Fred-sun
Copy link
Collaborator

Fred-sun commented Apr 7, 2022

ready_for_review

@xuzhang3
Copy link
Collaborator

LGTM

@xuzhang3 xuzhang3 merged commit 55efcd4 into ansible-collections:dev Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
medium_priority Medium priority ready_for_review The PR has been modified and can be reviewed and merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

azure_rm_virtualmachine loads only one image (-> top=1), therefore the "version" option does not work
3 participants