-
Notifications
You must be signed in to change notification settings - Fork 0
feat: push COGS to spreadsheet using helix forms #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sudo-buddy
wants to merge
2
commits into
main
Choose a base branch
from
SITES-18799
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2024 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
| import { badRequest, internalServerError, noContent } from '@adobe/spacecat-shared-http-utils'; | ||
| import { fetch } from '../support/utils.js'; | ||
|
|
||
| export default async function cogsHandler(message, context) { | ||
| const { log } = context; | ||
| const { monthYear, usageCost } = message; | ||
| const { COGS_EXCEL_POST_URL: cogsExcelUrl } = context.env; | ||
| if (!monthYear || monthYear === '') { | ||
| return badRequest('MonthYear is missing'); | ||
| } | ||
| if (!usageCost || Object.keys(usageCost).length === 0) { | ||
| return badRequest('UsageCost is missing'); | ||
| } | ||
| try { | ||
| const response = await fetch(cogsExcelUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify({ data: { MONTH: monthYear, ...usageCost } }), | ||
| }); | ||
| if (response.status !== 201) { | ||
| return badRequest(`Failed to post data to ${cogsExcelUrl}. Status: ${response.status}`); | ||
| } | ||
| log.info(`Successfully posted data to ${cogsExcelUrl}`); | ||
| return noContent(); | ||
| } catch (e) { | ||
| log.error(e); | ||
| return internalServerError('Failed to post data to COGS Excel'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| * Copyright 2024 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import sinon from 'sinon'; | ||
| import chai from 'chai'; | ||
| import sinonChai from 'sinon-chai'; | ||
| import chaiAsPromised from 'chai-as-promised'; | ||
| import nock from 'nock'; | ||
| import cogsHandler from '../../src/cogs/handler.js'; | ||
|
|
||
| chai.use(sinonChai); | ||
| chai.use(chaiAsPromised); | ||
| const { expect } = chai; | ||
|
|
||
| const sandbox = sinon.createSandbox(); | ||
| const COGS_HELIX_HOST = 'https://main--sharepoint--sudo-buddy.hlx.page'; | ||
| const COGS_SPREADSHEET_PATH = '/spacecatpoc'; | ||
| // eslint-disable-next-line no-undef | ||
| describe('cogs handler', () => { | ||
| let message; | ||
| let context; | ||
| // eslint-disable-next-line no-undef | ||
| beforeEach('setup', () => { | ||
| message = { | ||
| monthYear: 'Dec-23', | ||
| usageCost: { | ||
| LAMBDA: '14.67', | ||
| 'SECRETS MANAGER': '0.09', | ||
| DYNAMODB: '0.10', | ||
| SQS: '0.17', | ||
| S3: '0.00', | ||
| CLOUDWATCH: '0.05', | ||
| }, | ||
| }; | ||
| context = { | ||
| log: console, | ||
| env: { | ||
| COGS_EXCEL_POST_URL: `${COGS_HELIX_HOST}${COGS_SPREADSHEET_PATH}`, | ||
| }, | ||
| }; | ||
| }); | ||
| // eslint-disable-next-line no-undef | ||
| afterEach('clean', () => { | ||
| sandbox.restore(); | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| // eslint-disable-next-line no-undef | ||
| it('reject when MonthYear is missing', async () => { | ||
| delete message.monthYear; | ||
| const result = await cogsHandler(message, context); | ||
| expect(result.status).equal(400); | ||
| }); | ||
| // eslint-disable-next-line no-undef | ||
| it('reject when UsageCost is missing', async () => { | ||
| delete message.usageCost; | ||
| const result = await cogsHandler(message, context); | ||
| expect(result.status).equal(400); | ||
| }); | ||
| // eslint-disable-next-line no-undef | ||
| it('reject when helix returns 400', async () => { | ||
| nock(COGS_HELIX_HOST) | ||
| .post(COGS_SPREADSHEET_PATH) | ||
| .reply(400); | ||
| const result = await cogsHandler(message, context); | ||
| expect(result.status) | ||
| .equal(400); | ||
| }); | ||
| // eslint-disable-next-line no-undef | ||
| it('reject when COGS_EXCEL_POST_URL is missing', async () => { | ||
| delete context.env.COGS_EXCEL_POST_URL; | ||
| nock(COGS_HELIX_HOST) | ||
| .post(COGS_SPREADSHEET_PATH) | ||
| .reply(500); | ||
| const result = await cogsHandler(message, context); | ||
| expect(result.status) | ||
| .equal(500); | ||
| }); | ||
| // eslint-disable-next-line no-undef | ||
| it('should post data to COGS Excel', async () => { | ||
| nock(COGS_HELIX_HOST) | ||
| .post(COGS_SPREADSHEET_PATH, { data: { MONTH: 'Dec-23', ...message.usageCost } }) | ||
| .reply(201); | ||
| const result = await cogsHandler(message, context); | ||
| expect(result.status) | ||
| .equal(204); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can add the following eslint rule at the top of your file like this, so that you don't have to "disable-next-line no-undef" for all test cases: