| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
from __future__ import absolute_import, division, print_function |
| 21 |
|
| 22 |
__metaclass__ = type |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ["preview"], 'supported_by': 'community'} |
| 29 |
|
| 30 |
DOCUMENTATION = ''' |
| 31 |
--- |
| 32 |
module: gcp_compute_autoscaler_info |
| 33 |
description: |
| 34 |
- Gather info for GCP Autoscaler |
| 35 |
short_description: Gather info for GCP Autoscaler |
| 36 |
author: Google Inc. (@googlecloudplatform) |
| 37 |
requirements: |
| 38 |
- python >= 2.6 |
| 39 |
- requests >= 2.18.4 |
| 40 |
- google-auth >= 1.3.0 |
| 41 |
options: |
| 42 |
filters: |
| 43 |
description: |
| 44 |
- A list of filter value pairs. Available filters are listed here U(https://cloud.google.com/sdk/gcloud/reference/topic/filters). |
| 45 |
- Each additional filter in the list will act be added as an AND condition (filter1 |
| 46 |
and filter2) . |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
zone: |
| 50 |
description: |
| 51 |
- URL of the zone where the instance group resides. |
| 52 |
required: true |
| 53 |
type: str |
| 54 |
project: |
| 55 |
description: |
| 56 |
- The Google Cloud Platform project to use. |
| 57 |
type: str |
| 58 |
auth_kind: |
| 59 |
description: |
| 60 |
- The type of credential used. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
choices: |
| 64 |
- application |
| 65 |
- machineaccount |
| 66 |
- serviceaccount |
| 67 |
- accesstoken |
| 68 |
service_account_contents: |
| 69 |
description: |
| 70 |
- The contents of a Service Account JSON file, either in a dictionary or as a |
| 71 |
JSON string that represents it. |
| 72 |
type: jsonarg |
| 73 |
service_account_file: |
| 74 |
description: |
| 75 |
- The path of a Service Account JSON file if serviceaccount is selected as type. |
| 76 |
type: path |
| 77 |
service_account_email: |
| 78 |
description: |
| 79 |
- An optional service account email address if machineaccount is selected and |
| 80 |
the user does not wish to use the default email. |
| 81 |
type: str |
| 82 |
access_token: |
| 83 |
description: |
| 84 |
- An OAuth2 access token if credential type is accesstoken. |
| 85 |
type: str |
| 86 |
scopes: |
| 87 |
description: |
| 88 |
- Array of scopes to be used |
| 89 |
type: list |
| 90 |
elements: str |
| 91 |
env_type: |
| 92 |
description: |
| 93 |
- Specifies which Ansible environment you're running this module within. |
| 94 |
- This should not be set unless you know what you're doing. |
| 95 |
- This only alters the User Agent string for any API requests. |
| 96 |
type: str |
| 97 |
notes: |
| 98 |
- for authentication, you can set service_account_file using the C(GCP_SERVICE_ACCOUNT_FILE) |
| 99 |
env variable. |
| 100 |
- for authentication, you can set service_account_contents using the C(GCP_SERVICE_ACCOUNT_CONTENTS) |
| 101 |
env variable. |
| 102 |
- For authentication, you can set service_account_email using the C(GCP_SERVICE_ACCOUNT_EMAIL) |
| 103 |
env variable. |
| 104 |
- For authentication, you can set access_token using the C(GCP_ACCESS_TOKEN) |
| 105 |
env variable. |
| 106 |
- For authentication, you can set auth_kind using the C(GCP_AUTH_KIND) env variable. |
| 107 |
- For authentication, you can set scopes using the C(GCP_SCOPES) env variable. |
| 108 |
- Environment variables values will only be used if the playbook values are not set. |
| 109 |
- The I(service_account_email) and I(service_account_file) options are mutually exclusive. |
| 110 |
''' |
| 111 |
|
| 112 |
EXAMPLES = ''' |
| 113 |
- name: get info on an autoscaler |
| 114 |
gcp_compute_autoscaler_info: |
| 115 |
zone: us-central1-a |
| 116 |
filters: |
| 117 |
- name = test_object |
| 118 |
project: test_project |
| 119 |
auth_kind: serviceaccount |
| 120 |
service_account_file: "/tmp/auth.pem" |
| 121 |
''' |
| 122 |
|
| 123 |
RETURN = ''' |
| 124 |
resources: |
| 125 |
description: List of resources |
| 126 |
returned: always |
| 127 |
type: complex |
| 128 |
contains: |
| 129 |
id: |
| 130 |
description: |
| 131 |
- Unique identifier for the resource. |
| 132 |
returned: success |
| 133 |
type: int |
| 134 |
creationTimestamp: |
| 135 |
description: |
| 136 |
- Creation timestamp in RFC3339 text format. |
| 137 |
returned: success |
| 138 |
type: str |
| 139 |
name: |
| 140 |
description: |
| 141 |
- Name of the resource. The name must be 1-63 characters long and match the |
| 142 |
regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means the first character |
| 143 |
must be a lowercase letter, and all following characters must be a dash, lowercase |
| 144 |
letter, or digit, except the last character, which cannot be a dash. |
| 145 |
returned: success |
| 146 |
type: str |
| 147 |
description: |
| 148 |
description: |
| 149 |
- An optional description of this resource. |
| 150 |
returned: success |
| 151 |
type: str |
| 152 |
autoscalingPolicy: |
| 153 |
description: |
| 154 |
- 'The configuration parameters for the autoscaling algorithm. You can define |
| 155 |
one or more of the policies for an autoscaler: cpuUtilization, customMetricUtilizations, |
| 156 |
and loadBalancingUtilization.' |
| 157 |
- If none of these are specified, the default will be to autoscale based on |
| 158 |
cpuUtilization to 0.6 or 60%. |
| 159 |
returned: success |
| 160 |
type: complex |
| 161 |
contains: |
| 162 |
minNumReplicas: |
| 163 |
description: |
| 164 |
- The minimum number of replicas that the autoscaler can scale down to. |
| 165 |
This cannot be less than 0. If not provided, autoscaler will choose a |
| 166 |
default value depending on maximum number of instances allowed. |
| 167 |
returned: success |
| 168 |
type: int |
| 169 |
maxNumReplicas: |
| 170 |
description: |
| 171 |
- The maximum number of instances that the autoscaler can scale up to. This |
| 172 |
is required when creating or updating an autoscaler. The maximum number |
| 173 |
of replicas should not be lower than minimal number of replicas. |
| 174 |
returned: success |
| 175 |
type: int |
| 176 |
coolDownPeriodSec: |
| 177 |
description: |
| 178 |
- The number of seconds that the autoscaler should wait before it starts |
| 179 |
collecting information from a new instance. This prevents the autoscaler |
| 180 |
from collecting information when the instance is initializing, during |
| 181 |
which the collected usage would not be reliable. The default time autoscaler |
| 182 |
waits is 60 seconds. |
| 183 |
- Virtual machine initialization times might vary because of numerous factors. |
| 184 |
We recommend that you test how long an instance may take to initialize. |
| 185 |
To do this, create an instance and time the startup process. |
| 186 |
returned: success |
| 187 |
type: int |
| 188 |
mode: |
| 189 |
description: |
| 190 |
- Defines operating mode for this policy. |
| 191 |
returned: success |
| 192 |
type: str |
| 193 |
scaleInControl: |
| 194 |
description: |
| 195 |
- Defines scale in controls to reduce the risk of response latency and outages |
| 196 |
due to abrupt scale-in events . |
| 197 |
returned: success |
| 198 |
type: complex |
| 199 |
contains: |
| 200 |
maxScaledInReplicas: |
| 201 |
description: |
| 202 |
- A nested object resource. |
| 203 |
returned: success |
| 204 |
type: complex |
| 205 |
contains: |
| 206 |
fixed: |
| 207 |
description: |
| 208 |
- Specifies a fixed number of VM instances. This must be a positive |
| 209 |
integer. |
| 210 |
returned: success |
| 211 |
type: int |
| 212 |
percent: |
| 213 |
description: |
| 214 |
- Specifies a percentage of instances between 0 to 100%, inclusive. |
| 215 |
- For example, specify 80 for 80%. |
| 216 |
returned: success |
| 217 |
type: int |
| 218 |
timeWindowSec: |
| 219 |
description: |
| 220 |
- How long back autoscaling should look when computing recommendations |
| 221 |
to include directives regarding slower scale down, as described above. |
| 222 |
returned: success |
| 223 |
type: int |
| 224 |
cpuUtilization: |
| 225 |
description: |
| 226 |
- Defines the CPU utilization policy that allows the autoscaler to scale |
| 227 |
based on the average CPU utilization of a managed instance group. |
| 228 |
returned: success |
| 229 |
type: complex |
| 230 |
contains: |
| 231 |
utilizationTarget: |
| 232 |
description: |
| 233 |
- The target CPU utilization that the autoscaler should maintain. |
| 234 |
- Must be a float value in the range (0, 1]. If not specified, the default |
| 235 |
is 0.6. |
| 236 |
- If the CPU level is below the target utilization, the autoscaler scales |
| 237 |
down the number of instances until it reaches the minimum number of |
| 238 |
instances you specified or until the average CPU of your instances |
| 239 |
reaches the target utilization. |
| 240 |
- If the average CPU is above the target utilization, the autoscaler |
| 241 |
scales up until it reaches the maximum number of instances you specified |
| 242 |
or until the average utilization reaches the target utilization. |
| 243 |
returned: success |
| 244 |
type: str |
| 245 |
predictiveMethod: |
| 246 |
description: |
| 247 |
- 'Indicates whether predictive autoscaling based on CPU metric is enabled. |
| 248 |
Valid values are: - NONE (default). No predictive method is used. |
| 249 |
The autoscaler scales the group to meet current demand based on real-time |
| 250 |
metrics.' |
| 251 |
- "- OPTIMIZE_AVAILABILITY. Predictive autoscaling improves availability |
| 252 |
by monitoring daily and weekly load patterns and scaling out ahead |
| 253 |
of anticipated demand." |
| 254 |
returned: success |
| 255 |
type: str |
| 256 |
customMetricUtilizations: |
| 257 |
description: |
| 258 |
- Configuration parameters of autoscaling based on a custom metric. |
| 259 |
returned: success |
| 260 |
type: complex |
| 261 |
contains: |
| 262 |
metric: |
| 263 |
description: |
| 264 |
- The identifier (type) of the Stackdriver Monitoring metric. |
| 265 |
- The metric cannot have negative values. |
| 266 |
- The metric must have a value type of INT64 or DOUBLE. |
| 267 |
returned: success |
| 268 |
type: str |
| 269 |
utilizationTarget: |
| 270 |
description: |
| 271 |
- The target value of the metric that autoscaler should maintain. This |
| 272 |
must be a positive value. A utilization metric scales number of virtual |
| 273 |
machines handling requests to increase or decrease proportionally |
| 274 |
to the metric. |
| 275 |
- For example, a good metric to use as a utilizationTarget is U(www.googleapis.com/compute/instance/network/received_bytes_count). |
| 276 |
- The autoscaler will work to keep this value constant for each of the |
| 277 |
instances. |
| 278 |
returned: success |
| 279 |
type: str |
| 280 |
utilizationTargetType: |
| 281 |
description: |
| 282 |
- Defines how target utilization value is expressed for a Stackdriver |
| 283 |
Monitoring metric. |
| 284 |
returned: success |
| 285 |
type: str |
| 286 |
loadBalancingUtilization: |
| 287 |
description: |
| 288 |
- Configuration parameters of autoscaling based on a load balancer. |
| 289 |
returned: success |
| 290 |
type: complex |
| 291 |
contains: |
| 292 |
utilizationTarget: |
| 293 |
description: |
| 294 |
- Fraction of backend capacity utilization (set in HTTP(s) load balancing |
| 295 |
configuration) that autoscaler should maintain. Must be a positive |
| 296 |
float value. If not defined, the default is 0.8. |
| 297 |
returned: success |
| 298 |
type: str |
| 299 |
target: |
| 300 |
description: |
| 301 |
- URL of the managed instance group that this autoscaler will scale. |
| 302 |
returned: success |
| 303 |
type: dict |
| 304 |
zone: |
| 305 |
description: |
| 306 |
- URL of the zone where the instance group resides. |
| 307 |
returned: success |
| 308 |
type: str |
| 309 |
''' |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule |
| 315 |
import json |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
def main(): |
| 323 |
module = GcpModule(argument_spec=dict(filters=dict(type='list', elements='str'), zone=dict(required=True, type='str')), supports_check_mode=True) |
| 324 |
|
| 325 |
if not module.params['scopes']: |
| 326 |
module.params['scopes'] = ['https://www.googleapis.com/auth/compute'] |
| 327 |
|
| 328 |
return_value = {'resources': fetch_list(module, collection(module), query_options(module.params['filters']))} |
| 329 |
module.exit_json(**return_value) |
| 330 |
|
| 331 |
|
| 332 |
def collection(module): |
| 333 |
return "https://compute.googleapis.com/compute/v1/projects/{project}/zones/{zone}/autoscalers".format(**module.params) |
| 334 |
|
| 335 |
|
| 336 |
def fetch_list(module, link, query): |
| 337 |
auth = GcpSession(module, 'compute') |
| 338 |
return auth.list(link, return_if_object, array_name='items', params={'filter': query}) |
| 339 |
|
| 340 |
|
| 341 |
def query_options(filters): |
| 342 |
if not filters: |
| 343 |
return '' |
| 344 |
|
| 345 |
if len(filters) == 1: |
| 346 |
return filters[0] |
| 347 |
else: |
| 348 |
queries = [] |
| 349 |
for f in filters: |
| 350 |
|
| 351 |
if f[0] != '(' and f[-1] != ')': |
| 352 |
queries.append("(%s)" % ''.join(f)) |
| 353 |
else: |
| 354 |
queries.append(f) |
| 355 |
|
| 356 |
return ' '.join(queries) |
| 357 |
|
| 358 |
|
| 359 |
def return_if_object(module, response): |
| 360 |
|
| 361 |
if response.status_code == 404: |
| 362 |
return None |
| 363 |
|
| 364 |
|
| 365 |
if response.status_code == 204: |
| 366 |
return None |
| 367 |
|
| 368 |
try: |
| 369 |
module.raise_for_status(response) |
| 370 |
result = response.json() |
| 371 |
except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst: |
| 372 |
module.fail_json(msg="Invalid JSON response with error: %s" % inst) |
| 373 |
|
| 374 |
if navigate_hash(result, ['error', 'errors']): |
| 375 |
module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) |
| 376 |
|
| 377 |
return result |
| 378 |
|
| 379 |
|
| 380 |
if __name__ == "__main__": |
| 381 |
main() |