1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3# 4# Copyright (C) 2017 Google 5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) 6# ---------------------------------------------------------------------------- 7# 8# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** 9# 10# ---------------------------------------------------------------------------- 11# 12# This file is automatically generated by Magic Modules and manual 13# changes will be clobbered when the file is regenerated. 14# 15# Please read more about how to change this file at 16# https://www.github.com/GoogleCloudPlatform/magic-modules 17# 18# ---------------------------------------------------------------------------- 19 20from __future__ import absolute_import, division, print_function 21 22__metaclass__ = type 23 24################################################################################ 25# Documentation 26################################################################################ 27 28ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ["preview"], 'supported_by': 'community'} 29 30DOCUMENTATION = ''' 31--- 32module: gcp_compute_forwarding_rule_info 33description: 34- Gather info for GCP ForwardingRule 35- This module was called C(gcp_compute_forwarding_rule_facts) before Ansible 2.9. 36 The usage has not changed. 37short_description: Gather info for GCP ForwardingRule 38version_added: 2.7 39author: Google Inc. (@googlecloudplatform) 40requirements: 41- python >= 2.6 42- requests >= 2.18.4 43- google-auth >= 1.3.0 44options: 45 filters: 46 description: 47 - A list of filter value pairs. Available filters are listed here U(https://cloud.google.com/sdk/gcloud/reference/topic/filters). 48 - Each additional filter in the list will act be added as an AND condition (filter1 49 and filter2) . 50 type: list 51 region: 52 description: 53 - A reference to the region where the regional forwarding rule resides. 54 - This field is not applicable to global forwarding rules. 55 required: true 56 type: str 57extends_documentation_fragment: gcp 58''' 59 60EXAMPLES = ''' 61- name: get info on a forwarding rule 62 gcp_compute_forwarding_rule_info: 63 region: us-west1 64 filters: 65 - name = test_object 66 project: test_project 67 auth_kind: serviceaccount 68 service_account_file: "/tmp/auth.pem" 69''' 70 71RETURN = ''' 72resources: 73 description: List of resources 74 returned: always 75 type: complex 76 contains: 77 creationTimestamp: 78 description: 79 - Creation timestamp in RFC3339 text format. 80 returned: success 81 type: str 82 description: 83 description: 84 - An optional description of this resource. Provide this property when you create 85 the resource. 86 returned: success 87 type: str 88 id: 89 description: 90 - The unique identifier for the resource. 91 returned: success 92 type: int 93 IPAddress: 94 description: 95 - The IP address that this forwarding rule is serving on behalf of. 96 - Addresses are restricted based on the forwarding rule's load balancing scheme 97 (EXTERNAL or INTERNAL) and scope (global or regional). 98 - When the load balancing scheme is EXTERNAL, for global forwarding rules, the 99 address must be a global IP, and for regional forwarding rules, the address 100 must live in the same region as the forwarding rule. If this field is empty, 101 an ephemeral IPv4 address from the same scope (global or regional) will be 102 assigned. A regional forwarding rule supports IPv4 only. A global forwarding 103 rule supports either IPv4 or IPv6. 104 - When the load balancing scheme is INTERNAL, this can only be an RFC 1918 IP 105 address belonging to the network/subnet configured for the forwarding rule. 106 By default, if this field is empty, an ephemeral internal IP address will 107 be automatically allocated from the IP range of the subnet or network configured 108 for this forwarding rule. 109 - 'An address can be specified either by a literal IP address or a URL reference 110 to an existing Address resource. The following examples are all valid: * 100.1.2.3 111 * U(https://www.googleapis.com/compute/v1/projects/project/regions/region/addresses/address) 112 * projects/project/regions/region/addresses/address * regions/region/addresses/address 113 * global/addresses/address * address .' 114 returned: success 115 type: str 116 IPProtocol: 117 description: 118 - The IP protocol to which this rule applies. Valid options are TCP, UDP, ESP, 119 AH, SCTP or ICMP. 120 - When the load balancing scheme is INTERNAL, only TCP and UDP are valid. 121 returned: success 122 type: str 123 backendService: 124 description: 125 - A BackendService to receive the matched traffic. This is used only for INTERNAL 126 load balancing. 127 returned: success 128 type: dict 129 ipVersion: 130 description: 131 - ipVersion is not a valid field for regional forwarding rules. 132 returned: success 133 type: str 134 loadBalancingScheme: 135 description: 136 - 'This signifies what the ForwardingRule will be used for and can only take 137 the following values: INTERNAL, EXTERNAL The value of INTERNAL means that 138 this will be used for Internal Network Load Balancing (TCP, UDP). The value 139 of EXTERNAL means that this will be used for External Load Balancing (HTTP(S) 140 LB, External TCP/UDP LB, SSL Proxy) .' 141 returned: success 142 type: str 143 name: 144 description: 145 - Name of the resource; provided by the client when the resource is created. 146 The name must be 1-63 characters long, and comply with RFC1035. Specifically, 147 the name must be 1-63 characters long and match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` 148 which means the first character must be a lowercase letter, and all following 149 characters must be a dash, lowercase letter, or digit, except the last character, 150 which cannot be a dash. 151 returned: success 152 type: str 153 network: 154 description: 155 - For internal load balancing, this field identifies the network that the load 156 balanced IP should belong to for this Forwarding Rule. If this field is not 157 specified, the default network will be used. 158 - This field is only used for INTERNAL load balancing. 159 returned: success 160 type: dict 161 portRange: 162 description: 163 - This field is used along with the target field for TargetHttpProxy, TargetHttpsProxy, 164 TargetSslProxy, TargetTcpProxy, TargetVpnGateway, TargetPool, TargetInstance. 165 - Applicable only when IPProtocol is TCP, UDP, or SCTP, only packets addressed 166 to ports in the specified range will be forwarded to target. 167 - Forwarding rules with the same [IPAddress, IPProtocol] pair must have disjoint 168 port ranges. 169 - 'Some types of forwarding target have constraints on the acceptable ports: 170 * TargetHttpProxy: 80, 8080 * TargetHttpsProxy: 443 * TargetTcpProxy: 25, 171 43, 110, 143, 195, 443, 465, 587, 700, 993, 995, 1883, 5222 * TargetSslProxy: 172 25, 43, 110, 143, 195, 443, 465, 587, 700, 993, 995, 1883, 5222 * TargetVpnGateway: 173 500, 4500 .' 174 returned: success 175 type: str 176 ports: 177 description: 178 - This field is used along with the backend_service field for internal load 179 balancing. 180 - When the load balancing scheme is INTERNAL, a single port or a comma separated 181 list of ports can be configured. Only packets addressed to these ports will 182 be forwarded to the backends configured with this forwarding rule. 183 - You may specify a maximum of up to 5 ports. 184 returned: success 185 type: list 186 subnetwork: 187 description: 188 - The subnetwork that the load balanced IP should belong to for this Forwarding 189 Rule. This field is only used for INTERNAL load balancing. 190 - If the network specified is in auto subnet mode, this field is optional. However, 191 if the network is in custom subnet mode, a subnetwork must be specified. 192 returned: success 193 type: dict 194 target: 195 description: 196 - This field is only used for EXTERNAL load balancing. 197 - A reference to a TargetPool resource to receive the matched traffic. 198 - This target must live in the same region as the forwarding rule. 199 - The forwarded traffic must be of a type appropriate to the target object. 200 returned: success 201 type: dict 202 allPorts: 203 description: 204 - For internal TCP/UDP load balancing (i.e. load balancing scheme is INTERNAL 205 and protocol is TCP/UDP), set this to true to allow packets addressed to any 206 ports to be forwarded to the backends configured with this forwarding rule. 207 Used with backend service. Cannot be set if port or portRange are set. 208 returned: success 209 type: bool 210 networkTier: 211 description: 212 - 'The networking tier used for configuring this address. This field can take 213 the following values: PREMIUM or STANDARD. If this field is not specified, 214 it is assumed to be PREMIUM.' 215 returned: success 216 type: str 217 serviceLabel: 218 description: 219 - An optional prefix to the service name for this Forwarding Rule. 220 - If specified, will be the first label of the fully qualified service name. 221 - The label must be 1-63 characters long, and comply with RFC1035. 222 - Specifically, the label must be 1-63 characters long and match the regular 223 expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the first character must 224 be a lowercase letter, and all following characters must be a dash, lowercase 225 letter, or digit, except the last character, which cannot be a dash. 226 - This field is only used for INTERNAL load balancing. 227 returned: success 228 type: str 229 serviceName: 230 description: 231 - The internal fully qualified service name for this Forwarding Rule. 232 - This field is only used for INTERNAL load balancing. 233 returned: success 234 type: str 235 region: 236 description: 237 - A reference to the region where the regional forwarding rule resides. 238 - This field is not applicable to global forwarding rules. 239 returned: success 240 type: str 241''' 242 243################################################################################ 244# Imports 245################################################################################ 246from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest 247import json 248 249################################################################################ 250# Main 251################################################################################ 252 253 254def main(): 255 module = GcpModule(argument_spec=dict(filters=dict(type='list', elements='str'), region=dict(required=True, type='str'))) 256 257 if module._name == 'gcp_compute_forwarding_rule_facts': 258 module.deprecate("The 'gcp_compute_forwarding_rule_facts' module has been renamed to 'gcp_compute_forwarding_rule_info'", version='2.13') 259 260 if not module.params['scopes']: 261 module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] 262 263 return_value = {'resources': fetch_list(module, collection(module), query_options(module.params['filters']))} 264 module.exit_json(**return_value) 265 266 267def collection(module): 268 return "https://www.googleapis.com/compute/v1/projects/{project}/regions/{region}/forwardingRules".format(**module.params) 269 270 271def fetch_list(module, link, query): 272 auth = GcpSession(module, 'compute') 273 return auth.list(link, return_if_object, array_name='items', params={'filter': query}) 274 275 276def query_options(filters): 277 if not filters: 278 return '' 279 280 if len(filters) == 1: 281 return filters[0] 282 else: 283 queries = [] 284 for f in filters: 285 # For multiple queries, all queries should have () 286 if f[0] != '(' and f[-1] != ')': 287 queries.append("(%s)" % ''.join(f)) 288 else: 289 queries.append(f) 290 291 return ' '.join(queries) 292 293 294def return_if_object(module, response): 295 # If not found, return nothing. 296 if response.status_code == 404: 297 return None 298 299 # If no content, return nothing. 300 if response.status_code == 204: 301 return None 302 303 try: 304 module.raise_for_status(response) 305 result = response.json() 306 except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst: 307 module.fail_json(msg="Invalid JSON response with error: %s" % inst) 308 309 if navigate_hash(result, ['error', 'errors']): 310 module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) 311 312 return result 313 314 315if __name__ == "__main__": 316 main() 317