Skip to content

Commit

Permalink
CUMULUS-3243:Updated granule delete logic (#3338) (#3366)
Browse files Browse the repository at this point in the history
* CUMULUS-3243:Updated granule delete logic (#3338)

* CUMULUS-3243:Updated granule delete logic to delete granule which is not in DynamoDB

* add unit tests

* delete files not in master

* update CHANGELOG

* remove dynamodb from test
  • Loading branch information
jennyhliu authored May 2, 2023
1 parent 770bf98 commit 6780001
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
15 changes: 12 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,15 @@ Users/clients that do not make use of these endpoints will not be impacted.
- **CUMULUS-3165**
- Update example/cumulus-tf/orca.tf to use orca v6.0.3

## [v15.0.2] 2023-04-28
## [v15.0.3] 2023-04-28

### Fixed

- **CUMULUS-3243**
- Updated granule delete logic to delete granule which is not in DynamoDB
- Updated granule unpublish logic to handle granule which is not in DynamoDB and/or CMR

## [v15.0.2] 2023-04-25

### Fixed

Expand Down Expand Up @@ -7121,8 +7129,9 @@ Note: There was an issue publishing 1.12.0. Upgrade to 1.12.1.
## [v1.0.0] - 2018-02-23
[unreleased]: https://github.com/nasa/cumulus/compare/v15.0.2...HEAD
[v15.0.2]: https://github.com/nasa/cumulus/compare/v15.0.0...v15.0.2
[unreleased]: https://github.com/nasa/cumulus/compare/v15.0.3...HEAD
[v15.0.3]: https://github.com/nasa/cumulus/compare/v15.0.2...v15.0.3
[v15.0.2]: https://github.com/nasa/cumulus/compare/v15.0.1...v15.0.2
[v15.0.1]: https://github.com/nasa/cumulus/compare/v15.0.0...v15.0.1
[v15.0.0]: https://github.com/nasa/cumulus/compare/v14.1.0...v15.0.0
[v14.1.0]: https://github.com/nasa/cumulus/compare/v14.0.0...v14.1.0
Expand Down
8 changes: 5 additions & 3 deletions packages/api/lib/granule-remove-from-cmr.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { GranuleNotPublished } = require('@cumulus/errors');
const { CMR } = require('@cumulus/cmr-client');
const log = require('@cumulus/common/log');
const {
Expand Down Expand Up @@ -28,15 +27,18 @@ const { constructCollectionId } = require('@cumulus/message/Collections');
const _removeGranuleFromCmr = async (granule, collectionId) => {
log.info(`granules.removeGranuleFromCmrByGranule granule_id: ${granule.granule_id}, colletion_id: ${collectionId}`);
if (!granule.published || !granule.cmr_link) {
throw new GranuleNotPublished(`Granule ${granule.granule_id} in Collection ${collectionId} is not published to CMR, so cannot be removed from CMR`);
log.warn(`Granule ${granule.granule_id} in Collection ${collectionId} is not published to CMR, so cannot be removed from CMR`);
return;
}

const cmrSettings = await cmrjsCmrUtils.getCmrSettings();
const cmr = new CMR(cmrSettings);
const metadata = await cmr.getGranuleMetadata(granule.cmr_link);

// Use granule UR to delete from CMR
await cmr.deleteGranule(metadata.title);
if (metadata) {
await cmr.deleteGranule(metadata.title, collectionId);
}
};

/**
Expand Down
34 changes: 27 additions & 7 deletions packages/api/tests/lib/test-granule-remove-from-cmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,40 @@ test.after.always(async (t) => {
});
});

test('unpublishGranule() removing a granule from CMR fails if the granule is not in CMR', async (t) => {
test('unpublishGranule() removing a granule from CMR succeeds if the granule is not published to CMR', async (t) => {
const {
originalPgGranule,
pgGranuleCumulusId,
collectionId,
} = await createGranuleInPG(t, {
published: false,
cmrLink: undefined,
});
try {
await unpublishGranule({ knex: t.context.knex, pgGranuleRecord: originalPgGranule });
} catch (error) {
t.is(error.message, `Granule ${originalPgGranule.granule_id} in Collection ${collectionId} is not published to CMR, so cannot be removed from CMR`);
}
await unpublishGranule({ knex: t.context.knex, pgGranuleRecord: originalPgGranule });

t.like(
await t.context.granulePgModel.get(t.context.knex, { cumulus_id: pgGranuleCumulusId }),
{
published: false,
cmr_link: null,
}
);
});

test.serial('unpublishGranule() removing a granule from CMR succeeds if the granule is not in CMR', async (t) => {
const {
originalPgGranule,
pgGranuleCumulusId,
} = await createGranuleInPG(t, {
published: true,
cmrLink: 'example.com',
});

const cmrMetadataStub = sinon.stub(CMR.prototype, 'getGranuleMetadata').resolves(undefined);
t.teardown(() => {
cmrMetadataStub.restore();
});

await unpublishGranule({ knex: t.context.knex, pgGranuleRecord: originalPgGranule });

t.like(
await t.context.granulePgModel.get(t.context.knex, { cumulus_id: pgGranuleCumulusId }),
Expand Down

0 comments on commit 6780001

Please sign in to comment.