From 6ace2c8c2069eb7c0ee25584b1ade41015c47da1 Mon Sep 17 00:00:00 2001 From: yamatatsu Date: Tue, 16 Nov 2021 19:55:56 +0900 Subject: [PATCH] address comments --- packages/@aws-cdk/aws-iot-actions/README.md | 4 ++-- ...ion.ts => cloudwatch-put-metric-action.ts} | 23 ++++++++----------- .../@aws-cdk/aws-iot-actions/lib/index.ts | 2 +- .../@aws-cdk/aws-iot-actions/package.json | 2 ++ .../cloudwatch-put-metric-action.test.ts} | 6 ++--- .../integ.cloudwatch-logs-action.ts | 1 - ...loudwatch-put-metric-action.expected.json} | 0 .../integ.cloudwatch-put-metric-action.ts} | 3 +-- .../integ.firehose-stream-action.ts | 1 - .../lambda/integ.lambda-function-action.ts | 1 - .../test/s3/integ.s3-put-object-action.ts | 1 - 11 files changed, 19 insertions(+), 25 deletions(-) rename packages/@aws-cdk/aws-iot-actions/lib/{cloudwatch-metric-action.ts => cloudwatch-put-metric-action.ts} (77%) rename packages/@aws-cdk/aws-iot-actions/test/{cloudwatch-metric/cloudwatch-metric-action.test.ts => cloudwatch/cloudwatch-put-metric-action.test.ts} (96%) rename packages/@aws-cdk/aws-iot-actions/test/{cloudwatch-metric/integ.cloudwatch-metric-action.expected.json => cloudwatch/integ.cloudwatch-put-metric-action.expected.json} (100%) rename packages/@aws-cdk/aws-iot-actions/test/{cloudwatch-metric/integ.cloudwatch-metric-action.ts => cloudwatch/integ.cloudwatch-put-metric-action.ts} (88%) diff --git a/packages/@aws-cdk/aws-iot-actions/README.md b/packages/@aws-cdk/aws-iot-actions/README.md index d891a5b2405be..bf2955757abe8 100644 --- a/packages/@aws-cdk/aws-iot-actions/README.md +++ b/packages/@aws-cdk/aws-iot-actions/README.md @@ -138,13 +138,13 @@ const topicRule = new iot.TopicRule(this, 'TopicRule', { "SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'", ), actions: [ - new actions.CloudWatchMetricAction({ + new actions.CloudWatchPutMetricAction({ metricName: '${topic(2)}', metricNamespace: '${namespace}', metricUnit: '${unit}', metricValue: '${value}', metricTimestamp: '${timestamp}', - }) + }), ], }); ``` diff --git a/packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-metric-action.ts b/packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-put-metric-action.ts similarity index 77% rename from packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-metric-action.ts rename to packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-put-metric-action.ts index 771fc0aaa6957..90d7658e4a493 100644 --- a/packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-metric-action.ts +++ b/packages/@aws-cdk/aws-iot-actions/lib/cloudwatch-put-metric-action.ts @@ -1,4 +1,4 @@ -import * as iam from '@aws-cdk/aws-iam'; +import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; import * as iot from '@aws-cdk/aws-iot'; import { CommonActionProps } from './common-action-props'; import { singletonActionRole } from './private/role'; @@ -6,14 +6,14 @@ import { singletonActionRole } from './private/role'; /** * Configuration properties of an action for CloudWatch metric. */ -export interface CloudWatchMetricActionProps extends CommonActionProps { +export interface CloudWatchPutMetricActionProps extends CommonActionProps { /** * The CloudWatch metric name. * * Supports substitution templates. * @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html */ - readonly metricName: string, + readonly metricName: string; /** * The CloudWatch metric namespace name. @@ -21,7 +21,7 @@ export interface CloudWatchMetricActionProps extends CommonActionProps { * Supports substitution templates. * @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html */ - readonly metricNamespace: string, + readonly metricNamespace: string; /** * A string that contains the timestamp, expressed in seconds in Unix epoch time. @@ -31,7 +31,7 @@ export interface CloudWatchMetricActionProps extends CommonActionProps { * * @default - none -- Defaults to the current Unix epoch time. */ - readonly metricTimestamp?: string, + readonly metricTimestamp?: string; /** * The metric unit supported by CloudWatch. @@ -39,7 +39,7 @@ export interface CloudWatchMetricActionProps extends CommonActionProps { * Supports substitution templates. * @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html */ - readonly metricUnit: string, + readonly metricUnit: string; /** * A string that contains the CloudWatch metric value. @@ -47,22 +47,19 @@ export interface CloudWatchMetricActionProps extends CommonActionProps { * Supports substitution templates. * @see https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html */ - readonly metricValue: string, + readonly metricValue: string; } /** * The action to capture an Amazon CloudWatch metric. */ -export class CloudWatchMetricAction implements iot.IAction { - constructor(private readonly props: CloudWatchMetricActionProps) { +export class CloudWatchPutMetricAction implements iot.IAction { + constructor(private readonly props: CloudWatchPutMetricActionProps) { } bind(rule: iot.ITopicRule): iot.ActionConfig { const role = this.props.role ?? singletonActionRole(rule); - role.addToPrincipalPolicy(new iam.PolicyStatement({ - actions: ['cloudwatch:PutMetricData'], - resources: ['*'], - })); + cloudwatch.Metric.grantPutMetricData(role); return { configuration: { diff --git a/packages/@aws-cdk/aws-iot-actions/lib/index.ts b/packages/@aws-cdk/aws-iot-actions/lib/index.ts index c5c8211b3c938..4ad9c1d2a1fb6 100644 --- a/packages/@aws-cdk/aws-iot-actions/lib/index.ts +++ b/packages/@aws-cdk/aws-iot-actions/lib/index.ts @@ -1,5 +1,5 @@ export * from './cloudwatch-logs-action'; -export * from './cloudwatch-metric-action'; +export * from './cloudwatch-put-metric-action'; export * from './common-action-props'; export * from './firehose-stream-action'; export * from './lambda-function-action'; diff --git a/packages/@aws-cdk/aws-iot-actions/package.json b/packages/@aws-cdk/aws-iot-actions/package.json index b996897b7719d..82d33a57fac38 100644 --- a/packages/@aws-cdk/aws-iot-actions/package.json +++ b/packages/@aws-cdk/aws-iot-actions/package.json @@ -80,6 +80,7 @@ "jest": "^27.3.1" }, "dependencies": { + "@aws-cdk/aws-cloudwatch": "0.0.0", "@aws-cdk/aws-iam": "0.0.0", "@aws-cdk/aws-iot": "0.0.0", "@aws-cdk/aws-kinesisfirehose": "0.0.0", @@ -92,6 +93,7 @@ }, "homepage": "https://github.com/aws/aws-cdk", "peerDependencies": { + "@aws-cdk/aws-cloudwatch": "0.0.0", "@aws-cdk/aws-iam": "0.0.0", "@aws-cdk/aws-iot": "0.0.0", "@aws-cdk/aws-kinesisfirehose": "0.0.0", diff --git a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/cloudwatch-metric-action.test.ts b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/cloudwatch-put-metric-action.test.ts similarity index 96% rename from packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/cloudwatch-metric-action.test.ts rename to packages/@aws-cdk/aws-iot-actions/test/cloudwatch/cloudwatch-put-metric-action.test.ts index d5b59b7e3e322..ed6b23bb90bde 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/cloudwatch-metric-action.test.ts +++ b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/cloudwatch-put-metric-action.test.ts @@ -13,7 +13,7 @@ test('Default cloudwatch metric action', () => { // WHEN topicRule.addAction( - new actions.CloudWatchMetricAction({ + new actions.CloudWatchPutMetricAction({ metricName: '${topic(2)}', metricNamespace: '${namespace}', metricUnit: '${unit}', @@ -80,7 +80,7 @@ test('can set timestamp', () => { // WHEN topicRule.addAction( - new actions.CloudWatchMetricAction({ + new actions.CloudWatchPutMetricAction({ metricName: '${topic(2)}', metricNamespace: '${namespace}', metricUnit: '${unit}', @@ -109,7 +109,7 @@ test('can set role', () => { // WHEN topicRule.addAction( - new actions.CloudWatchMetricAction({ + new actions.CloudWatchPutMetricAction({ metricName: '${topic(2)}', metricNamespace: '${namespace}', metricUnit: '${unit}', diff --git a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-logs-action.ts b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-logs-action.ts index 802f485b77e37..ce8b3a8111c5d 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-logs-action.ts +++ b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-logs-action.ts @@ -1,4 +1,3 @@ -/// !cdk-integ pragma:ignore-assets import * as iot from '@aws-cdk/aws-iot'; import * as logs from '@aws-cdk/aws-logs'; import * as cdk from '@aws-cdk/core'; diff --git a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/integ.cloudwatch-metric-action.expected.json b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-put-metric-action.expected.json similarity index 100% rename from packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/integ.cloudwatch-metric-action.expected.json rename to packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-put-metric-action.expected.json diff --git a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/integ.cloudwatch-metric-action.ts b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-put-metric-action.ts similarity index 88% rename from packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/integ.cloudwatch-metric-action.ts rename to packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-put-metric-action.ts index 9c96d16a1dc52..5a70d0bca3652 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/cloudwatch-metric/integ.cloudwatch-metric-action.ts +++ b/packages/@aws-cdk/aws-iot-actions/test/cloudwatch/integ.cloudwatch-put-metric-action.ts @@ -1,4 +1,3 @@ -/// !cdk-integ pragma:ignore-assets import * as iot from '@aws-cdk/aws-iot'; import * as cdk from '@aws-cdk/core'; import * as actions from '../../lib'; @@ -13,7 +12,7 @@ class TestStack extends cdk.Stack { sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, namespace, unit, value, timestamp FROM 'device/+/data'"), }); - topicRule.addAction(new actions.CloudWatchMetricAction({ + topicRule.addAction(new actions.CloudWatchPutMetricAction({ metricName: '${topic(2)}', metricNamespace: '${namespace}', metricUnit: '${unit}', diff --git a/packages/@aws-cdk/aws-iot-actions/test/kinesis-firehose/integ.firehose-stream-action.ts b/packages/@aws-cdk/aws-iot-actions/test/kinesis-firehose/integ.firehose-stream-action.ts index 9287f1294b4dd..2c6c93cf0460f 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/kinesis-firehose/integ.firehose-stream-action.ts +++ b/packages/@aws-cdk/aws-iot-actions/test/kinesis-firehose/integ.firehose-stream-action.ts @@ -1,4 +1,3 @@ -/// !cdk-integ pragma:ignore-assets import * as iot from '@aws-cdk/aws-iot'; import * as firehose from '@aws-cdk/aws-kinesisfirehose'; import * as destinations from '@aws-cdk/aws-kinesisfirehose-destinations'; diff --git a/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.ts b/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.ts index 58a7773afec03..b6052c80aeabf 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.ts +++ b/packages/@aws-cdk/aws-iot-actions/test/lambda/integ.lambda-function-action.ts @@ -1,4 +1,3 @@ -/// !cdk-integ pragma:ignore-assets import * as iot from '@aws-cdk/aws-iot'; import * as lambda from '@aws-cdk/aws-lambda'; import * as cdk from '@aws-cdk/core'; diff --git a/packages/@aws-cdk/aws-iot-actions/test/s3/integ.s3-put-object-action.ts b/packages/@aws-cdk/aws-iot-actions/test/s3/integ.s3-put-object-action.ts index 9e100e0254eaf..eb9773fa395cd 100644 --- a/packages/@aws-cdk/aws-iot-actions/test/s3/integ.s3-put-object-action.ts +++ b/packages/@aws-cdk/aws-iot-actions/test/s3/integ.s3-put-object-action.ts @@ -1,4 +1,3 @@ -/// !cdk-integ pragma:ignore-assets import * as iot from '@aws-cdk/aws-iot'; import * as s3 from '@aws-cdk/aws-s3'; import * as cdk from '@aws-cdk/core';