@google-cloud/nodejs-common 1.1.0 → 1.2.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.
@@ -30,11 +30,36 @@ const API_VERSION = 'v1';
30
30
  * to get/pause/resume a job.
31
31
  */
32
32
  class CloudScheduler {
33
- constructor(projectId = process.env['GCP_PROJECT']) {
34
- /** @const {!AuthClient} */
35
- const authClient = new AuthClient(API_SCOPES);
36
- this.auth = authClient.getApplicationDefaultCredentials();
37
- this.projectId = projectId;
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 jobs = (await this.listJobs_(targetLocations)).filter(
117
- (job) => regex.test(job));
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
- try {
139
- const response = await this.instance.projects.locations.jobs.list(
140
- request);
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 {{keyFilename:(string|undefined)}} options
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
  }