@google-cloud/nodejs-common 1.1.0 → 1.1.1-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/bigquery.sh +53 -0
- package/bin/google_ads.sh +115 -0
- package/bin/install_functions.sh +97 -24
- package/package.json +2 -1
- package/src/components/automl.js +36 -56
- package/src/components/scheduler.js +36 -24
- package/src/components/vertex_ai.js +4 -3
package/bin/bigquery.sh
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2022 Google Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
#######################################
|
|
18
|
+
# Checks whether the BigQuery object (table or view) exists.
|
|
19
|
+
# Globals:
|
|
20
|
+
# GCP_PROJECT
|
|
21
|
+
# DATASET
|
|
22
|
+
# BIGQUERY_LOG_TABLE
|
|
23
|
+
# Arguments:
|
|
24
|
+
# None
|
|
25
|
+
#######################################
|
|
26
|
+
check_existence_in_bigquery() {
|
|
27
|
+
bq show "${1}" >/dev/null 2>&1
|
|
28
|
+
printf '%d' $?
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#######################################
|
|
32
|
+
# Creates or updates the BigQuery view.
|
|
33
|
+
# Globals:
|
|
34
|
+
# GCP_PROJECT
|
|
35
|
+
# DATASET
|
|
36
|
+
# Arguments:
|
|
37
|
+
# The name of view.
|
|
38
|
+
# The query of view.
|
|
39
|
+
#######################################
|
|
40
|
+
create_or_update_view() {
|
|
41
|
+
local viewName viewQuery
|
|
42
|
+
viewName="${1}"
|
|
43
|
+
viewQuery=${2}
|
|
44
|
+
local action="mk"
|
|
45
|
+
if [[ $(check_existence_in_bigquery "${DATASET}.${viewName}") -eq 0 ]]; then
|
|
46
|
+
action="update"
|
|
47
|
+
fi
|
|
48
|
+
bq "${action}" \
|
|
49
|
+
--use_legacy_sql=false \
|
|
50
|
+
--view "${viewQuery}" \
|
|
51
|
+
--project_id ${GCP_PROJECT} \
|
|
52
|
+
"${DATASET}.${viewName}"
|
|
53
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2022 Google Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
# Google Ads API version
|
|
18
|
+
GOOGLE_ADS_API_VERSION=10
|
|
19
|
+
|
|
20
|
+
#######################################
|
|
21
|
+
# Verify whether the current OAuth token, CID and developer token can work.
|
|
22
|
+
# Globals:
|
|
23
|
+
# None
|
|
24
|
+
# Arguments:
|
|
25
|
+
# MCC CID
|
|
26
|
+
# Developer token
|
|
27
|
+
#######################################
|
|
28
|
+
validate_googleads_account() {
|
|
29
|
+
local cid developerToken accessToken request response
|
|
30
|
+
cid=${1}
|
|
31
|
+
developerToken=${2}
|
|
32
|
+
accessToken=$(get_oauth_access_token)
|
|
33
|
+
request=(
|
|
34
|
+
-H "Accept: application/json"
|
|
35
|
+
-H "Content-Type: application/json"
|
|
36
|
+
-H "developer-token: ${developerToken}"
|
|
37
|
+
-H "Authorization: Bearer ${accessToken}"
|
|
38
|
+
-X POST "https://googleads.googleapis.com/v${GOOGLE_ADS_API_VERSION}/customers/${cid}/googleAds:search"
|
|
39
|
+
-d '{"query": "SELECT customer.id FROM customer"}'
|
|
40
|
+
)
|
|
41
|
+
response=$(curl "${request[@]}" 2>/dev/null)
|
|
42
|
+
local errorCode errorMessage
|
|
43
|
+
errorCode=$(get_value_from_json_string "${response}" "error.code")
|
|
44
|
+
if [[ -n "${errorCode}" ]]; then
|
|
45
|
+
errorMessage=$(get_value_from_json_string "${response}" "error.message")
|
|
46
|
+
printf '%s\n' "Validate failed: ${errorMessage}" >&2
|
|
47
|
+
return 1
|
|
48
|
+
fi
|
|
49
|
+
return 0
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#######################################
|
|
53
|
+
# Let user input MCC CID and developer token for cronjob(s).
|
|
54
|
+
# Globals:
|
|
55
|
+
# MCC_CIDS
|
|
56
|
+
# DEVELOPER_TOKEN
|
|
57
|
+
# Arguments:
|
|
58
|
+
# None
|
|
59
|
+
#######################################
|
|
60
|
+
set_google_ads_account() {
|
|
61
|
+
printf '%s\n' "Setting up Google Ads account information..."
|
|
62
|
+
local developToken mccCids
|
|
63
|
+
while :; do
|
|
64
|
+
# Developer token
|
|
65
|
+
while [[ -z ${developToken} ]]; do
|
|
66
|
+
printf '%s' " Enter the developer token[${DEVELOPER_TOKEN}]: "
|
|
67
|
+
read -r input
|
|
68
|
+
developToken="${input:-${DEVELOPER_TOKEN}}"
|
|
69
|
+
done
|
|
70
|
+
DEVELOPER_TOKEN="${developToken}"
|
|
71
|
+
mccCids=""
|
|
72
|
+
# MCC CIDs
|
|
73
|
+
while :; do
|
|
74
|
+
printf '%s' " Enter the MCC CID: "
|
|
75
|
+
read -r input
|
|
76
|
+
if [[ -z ${input} ]]; then
|
|
77
|
+
continue
|
|
78
|
+
fi
|
|
79
|
+
input="$(printf '%s' "${input}" | sed -r 's/-//g')"
|
|
80
|
+
printf '%s' " validating ${input}...... "
|
|
81
|
+
validate_googleads_account ${input} ${DEVELOPER_TOKEN}
|
|
82
|
+
if [[ $? -eq 1 ]]; then
|
|
83
|
+
printf '%s\n' "failed.
|
|
84
|
+
Press 'd' to re-enter developer token ["${developToken}"] or
|
|
85
|
+
'C' to continue with this MCC CID or
|
|
86
|
+
any other key to enter another MCC CID..."
|
|
87
|
+
local any
|
|
88
|
+
read -n1 -s any
|
|
89
|
+
if [[ "${any}" == "d" ]]; then
|
|
90
|
+
developToken=""
|
|
91
|
+
continue 2
|
|
92
|
+
elif [[ "${any}" == "C" ]]; then
|
|
93
|
+
printf '%s\n' "WARNING! Continue with FAILED MCC ${input}."
|
|
94
|
+
else
|
|
95
|
+
continue
|
|
96
|
+
fi
|
|
97
|
+
else
|
|
98
|
+
printf '%s\n' "succeeded."
|
|
99
|
+
fi
|
|
100
|
+
mccCids+=",${input}"
|
|
101
|
+
printf '%s' " Do you want to add another MCC CID? [Y/n]: "
|
|
102
|
+
read -r input
|
|
103
|
+
if [[ ${input} == 'n' || ${input} == 'N' ]]; then
|
|
104
|
+
break
|
|
105
|
+
fi
|
|
106
|
+
done
|
|
107
|
+
# Left Shift one position to remove the first comma.
|
|
108
|
+
# After shifting, MCC_CIDS would like "11111,22222".
|
|
109
|
+
MCC_CIDS="${mccCids:1}"
|
|
110
|
+
printf '%s\n' "Using Google Ads MCC CIDs: ${MCC_CIDS}."
|
|
111
|
+
break
|
|
112
|
+
done
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
printf '%s\n' "Google Ads Bash Library is loaded."
|
package/bin/install_functions.sh
CHANGED
|
@@ -573,7 +573,7 @@ API(s):"
|
|
|
573
573
|
printf '%s\n' "OK. OAuth is required by selected API(s)."
|
|
574
574
|
return 0
|
|
575
575
|
fi
|
|
576
|
-
printf '%s\n' "Selected API(s) require authentication.
|
|
576
|
+
printf '%s\n' "Selected API(s) require authentication. Choose the \
|
|
577
577
|
authentication method:"
|
|
578
578
|
local auths=("Service Account (recommended)" "OAuth")
|
|
579
579
|
select auth in "${auths[@]}"; do
|
|
@@ -604,28 +604,40 @@ check_permissions() {
|
|
|
604
604
|
(( STEP += 1 ))
|
|
605
605
|
printf '%s\n' "Step ${STEP}: Checking current user's access..."
|
|
606
606
|
local role error
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
607
|
+
while :; do
|
|
608
|
+
error=0
|
|
609
|
+
for role in "${!GOOGLE_CLOUD_PERMISSIONS[@]}"; do
|
|
610
|
+
printf '%s' " Checking permissions for ${role}... "
|
|
611
|
+
local permissions
|
|
612
|
+
permissions=(${GOOGLE_CLOUD_PERMISSIONS[${role}]})
|
|
613
|
+
local missed
|
|
614
|
+
missed=$(get_number_of_missed_permissions "${permissions[@]}")
|
|
615
|
+
if [[ $missed -gt 0 ]]; then
|
|
616
|
+
message="missed ${missed}, failed"
|
|
617
|
+
error=1
|
|
618
|
+
else
|
|
619
|
+
message='successfully'
|
|
620
|
+
fi
|
|
621
|
+
printf '%s\n' " ${message}."
|
|
622
|
+
done
|
|
623
|
+
if [[ ${error} -gt 0 ]]; then
|
|
624
|
+
printf '%s\n' "Permissions check failed."
|
|
625
|
+
printf '%s\n' "Would you like to login with another account? [Y/n]: "
|
|
626
|
+
local reLogin
|
|
627
|
+
read -r reLogin
|
|
628
|
+
reLogin=${reLogin:-"Y"}
|
|
629
|
+
if [[ ${reLogin} == "Y" || ${reLogin} == "y" ]]; then
|
|
630
|
+
gcloud auth login
|
|
631
|
+
continue
|
|
632
|
+
else
|
|
633
|
+
return 1
|
|
634
|
+
fi
|
|
616
635
|
else
|
|
617
|
-
|
|
636
|
+
echo "OK. Permissions check passed for Google Cloud project \
|
|
637
|
+
[${GCP_PROJECT}]."
|
|
638
|
+
return 0
|
|
618
639
|
fi
|
|
619
|
-
printf '%s\n' " ${message}."
|
|
620
640
|
done
|
|
621
|
-
if [[ ${error} -gt 0 ]]; then
|
|
622
|
-
printf '%s\n' "Permissions check failed."
|
|
623
|
-
return 1
|
|
624
|
-
else
|
|
625
|
-
echo "OK. Permissions check passed for Google Cloud project \
|
|
626
|
-
[${GCP_PROJECT}]."
|
|
627
|
-
return 0
|
|
628
|
-
fi
|
|
629
641
|
}
|
|
630
642
|
|
|
631
643
|
#######################################
|
|
@@ -1129,6 +1141,7 @@ Project. Continuing to enter another bucket..."
|
|
|
1129
1141
|
fi
|
|
1130
1142
|
done
|
|
1131
1143
|
declare -g "${gcsName}=${bucket}"
|
|
1144
|
+
confirm_bucket_lifecycle "${bucket}"
|
|
1132
1145
|
}
|
|
1133
1146
|
|
|
1134
1147
|
#######################################
|
|
@@ -1149,6 +1162,58 @@ confirm_bucket_with_location() {
|
|
|
1149
1162
|
confirm_located_bucket ${gcsName} ${locationName}
|
|
1150
1163
|
}
|
|
1151
1164
|
|
|
1165
|
+
#######################################
|
|
1166
|
+
# Manage the lifecycle of a GCS bucket: setting or removing the GCS lifecycle
|
|
1167
|
+
# rule of 'age'.
|
|
1168
|
+
# See: https://cloud.google.com/storage/docs/lifecycle#age
|
|
1169
|
+
# Arguments:
|
|
1170
|
+
# Bucket name
|
|
1171
|
+
#######################################
|
|
1172
|
+
confirm_bucket_lifecycle() {
|
|
1173
|
+
local bucket bucketMetadata lifecycle
|
|
1174
|
+
bucket="${1}"
|
|
1175
|
+
bucketMetadata="$(get_bucket_metadata "${bucket}")"
|
|
1176
|
+
lifecycle="$(get_value_from_json_string "${bucketMetadata}" "lifecycle")"
|
|
1177
|
+
if [[ -n "${lifecycle}" ]]; then
|
|
1178
|
+
printf '%s\n' "There are lifecycle rules in this bucket: ${lifecycle}."
|
|
1179
|
+
printf '%s' "Would you like to overwrite it? [N/y]:"
|
|
1180
|
+
local confirmContinue
|
|
1181
|
+
read -r confirmContinue
|
|
1182
|
+
confirmContinue=${confirmContinue:-"N"}
|
|
1183
|
+
if [[ ${confirmContinue} == "N" || ${confirmContinue} == "n" ]]; then
|
|
1184
|
+
return 0
|
|
1185
|
+
fi
|
|
1186
|
+
else
|
|
1187
|
+
printf '%s\n' "There is no lifecycle rules in this bucket."
|
|
1188
|
+
printf '%s' "Would you like to create it? [Y/n]:"
|
|
1189
|
+
local confirmContinue
|
|
1190
|
+
read -r confirmContinue
|
|
1191
|
+
confirmContinue=${confirmContinue:-"Y"}
|
|
1192
|
+
if [[ ${confirmContinue} == "N" || ${confirmContinue} == "n" ]]; then
|
|
1193
|
+
return 0
|
|
1194
|
+
fi
|
|
1195
|
+
fi
|
|
1196
|
+
while :; do
|
|
1197
|
+
printf '%s' "Enter the number of days that a file will be kept before it \
|
|
1198
|
+
is automatically removed in the bucket[${bucket}]. (enter 0 to remove all \
|
|
1199
|
+
existing lifecycle rules): "
|
|
1200
|
+
local days
|
|
1201
|
+
read -r days
|
|
1202
|
+
days=${days}
|
|
1203
|
+
if [[ "${days}" =~ ^[0-9]+$ ]]; then
|
|
1204
|
+
local lifecycle
|
|
1205
|
+
if [[ "${days}" == "0" ]]; then
|
|
1206
|
+
lifecycle="{}"
|
|
1207
|
+
else
|
|
1208
|
+
lifecycle='{"rule":[{"action":{"type":"Delete"},"condition":{"age":'\
|
|
1209
|
+
"${days}}}]}"
|
|
1210
|
+
fi
|
|
1211
|
+
gsutil lifecycle set /dev/stdin gs://${bucket} <<< ${lifecycle}
|
|
1212
|
+
return $?
|
|
1213
|
+
fi
|
|
1214
|
+
done
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1152
1217
|
#######################################
|
|
1153
1218
|
# Confirm the monitored folder.
|
|
1154
1219
|
# Globals:
|
|
@@ -1507,6 +1572,7 @@ set_authentication_env_for_cloud_functions() {
|
|
|
1507
1572
|
create_or_update_cloud_scheduler_for_pubsub(){
|
|
1508
1573
|
check_authentication
|
|
1509
1574
|
quit_if_failed $?
|
|
1575
|
+
local location_flag="--location=${REGION}"
|
|
1510
1576
|
local scheduler_flag=()
|
|
1511
1577
|
scheduler_flag+=(--schedule="$2")
|
|
1512
1578
|
scheduler_flag+=(--time-zone="$3")
|
|
@@ -1514,13 +1580,13 @@ create_or_update_cloud_scheduler_for_pubsub(){
|
|
|
1514
1580
|
scheduler_flag+=(--message-body="$5")
|
|
1515
1581
|
local exist_job
|
|
1516
1582
|
exist_job=($(gcloud scheduler jobs list --filter="name~${1}" \
|
|
1517
|
-
--format="value(state)"))
|
|
1583
|
+
--format="value(state)" "${location_flag}"))
|
|
1518
1584
|
local action needPause
|
|
1519
1585
|
if [[ ${#exist_job[@]} -gt 0 ]]; then
|
|
1520
1586
|
action="update"
|
|
1521
1587
|
scheduler_flag+=(--update-attributes=$6)
|
|
1522
1588
|
if [[ "${exist_job[0]}" == "PAUSED" ]]; then
|
|
1523
|
-
gcloud scheduler jobs resume "${1}"
|
|
1589
|
+
gcloud scheduler jobs resume "${1}" "${location_flag}"
|
|
1524
1590
|
if [[ $? -gt 0 ]]; then
|
|
1525
1591
|
printf '%s\n' "Failed to resume paused Cloud Scheduler job [${1}]."
|
|
1526
1592
|
return 1
|
|
@@ -1531,9 +1597,9 @@ create_or_update_cloud_scheduler_for_pubsub(){
|
|
|
1531
1597
|
action="create"
|
|
1532
1598
|
scheduler_flag+=(--attributes=$6)
|
|
1533
1599
|
fi
|
|
1534
|
-
gcloud scheduler jobs ${action} pubsub "$1" "${scheduler_flag[@]}"
|
|
1600
|
+
gcloud scheduler jobs ${action} pubsub "$1" "${scheduler_flag[@]}" "${location_flag}"
|
|
1535
1601
|
if [[ "${needPause}" == "true" ]]; then
|
|
1536
|
-
gcloud scheduler jobs pause "${1}"
|
|
1602
|
+
gcloud scheduler jobs pause "${1}" "${location_flag}"
|
|
1537
1603
|
fi
|
|
1538
1604
|
}
|
|
1539
1605
|
|
|
@@ -1921,3 +1987,10 @@ join_string_array() {
|
|
|
1921
1987
|
shift
|
|
1922
1988
|
printf %s "$first" "${@/#/$separator}"
|
|
1923
1989
|
}
|
|
1990
|
+
|
|
1991
|
+
# Import other bash files.
|
|
1992
|
+
_SELF="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
1993
|
+
source "${_SELF}/google_ads.sh"
|
|
1994
|
+
source "${_SELF}/bigquery.sh"
|
|
1995
|
+
|
|
1996
|
+
printf '%s\n' "Common Bash Library is loaded."
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@google-cloud/nodejs-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1-beta",
|
|
4
4
|
"description": "A NodeJs common library for solutions based on Cloud Functions",
|
|
5
5
|
"author": "Google Inc.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"@google-cloud/logging-winston": "^4.2.2",
|
|
25
25
|
"@google-cloud/pubsub": "^2.19.0",
|
|
26
26
|
"@google-cloud/storage": "^5.18.3",
|
|
27
|
+
"@google-cloud/scheduler": "^2.3.0",
|
|
27
28
|
"gaxios": "^4.3.2",
|
|
28
29
|
"google-ads-api": "^10.0.1",
|
|
29
30
|
"google-ads-node":"^8.0.1",
|
package/src/components/automl.js
CHANGED
|
@@ -17,15 +17,9 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
'use strict';
|
|
20
|
-
const {
|
|
21
|
-
const {JWT, Compute} = require('google-auth-library');
|
|
20
|
+
const { ClientOptions } = require('google-gax');
|
|
22
21
|
const automl = require('@google-cloud/automl');
|
|
23
22
|
const google = automl.protos.google;
|
|
24
|
-
const AuthClient = require('../apis/auth_client.js');
|
|
25
|
-
|
|
26
|
-
const API_SCOPES = Object.freeze([
|
|
27
|
-
'https://www.googleapis.com/auth/cloud-platform',
|
|
28
|
-
]);
|
|
29
23
|
|
|
30
24
|
/**
|
|
31
25
|
* For version `v1beta1`, BigQuery can be the source and destination.
|
|
@@ -40,46 +34,49 @@ const API_SCOPES = Object.freeze([
|
|
|
40
34
|
* @type {string}
|
|
41
35
|
*/
|
|
42
36
|
const API_VERSION = 'v1beta1';
|
|
43
|
-
const PredictionServiceClient = automl[API_VERSION]
|
|
37
|
+
const { PredictionServiceClient } = automl[API_VERSION];
|
|
44
38
|
|
|
45
39
|
/**
|
|
46
|
-
* AutoML Tables API
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* https://
|
|
51
|
-
* 2. 'get Operations' based on REST API, see:
|
|
52
|
-
* https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.operations/get
|
|
53
|
-
* https://cloud.google.com/automl-tables/docs/long-operations#get-operation
|
|
40
|
+
* AutoML Tables API class for:
|
|
41
|
+
* 1. start a batch predict job:
|
|
42
|
+
* @see https://cloud.google.com/nodejs/docs/reference/automl/latest/automl/v1beta1.predictionserviceclient#_google_cloud_automl_v1beta1_PredictionServiceClient_batchPredict_member_3_
|
|
43
|
+
* 2. get the status of an operation:
|
|
44
|
+
* @see https://cloud.google.com/nodejs/docs/reference/automl/latest/automl/v1beta1.predictionserviceclient#_google_cloud_automl_v1beta1_PredictionServiceClient_checkBatchPredictProgress_member_1_
|
|
54
45
|
*/
|
|
55
46
|
class AutoMl {
|
|
56
|
-
|
|
57
47
|
/**
|
|
58
48
|
* Initialize an instance.
|
|
59
|
-
* @param {
|
|
49
|
+
* @param {ClientOptions=} options
|
|
60
50
|
*/
|
|
61
51
|
constructor(options = {}) {
|
|
62
|
-
this.
|
|
52
|
+
this.client = new PredictionServiceClient(options);
|
|
63
53
|
}
|
|
64
54
|
|
|
65
55
|
/**
|
|
66
56
|
* Batch predicts based on Google Cloud Client Library.
|
|
67
|
-
* See https://googleapis.dev/nodejs/automl/latest/index.html
|
|
68
57
|
* @param {string} projectId
|
|
69
58
|
* @param {string} computeRegion
|
|
70
59
|
* @param {string} modelId
|
|
71
|
-
* @param {google.cloud.automl.
|
|
72
|
-
* @param {google.cloud.automl.
|
|
60
|
+
* @param {google.cloud.automl.v1beta1.IBatchPredictInputConfig} inputConfig
|
|
61
|
+
* @param {google.cloud.automl.v1beta1.IBatchPredictOutputConfig} outputConfig
|
|
73
62
|
* @return {Promise<string>} Predict operation name.
|
|
74
63
|
*/
|
|
75
|
-
async batchPredict(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
64
|
+
async batchPredict(
|
|
65
|
+
projectId,
|
|
66
|
+
computeRegion,
|
|
67
|
+
modelId,
|
|
68
|
+
inputConfig,
|
|
69
|
+
outputConfig
|
|
70
|
+
) {
|
|
71
|
+
const modelFullId = this.client.modelPath(
|
|
72
|
+
projectId,
|
|
73
|
+
computeRegion,
|
|
74
|
+
modelId
|
|
75
|
+
);
|
|
76
|
+
const responses = await this.client.batchPredict({
|
|
80
77
|
name: modelFullId,
|
|
81
|
-
inputConfig
|
|
82
|
-
outputConfig
|
|
78
|
+
inputConfig,
|
|
79
|
+
outputConfig,
|
|
83
80
|
});
|
|
84
81
|
const operation = responses[1];
|
|
85
82
|
console.log(`Operation name: ${operation.name}`);
|
|
@@ -87,37 +84,20 @@ class AutoMl {
|
|
|
87
84
|
}
|
|
88
85
|
|
|
89
86
|
/**
|
|
90
|
-
* Gets
|
|
91
|
-
* https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.operations/get
|
|
92
|
-
* https://cloud.google.com/automl-tables/docs/long-operations#get-operation
|
|
87
|
+
* Gets status of an operation.
|
|
93
88
|
* @param {string} operationName
|
|
94
|
-
* @return {Promise<
|
|
89
|
+
* @return {Promise<{{
|
|
90
|
+
* done: boolean,
|
|
91
|
+
* error: Error|undefined,
|
|
92
|
+
* metadata: OperationMetadata,
|
|
93
|
+
* name: string,
|
|
94
|
+
* }}>}
|
|
95
95
|
*/
|
|
96
96
|
async getOperation(operationName) {
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
method: 'GET',
|
|
101
|
-
headers,
|
|
102
|
-
url,
|
|
103
|
-
};
|
|
104
|
-
const response = await request(requestOptions);
|
|
105
|
-
return response.data;
|
|
97
|
+
const response = await this.client.checkBatchPredictProgress(operationName);
|
|
98
|
+
const { done, error, metadata, name } = response;
|
|
99
|
+
return { done, error, metadata, name };
|
|
106
100
|
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Gets authentication client of AutoML API.
|
|
110
|
-
* This API belongs to GCP, however it is not fully supported by Google Cloud
|
|
111
|
-
* Client Library. So we need to manage some functions on its REST API,
|
|
112
|
-
* in the way like we invoke other external APIs.
|
|
113
|
-
* @returns {(!JWT|!Compute)}
|
|
114
|
-
* @private
|
|
115
|
-
*/
|
|
116
|
-
getAuthClient_() {
|
|
117
|
-
/** @const {!AuthClient} */ const authClient = new AuthClient(API_SCOPES);
|
|
118
|
-
return authClient.getApplicationDefaultCredentials();
|
|
119
|
-
}
|
|
120
|
-
|
|
121
101
|
}
|
|
122
102
|
|
|
123
103
|
exports.AutoMl = AutoMl;
|
|
@@ -30,11 +30,36 @@ const API_VERSION = 'v1';
|
|
|
30
30
|
* to get/pause/resume a job.
|
|
31
31
|
*/
|
|
32
32
|
class CloudScheduler {
|
|
33
|
-
constructor(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
constructor(env = process.env, options = {}) {
|
|
34
|
+
if (!options.authClient) {
|
|
35
|
+
/** @const {!AuthClient} */
|
|
36
|
+
const authClient = new AuthClient(API_SCOPES, env);
|
|
37
|
+
/**
|
|
38
|
+
* By default, `AuthClient` (getDefaultAuth()) will return an auth client
|
|
39
|
+
* based on the settings in ENV while the OAuth is the most preferred.
|
|
40
|
+
* This works for most of the external API clients (in the '../apis'
|
|
41
|
+
* folder), however this won't work in the Cloud Functions, as those OAuth
|
|
42
|
+
* token usually won't have enough permission to invoke Google Cloud API.
|
|
43
|
+
* Using the method `getApplicationDefaultCredentials` to force
|
|
44
|
+
* `AuthClient` return an ADC auth client, which will work in the Cloud.
|
|
45
|
+
*
|
|
46
|
+
* Cloud Scheduler API Client Library is used here as Cloud Client Library
|
|
47
|
+
* is still at beta stage. (For the difference, see
|
|
48
|
+
* https://cloud.google.com/apis/docs/client-libraries-explained)
|
|
49
|
+
* Eventually, when we migrate this to the cloud client library, which
|
|
50
|
+
* automatically takes ADC as the authentication method, the 'AuthClient'
|
|
51
|
+
* is not required here and can be removed.
|
|
52
|
+
*/
|
|
53
|
+
this.auth = authClient.getApplicationDefaultCredentials();
|
|
54
|
+
} else {
|
|
55
|
+
/**
|
|
56
|
+
* `authClient` can be consumed by cloud client library as the auth
|
|
57
|
+
* client. By passing this in, we can offer more flexible auth clients in
|
|
58
|
+
* test cases for API client library and cloud client library in future.
|
|
59
|
+
*/
|
|
60
|
+
this.auth = options.authClient;
|
|
61
|
+
}
|
|
62
|
+
this.projectId = env['GCP_PROJECT'];
|
|
38
63
|
this.instance = cloudscheduler({
|
|
39
64
|
version: API_VERSION,
|
|
40
65
|
auth: this.auth,
|
|
@@ -113,8 +138,8 @@ class CloudScheduler {
|
|
|
113
138
|
*/
|
|
114
139
|
async getJobs_(name, targetLocations = undefined) {
|
|
115
140
|
const regex = new RegExp(`/jobs/${name}$`);
|
|
116
|
-
const
|
|
117
|
-
|
|
141
|
+
const allJobs = await this.listJobs_(targetLocations);
|
|
142
|
+
const jobs = allJobs.filter((job) => regex.test(job));
|
|
118
143
|
if (jobs.length === 0) console.error(`Can not find job: ${name}`);
|
|
119
144
|
return jobs;
|
|
120
145
|
}
|
|
@@ -134,23 +159,10 @@ class CloudScheduler {
|
|
|
134
159
|
const projectId = await this.getProjectId_();
|
|
135
160
|
const requestPrefix = `projects/${projectId}/locations`;
|
|
136
161
|
const jobs = locations.map(async (location) => {
|
|
137
|
-
const request = {parent: `${requestPrefix}/${location}`};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return response.data.jobs.map((job) => job.name);
|
|
142
|
-
} catch (error) {
|
|
143
|
-
// Currently, listLocations always returns an array with one location.
|
|
144
|
-
// Not sure whether this will be changed or not in future. If one day
|
|
145
|
-
// the Cloud Scheduler let users to select a location for a job, then
|
|
146
|
-
// it may return multiple locations when listLocation, however the
|
|
147
|
-
// target job may exist in one of the locations. In this case, when we
|
|
148
|
-
// iterate the job with all possible locations to get the complete job
|
|
149
|
-
// path to operate, it will likely generate an error for those wrong
|
|
150
|
-
// location(s). So set the try-catch here to handle this situation.
|
|
151
|
-
console.error(error.message);
|
|
152
|
-
return [];
|
|
153
|
-
}
|
|
162
|
+
const request = { parent: `${requestPrefix}/${location}` };
|
|
163
|
+
const response = await this.instance.projects.locations.jobs.list(request);
|
|
164
|
+
if (!response.data.jobs) return [];
|
|
165
|
+
return response.data.jobs.map((job) => job.name);
|
|
154
166
|
});
|
|
155
167
|
// Waits for all jobs names and flattens nested job name arrays, however
|
|
156
168
|
// there is no 'flat' available in current Cloud Functions runtime.
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
|
+
const { ClientOptions } = require('google-gax');
|
|
21
22
|
const {JobServiceClient} = require('@google-cloud/aiplatform');
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -29,7 +30,7 @@ class VertexAi {
|
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* Initialize an instance.
|
|
32
|
-
* @param {
|
|
33
|
+
* @param {ClientOptions=} options
|
|
33
34
|
*/
|
|
34
35
|
constructor(options = {}) {
|
|
35
36
|
this.options = options;
|
|
@@ -105,9 +106,9 @@ class VertexAi {
|
|
|
105
106
|
* @private
|
|
106
107
|
*/
|
|
107
108
|
getJobServiceClient_(location) {
|
|
108
|
-
const clientOptions = {
|
|
109
|
+
const clientOptions = Object.assign({}, this.options, {
|
|
109
110
|
apiEndpoint: this.getServiceEndpoint_(location),
|
|
110
|
-
};
|
|
111
|
+
});
|
|
111
112
|
return new JobServiceClient(clientOptions);
|
|
112
113
|
}
|
|
113
114
|
}
|