Skip to content

Commit 3f05325

Browse files
ofrobotsstephenplusplus
authored andcommitted
feat: we can omit project_id from the resource (googleapis#2502)
1 parent 3e91324 commit 3f05325

2 files changed

Lines changed: 37 additions & 104 deletions

File tree

packages/logging/src/metadata.js

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ function Metadata(logging) {
4343
/**
4444
* Create a descriptor for Cloud Functions.
4545
*
46-
* @param {string} projectId - The project ID.
4746
* @returns {object}
4847
*/
49-
Metadata.getCloudFunctionDescriptor = function(projectId) {
48+
Metadata.getCloudFunctionDescriptor = function() {
5049
return {
5150
type: 'cloud_function',
5251
labels: {
53-
project_id: projectId,
5452
function_name: process.env.FUNCTION_NAME,
5553
region: process.env.SUPERVISOR_REGION
5654
}
@@ -60,14 +58,12 @@ Metadata.getCloudFunctionDescriptor = function(projectId) {
6058
/**
6159
* Create a descriptor for Google App Engine.
6260
*
63-
* @param {string} projectId - The project ID.
6461
* @returns {object}
6562
*/
66-
Metadata.getGAEDescriptor = function(projectId) {
63+
Metadata.getGAEDescriptor = function() {
6764
return {
6865
type: 'gae_app',
6966
labels: {
70-
project_id: projectId,
7167
module_id: process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME,
7268
version_id: process.env.GAE_VERSION
7369
}
@@ -79,15 +75,11 @@ Metadata.getGAEDescriptor = function(projectId) {
7975
*
8076
* @private
8177
*
82-
* @param {string} projectId - The project ID.
8378
* @return {object}
8479
*/
85-
Metadata.getGCEDescriptor = function(projectId) {
80+
Metadata.getGCEDescriptor = function() {
8681
return {
87-
type: 'gce_instance',
88-
labels: {
89-
project_id: projectId
90-
}
82+
type: 'gce_instance'
9183
};
9284
};
9385

@@ -96,11 +88,10 @@ Metadata.getGCEDescriptor = function(projectId) {
9688
*
9789
* @private
9890
*
99-
* @param {string} projectId - The project ID.
10091
* @param {function} callback - The callback function.
10192
* @return {object}
10293
*/
103-
Metadata.getGKEDescriptor = function(projectId, callback) {
94+
Metadata.getGKEDescriptor = function(callback) {
10495
gcpMetadata.instance('attributes/clusterName', function(err, _, clusterName) {
10596
if (err) {
10697
callback(err);
@@ -111,8 +102,7 @@ Metadata.getGKEDescriptor = function(projectId, callback) {
111102
type: 'container',
112103
labels: {
113104
// TODO(ofrobots): it would be good to include the namespace_id as well.
114-
cluster_name: clusterName,
115-
project_id: projectId
105+
cluster_name: clusterName
116106
}
117107
});
118108
});
@@ -123,15 +113,11 @@ Metadata.getGKEDescriptor = function(projectId, callback) {
123113
*
124114
* @private
125115
*
126-
* @param {string} projectId - The project ID.
127116
* @returns {object}
128117
*/
129-
Metadata.getGlobalDescriptor = function(projectId) {
118+
Metadata.getGlobalDescriptor = function() {
130119
return {
131-
type: 'global',
132-
labels: {
133-
project_id: projectId
134-
}
120+
type: 'global'
135121
};
136122
};
137123

@@ -141,34 +127,25 @@ Metadata.getGlobalDescriptor = function(projectId) {
141127
* @param {function} callback - The callback function.
142128
*/
143129
Metadata.prototype.getDefaultResource = function(callback) {
144-
var self = this;
145-
146-
this.logging.auth.getProjectId(function(err, projectId) {
147-
if (err) {
148-
callback(err);
130+
this.logging.auth.getEnvironment(function(err, env) {
131+
if (env.IS_CONTAINER_ENGINE) {
132+
Metadata.getGKEDescriptor(callback);
149133
return;
150134
}
151135

152-
self.logging.auth.getEnvironment(function(err, env) {
153-
if (env.IS_CONTAINER_ENGINE) {
154-
Metadata.getGKEDescriptor(projectId, callback);
155-
return;
156-
}
157-
158-
var defaultResource;
136+
var defaultResource;
159137

160-
if (env.IS_APP_ENGINE) {
161-
defaultResource = Metadata.getGAEDescriptor(projectId);
162-
} else if (env.IS_CLOUD_FUNCTION) {
163-
defaultResource = Metadata.getCloudFunctionDescriptor(projectId);
164-
} else if (env.IS_COMPUTE_ENGINE) {
165-
defaultResource = Metadata.getGCEDescriptor(projectId);
166-
} else {
167-
defaultResource = Metadata.getGlobalDescriptor(projectId);
168-
}
138+
if (env.IS_APP_ENGINE) {
139+
defaultResource = Metadata.getGAEDescriptor();
140+
} else if (env.IS_CLOUD_FUNCTION) {
141+
defaultResource = Metadata.getCloudFunctionDescriptor();
142+
} else if (env.IS_COMPUTE_ENGINE) {
143+
defaultResource = Metadata.getGCEDescriptor();
144+
} else {
145+
defaultResource = Metadata.getGlobalDescriptor();
146+
}
169147

170-
callback(null, defaultResource);
171-
});
148+
callback(null, defaultResource);
172149
});
173150
};
174151

packages/logging/test/metadata.js

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe('metadata', function() {
3535
var Metadata;
3636
var metadata;
3737

38-
var PROJECT_ID = 'project-id';
3938
var LOGGING;
4039

4140
var ENV_CACHED = extend({}, process.env);
@@ -77,10 +76,9 @@ describe('metadata', function() {
7776
});
7877

7978
it('should return the correct descriptor', function() {
80-
assert.deepEqual(Metadata.getCloudFunctionDescriptor(PROJECT_ID), {
79+
assert.deepEqual(Metadata.getCloudFunctionDescriptor(), {
8180
type: 'cloud_function',
8281
labels: {
83-
project_id: PROJECT_ID,
8482
function_name: FUNCTION_NAME,
8583
region: SUPERVISOR_REGION
8684
}
@@ -100,10 +98,9 @@ describe('metadata', function() {
10098
});
10199

102100
it('should return the correct descriptor', function() {
103-
assert.deepEqual(Metadata.getGAEDescriptor(PROJECT_ID), {
101+
assert.deepEqual(Metadata.getGAEDescriptor(), {
104102
type: 'gae_app',
105103
labels: {
106-
project_id: PROJECT_ID,
107104
module_id: GAE_SERVICE,
108105
version_id: GAE_VERSION
109106
}
@@ -113,7 +110,7 @@ describe('metadata', function() {
113110
it('should use GAE_MODULE_NAME for module_id', function() {
114111
delete process.env.GAE_SERVICE;
115112

116-
var moduleId = Metadata.getGAEDescriptor(PROJECT_ID).labels.module_id;
113+
var moduleId = Metadata.getGAEDescriptor().labels.module_id;
117114
assert.strictEqual(moduleId, GAE_MODULE_NAME);
118115
});
119116
});
@@ -124,13 +121,12 @@ describe('metadata', function() {
124121
it('should return the correct descriptor', function(done) {
125122
instanceArgsOverride = [null, null, CLUSTER_NAME];
126123

127-
Metadata.getGKEDescriptor(PROJECT_ID, function(err, descriptor) {
124+
Metadata.getGKEDescriptor(function(err, descriptor) {
128125
assert.ifError(err);
129126
assert.deepEqual(descriptor, {
130127
type: 'container',
131128
labels: {
132-
cluster_name: CLUSTER_NAME,
133-
project_id: PROJECT_ID
129+
cluster_name: CLUSTER_NAME
134130
}
135131
});
136132
done();
@@ -141,7 +137,7 @@ describe('metadata', function() {
141137
var FAKE_ERROR = new Error();
142138
instanceArgsOverride = [ FAKE_ERROR ];
143139

144-
Metadata.getGKEDescriptor(PROJECT_ID, function(err) {
140+
Metadata.getGKEDescriptor(function(err) {
145141
assert.strictEqual(err, FAKE_ERROR);
146142
done();
147143
});
@@ -150,56 +146,21 @@ describe('metadata', function() {
150146

151147
describe('getGCEDescriptor', function() {
152148
it('should return the correct descriptor', function() {
153-
assert.deepEqual(Metadata.getGCEDescriptor(PROJECT_ID), {
154-
type: 'gce_instance',
155-
labels: {
156-
project_id: PROJECT_ID
157-
}
149+
assert.deepEqual(Metadata.getGCEDescriptor(), {
150+
type: 'gce_instance'
158151
});
159152
});
160153
});
161154

162155
describe('getGlobalDescriptor', function() {
163156
it('should return the correct descriptor', function() {
164-
assert.deepEqual(Metadata.getGlobalDescriptor(PROJECT_ID), {
165-
type: 'global',
166-
labels: {
167-
project_id: PROJECT_ID
168-
}
157+
assert.deepEqual(Metadata.getGlobalDescriptor(), {
158+
type: 'global'
169159
});
170160
});
171161
});
172162

173163
describe('getDefaultResource', function() {
174-
var RETURNED_PROJECT_ID = 'project-id';
175-
176-
beforeEach(function() {
177-
metadata.logging.auth.getProjectId = function(callback) {
178-
callback(null, RETURNED_PROJECT_ID);
179-
};
180-
});
181-
182-
it('should get the project ID', function(done) {
183-
metadata.logging.auth.getProjectId = function() {
184-
done();
185-
};
186-
187-
metadata.getDefaultResource(assert.ifError);
188-
});
189-
190-
it('should return error from getProjectId', function(done) {
191-
var error = new Error('Error.');
192-
193-
metadata.logging.auth.getProjectId = function(callback) {
194-
callback(error);
195-
};
196-
197-
metadata.getDefaultResource(function(err) {
198-
assert.strictEqual(err, error);
199-
done();
200-
});
201-
});
202-
203164
it('should get the environment from auth client', function(done) {
204165
metadata.logging.auth.getEnvironment = function() {
205166
done();
@@ -213,8 +174,7 @@ describe('metadata', function() {
213174
it('should return correct descriptor', function(done) {
214175
var DESCRIPTOR = {};
215176

216-
Metadata.getGAEDescriptor = function(projectId) {
217-
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
177+
Metadata.getGAEDescriptor = function() {
218178
return DESCRIPTOR;
219179
};
220180

@@ -237,8 +197,7 @@ describe('metadata', function() {
237197
it('should return correct descriptor', function(done) {
238198
var DESCRIPTOR = {};
239199

240-
Metadata.getCloudFunctionDescriptor = function(projectId) {
241-
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
200+
Metadata.getCloudFunctionDescriptor = function() {
242201
return DESCRIPTOR;
243202
};
244203

@@ -261,8 +220,7 @@ describe('metadata', function() {
261220
it('should return correct descriptor', function(done) {
262221
var DESCRIPTOR = {};
263222

264-
Metadata.getGCEDescriptor = function(projectId) {
265-
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
223+
Metadata.getGCEDescriptor = function() {
266224
return DESCRIPTOR;
267225
};
268226

@@ -297,8 +255,7 @@ describe('metadata', function() {
297255
assert.deepStrictEqual(defaultResource, {
298256
type: 'container',
299257
labels: {
300-
cluster_name: CLUSTER_NAME,
301-
project_id: RETURNED_PROJECT_ID
258+
cluster_name: CLUSTER_NAME
302259
}
303260
});
304261
done();
@@ -310,8 +267,7 @@ describe('metadata', function() {
310267
it('should return correct descriptor', function(done) {
311268
var DESCRIPTOR = {};
312269

313-
Metadata.getGlobalDescriptor = function(projectId) {
314-
assert.strictEqual(projectId, RETURNED_PROJECT_ID);
270+
Metadata.getGlobalDescriptor = function() {
315271
return DESCRIPTOR;
316272
};
317273

0 commit comments

Comments
 (0)