Repository: cdklabs/cdk-monitoring-constructs (language: python)

Issue:
Introduce OpsItemActionStrategy for creating ops items in ops center
### Feature scope

N/A

### Describe your suggested feature

It would be great to have a `OpsItemActionStrategy` which creates ops items in ops center with a default category and severity which could be overridden for each alarm.


Discussion / hints:

Fix patch:
diff --git a/API.md b/API.md
index a2065b66..915355fc 100644
--- a/API.md
+++ b/API.md
@@ -45176,6 +45176,94 @@ Returns widgets to be placed on the main dashboard.
 
 
 
+### OpsItemAlarmActionStrategy <a name="OpsItemAlarmActionStrategy" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy"></a>
+
+- *Implements:* <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>
+
+Alarm action strategy that creates an AWS OpsCenter OpsItem.
+
+#### Initializers <a name="Initializers" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.Initializer"></a>
+
+```typescript
+import { OpsItemAlarmActionStrategy } from 'cdk-monitoring-constructs'
+
+new OpsItemAlarmActionStrategy(severity: OpsItemSeverity, category?: OpsItemCategory)
+```
+
+| **Name** | **Type** | **Description** |
+| --- | --- | --- |
+| <code><a href="#cdk-monitoring-constructs.OpsItemAlarmActionStrategy.Initializer.parameter.severity">severity</a></code> | <code>aws-cdk-lib.aws_cloudwatch_actions.OpsItemSeverity</code> | *No description.* |
+| <code><a href="#cdk-monitoring-constructs.OpsItemAlarmActionStrategy.Initializer.parameter.category">category</a></code> | <code>aws-cdk-lib.aws_cloudwatch_actions.OpsItemCategory</code> | *No description.* |
+
+---
+
+##### `severity`<sup>Required</sup> <a name="severity" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.Initializer.parameter.severity"></a>
+
+- *Type:* aws-cdk-lib.aws_cloudwatch_actions.OpsItemSeverity
+
+---
+
+##### `category`<sup>Optional</sup> <a name="category" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.Initializer.parameter.category"></a>
+
+- *Type:* aws-cdk-lib.aws_cloudwatch_actions.OpsItemCategory
+
+---
+
+#### Methods <a name="Methods" id="Methods"></a>
+
+| **Name** | **Description** |
+| --- | --- |
+| <code><a href="#cdk-monitoring-constructs.OpsItemAlarmActionStrategy.addAlarmActions">addAlarmActions</a></code> | *No description.* |
+
+---
+
+##### `addAlarmActions` <a name="addAlarmActions" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.addAlarmActions"></a>
+
+```typescript
+public addAlarmActions(props: AlarmActionStrategyProps): void
+```
+
+###### `props`<sup>Required</sup> <a name="props" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.addAlarmActions.parameter.props"></a>
+
+- *Type:* <a href="#cdk-monitoring-constructs.AlarmActionStrategyProps">AlarmActionStrategyProps</a>
+
+---
+
+
+#### Properties <a name="Properties" id="Properties"></a>
+
+| **Name** | **Type** | **Description** |
+| --- | --- | --- |
+| <code><a href="#cdk-monitoring-constructs.OpsItemAlarmActionStrategy.property.severity">severity</a></code> | <code>aws-cdk-lib.aws_cloudwatch_actions.OpsItemSeverity</code> | OPS Item Severity. |
+| <code><a href="#cdk-monitoring-constructs.OpsItemAlarmActionStrategy.property.category">category</a></code> | <code>aws-cdk-lib.aws_cloudwatch_actions.OpsItemCategory</code> | OPS Item Category. |
+
+---
+
+##### `severity`<sup>Required</sup> <a name="severity" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.property.severity"></a>
+
+```typescript
+public readonly severity: OpsItemSeverity;
+```
+
+- *Type:* aws-cdk-lib.aws_cloudwatch_actions.OpsItemSeverity
+
+OPS Item Severity.
+
+---
+
+##### `category`<sup>Optional</sup> <a name="category" id="cdk-monitoring-constructs.OpsItemAlarmActionStrategy.property.category"></a>
+
+```typescript
+public readonly category: OpsItemCategory;
+```
+
+- *Type:* aws-cdk-lib.aws_cloudwatch_actions.OpsItemCategory
+
+OPS Item Category.
+
+---
+
+
 ### QueueAlarmFactory <a name="QueueAlarmFactory" id="cdk-monitoring-constructs.QueueAlarmFactory"></a>
 
 #### Initializers <a name="Initializers" id="cdk-monitoring-constructs.QueueAlarmFactory.Initializer"></a>
@@ -49346,7 +49434,7 @@ public with(options: MathExpressionOptions): IMetric
 
 ### IAlarmActionStrategy <a name="IAlarmActionStrategy" id="cdk-monitoring-constructs.IAlarmActionStrategy"></a>
 
-- *Implemented By:* <a href="#cdk-monitoring-constructs.MultipleAlarmActionStrategy">MultipleAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.NoopAlarmActionStrategy">NoopAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.SnsAlarmActionStrategy">SnsAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>
+- *Implemented By:* <a href="#cdk-monitoring-constructs.MultipleAlarmActionStrategy">MultipleAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.NoopAlarmActionStrategy">NoopAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.OpsItemAlarmActionStrategy">OpsItemAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.SnsAlarmActionStrategy">SnsAlarmActionStrategy</a>, <a href="#cdk-monitoring-constructs.IAlarmActionStrategy">IAlarmActionStrategy</a>
 
 An object that appends actions to alarms.
 
diff --git a/lib/common/alarm/OpsItemAlarmActionStrategy.ts b/lib/common/alarm/OpsItemAlarmActionStrategy.ts
new file mode 100644
index 00000000..768acd19
--- /dev/null
+++ b/lib/common/alarm/OpsItemAlarmActionStrategy.ts
@@ -0,0 +1,77 @@
+import {
+  OpsItemCategory,
+  OpsItemSeverity,
+  SsmAction,
+} from "aws-cdk-lib/aws-cloudwatch-actions";
+import {
+  AlarmActionStrategyProps,
+  IAlarmActionStrategy,
+} from "./IAlarmActionStrategy";
+
+/**
+ * Creates an AWS OpsCenter OpsItem with critical severity.
+ * @param category optional category (no category by default)
+ */
+export function createCriticalSeverityOpsItem(category?: OpsItemCategory) {
+  return createOpsItem(OpsItemSeverity.CRITICAL, category);
+}
+
+/**
+ * Creates an AWS OpsCenter OpsItem with high severity.
+ * @param category optional category (no category by default)
+ */
+export function createHighSeverityOpsItem(category?: OpsItemCategory) {
+  return createOpsItem(OpsItemSeverity.HIGH, category);
+}
+
+/**
+ * Creates an AWS OpsCenter OpsItem with medium severity.
+ * @param category optional category (no category by default)
+ */
+export function createMediumSeverityOpsItem(category?: OpsItemCategory) {
+  return createOpsItem(OpsItemSeverity.MEDIUM, category);
+}
+
+/**
+ * Creates an AWS OpsCenter OpsItem with low severity.
+ * @param category optional category (no category by default)
+ */
+export function createLowSeverityOpsItem(category?: OpsItemCategory) {
+  return createOpsItem(OpsItemSeverity.LOW, category);
+}
+
+/**
+ * Creates an AWS OpsCenter OpsItem.
+ * @param severity desired item severity
+ * @param category optional category (no category by default)
+ */
+export function createOpsItem(
+  severity: OpsItemSeverity,
+  category?: OpsItemCategory
+) {
+  return new OpsItemAlarmActionStrategy(severity, category);
+}
+
+/**
+ * Alarm action strategy that creates an AWS OpsCenter OpsItem.
+ */
+export class OpsItemAlarmActionStrategy implements IAlarmActionStrategy {
+  /**
+   * OPS Item Severity
+   */
+  readonly severity: OpsItemSeverity;
+
+  /**
+   * OPS Item Category
+   */
+  readonly category?: OpsItemCategory;
+
+  constructor(severity: OpsItemSeverity, category?: OpsItemCategory) {
+    this.severity = severity;
+    this.category = category;
+  }
+
+  addAlarmActions(props: AlarmActionStrategyProps): void {
+    props.alarm.addAlarmAction(new SsmAction(this.severity, this.category));
+  }
+}
diff --git a/lib/common/alarm/index.ts b/lib/common/alarm/index.ts
index 552a8fbb..2b79ae9a 100644
--- a/lib/common/alarm/index.ts
+++ b/lib/common/alarm/index.ts
@@ -6,4 +6,5 @@ export * from "./IAlarmAnnotationStrategy";
 export * from "./IAlarmDedupeStringProcessor";
 export * from "./MultipleAlarmActionStrategy";
 export * from "./NoopAlarmActionStrategy";
+export * from "./OpsItemAlarmActionStrategy";
 export * from "./SnsAlarmActionStrategy";


Test patch:
diff --git a/test/common/alarm/OpsItemAlarmActionStrategy.test.ts b/test/common/alarm/OpsItemAlarmActionStrategy.test.ts
new file mode 100644
index 00000000..683f20ee
--- /dev/null
+++ b/test/common/alarm/OpsItemAlarmActionStrategy.test.ts
@@ -0,0 +1,25 @@
+import { Stack } from "aws-cdk-lib";
+import { Template } from "aws-cdk-lib/assertions";
+import { Alarm, Metric } from "aws-cdk-lib/aws-cloudwatch";
+import {
+  OpsItemCategory,
+  OpsItemSeverity,
+} from "aws-cdk-lib/aws-cloudwatch-actions";
+
+import { OpsItemAlarmActionStrategy } from "../../../lib";
+
+test("snapshot test", () => {
+  const stack = new Stack();
+  const alarm = new Alarm(stack, "DummyAlarm", {
+    evaluationPeriods: 1,
+    threshold: 0,
+    metric: new Metric({ namespace: "Dummy", metricName: "Dummy" }),
+  });
+  const action = new OpsItemAlarmActionStrategy(
+    OpsItemSeverity.LOW,
+    OpsItemCategory.AVAILABILITY
+  );
+  action.addAlarmActions({ alarm, action });
+
+  expect(Template.fromStack(stack)).toMatchSnapshot();
+});
diff --git a/test/common/alarm/__snapshots__/OpsItemAlarmActionStrategy.test.ts.snap b/test/common/alarm/__snapshots__/OpsItemAlarmActionStrategy.test.ts.snap
new file mode 100644
index 00000000..4b615ae0
--- /dev/null
+++ b/test/common/alarm/__snapshots__/OpsItemAlarmActionStrategy.test.ts.snap
@@ -0,0 +1,72 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`snapshot test 1`] = `
+Object {
+  "Parameters": Object {
+    "BootstrapVersion": Object {
+      "Default": "/cdk-bootstrap/hnb659fds/version",
+      "Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]",
+      "Type": "AWS::SSM::Parameter::Value<String>",
+    },
+  },
+  "Resources": Object {
+    "DummyAlarm234203A9": Object {
+      "Properties": Object {
+        "AlarmActions": Array [
+          Object {
+            "Fn::Join": Array [
+              "",
+              Array [
+                "arn:aws:ssm:",
+                Object {
+                  "Ref": "AWS::Region",
+                },
+                ":",
+                Object {
+                  "Ref": "AWS::AccountId",
+                },
+                ":opsitem:4#CATEGORY=Availability",
+              ],
+            ],
+          },
+        ],
+        "ComparisonOperator": "GreaterThanOrEqualToThreshold",
+        "EvaluationPeriods": 1,
+        "MetricName": "Dummy",
+        "Namespace": "Dummy",
+        "Period": 300,
+        "Statistic": "Average",
+        "Threshold": 0,
+      },
+      "Type": "AWS::CloudWatch::Alarm",
+    },
+  },
+  "Rules": Object {
+    "CheckBootstrapVersion": Object {
+      "Assertions": Array [
+        Object {
+          "Assert": Object {
+            "Fn::Not": Array [
+              Object {
+                "Fn::Contains": Array [
+                  Array [
+                    "1",
+                    "2",
+                    "3",
+                    "4",
+                    "5",
+                  ],
+                  Object {
+                    "Ref": "BootstrapVersion",
+                  },
+                ],
+              },
+            ],
+          },
+          "AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.",
+        },
+      ],
+    },
+  },
+}
+`;