-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
base_job_sample.py
executable file
·188 lines (146 loc) · 5.06 KB
/
base_job_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
import string
from googleapiclient.discovery import build
from googleapiclient.errors import Error
client_service = build("jobs", "v3")
parent = "projects/" + os.environ["GOOGLE_CLOUD_PROJECT"]
# [START job_basic_job]
# [START basic_job]
def generate_job_with_required_fields(company_name):
# Requisition id should be a unique Id in your system.
requisition_id = "job_with_required_fields:" + "".join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(16)
)
job_title = "Software Engineer"
application_uris = ["http://careers.google.com"]
description = "Design, develop, test, deploy, maintain and improve " "software."
job = {
"requisition_id": requisition_id,
"title": job_title,
"application_info": {"uris": application_uris},
"description": description,
"company_name": company_name,
}
print("Job generated: %s" % job)
return job
# [END basic_job]
# [END job_basic_job]
# [START job_create_job]
# [START create_job]
def create_job(client_service, job_to_be_created):
try:
request = {"job": job_to_be_created}
job_created = (
client_service.projects()
.jobs()
.create(parent=parent, body=request)
.execute()
)
print("Job created: %s" % job_created)
return job_created
except Error as e:
print("Got exception while creating job")
raise e
# [END create_job]
# [END job_create_job]
# [START job_get_job]
def get_job(client_service, job_name):
try:
job_existed = client_service.projects().jobs().get(name=job_name).execute()
print("Job existed: %s" % job_existed)
return job_existed
except Error as e:
print("Got exception while getting job")
raise e
# [END job_get_job]
# [START update_job]
# [START job_update_job]
def update_job(client_service, job_name, job_to_be_updated):
try:
request = {"job": job_to_be_updated}
job_updated = (
client_service.projects()
.jobs()
.patch(name=job_name, body=request)
.execute()
)
print("Job updated: %s" % job_updated)
return job_updated
except Error as e:
print("Got exception while updating job")
raise e
# [END update_job]
# [END job_update_job]
# [START job_update_job_with_field_mask]
# [START update_job_with_field_mask]
def update_job_with_field_mask(client_service, job_name, job_to_be_updated, field_mask):
try:
request = {"job": job_to_be_updated, "update_mask": field_mask}
job_updated = (
client_service.projects()
.jobs()
.patch(name=job_name, body=request)
.execute()
)
print("Job updated: %s" % job_updated)
return job_updated
except Error as e:
print("Got exception while updating job with field mask")
raise e
# [END update_job_with_field_mask]
# [END job_update_job_with_field_mask]
# [START job_delete_job]
# [START delete_job]
def delete_job(client_service, job_name):
try:
client_service.projects().jobs().delete(name=job_name).execute()
print("Job deleted")
except Error as e:
print("Got exception while deleting job")
raise e
# [END delete_job]
# [END job_delete_job]
def run_sample():
import base_company_sample
# Create a company before creating jobs
company_to_be_created = base_company_sample.generate_company()
company_created = base_company_sample.create_company(
client_service, company_to_be_created
)
company_name = company_created.get("name")
# Construct a job
job_to_be_created = generate_job_with_required_fields(company_name)
# Create a job
job_created = create_job(client_service, job_to_be_created)
# Get a job
job_name = job_created.get("name")
get_job(client_service, job_name)
# Update a job
job_to_be_updated = job_created
job_to_be_updated.update({"description": "changedDescription"})
update_job(client_service, job_name, job_to_be_updated)
# Update a job with field mask
update_job_with_field_mask(
client_service, job_name, {"title": "changedJobTitle"}, "title"
)
# Delete a job
delete_job(client_service, job_name)
# Delete company only after cleaning all jobs under this company
base_company_sample.delete_company(client_service, company_name)
if __name__ == "__main__":
run_sample()