|
| 1 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import print_function |
| 16 | + |
| 17 | +import argparse |
| 18 | +import base64 |
| 19 | +import datetime |
| 20 | +import json |
| 21 | + |
| 22 | +from googleapiclient import discovery |
| 23 | + |
| 24 | + |
| 25 | +def seconds_from_now_to_rfc3339_datetime(seconds): |
| 26 | + """Return an RFC 3339 datetime string for a number of seconds from now.""" |
| 27 | + d = datetime.datetime.utcnow() + datetime.timedelta(seconds=seconds) |
| 28 | + return d.isoformat('T') + 'Z' |
| 29 | + |
| 30 | + |
| 31 | +def create_task(project, queue, location, payload=None, in_seconds=None): |
| 32 | + """Create a task for a given queue with an arbitrary payload.""" |
| 33 | + |
| 34 | + # Create a client. |
| 35 | + DISCOVERY_URL = ( |
| 36 | + 'https://cloudtasks.googleapis.com/$discovery/rest?version=v2beta2') |
| 37 | + client = discovery.build( |
| 38 | + 'cloudtasks', 'v2beta2', discoveryServiceUrl=DISCOVERY_URL) |
| 39 | + |
| 40 | + url = '/log_payload' |
| 41 | + body = { |
| 42 | + 'task': { |
| 43 | + 'app_engine_task_target': { |
| 44 | + 'http_method': 'POST', |
| 45 | + 'relative_url': url |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if payload is not None: |
| 51 | + # Payload is a string (unicode), and must be encoded for base64. |
| 52 | + # The finished request body is JSON, which requires unicode. |
| 53 | + body['task']['app_engine_task_target']['payload'] = base64.b64encode( |
| 54 | + payload.encode()).decode() |
| 55 | + |
| 56 | + if in_seconds is not None: |
| 57 | + scheduled_time = seconds_from_now_to_rfc3339_datetime(in_seconds) |
| 58 | + body['task']['schedule_time'] = scheduled_time |
| 59 | + |
| 60 | + queue_name = 'projects/{}/locations/{}/queues/{}'.format( |
| 61 | + project, location, queue) |
| 62 | + |
| 63 | + print('Sending task {}'.format(json.dumps(body))) |
| 64 | + |
| 65 | + response = client.projects().locations().queues().tasks().create( |
| 66 | + parent=queue_name, body=body).execute() |
| 67 | + |
| 68 | + # By default CreateTaskRequest.responseView is BASIC, so not all |
| 69 | + # information is retrieved by default because some data, such as payloads, |
| 70 | + # might be desirable to return only when needed because of its large size |
| 71 | + # or because of the sensitivity of data that it contains. |
| 72 | + print('Created task {}'.format(response['name'])) |
| 73 | + return response |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + parser = argparse.ArgumentParser( |
| 78 | + description=create_task.__doc__, |
| 79 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 80 | + |
| 81 | + parser.add_argument( |
| 82 | + '--project', |
| 83 | + help='Project of the queue to add the task to.' |
| 84 | + ) |
| 85 | + |
| 86 | + parser.add_argument( |
| 87 | + '--queue', |
| 88 | + help='ID (short name) of the queue to add the task to.' |
| 89 | + ) |
| 90 | + |
| 91 | + parser.add_argument( |
| 92 | + '--location', |
| 93 | + help='Location of the queue to add the task to.' |
| 94 | + ) |
| 95 | + |
| 96 | + parser.add_argument( |
| 97 | + '--payload', |
| 98 | + help='Optional payload to attach to the push queue.' |
| 99 | + ) |
| 100 | + |
| 101 | + parser.add_argument( |
| 102 | + '--in_seconds', |
| 103 | + help='The number of seconds from now to schedule task attempt.' |
| 104 | + ) |
| 105 | + |
| 106 | + args = parser.parse_args() |
| 107 | + |
| 108 | + create_task( |
| 109 | + args.project, args.queue, args.location, |
| 110 | + args.payload, args.in_seconds) |
0 commit comments