8000 gcp_compute: use env variables on inventory script by rambleraptor · Pull Request #52685 · ansible/ansible · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

gcp_compute: use env variables on inventory script #52685

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 priv 8000 acy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

rambleraptor
Copy link
Contributor
SUMMARY

Use environment variables on gcp_compute inventory script.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

gcp_compute

ADDITIONAL INFORMATION

@ansibot
Copy link
Contributor
ansibot commented Feb 20, 2019

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. community_review In order to be merged, this PR must follow the community review workflow. inventory Inventory category support:community This issue/PR relates to code supported by the Ansible community. labels Feb 20, 2019
@ansibot ansibot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed community_review In order to be merged, this PR must follow the community review workflow. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Feb 20, 2019
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Mar 1, 2019
@rambleraptor
Copy link
Contributor Author

@sivel please take a look when you can!

@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html and removed stale_review Updates were made after the last review and the last review is more than 7 days old. labels Mar 29, 2019
@ansibot ansibot added stale_review Updates were made after the last review and the last review is more than 7 days old. and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels May 24, 2019

def fail_json(self, *args, **kwargs):
raise AnsibleError(kwargs['msg'])

def _set_fallbacks(self):
self._set_fallback('project', 'GCP_PROJECT')
Copy link
Contributor
@s-hertel s-hertel Jun 6, 2019

Choose a reason for hiding this comment

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

Plugins can use documentation as an arg spec and validator (e.g. if someone doesn't provide an option with required: True or provides an option of the wrong type they'll get a helpful error without the plugin itself needing to do that). You can also specify options that fall back to environment variables without having to retrieve those yourself with os.environ.

To do that you should document your options like this:

        projects:
          description: A list of projects in which to describe GCE instances.
          env:
            - name: GCP_PROJECT
        ...

I also think pulling in the gcp doc fragment is a little confusing. Do you really want the plugin to be able to use the option project and projects? Some of the other options are now duplicated. And it doesn't list environment variables in a way the plugin can use. In the case of AWS credentials, a new documentation fragment was created (aws_credentials) for plugins specifically. GCP may want to do the same if you find yourself duplicating documentation between plugins.

Looking at the rest of the plugin, I notice there's some unnecessary validation happening as well that should be put in the documentation/argspec. A plugin doesn't need to do any of this:

        # get user specifications
        if 'zones' in config_data:
            if not isinstance(config_data['zones'], list):
                raise AnsibleParserError("Zones must be a list in GCP inventory YAML files")

        # get user specifications
        if 'projects' not in config_data:
            raise AnsibleParserError("Projects must be included in inventory YAML file")

        if not isinstance(config_data['projects'], list):
            raise AnsibleParserError("Projects must be a list in GCP inventory YAML files")

        # add in documented defaults
        if 'filters' not in config_data:
            config_data['filters'] = None

just use type: list or required: True for those options. Then reference your options with self.get_option(option_name) rather than config_data[option_name], since the latter will not set the default (None unless otherwise specified, like module parameters) or fall back to environment variables if you have one documented.

There's some documentation here: https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html#plugin-configuration-documentation-standards. Also feel free to ping me (shertel) on IRC if you have questions.

@kustodian
Copy link
Contributor
kustodian commented Jun 13, 2019

This PR can be closed, because it should be resolved with #57776

@ansibot ansibot removed the stale_review Updates were made after the last review and the last review is more than 7 days old. label Jun 29, 2019
@ansibot ansibot added the stale_review Updates were made after the last review and the last review is more than 7 days old. label Jul 7, 2019
@s-hertel
Copy link
Contributor

Closing as per above, please reopen if I've missed something.

@s-hertel s-hertel closed this Jul 10, 2019
@ansible ansible locked and limited conversation to collaborators Aug 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. inventory Inventory category needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0