| 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_cloudscheduler_job_info |
| 33 |
description: |
| 34 |
- Gather info for GCP Job |
| 35 |
short_description: Gather info for GCP Job |
| 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 |
region: |
| 43 |
description: |
| 44 |
- Region where the scheduler job resides . |
| 45 |
required: true |
| 46 |
type: str |
| 47 |
project: |
| 48 |
description: |
| 49 |
- The Google Cloud Platform project to use. |
| 50 |
type: str |
| 51 |
auth_kind: |
| 52 |
description: |
| 53 |
- The type of credential used. |
| 54 |
type: str |
| 55 |
required: true |
| 56 |
choices: |
| 57 |
- application |
| 58 |
- machineaccount |
| 59 |
- serviceaccount |
| 60 |
- accesstoken |
| 61 |
service_account_contents: |
| 62 |
description: |
| 63 |
- The contents of a Service Account JSON file, either in a dictionary or as a |
| 64 |
JSON string that represents it. |
| 65 |
type: jsonarg |
| 66 |
service_account_file: |
| 67 |
description: |
| 68 |
- The path of a Service Account JSON file if serviceaccount is selected as type. |
| 69 |
type: path |
| 70 |
service_account_email: |
| 71 |
description: |
| 72 |
- An optional service account email address if machineaccount is selected and |
| 73 |
the user does not wish to use the default email. |
| 74 |
type: str |
| 75 |
access_token: |
| 76 |
description: |
| 77 |
- An OAuth2 access token if credential type is accesstoken. |
| 78 |
type: str |
| 79 |
scopes: |
| 80 |
description: |
| 81 |
- Array of scopes to be used |
| 82 |
type: list |
| 83 |
elements: str |
| 84 |
env_type: |
| 85 |
description: |
| 86 |
- Specifies which Ansible environment you're running this module within. |
| 87 |
- This should not be set unless you know what you're doing. |
| 88 |
- This only alters the User Agent string for any API requests. |
| 89 |
type: str |
| 90 |
notes: |
| 91 |
- for authentication, you can set service_account_file using the C(GCP_SERVICE_ACCOUNT_FILE) |
| 92 |
env variable. |
| 93 |
- for authentication, you can set service_account_contents using the C(GCP_SERVICE_ACCOUNT_CONTENTS) |
| 94 |
env variable. |
| 95 |
- For authentication, you can set service_account_email using the C(GCP_SERVICE_ACCOUNT_EMAIL) |
| 96 |
env variable. |
| 97 |
- For authentication, you can set access_token using the C(GCP_ACCESS_TOKEN) |
| 98 |
env variable. |
| 99 |
- For authentication, you can set auth_kind using the C(GCP_AUTH_KIND) env variable. |
| 100 |
- For authentication, you can set scopes using the C(GCP_SCOPES) env variable. |
| 101 |
- Environment variables values will only be used if the playbook values are not set. |
| 102 |
- The I(service_account_email) and I(service_account_file) options are mutually exclusive. |
| 103 |
''' |
| 104 |
|
| 105 |
EXAMPLES = ''' |
| 106 |
- name: get info on a job |
| 107 |
gcp_cloudscheduler_job_info: |
| 108 |
region: us-central1 |
| 109 |
project: test_project |
| 110 |
auth_kind: serviceaccount |
| 111 |
service_account_file: "/tmp/auth.pem" |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = ''' |
| 115 |
resources: |
| 116 |
description: List of resources |
| 117 |
returned: always |
| 118 |
type: complex |
| 119 |
contains: |
| 120 |
name: |
| 121 |
description: |
| 122 |
- The name of the job. |
| 123 |
returned: success |
| 124 |
type: str |
| 125 |
description: |
| 126 |
description: |
| 127 |
- A human-readable description for the job. This string must not contain more |
| 128 |
than 500 characters. |
| 129 |
returned: success |
| 130 |
type: str |
| 131 |
schedule: |
| 132 |
description: |
| 133 |
- Describes the schedule on which the job will be executed. |
| 134 |
returned: success |
| 135 |
type: str |
| 136 |
timeZone: |
| 137 |
description: |
| 138 |
- Specifies the time zone to be used in interpreting schedule. |
| 139 |
- The value of this field must be a time zone name from the tz database. |
| 140 |
returned: success |
| 141 |
type: str |
| 142 |
attemptDeadline: |
| 143 |
description: |
| 144 |
- The deadline for job attempts. If the request handler does not respond by |
| 145 |
this deadline then the request is cancelled and the attempt is marked as a |
| 146 |
DEADLINE_EXCEEDED failure. The failed attempt can be viewed in execution logs. |
| 147 |
Cloud Scheduler will retry the job according to the RetryConfig. |
| 148 |
- 'The allowed duration for this deadline is: * For HTTP targets, between 15 |
| 149 |
seconds and 30 minutes.' |
| 150 |
- "* For App Engine HTTP targets, between 15 seconds and 24 hours." |
| 151 |
- "* **Note**: For PubSub targets, this field is ignored - setting it will introduce |
| 152 |
an unresolvable diff." |
| 153 |
- 'A duration in seconds with up to nine fractional digits, terminated by ''s''. |
| 154 |
Example: "3.5s" .' |
| 155 |
returned: success |
| 156 |
type: str |
| 157 |
retryConfig: |
| 158 |
description: |
| 159 |
- By default, if a job does not complete successfully, meaning that an acknowledgement |
| 160 |
is not received from the handler, then it will be retried with exponential |
| 161 |
backoff according to the settings . |
| 162 |
returned: success |
| 163 |
type: complex |
| 164 |
contains: |
| 165 |
retryCount: |
| 166 |
description: |
| 167 |
- The number of attempts that the system will make to run a job using the |
| 168 |
exponential backoff procedure described by maxDoublings. |
| 169 |
- Values greater than 5 and negative values are not allowed. |
| 170 |
returned: success |
| 171 |
type: int |
| 172 |
maxRetryDuration: |
| 173 |
description: |
| 174 |
- The time limit for retrying a failed job, measured from time when an execution |
| 175 |
was first attempted. If specified with retryCount, the job will be retried |
| 176 |
until both limits are reached. |
| 177 |
- A duration in seconds with up to nine fractional digits, terminated by |
| 178 |
's'. |
| 179 |
returned: success |
| 180 |
type: str |
| 181 |
minBackoffDuration: |
| 182 |
description: |
| 183 |
- The minimum amount of time to wait before retrying a job after it fails. |
| 184 |
- A duration in seconds with up to nine fractional digits, terminated by |
| 185 |
's'. |
| 186 |
returned: success |
| 187 |
type: str |
| 188 |
maxBackoffDuration: |
| 189 |
description: |
| 190 |
- The maximum amount of time to wait before retrying a job after it fails. |
| 191 |
- A duration in seconds with up to nine fractional digits, terminated by |
| 192 |
's'. |
| 193 |
returned: success |
| 194 |
type: str |
| 195 |
maxDoublings: |
| 196 |
description: |
| 197 |
- The time between retries will double maxDoublings times. |
| 198 |
- A job's retry interval starts at minBackoffDuration, then doubles maxDoublings |
| 199 |
times, then increases linearly, and finally retries retries at intervals |
| 200 |
of maxBackoffDuration up to retryCount times. |
| 201 |
returned: success |
| 202 |
type: int |
| 203 |
pubsubTarget: |
| 204 |
description: |
| 205 |
- Pub/Sub target If the job providers a Pub/Sub target the cron will publish |
| 206 |
a message to the provided topic . |
| 207 |
returned: success |
| 208 |
type: complex |
| 209 |
contains: |
| 210 |
topicName: |
| 211 |
description: |
| 212 |
- The full resource name for the Cloud Pub/Sub topic to which messages will |
| 213 |
be published when a job is delivered. ~>**NOTE:** The topic name must |
| 214 |
be in the same format as required by PubSub's PublishRequest.name, e.g. |
| 215 |
`projects/my-project/topics/my-topic`. |
| 216 |
returned: success |
| 217 |
type: str |
| 218 |
data: |
| 219 |
description: |
| 220 |
- The message payload for PubsubMessage. |
| 221 |
- Pubsub message must contain either non-empty data, or at least one attribute. |
| 222 |
- A base64-encoded string. |
| 223 |
returned: success |
| 224 |
type: str |
| 225 |
attributes: |
| 226 |
description: |
| 227 |
- Attributes for PubsubMessage. |
| 228 |
- Pubsub message must contain either non-empty data, or at least one attribute. |
| 229 |
returned: success |
| 230 |
type: dict |
| 231 |
appEngineHttpTarget: |
| 232 |
description: |
| 233 |
- App Engine HTTP target. |
| 234 |
- If the job providers a App Engine HTTP target the cron will send a request |
| 235 |
to the service instance . |
| 236 |
returned: success |
| 237 |
type: complex |
| 238 |
contains: |
| 239 |
httpMethod: |
| 240 |
description: |
| 241 |
- Which HTTP method to use for the request. |
| 242 |
returned: success |
| 243 |
type: str |
| 244 |
appEngineRouting: |
| 245 |
description: |
| 246 |
- App Engine Routing setting for the job. |
| 247 |
returned: success |
| 248 |
type: complex |
| 249 |
contains: |
| 250 |
service: |
| 251 |
description: |
| 252 |
- App service. |
| 253 |
- By default, the job is sent to the service which is the default service |
| 254 |
when the job is attempted. |
| 255 |
returned: success |
| 256 |
type: str |
| 257 |
version: |
| 258 |
description: |
| 259 |
- App version. |
| 260 |
- By default, the job is sent to the version which is the default version |
| 261 |
when the job is attempted. |
| 262 |
returned: success |
| 263 |
type: str |
| 264 |
instance: |
| 265 |
description: |
| 266 |
- App instance. |
| 267 |
- By default, the job is sent to an instance which is available when |
| 268 |
the job is attempted. |
| 269 |
returned: success |
| 270 |
type: str |
| 271 |
relativeUri: |
| 272 |
description: |
| 273 |
- The relative URI. |
| 274 |
returned: success |
| 275 |
type: str |
| 276 |
body: |
| 277 |
description: |
| 278 |
- HTTP request body. A request body is allowed only if the HTTP method is |
| 279 |
POST or PUT. It will result in invalid argument error to set a body on |
| 280 |
a job with an incompatible HttpMethod. |
| 281 |
- A base64-encoded string. |
| 282 |
returned: success |
| 283 |
type: str |
| 284 |
headers: |
| 285 |
description: |
| 286 |
- HTTP request headers. |
| 287 |
- This map contains the header field names and values. Headers can be set |
| 288 |
when the job is created. |
| 289 |
returned: success |
| 290 |
type: dict |
| 291 |
httpTarget: |
| 292 |
description: |
| 293 |
- HTTP target. |
| 294 |
- If the job providers a http_target the cron will send a request to the targeted |
| 295 |
url . |
| 296 |
returned: success |
| 297 |
type: complex |
| 298 |
contains: |
| 299 |
uri: |
| 300 |
description: |
| 301 |
- The full URI path that the request will be sent to. |
| 302 |
returned: success |
| 303 |
type: str |
| 304 |
httpMethod: |
| 305 |
description: |
| 306 |
- Which HTTP method to use for the request. |
| 307 |
returned: success |
| 308 |
type: str |
| 309 |
body: |
| 310 |
description: |
| 311 |
- HTTP request body. A request body is allowed only if the HTTP method is |
| 312 |
POST, PUT, or PATCH. It is an error to set body on a job with an incompatible |
| 313 |
HttpMethod. |
| 314 |
- A base64-encoded string. |
| 315 |
returned: success |
| 316 |
type: str |
| 317 |
headers: |
| 318 |
description: |
| 319 |
- This map contains the header field names and values. Repeated headers |
| 320 |
are not supported, but a header value can contain commas. |
| 321 |
returned: success |
| 322 |
type: dict |
| 323 |
oauthToken: |
| 324 |
description: |
| 325 |
- Contains information needed for generating an OAuth token. |
| 326 |
- This type of authorization should be used when sending requests to a GCP |
| 327 |
endpoint. |
| 328 |
returned: success |
| 329 |
type: complex |
| 330 |
contains: |
| 331 |
serviceAccountEmail: |
| 332 |
description: |
| 333 |
- Service account email to be used for generating OAuth token. |
| 334 |
- The service account must be within the same project as the job. |
| 335 |
returned: success |
| 336 |
type: str |
| 337 |
scope: |
| 338 |
description: |
| 339 |
- OAuth scope to be used for generating OAuth access token. If not specified, |
| 340 |
"U(https://www.googleapis.com/auth/cloud-platform") will be used. |
| 341 |
returned: success |
| 342 |
type: str |
| 343 |
oidcToken: |
| 344 |
description: |
| 345 |
- Contains information needed for generating an OpenID Connect token. |
| 346 |
- This type of authorization should be used when sending requests to third |
| 347 |
party endpoints or Cloud Run. |
| 348 |
returned: success |
| 349 |
type: complex |
| 350 |
contains: |
| 351 |
serviceAccountEmail: |
| 352 |
description: |
| 353 |
- Service account email to be used for generating OAuth token. |
| 354 |
- The service account must be within the same project as the job. |
| 355 |
returned: success |
| 356 |
type: str |
| 357 |
audience: |
| 358 |
description: |
| 359 |
- Audience to be used when generating OIDC token. If not specified, |
| 360 |
the URI specified in target will be used. |
| 361 |
returned: success |
| 362 |
type: str |
| 363 |
region: |
| 364 |
description: |
| 365 |
- Region where the scheduler job resides . |
| 366 |
returned: success |
| 367 |
type: str |
| 368 |
''' |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule |
| 374 |
import json |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
def main(): |
| 382 |
module = GcpModule(argument_spec=dict(region=dict(required=True, type='str')), supports_check_mode=True) |
| 383 |
|
| 384 |
if not module.params['scopes']: |
| 385 |
module.params['scopes'] = ['https://www.googleapis.com/auth/cloud-platform'] |
| 386 |
|
| 387 |
return_value = {'resources': fetch_list(module, collection(module))} |
| 388 |
module.exit_json(**return_value) |
| 389 |
|
| 390 |
|
| 391 |
def collection(module): |
| 392 |
return "https://cloudscheduler.googleapis.com/v1/projects/{project}/locations/{region}/jobs".format(**module.params) |
| 393 |
|
| 394 |
|
| 395 |
def fetch_list(module, link): |
| 396 |
auth = GcpSession(module, 'cloudscheduler') |
| 397 |
return auth.list(link, return_if_object, array_name='jobs') |
| 398 |
|
| 399 |
|
| 400 |
def return_if_object(module, response): |
| 401 |
|
| 402 |
if response.status_code == 404: |
| 403 |
return None |
| 404 |
|
| 405 |
|
| 406 |
if response.status_code == 204: |
| 407 |
return None |
| 408 |
|
| 409 |
try: |
| 410 |
module.raise_for_status(response) |
| 411 |
result = response.json() |
| 412 |
except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst: |
| 413 |
module.fail_json(msg="Invalid JSON response with error: %s" % inst) |
| 414 |
|
| 415 |
if navigate_hash(result, ['error', 'errors']): |
| 416 |
module.fail_json(msg=navigate_hash(result, ['error', 'errors'])) |
| 417 |
|
| 418 |
return result |
| 419 |
|
| 420 |
|
| 421 |
if __name__ == "__main__": |
| 422 |
main() |