@cumulus/integration-tests 20.3.0 → 21.0.0-echo10

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.
Files changed (4) hide show
  1. package/cmr.js +85 -26
  2. package/dist/index.js +8891 -10400
  3. package/index.js +1 -0
  4. package/package.json +13 -12
package/cmr.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  const got = require('got');
4
4
  const pWaitFor = require('p-wait-for');
5
- const xml2js = require('xml2js');
5
+ const js2xmlParser = require('js2xmlparser');
6
6
  const { s3 } = require('@cumulus/aws-client/services');
7
7
  const { buildS3Uri } = require('@cumulus/aws-client/S3');
8
8
  const { sleep } = require('@cumulus/common');
@@ -182,11 +182,13 @@ async function waitForConceptExistsOutcome(cmrLink, expectation) {
182
182
  * @param {Object} granule - granule object
183
183
  * @param {Object} collection - collection object
184
184
  * @param {Array<string>} additionalUrls - URLs to convert to online resources
185
+ * @param {boolean} matchFilesWithProducerGranuleId - When set to true, use the 'producerGranuleId'
185
186
  * @returns {Promise<Array<string>>} - Promise of the generated granule xml string
186
187
  * CMR xml files
187
188
  */
188
- function generateCmrXml(granule, collection, additionalUrls) {
189
- const xmlObject = sampleEcho10Granule;
189
+ function generateCmrXml(granule, collection, additionalUrls,
190
+ matchFilesWithProducerGranuleId = false) {
191
+ const xmlObject = structuredClone(sampleEcho10Granule);
190
192
  const oldGranuleId = xmlObject.Granule.GranuleUR;
191
193
  xmlObject.Granule.GranuleUR = granule.granuleId;
192
194
 
@@ -196,10 +198,17 @@ function generateCmrXml(granule, collection, additionalUrls) {
196
198
  };
197
199
 
198
200
  xmlObject.Granule.OnlineAccessURLs.forEach((url) => {
201
+ const replacementId = (matchFilesWithProducerGranuleId && granule.producerGranuleId)
202
+ ? granule.producerGranuleId : granule.granuleId;
199
203
  // eslint-disable-next-line no-param-reassign
200
- url.OnlineAccessURL.URL = url.OnlineAccessURL.URL.replace(oldGranuleId, granule.granuleId);
204
+ url.OnlineAccessURL.URL = url.OnlineAccessURL.URL.replace(oldGranuleId, replacementId);
201
205
  });
202
206
 
207
+ xmlObject.Granule.DataGranule = new Map();
208
+ xmlObject.Granule.DataGranule.set('SizeMBDataGranule', '10');
209
+ xmlObject.Granule.DataGranule.set('DayNightFlag', 'BOTH');
210
+ xmlObject.Granule.DataGranule.set('ProductionDateTime', '2018-07-19T12:01:01Z');
211
+
203
212
  if (additionalUrls) {
204
213
  xmlObject.Granule.OnlineAccessURLs = additionalUrls.map((url) => ({
205
214
  OnlineAccessURL: {
@@ -208,8 +217,12 @@ function generateCmrXml(granule, collection, additionalUrls) {
208
217
  },
209
218
  }));
210
219
  }
220
+ const granuleMap = new Map();
221
+ Object.keys(xmlObject.Granule).forEach((key) => {
222
+ granuleMap.set(key, xmlObject.Granule[key]);
223
+ });
211
224
 
212
- const xml = new xml2js.Builder().buildObject(xmlObject);
225
+ const xml = js2xmlParser.parse('Granule', granuleMap);
213
226
  return xml;
214
227
  }
215
228
 
@@ -220,21 +233,28 @@ function generateCmrXml(granule, collection, additionalUrls) {
220
233
  * @param {Object} collection - collection object
221
234
  * @param {string} bucket - bucket to save the xml file to
222
235
  * @param {Array<string>} additionalUrls - URLs to convert to online resources
236
+ * @param {string} stagingDir - staging directory
237
+ * @param {boolean} matchFilesWithProducerGranuleId - When set to true, use the 'producerGranuleId'
238
+ * instead default behavior of using 'granuleId' when generating filenames.
223
239
  * @returns {Promise<Array<string>>} - Promise of a list of granule files including the created
224
240
  * CMR xml files
225
241
  */
226
- async function generateAndStoreCmrXml(granule, collection, bucket, additionalUrls, stagingDir = 'file-staging') {
227
- const xml = generateCmrXml(granule, collection, additionalUrls);
242
+ async function generateAndStoreCmrXml(granule, collection, bucket, additionalUrls, stagingDir = 'file-staging',
243
+ matchFilesWithProducerGranuleId = false) {
244
+ const xml = generateCmrXml(granule, collection, additionalUrls, matchFilesWithProducerGranuleId);
228
245
  const granuleFiles = granule.files.map((f) => `s3://${f.bucket}/${f.key}`);
229
246
 
230
- const fileKey = `${stagingDir}/${granule.granuleId}.cmr.xml`;
247
+ const id = (matchFilesWithProducerGranuleId && granule.producerGranuleId)
248
+ ? granule.producerGranuleId
249
+ : granule.granuleId;
250
+ const fileKey = `${stagingDir}/${granule.granuleId}/${id}.cmr.xml`;
231
251
 
232
252
  const params = {
233
253
  Bucket: bucket,
234
254
  Key: fileKey,
235
255
  Body: xml,
236
256
  ContentType: 'application/xml',
237
- Tagging: `granuleId=${granule.granuleId}`,
257
+ Tagging: `granuleId=${id}`,
238
258
  };
239
259
 
240
260
  await s3().putObject(params);
@@ -268,6 +288,16 @@ function isUMMGMetadataFormat(cmrMetadataFormat) {
268
288
  return cmrMetadataFormat && cmrMetadataFormat.match(/umm_json_v/);
269
289
  }
270
290
 
291
+ async function getCmrMetadataECHO10(cmrLink) {
292
+ const response = await got.get(cmrLink);
293
+
294
+ if (response.statusCode !== 200) {
295
+ console.log(`Error fetching CMR metadata, status code: ${response.statusCode}, response: ${JSON.stringify(response.body)}`);
296
+ return null;
297
+ }
298
+
299
+ return JSON.parse(response.body);
300
+ }
271
301
  /**
272
302
  * Get the online resource links from the CMR objects for ECH010
273
303
  *
@@ -280,15 +310,17 @@ function isUMMGMetadataFormat(cmrMetadataFormat) {
280
310
  href: 'https://opendap.cr.usgs.gov/opendap/hyrax/MYD13Q1.006/contents.html' }
281
311
  */
282
312
  async function getOnlineResourcesECHO10(cmrLink) {
283
- const response = await got.get(cmrLink);
313
+ const body = await getCmrMetadataECHO10(cmrLink);
314
+ return body.links;
315
+ }
284
316
 
317
+ async function getCmrMetadataUMMG(cmrLink) {
318
+ const response = await got.get(cmrLink);
285
319
  if (response.statusCode !== 200) {
320
+ console.log(`Error fetching CMR metadata, status code: ${response.statusCode}, response: ${JSON.stringify(response.body)}`);
286
321
  return null;
287
322
  }
288
-
289
- const body = JSON.parse(response.body);
290
-
291
- return body.links;
323
+ return JSON.parse(response.body);
292
324
  }
293
325
 
294
326
  /**
@@ -302,20 +334,36 @@ async function getOnlineResourcesECHO10(cmrLink) {
302
334
  Type: "GET DATA" }
303
335
  */
304
336
  async function getOnlineResourcesUMMG(cmrLink) {
305
- const response = await got.get(cmrLink);
306
-
307
- if (response.statusCode !== 200) {
308
- return null;
309
- }
310
-
311
- const body = JSON.parse(response.body);
312
-
337
+ const body = await getCmrMetadataUMMG(cmrLink);
313
338
  const links = body.items.map((item) => item.umm.RelatedUrls);
314
339
 
315
340
  // Links is a list of a list, so flatten to be one list
316
341
  return [].concat(...links);
317
342
  }
318
343
 
344
+ /**
345
+ * Fetches full granule object from CMR based on file type (ECHO10, UMM-G)
346
+ *
347
+ * @param {Object} granule
348
+ * @param {string} granule.cmrMetadataFormat - the cmr file type (e.g. echo10, umm-g)
349
+ * @param {Object} granule.cmrConceptId - the CMR granule concept ID
350
+ * @param {Object} granule.cmrLink - the metadata's granuleId
351
+ *
352
+ * @returns {Promise<Array<Object>>} - Promise returning array of links
353
+ */
354
+ async function getCmrMetadata({ cmrMetadataFormat, cmrConceptId, cmrLink }) {
355
+ console.log('Running getCmrMetadata');
356
+ console.log(cmrLink);
357
+ console.log(`${getSearchUrl()}granules.umm_json?concept_id=${cmrConceptId}`);
358
+ if (cmrMetadataFormat === 'echo10') {
359
+ return await getCmrMetadataECHO10(cmrLink.replace(/(.echo10)$/, '.json'));
360
+ }
361
+ if (isUMMGMetadataFormat(cmrMetadataFormat)) {
362
+ return await getCmrMetadataUMMG(`${getSearchUrl()}granules.umm_json?concept_id=${cmrConceptId}`);
363
+ }
364
+ throw new Error(`Invalid cmrMetadataFormat passed to getOnlineResources: ${cmrMetadataFormat}`);
365
+ }
366
+
319
367
  /**
320
368
  * Fetches online resources from CMR based on file type (ECHO10, UMM-G)
321
369
  *
@@ -347,6 +395,9 @@ async function getOnlineResources({ cmrMetadataFormat, cmrConceptId, cmrLink })
347
395
  * @param {string} bucket - bucket to save the xml file to
348
396
  * @param {Array<string>} additionalUrls - URLs to convert to related urls
349
397
  * @param {string} cmrMetadataFormat - CMR UMM-G version string <umm_json_v[x.y]>
398
+ * @param {string} stagingDir - staging directory
399
+ * @param {boolean} matchFilesWithProducerGranuleId - When set to true, use the 'producerGranuleId'
400
+ * instead default behavior of using 'granuleId' when generating filenames.
350
401
  * @returns {Promise<Array<string>>} - Promise of a list of granule files including the created
351
402
  * CMR files
352
403
  */
@@ -356,7 +407,8 @@ async function generateAndStoreCmrUmmJson(
356
407
  bucket,
357
408
  additionalUrls,
358
409
  cmrMetadataFormat,
359
- stagingDir = 'file-staging'
410
+ stagingDir = 'file-staging',
411
+ matchFilesWithProducerGranuleId = false
360
412
  ) {
361
413
  const versionString = metadataFormatToVersion(cmrMetadataFormat);
362
414
  const jsonObject = sampleUmmGranule;
@@ -384,7 +436,10 @@ async function generateAndStoreCmrUmmJson(
384
436
  };
385
437
  }
386
438
 
387
- const fileKey = `${stagingDir}/${granule.granuleId}.cmr.json`;
439
+ const fileNameBase = matchFilesWithProducerGranuleId
440
+ ? granule.producerGranuleId
441
+ : granule.granuleId;
442
+ const fileKey = `${stagingDir}/${granule.granuleId}/${fileNameBase}.cmr.json`;
388
443
 
389
444
  const params = {
390
445
  Bucket: bucket,
@@ -423,6 +478,7 @@ async function generateCmrFilesForGranules({
423
478
  cmrMetadataFormat,
424
479
  additionalUrls,
425
480
  stagingDir,
481
+ matchFilesWithProducerGranuleId = false,
426
482
  }) {
427
483
  let files;
428
484
 
@@ -437,7 +493,8 @@ async function generateCmrFilesForGranules({
437
493
  bucket,
438
494
  additionalUrls,
439
495
  cmrMetadataFormat,
440
- stagingDir
496
+ stagingDir,
497
+ matchFilesWithProducerGranuleId
441
498
  ))
442
499
  );
443
500
  } else {
@@ -448,7 +505,8 @@ async function generateCmrFilesForGranules({
448
505
  collection,
449
506
  bucket,
450
507
  additionalUrls,
451
- stagingDir
508
+ stagingDir,
509
+ matchFilesWithProducerGranuleId
452
510
  ))
453
511
  );
454
512
  }
@@ -459,6 +517,7 @@ async function generateCmrFilesForGranules({
459
517
  module.exports = {
460
518
  conceptExists,
461
519
  getOnlineResources,
520
+ getCmrMetadata,
462
521
  generateCmrFilesForGranules,
463
522
  generateCmrXml,
464
523
  waitForConceptExistsOutcome,