Skip to content

Commit

Permalink
JavaScript (v3): Formatting and linting fixes (CodeCommit, CloudWatch) (
Browse files Browse the repository at this point in the history
  • Loading branch information
cpyle0819 authored Oct 13, 2023
1 parent de8b961 commit e7b2632
Show file tree
Hide file tree
Showing 51 changed files with 1,628 additions and 524 deletions.
42 changes: 21 additions & 21 deletions .doc_gen/metadata/cloudwatch_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ cloudwatch_DeleteAlarms:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v3/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-deleting
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.DeleteAlarms
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-deleting
Expand Down Expand Up @@ -336,12 +336,12 @@ cloudwatch_DescribeAlarmsForMetric:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-describing
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.DescribeAlarms
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-describing
Expand Down Expand Up @@ -464,12 +464,12 @@ cloudwatch_DisableAlarmActions:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v3/developer-guide/cloudwatch-examples-using-alarm-actions.html#cloudwatch-examples-using-alarm-actions-disabling
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.DisableAlarmActions
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-using-alarm-actions.html#cloudwatch-examples-using-alarm-actions-disabling
Expand Down Expand Up @@ -548,12 +548,12 @@ cloudwatch_EnableAlarmActions:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v3/developer-guide/cloudwatch-examples-using-alarm-actions.html#cloudwatch-examples-using-alarm-actions-enabling
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.EnableAlarmActions
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-using-alarm-actions.html#cloudwatch-examples-using-alarm-actions-enabling
Expand Down Expand Up @@ -809,12 +809,12 @@ cloudwatch_ListMetrics:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v3/developer-guide/cloudwatch-examples-getting-metrics.html#cloudwatch-examples-getting-metrics-listing
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.ListMetrics
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-getting-metrics.html#cloudwatch-examples-getting-metrics-listing
Expand Down Expand Up @@ -973,12 +973,12 @@ cloudwatch_PutMetricAlarm:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-putmetricalarm
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.PutMetricAlarm
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-putmetricalarm
Expand Down Expand Up @@ -1059,12 +1059,12 @@ cloudwatch_PutMetricData:
github: javascriptv3/example_code/cloudwatch
sdkguide: sdk-for-javascript/v3/developer-guide/cloudwatch-examples-getting-metrics.html#cloudwatch-examples-getting-metrics-publishing-custom
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- javascript.v3.cloudwatch.actions.PutMetricData
- description: Create the client in a separate module and export it.
snippet_tags:
- javascript.v3.cloudwatch.client
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-getting-metrics.html#cloudwatch-examples-getting-metrics-publishing-custom
Expand Down
2 changes: 1 addition & 1 deletion javascriptv3/example_code/cloudwatch-logs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--Generated by WRITEME on 2023-09-25 14:13:54.705249 (UTC)-->
<!--Generated by WRITEME on 2023-10-11 16:21:09.354155 (UTC)-->

# CloudWatch Logs code examples for the SDK for JavaScript (v3)

Expand Down
5 changes: 2 additions & 3 deletions javascriptv3/example_code/cloudwatch-logs/libs/iam-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const createRole = async (roleName, statement) => {
return Arn;
};

export const createLambdaRole = async (roleName) => {
export const createLambdaRole = (roleName) => {
return createRole(roleName, [
{
Effect: "Allow",
Expand All @@ -47,7 +47,7 @@ export const deleteRole = (roleName) => {
return client.send(command);
};

export const attachRolePolicy = async (roleName, policyArn) => {
export const attachRolePolicy = (roleName, policyArn) => {
const command = new AttachRolePolicyCommand({
PolicyArn: policyArn, // For example, arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
RoleName: roleName, // For example, lambda-basic-execution-role
Expand All @@ -56,7 +56,6 @@ export const attachRolePolicy = async (roleName, policyArn) => {
return client.send(command);
};


export const detachRolePolicy = (roleName, policyArn) => {
const command = new DetachRolePolicyCommand({
PolicyArn: policyArn,
Expand Down
94 changes: 62 additions & 32 deletions javascriptv3/example_code/cloudwatch/README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,91 @@
# Cloudwatch examples for the AWS SDK for JavaScript (v3)
<!--Generated by WRITEME on 2023-10-11 16:26:01.796348 (UTC)-->

# CloudWatch code examples for the SDK for JavaScript (v3)

## Overview

These code examples demonstrate how to work with Amazon CloudWatch
using the AWS SDK for JavaScript (v3).
Shows how to use the AWS SDK for JavaScript (v3) to work with Amazon CloudWatch.

<!--custom.overview.start-->
<!--custom.overview.end-->

Amazon CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes.
You no longer need to set up, manage, and scale your own monitoring systems and infrastructure.
_CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes._

## Important
## ⚠ Important

- Running this code might result in charges to your AWS account.
- Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/?aws-products-pricing.sort-by=item.additionalFields.productNameLowercase&aws-products-pricing.sort-order=asc&awsf.Free%20Tier%20Type=*all&awsf.tech-category=*all) and [Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all).
- Running the tests might result in charges to your AWS account.
- We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
- This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Single Actions
### Prerequisites

- [Create an alarm that watches a metric](actions/put-metric-alarm.js)
- [Delete alarms](actions/delete-alarms.js)
- [Describe alarms for a metric](actions/describe-alarms.js)
- [Disable alarm actions](actions/disable-alarm-actions.js)
- [Enable alarm actions](actions/enable-alarm-actions.js)
- [List metrics](actions/list-metrics.js)
- [Put data into a metric](actions/put-metric-data.js)
For prerequisites, see the [README](../../README.md#Prerequisites) in the `javascriptv3` folder.

**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).
<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->

## Run the examples
### Single actions

### Prerequisites
Code excerpts that show you how to call individual service functions.

1. [Set up AWS SDK for JavaScript](../README.md).
1. Run `npm i`.
- [Create a metric alarm](actions/put-metric-alarm.js#L6) (`PutMetricAlarm`)
- [Delete alarms](actions/delete-alarms.js#L6) (`DeleteAlarms`)
- [Describe alarms for a metric](actions/describe-alarms.js#L6) (`DescribeAlarmsForMetric`)
- [Disable alarm actions](actions/disable-alarm-actions.js#L6) (`DisableAlarmActions`)
- [Enable alarm actions](actions/enable-alarm-actions.js#L6) (`EnableAlarmActions`)
- [List metrics](actions/list-metrics.js#L6) (`ListMetrics`)
- [Put data into a metric](actions/put-metric-data.js#L6) (`PutMetricData`)

## Run the examples

### Instructions

**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).

**Run a single action**

1. Run `node ./actions/<fileName>`.
OR
1. Import `./actions/fileName` into another module.
```bash
node ./actions/<fileName>
```

**Run a scenario**
Most scenarios can be run with the following command:

```bash
node ./scenarios/<fileName>
```

## Tests
<!--custom.instructions.start-->
<!--custom.instructions.end-->

⚠️ Running the tests might result in charges to your AWS account.
### Tests

1. Run `npm i`.
1. Run `npm run integration-test`.
⚠ Running tests might result in charges to your AWS account.

To find instructions for running these tests, see the [README](../../README.md#Tests)
in the `javascriptv3` folder.

<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

- [Amazon CloudWatch User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html)
- [Amazon CloudWatch API reference](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/Welcome.html)
- [Amazon CloudWatch Client - AWS SDK for JavaScript (v3)](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/cloudwatch/index.html)
- [CloudWatch User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html)
- [CloudWatch API Reference](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/Welcome.html)
- [SDK for JavaScript (v3) CloudWatch reference](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/cloudwatch)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0
17 changes: 9 additions & 8 deletions javascriptv3/example_code/cloudwatch/actions/list-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { ListMetricsCommand } from "@aws-sdk/client-cloudwatch";
import { client } from "../libs/client.js";

export const run = async () => {
export const main = () => {
// Use the AWS console to see available namespaces and metric names. Custom metrics can also be created.
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/viewing_metrics_with_cloudwatch.html
const command = new ListMetricsCommand({
Expand All @@ -20,12 +20,13 @@ export const run = async () => {
Namespace: "AWS/Logs",
});

try {
return await client.send(command);
} catch (err) {
console.error(err);
}
return client.send(command);
};

export default run();
// snippet-end:[javascript.v3.cloudwatch.actions.ListMetrics]

// Call a function if this file was run directly. This allows the file
// to be runnable without running on import.
import { fileURLToPath } from "url";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
main();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, it, expect } from 'vitest';
import { describe, it, expect } from "vitest";
import { main as listMetrics } from "../actions/list-metrics.js";

describe('list-metrics', () => {
it('should list metrics', async () => {

const mod = await import('../actions/list-metrics.js');
const { Metrics } = await mod.default;

expect(Metrics.length).toBeGreaterThan(0)
})
})
describe("list-metrics", () => {
it("should list metrics", async () => {
const { Metrics } = await listMetrics();
expect(Metrics.length).toBeGreaterThan(0);
});
});
16 changes: 16 additions & 0 deletions javascriptv3/example_code/codecommit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "example-javascriptv3-codecommit",
"version": "1.0.0",
"author": "Corey Pyle <[email protected]>",
"license": "Apache-2.0",
"type": "module",
"scripts": {
"test": "vitest run **/*.unit.test.js"
},
"dependencies": {
"@aws-sdk/client-codecommit": "^3.427.0"
},
"devDependencies": {
"vitest": "^0.34.6"
}
}
Loading

0 comments on commit e7b2632

Please sign in to comment.