@google-cloud/nodejs-common 1.1.1-beta → 1.2.0

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.
@@ -0,0 +1,209 @@
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
+ # Const the folder name of Apps Script.
18
+ DEFAULT_APPS_SCRIPT_FOLDER="apps_script"
19
+
20
+ #######################################
21
+ # Clasp login.
22
+ # Globals:
23
+ # None
24
+ # Arguments:
25
+ # None
26
+ #######################################
27
+ clasp_login() {
28
+ while :; do
29
+ local claspLogin=$(clasp login --status)
30
+ if [[ "${claspLogin}" != "You are not logged in." ]]; then
31
+ printf '%s' "${claspLogin} Would you like to continue with it? [Y/n]"
32
+ local logout
33
+ read -r logout
34
+ logout=${logout:-"Y"}
35
+ if [[ ${logout} == "Y" || ${logout} == "y" ]]; then
36
+ break
37
+ else
38
+ clasp logout
39
+ fi
40
+ fi
41
+ clasp login --no-localhost
42
+ done
43
+ }
44
+
45
+ #######################################
46
+ # Initialize a AppsScript project. Usually it involves following steps:
47
+ # 1. Create a AppsScript project within a new Google Sheet.
48
+ # 2. Prompt to update the Google Cloud Project number of the AppsScript project
49
+ # to enable external APIs for this AppsScript project.
50
+ # 3. Prompt to grant the access of Cloud Functions' default service account to
51
+ # this Google Sheet, so the Cloud Functions can query this Sheet later.
52
+ # 4. Initialize the Sheet based on requests.
53
+ # Globals:
54
+ # None
55
+ # Arguments:
56
+ # The Google Sheet name.
57
+ # The folder for Apps Script code, default value ${DEFAULT_APPS_SCRIPT_FOLDER}
58
+ #######################################
59
+ clasp_initialize() {
60
+ ((STEP += 1))
61
+ printf '%s\n' "Step ${STEP}: Starting to create Google Sheets..."
62
+ local sheetName="${1}"
63
+ local apps_script_src="${2-"${DEFAULT_APPS_SCRIPT_FOLDER}"}"
64
+ clasp_login
65
+ while :; do
66
+ local claspStatus=$(
67
+ clasp status -P "${apps_script_src}" >/dev/null 2>&1
68
+ echo $?
69
+ )
70
+ if [[ $claspStatus -gt 0 ]]; then
71
+ clasp create --type sheets --title "${sheetName}" --rootDir "${apps_script_src}"
72
+ local createResult=$?
73
+ if [[ $createResult -gt 0 ]]; then
74
+ printf '%s' "Press any key to continue after you enable the Google \
75
+ Apps Script API: https://script.google.com/home/usersettings..."
76
+ local any
77
+ read -n1 -s any
78
+ printf '\n\n'
79
+ continue
80
+ fi
81
+ break
82
+ else
83
+ printf '%s' "AppsScript project exists. Would you like to continue with \
84
+ it? [Y/n]"
85
+ local useCurrent
86
+ read -r useCurrent
87
+ useCurrent=${useCurrent:-"Y"}
88
+ if [[ ${useCurrent} = "Y" || ${useCurrent} = "y" ]]; then
89
+ break
90
+ else
91
+ printf '%s' "Would you like to delete current AppsScript and create a \
92
+ new one? [N/y]"
93
+ local deleteCurrent
94
+ read -r deleteCurrent
95
+ deleteCurrent=${deleteCurrent:-"N"}
96
+ if [[ ${deleteCurrent} = "Y" || ${deleteCurrent} = "y" ]]; then
97
+ rm "${apps_script_src}/.clasp.json"
98
+ continue
99
+ fi
100
+ fi
101
+ fi
102
+ done
103
+ }
104
+
105
+ #######################################
106
+ # Copy GCP project configuration file to AppsScript codes as a constant named
107
+ # `GCP_CONFIG`.
108
+ # Globals:
109
+ # None
110
+ # Arguments:
111
+ # The folder for Apps Script code, default value ${DEFAULT_APPS_SCRIPT_FOLDER}
112
+ #######################################
113
+ generate_config_js_for_apps_script() {
114
+ local apps_script_src="${1-"${DEFAULT_APPS_SCRIPT_FOLDER}"}"
115
+ local generated_file="${apps_script_src}/.generated_config.js"
116
+ if [[ -f "${CONFIG_FILE}" ]]; then
117
+ echo '// Copyright 2022 Google Inc.
118
+ //
119
+ // Licensed under the Apache License, Version 2.0 (the "License");
120
+ // you may not use this file except in compliance with the License.
121
+ // You may obtain a copy of the License at
122
+ //
123
+ // http://www.apache.org/licenses/LICENSE-2.0
124
+ //
125
+ // Unless required by applicable law or agreed to in writing, software
126
+ // distributed under the License is distributed on an "AS IS" BASIS,
127
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128
+ // See the License for the specific language governing permissions and
129
+ // limitations under the License.
130
+
131
+ /** @fileoverview Auto-generated configuration file for Apps Script. */
132
+ '>"${generated_file}"
133
+ echo -n "const GCP_CONFIG = " >>"${generated_file}"
134
+ cat "${CONFIG_FILE}" >>"${generated_file}"
135
+ else
136
+ printf '%s\n' "Couldn't find ${CONFIG_FILE}."
137
+ fi
138
+ }
139
+
140
+ #######################################
141
+ # Clasp pushes AppsScript codes.
142
+ # Globals:
143
+ # None
144
+ # Arguments:
145
+ # The folder for Apps Script code, default value ${DEFAULT_APPS_SCRIPT_FOLDER}
146
+ #######################################
147
+ clasp_push_codes() {
148
+ ((STEP += 1))
149
+ printf '%s\n' "Step ${STEP}: Starting to push codes to the Google Sheets..."
150
+ local apps_script_src="${1-"${DEFAULT_APPS_SCRIPT_FOLDER}"}"
151
+ clasp status -P "${apps_script_src}" >>/dev/null
152
+ local project_status=$?
153
+ if [[ ${project_status} -gt 0 ]]; then
154
+ return ${project_status}
155
+ else
156
+ generate_config_js_for_apps_script "${apps_script_src}"
157
+ clasp push --force -P "${apps_script_src}"
158
+ fi
159
+ }
160
+
161
+ #######################################
162
+ # Ask user to update the GCP number of this AppsScript.
163
+ # Globals:
164
+ # GCP_PROJECT
165
+ # Arguments:
166
+ # The folder for Apps Script code, default value ${DEFAULT_APPS_SCRIPT_FOLDER}
167
+ #######################################
168
+ clasp_update_project_number() {
169
+ ((STEP += 1))
170
+ local projectNumber=$(get_project_number)
171
+ local apps_script_src="${1-"${DEFAULT_APPS_SCRIPT_FOLDER}"}"
172
+ printf '%s\n' "Step ${STEP}: Update Google Cloud Platform (GCP) Project for \
173
+ Apps Script."
174
+ printf '%s' " "
175
+ clasp open -P "${apps_script_src}"
176
+ printf '%s\n' " On the open tab of Apps Script, use 'Project \
177
+ Settings' to set the Google Cloud Platform (GCP) Project as: ${projectNumber}"
178
+ printf '%s' "Press any key to continue after you update the GCP number..."
179
+ local any
180
+ read -n1 -s any
181
+ printf '\n'
182
+ }
183
+
184
+ #######################################
185
+ # Ask user to grant the access to CF's default service account.
186
+ # Note: the target GCP needs to have OAuth consent screen.
187
+ # Globals:
188
+ # SHEET_URL
189
+ # Arguments:
190
+ # The folder for Apps Script code, default value ${DEFAULT_APPS_SCRIPT_FOLDER}
191
+ #######################################
192
+ grant_access_to_service_account() {
193
+ ((STEP += 1))
194
+ local apps_script_src="${1-"${DEFAULT_APPS_SCRIPT_FOLDER}"}"
195
+ local defaultServiceAccount=$(get_cloud_functions_service_account \
196
+ "${PROJECT_NAMESPACE}_main")
197
+ local parentId=$(get_value_from_json_file "${apps_script_src}"/.clasp.json \
198
+ parentId|cut -d\" -f2)
199
+ printf '%s\n' "Step ${STEP}: Share the Google Sheet with ${SOLUTION_NAME}."
200
+
201
+ printf '%s\n' " Open Google Sheet: \
202
+ https://drive.google.com/open?id=${parentId}"
203
+ printf '%s\n' " Click 'Share' and grant the Viewer access to: \
204
+ ${defaultServiceAccount}"
205
+ printf '%s' "Press any key to continue after you grant the access..."
206
+ local any
207
+ read -n1 -s any
208
+ printf '\n'
209
+ }
@@ -1043,13 +1043,24 @@ refresh the list:"
1043
1043
  # Globals:
1044
1044
  # GCP_PROJECT
1045
1045
  # Arguments:
1046
- # Bucket name var, default value 'GCS_BUCKET'
1046
+ # Bucket name var with optional usage, e.g. 'GCS_BUCKET_REPORT:reports'.
1047
+ # The default value is 'GCS_BUCKET'.
1047
1048
  # Location var name, default value 'REGION'
1048
1049
  # If the second location var is unset, use this var as default value
1049
1050
  #######################################
1050
1051
  confirm_located_bucket() {
1051
- local gcsName defaultValue defaultBucketName locationName location
1052
- gcsName="${1:-"GCS_BUCKET"}"
1052
+ local gcsName usage defaultValue defaultBucketName locationName location
1053
+ if [[ -z "${1}" ]]; then
1054
+ gcsName="GCS_BUCKET"
1055
+ elif [[ "${1}" == *":"* ]]; then
1056
+ gcsName=$(echo "${1}" | cut -d\: -f1)
1057
+ usage=$(echo "${1}" | cut -d\: -f2)
1058
+ if [[ -n "${usage}" ]]; then
1059
+ usage=" for ${usage}"
1060
+ fi
1061
+ else
1062
+ gcsName="${1}"
1063
+ fi
1053
1064
  locationName="${2:-"REGION"}"
1054
1065
  defaultValue="${!gcsName}"
1055
1066
  defaultBucketName=$(get_default_bucket_name "${GCP_PROJECT}")
@@ -1064,7 +1075,8 @@ confirm_located_bucket() {
1064
1075
 
1065
1076
  (( STEP += 1 ))
1066
1077
  if [[ -z "${location}" ]]; then
1067
- printf '%s\n' "Step ${STEP}: Checking or creating a Cloud Storage Bucket..."
1078
+ printf '%s\n' "Step ${STEP}: Checking or creating a Cloud Storage \
1079
+ Bucket${usage}..."
1068
1080
  if [[ "${locationName}" == "REGION" ]]; then
1069
1081
  select_functions_location ${locationName}
1070
1082
  else
@@ -1072,8 +1084,8 @@ confirm_located_bucket() {
1072
1084
  fi
1073
1085
  location="${!locationName}"
1074
1086
  else
1075
- printf '%s\n' "Step ${STEP}: Checking or creating a Cloud Storage Bucket in \
1076
- location [${location}] ..."
1087
+ printf '%s\n' "Step ${STEP}: Checking or creating a Cloud Storage \
1088
+ Bucket${usage} in location [${location}] ..."
1077
1089
  fi
1078
1090
  declare -g "${locationName}=${location}"
1079
1091
  while :; do
@@ -1474,20 +1486,18 @@ EOF
1474
1486
  "client_id=${client_id}" "scope=${scope}")
1475
1487
  local auth_url
1476
1488
  auth_url="${OAUTH_BASE_URL}auth?${parameters}"
1477
- printf '%s\n' "3. Open the link in browser and finish authentication: \
1478
- ${auth_url}"
1489
+ printf '%s\n' "3. Open the link in your browser and finish authentication. \
1490
+ Do not close the redirected page: ${auth_url}"
1479
1491
  cat <<EOF
1480
1492
  Note:
1493
+ The succeeded OAuth flow will land the browser on an error page - \
1494
+ "This site can't be reached". This is expected behavior. Copy the whole URL and continue.
1481
1495
  If the OAuth client is not for a native application, there will be an \
1482
1496
  "Error 400: redirect_uri_mismatch" shown up on the page. In this case, press \
1483
1497
  "Enter" to start again with a native application OAuth client ID.
1484
- If there is no local web server serving at ${REDIRECT_URI}, the \
1485
- succeeded OAuth flow will land the browser on an error page ("This site can't \
1486
- be reached"). This is an expected behavior. Copy the whole URL and continue.
1487
1498
 
1488
1499
  EOF
1489
- printf '%s' "4. Copy the authorization code or complete url from browser \
1490
- and paste here: "
1500
+ printf '%s' "4. Copy the complete URL from your browser and paste here: "
1491
1501
  read -r auth_code
1492
1502
  if [[ -z ${auth_code} ]]; then
1493
1503
  printf '%s\n\n' "No authorization code. Starting from beginning again..."
@@ -1679,7 +1689,9 @@ customized_install() {
1679
1689
  local tasks=("$@")
1680
1690
  local task
1681
1691
  for task in "${tasks[@]}"; do
1682
- ${task}
1692
+ local cmd
1693
+ eval "cmd=(${task})"
1694
+ "${cmd[@]}"
1683
1695
  quit_if_failed $?
1684
1696
  done
1685
1697
  }
@@ -1992,5 +2004,6 @@ join_string_array() {
1992
2004
  _SELF="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1993
2005
  source "${_SELF}/google_ads.sh"
1994
2006
  source "${_SELF}/bigquery.sh"
2007
+ source "${_SELF}/apps_scripts.sh"
1995
2008
 
1996
2009
  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.1-beta",
3
+ "version": "1.2.0",
4
4
  "description": "A NodeJs common library for solutions based on Cloud Functions",
5
5
  "author": "Google Inc.",
6
6
  "license": "Apache-2.0",
@@ -18,24 +18,23 @@
18
18
  "dependencies": {
19
19
  "@google-cloud/aiplatform": "^1.19.0",
20
20
  "@google-cloud/automl": "^2.5.2",
21
- "@google-cloud/bigquery": "^5.12.0",
21
+ "@google-cloud/bigquery": "^6.0.0",
22
22
  "@google-cloud/datastore": "^6.6.2",
23
23
  "@google-cloud/firestore": "^5.0.2",
24
- "@google-cloud/logging-winston": "^4.2.2",
25
- "@google-cloud/pubsub": "^2.19.0",
26
- "@google-cloud/storage": "^5.18.3",
27
- "@google-cloud/scheduler": "^2.3.0",
28
- "gaxios": "^4.3.2",
24
+ "@google-cloud/logging-winston": "^5.1.0",
25
+ "@google-cloud/pubsub": "^3.0.1",
26
+ "@google-cloud/storage": "^6.0.1",
27
+ "@google-cloud/scheduler": "^3.0.0",
28
+ "gaxios": "^5.0.0",
29
29
  "google-ads-api": "^10.0.1",
30
30
  "google-ads-node":"^8.0.1",
31
- "google-auth-library": "^7.14.1",
31
+ "google-auth-library": "^8.0.2",
32
32
  "googleapis": "^100.0.0",
33
- "soap": "^0.43.0",
34
33
  "winston": "^3.7.2",
35
34
  "lodash": "^4.17.21"
36
35
  },
37
36
  "devDependencies": {
38
- "jasmine": "^4.0.2"
37
+ "jasmine": "^4.1.0"
39
38
  },
40
39
  "scripts": {
41
40
  "test": "node node_modules/jasmine/bin/jasmine"