fix: remove version_added in gcp_compute

The version_added field is documented for a very small set of
fields, specifically in gcp_compute today.

Removing these values as they are specifying the ansible-core version
which is confusing for those who are installing via ansible-galaxy,
and the value is not set for any other module.

fixes #550.
This commit is contained in:
Yusuke Tsutsumi 2023-02-18 19:08:46 +00:00 committed by Yusuke Tsutsumi
parent 791e11d45d
commit 252f215f2c

View file

@ -60,23 +60,19 @@ DOCUMENTATION = """
choices: ['application', 'serviceaccount', 'machineaccount'] choices: ['application', 'serviceaccount', 'machineaccount']
env: env:
- name: GCP_AUTH_KIND - name: GCP_AUTH_KIND
version_added: "2.8.2"
scopes: scopes:
description: list of authentication scopes description: list of authentication scopes
type: list type: list
default: ['https://www.googleapis.com/auth/compute'] default: ['https://www.googleapis.com/auth/compute']
env: env:
- name: GCP_SCOPES - name: GCP_SCOPES
version_added: "2.8.2"
service_account_file: service_account_file:
description: description:
- The path of a Service Account JSON file if serviceaccount is selected as type. - The path of a Service Account JSON file if serviceaccount is selected as type.
type: path type: path
env: env:
- name: GCP_SERVICE_ACCOUNT_FILE - name: GCP_SERVICE_ACCOUNT_FILE
version_added: "2.8.2"
- name: GCE_CREDENTIALS_FILE_PATH - name: GCE_CREDENTIALS_FILE_PATH
version_added: "2.8"
service_account_contents: service_account_contents:
description: description:
- A string representing the contents of a Service Account JSON file. This should not be passed in as a dictionary, - A string representing the contents of a Service Account JSON file. This should not be passed in as a dictionary,
@ -84,14 +80,12 @@ DOCUMENTATION = """
type: string type: string
env: env:
- name: GCP_SERVICE_ACCOUNT_CONTENTS - name: GCP_SERVICE_ACCOUNT_CONTENTS
version_added: "2.8.2"
service_account_email: service_account_email:
description: description:
- An optional service account email address if machineaccount is selected - An optional service account email address if machineaccount is selected
and the user does not wish to use the default email. and the user does not wish to use the default email.
env: env:
- name: GCP_SERVICE_ACCOUNT_EMAIL - name: GCP_SERVICE_ACCOUNT_EMAIL
version_added: "2.8.2"
vars_prefix: vars_prefix:
description: prefix to apply to host variables, does not include facts nor params description: prefix to apply to host variables, does not include facts nor params
default: '' default: ''
@ -105,7 +99,6 @@ DOCUMENTATION = """
which group names end up being used as. which group names end up being used as.
type: bool type: bool
default: False default: False
version_added: '2.8'
retrieve_image_info: retrieve_image_info:
description: description:
- Populate the C(image) host fact for the instances returned with the GCP image name - Populate the C(image) host fact for the instances returned with the GCP image name
@ -114,7 +107,6 @@ DOCUMENTATION = """
- Unless this option is enabled, the C(image) host variable will be C(null) - Unless this option is enabled, the C(image) host variable will be C(null)
type: bool type: bool
default: False default: False
version_added: '2.8'
""" """
EXAMPLES = """ EXAMPLES = """
@ -170,7 +162,9 @@ class GcpMockModule(object):
class GcpInstance(object): class GcpInstance(object):
def __init__(self, json, hostname_ordering, project_disks, should_format=True, name_suffix=""): def __init__(
self, json, hostname_ordering, project_disks, should_format=True, name_suffix=""
):
self.hostname_ordering = hostname_ordering self.hostname_ordering = hostname_ordering
self.project_disks = project_disks self.project_disks = project_disks
self.name_suffix = name_suffix self.name_suffix = name_suffix
@ -210,8 +204,8 @@ class GcpInstance(object):
def _format_network_info(self, address): def _format_network_info(self, address):
""" """
:param address: A GCP network address :param address: A GCP network address
:return a dict with network shortname and region :return a dict with network shortname and region
""" """
split = address.split("/") split = address.split("/")
region = "" region = ""
@ -223,8 +217,8 @@ class GcpInstance(object):
def _format_metadata(self, metadata): def _format_metadata(self, metadata):
""" """
:param metadata: A list of dicts where each dict has keys "key" and "value" :param metadata: A list of dicts where each dict has keys "key" and "value"
:return a dict with key/value pairs for each in list. :return a dict with key/value pairs for each in list.
""" """
new_metadata = {} new_metadata = {}
for pair in metadata: for pair in metadata:
@ -233,18 +227,18 @@ class GcpInstance(object):
def hostname(self): def hostname(self):
""" """
:return the hostname of this instance :return the hostname of this instance
""" """
for order in self.hostname_ordering: for order in self.hostname_ordering:
name = None name = None
if order.startswith("labels."): if order.startswith("labels."):
name = self.json[u"labels"].get(order[7:]) name = self.json["labels"].get(order[7:])
elif order == "public_ip": elif order == "public_ip":
name = self._get_publicip() name = self._get_publicip()
elif order == "private_ip": elif order == "private_ip":
name = self._get_privateip() name = self._get_privateip()
elif order == "name": elif order == "name":
name = self.json[u"name"] + self.name_suffix name = self.json["name"] + self.name_suffix
else: else:
raise AnsibleParserError("%s is not a valid hostname precedent" % order) raise AnsibleParserError("%s is not a valid hostname precedent" % order)
@ -255,20 +249,20 @@ class GcpInstance(object):
def _get_publicip(self): def _get_publicip(self):
""" """
:return the publicIP of this instance or None :return the publicIP of this instance or None
""" """
# Get public IP if exists # Get public IP if exists
for interface in self.json["networkInterfaces"]: for interface in self.json["networkInterfaces"]:
if "accessConfigs" in interface: if "accessConfigs" in interface:
for accessConfig in interface["accessConfigs"]: for accessConfig in interface["accessConfigs"]:
if "natIP" in accessConfig: if "natIP" in accessConfig:
return accessConfig[u"natIP"] return accessConfig["natIP"]
return None return None
def _get_image(self): def _get_image(self):
""" """
:param instance: A instance response from GCP :param instance: A instance response from GCP
:return the image of this instance or None :return the image of this instance or None
""" """
image = None image = None
if self.project_disks and "disks" in self.json: if self.project_disks and "disks" in self.json:
@ -279,13 +273,13 @@ class GcpInstance(object):
def _get_privateip(self): def _get_privateip(self):
""" """
:param item: A host response from GCP :param item: A host response from GCP
:return the privateIP of this instance or None :return the privateIP of this instance or None
""" """
# Fallback: Get private IP # Fallback: Get private IP
for interface in self.json[u"networkInterfaces"]: for interface in self.json["networkInterfaces"]:
if "networkIP" in interface: if "networkIP" in interface:
return interface[u"networkIP"] return interface["networkIP"]
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
@ -303,7 +297,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _populate_host(self, item): def _populate_host(self, item):
""" """
:param item: A GCP instance :param item: A GCP instance
""" """
hostname = item.hostname() hostname = item.hostname()
self.inventory.add_host(hostname) self.inventory.add_host(hostname)
@ -321,8 +315,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def verify_file(self, path): def verify_file(self, path):
""" """
:param path: the path to the inventory config file :param path: the path to the inventory config file
:return the contents of the config file :return the contents of the config file
""" """
if super(InventoryModule, self).verify_file(path): if super(InventoryModule, self).verify_file(path):
if path.endswith(("gcp.yml", "gcp.yaml")): if path.endswith(("gcp.yml", "gcp.yaml")):
@ -333,10 +327,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def fetch_list(self, params, link, query): def fetch_list(self, params, link, query):
""" """
:param params: a dict containing all of the fields relevant to build URL :param params: a dict containing all of the fields relevant to build URL
:param link: a formatted URL :param link: a formatted URL
:param query: a formatted query string :param query: a formatted query string
:return the JSON response containing a list of instances. :return the JSON response containing a list of instances.
""" """
lists = [] lists = []
resp = self._return_if_object( resp = self._return_if_object(
@ -349,7 +343,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.fake_module, self.fake_module,
self.auth_session.get( self.auth_session.get(
link, link,
params={"filter": query, "pageToken": resp.get("nextPageToken")}, params={
"filter": query,
"pageToken": resp.get("nextPageToken"),
},
), ),
) )
lists.append(resp.get("items")) lists.append(resp.get("items"))
@ -370,8 +367,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _get_query_options(self, filters): def _get_query_options(self, filters):
""" """
:param config_data: contents of the inventory config file :param config_data: contents of the inventory config file
:return A fully built query string :return A fully built query string
""" """
if not filters: if not filters:
return "" return ""
@ -391,9 +388,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _return_if_object(self, module, response): def _return_if_object(self, module, response):
""" """
:param module: A GcpModule :param module: A GcpModule
:param response: A Requests response object :param response: A Requests response object
:return JSON response :return JSON response
""" """
# If not found, return nothing. # If not found, return nothing.
if response.status_code == 404: if response.status_code == 404:
@ -418,9 +415,9 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _add_hosts(self, items, config_data, format_items=True, project_disks=None): def _add_hosts(self, items, config_data, format_items=True, project_disks=None):
""" """
:param items: A list of hosts :param items: A list of hosts
:param config_data: configuration data :param config_data: configuration data
:param format_items: format items or not :param format_items: format items or not
""" """
if not items: if not items:
return return
@ -450,7 +447,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _get_project_disks(self, config_data, query): def _get_project_disks(self, config_data, query):
""" """
project space disk images project space disk images
""" """
try: try:
@ -516,20 +513,20 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def fetch_projects(self, params, link, query): def fetch_projects(self, params, link, query):
module = GcpMockModule(params) module = GcpMockModule(params)
auth = GcpSession(module, 'cloudresourcemanager') auth = GcpSession(module, "cloudresourcemanager")
response = auth.get(link, params={'filter': query}) response = auth.get(link, params={"filter": query})
return self._return_if_object(module, response) return self._return_if_object(module, response)
def projects_for_folder(self, config_data, folder): def projects_for_folder(self, config_data, folder):
link = 'https://cloudresourcemanager.googleapis.com/v1/projects' link = "https://cloudresourcemanager.googleapis.com/v1/projects"
query = 'parent.id = {0}'.format(folder) query = "parent.id = {0}".format(folder)
projects = [] projects = []
config_data['scopes'] = ['https://www.googleapis.com/auth/cloud-platform'] config_data["scopes"] = ["https://www.googleapis.com/auth/cloud-platform"]
projects_response = self.fetch_projects(config_data, link, query) projects_response = self.fetch_projects(config_data, link, query)
if 'projects' in projects_response: if "projects" in projects_response:
for item in projects_response.get('projects'): for item in projects_response.get("projects"):
projects.append(item['projectId']) projects.append(item["projectId"])
return projects return projects
def parse(self, inventory, loader, path, cache=True): def parse(self, inventory, loader, path, cache=True):