

# 使用 AWS CLI 的 AWS CodeStar Notifications 示例
<a name="cli_codestar-notifications_code_examples"></a>

以下代码示例演示如何通过将 AWS Command Line Interface与 AWS CodeStar Notifications 结合使用，来执行操作和实现常见场景。

*操作是大型程序的代码摘录*，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)

## 操作
<a name="actions"></a>

### `create-notification-rule`
<a name="codestar-notifications_CreateNotificationRule_cli_topic"></a>

以下代码示例演示了如何使用 `create-notification-rule`。

**AWS CLI**  
**创建通知规则**  
以下 `create-notification-rule` 示例使用名为 `rule.json` 的 JSON 文件为指定 AWS 账户中名为 `MyDemoRepo` 的存储库创建名为 `MyNotificationRule` 的通知规则。在创建分支和标签时，具有 `FULL` 详情类型的通知将发送到指定的目标 Amazon SNS 主题。  

```
aws codestar-notifications create-notification-rule \
    --cli-input-json file://rule.json
```
 的内容`rule.json`：  

```
{
    "Name": "MyNotificationRule",
    "EventTypeIds": [
        "codecommit-repository-branches-and-tags-created"
    ],
    "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo",
    "Targets": [
        {
            "TargetType": "SNS",
            "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic"
        }
    ],
    "Status": "ENABLED",
    "DetailType": "FULL"
}
```
输出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[创建通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [CreateNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/create-notification-rule.html)。

### `delete-notification-rule`
<a name="codestar-notifications_DeleteNotificationRule_cli_topic"></a>

以下代码示例演示了如何使用 `delete-notification-rule`。

**AWS CLI**  
**删除通知规则**  
以下 `delete-notification-rule` 示例删除指定的通知规则。  

```
aws codestar-notifications delete-notification-rule \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE
```
输出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[删除通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-delete.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/delete-notification-rule.html)。

### `delete-target`
<a name="codestar-notifications_DeleteTarget_cli_topic"></a>

以下代码示例演示了如何使用 `delete-target`。

**AWS CLI**  
**删除通知规则目标**  
以下 `delete-target` 示例将指定目标从所有将其配置为目标的通知规则中移除，然后删除目标。  

```
aws codestar-notifications  delete-target \
    --target-address arn:aws:sns:us-east-1:123456789012:MyNotificationTopic \
    --force-unsubscribe-all
```
此命令不生成任何输出。  
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[删除通知规则目标](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-delete.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DeleteTarget](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/delete-target.html)。

### `describe-notification-rule`
<a name="codestar-notifications_DescribeNotificationRule_cli_topic"></a>

以下代码示例演示了如何使用 `describe-notification-rule`。

**AWS CLI**  
**检索通知规则的详细信息**  
以下 `describe-notification-rule` 示例检索指定通知规则的详细信息。  

```
aws codestar-notifications describe-notification-rule \
    --arn arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/dc82df7a-EXAMPLE
```
输出：  

```
{
    "LastModifiedTimestamp": 1569199844.857,
    "EventTypes": [
        {
            "ServiceName": "CodeCommit",
            "EventTypeName": "Branches and tags: Created",
            "ResourceType": "Repository",
            "EventTypeId": "codecommit-repository-branches-and-tags-created"
        }
    ],
    "Status": "ENABLED",
    "DetailType": "FULL",
    "Resource": "arn:aws:codecommit:us-west-2:123456789012:MyDemoRepo",
    "Arn": "arn:aws:codestar-notifications:us-west-w:123456789012:notificationrule/dc82df7a-EXAMPLE",
    "Targets": [
        {
            "TargetStatus": "ACTIVE",
            "TargetAddress": "arn:aws:sns:us-west-2:123456789012:MyNotificationTopic",
            "TargetType": "SNS"
        }
    ],
    "Name": "MyNotificationRule",
    "CreatedTimestamp": 1569199844.857,
    "CreatedBy": "arn:aws:iam::123456789012:user/Mary_Major"
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[查看通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-view.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [DescribeNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/describe-notification-rule.html)。

### `list-event-types`
<a name="codestar-notifications_ListEventTypes_cli_topic"></a>

以下代码示例演示了如何使用 `list-event-types`。

**AWS CLI**  
**获取通知规则的事件类型列表**  
以下 `list-event-types` 示例检索 CodeDeploy 应用程序所有可用通知事件类型的筛选列表。相反，如果您不使用任何筛选条件，则该命令将返回所有资源类型的所有通知事件类型。  

```
aws codestar-notifications list-event-types \
    --filters Name=SERVICE_NAME,Value=CodeDeploy
```
输出：  

```
{
    "EventTypes": [
        {
            "EventTypeId": "codedeploy-application-deployment-succeeded",
            "ServiceName": "CodeDeploy",
            "EventTypeName": "Deployment: Succeeded",
            "ResourceType": "Application"
        },
        {
            "EventTypeId": "codedeploy-application-deployment-failed",
            "ServiceName": "CodeDeploy",
            "EventTypeName": "Deployment: Failed",
            "ResourceType": "Application"
        },
        {
            "EventTypeId": "codedeploy-application-deployment-started",
            "ServiceName": "CodeDeploy",
            "EventTypeName": "Deployment: Started",
            "ResourceType": "Application"
        }
    ]
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[创建通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListEventTypes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-event-types.html)。

### `list-notification-rules`
<a name="codestar-notifications_ListNotificationRules_cli_topic"></a>

以下代码示例演示了如何使用 `list-notification-rules`。

**AWS CLI**  
**检索通知规则列表**  
以下 `list-notification-rules` 示例检索指定 AWS 区域内的所有通知规则的列表。  

```
aws codestar-notifications list-notification-rules --region us-east-1
```
输出：  

```
{
    "NotificationRules": [
        {
            "Id": "dc82df7a-EXAMPLE",
            "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
        },
        {
            "Id": "8d1f0983-EXAMPLE",
            "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/8d1f0983-EXAMPLE"
        }
    ]
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[查看通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-view.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListNotificationRules](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-notification-rules.html)。

### `list-tags-for-resource`
<a name="codestar-notifications_ListTagsForResource_cli_topic"></a>

以下代码示例演示了如何使用 `list-tags-for-resource`。

**AWS CLI**  
**获取附加到通知规则的标签列表**  
以下 `list-tags-for-resource` 示例检索附加到指定通知规则的所有标签的列表。在此示例中，通知规则当前没有与之关联的标签。  

```
aws codestar-notifications list-tags-for-resource \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE
```
输出：  

```
{
    "Tags": {}
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[创建通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-tags-for-resource.html)。

### `list-targets`
<a name="codestar-notifications_ListTargets_cli_topic"></a>

以下代码示例演示了如何使用 `list-targets`。

**AWS CLI**  
**检索通知规则目标的列表**  
以下 `list-targets` 示例检索指定 AWS 区域内的所有通知规则目标的列表。  

```
aws codestar-notifications list-targets \
    --region us-east-1
```
输出：  

```
{
    "Targets": [
        {
            "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MySNSTopicForNotificationRules",
            "TargetType": "SNS",
            "TargetStatus": "ACTIVE"
        },
        {
            "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MySNSTopicForNotificationsAboutMyDemoRepo",
            "TargetType": "SNS",
            "TargetStatus": "ACTIVE"
        }
    ]
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[查看通知规则目标](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-view.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [ListTargets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/list-targets.html)。

### `subscribe`
<a name="codestar-notifications_Subscribe_cli_topic"></a>

以下代码示例演示了如何使用 `subscribe`。

**AWS CLI**  
**向通知规则添加目标**  
以下 `subscribe` 示例将 Amazon SNS 主题添加为指定通知规则的目标。  

```
aws codestar-notifications subscribe \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE \
    --target TargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopic
```
输出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[添加或删除 Amazon SNS 主题作为通知规则的目标](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-change-rule.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [Subscribe](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/subscribe.html)。

### `tag-resource`
<a name="codestar-notifications_TagResource_cli_topic"></a>

以下代码示例演示了如何使用 `tag-resource`。

**AWS CLI**  
**向通知规则添加标签**  
以下 `tag-resource` 示例向指定的通知规则添加了键名称为 `Team`、值为 `Li_Juan` 的标签。  

```
aws codestar-notifications tag-resource \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE \
    --tags Team=Li_Juan
```
输出：  

```
{
    "Tags": {
        "Team": "Li_Juan"
    }
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[创建通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-create.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/tag-resource.html)。

### `unsubscribe`
<a name="codestar-notifications_Unsubscribe_cli_topic"></a>

以下代码示例演示了如何使用 `unsubscribe`。

**AWS CLI**  
**从通知规则中移除目标**  
以下 `unsubscribe` 示例从指定的通知规则中移除了作为目标的 Amazon SNS 主题。  

```
aws codestar-notifications unsubscribe \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE \
    --target TargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopic
```
输出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
    "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic"
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[添加或删除 Amazon SNS 主题作为通知规则的目标](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-target-change-rule.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [Unsubscribe](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/unsubscribe.html)。

### `untag-resource`
<a name="codestar-notifications_UntagResource_cli_topic"></a>

以下代码示例演示了如何使用 `untag-resource`。

**AWS CLI**  
**从通知规则中移除标签**  
以下 `untag-resource` 示例从指定通知规则移除键名为 `Team` 的标签。  

```
aws codestar-notifications untag-resource \
    --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE \
    --tag-keys Team
```
此命令不生成任何输出。  
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[编辑通知规则](https://docs.aws.amazon.com/codestar-notifications/latest/userguide/notification-rule-edit.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/untag-resource.html)。

### `update-notification-rule`
<a name="codestar-notifications_UpdateNotificationRule_cli_topic"></a>

以下代码示例演示了如何使用 `update-notification-rule`。

**AWS CLI**  
**更新通知规则**  
以下 `update-notification-rule` 示例使用名为 `update.json` 的 JSON 文件更新 AWS 账户 `123456789012` 中名为 `MyNotificationRule` 的通知规则。  

```
aws codestar-notifications update-notification-rule \
    --cli-input-json file://update.json
```
 的内容`update.json`：  

```
{
    "Name": "MyUpdatedNotificationRule",
    "EventTypeIds": [
        "codecommit-repository-branches-and-tags-created"
    ],
    "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo",
    "Targets": [
        {
            "TargetType": "SNS",
            "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic"
        }
    ],
    "Status": "ENABLED",
    "DetailType": "FULL"
}
```
输出：  

```
{
    "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE"
}
```
有关更多信息，请参阅《AWS 开发人员工具控制台用户指南》**中的[编辑通知规则](https://docs.aws.amazon.com/dtconsole/latest/userguide/notification-rule-edit.html)。  
+  有关 API 详细信息，请参阅《AWS CLI 命令参考》**中的 [UpdateNotificationRule](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-notifications/update-notification-rule.html)。