

# AWS CLI を使用した CodeConnections の例
<a name="cli_codestar-connections_code_examples"></a>

次のコード例は、CodeConnections で AWS Command Line Interfaceを使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**Topics**
+ [アクション](#actions)

## アクション
<a name="actions"></a>

### `create-connection`
<a name="codestar-connections_CreateConnection_cli_topic"></a>

次のコード例は、`create-connection` を使用する方法を示しています。

**AWS CLI**  
**接続を作成するには**  
次の `create-connection` の例では、サードパーティーのリポジトリへの接続を作成する方法を示しています。この例では、サードパーティーのプロバイダーが Bitbucket である接続を作成しています。  
AWS CLI または AWS CloudFormation を使用して作成された接続は、デフォルトで [保留中] ステータスになっています。CLI または AWS CloudFormation を使用して接続を作成した後、コンソールを使用して接続を編集し、ステータスを [使用可能] にします。  

```
aws codestar-connections create-connection \
    --provider-type Bitbucket \
    --connection-name MyConnection
```
出力:  

```
{
    "ConnectionArn": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f"
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[Create a connection](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-create.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[CreateConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/create-connection.html)」を参照してください。

### `create-host`
<a name="codestar-connections_CreateHost_cli_topic"></a>

次のコード例は、`create-host` を使用する方法を示しています。

**AWS CLI**  
**ホストを作成するには**  
次の `create-host` の例では、ホストを作成して、サードパーティーのプロバイダーがインストールされているインフラストラクチャのエンドポイントを表す方法を示しています。この例では、サードパーティーのインストールされたプロバイダーが GitHub Enterprise Server であるホストを作成しています。  
AWS CLI で作成されたホストは、デフォルトでステータスが [保留中] になっています。CLI でホストを作成した後、コンソールまたは CLI を使用してホストを設定し、ステータスを [使用可能] にします。  

```
aws codestar-connections create-host \
    --name MyHost \
    --provider-type GitHubEnterpriseServer \
    --provider-endpoint "https://my-instance.dev"
```
出力:  

```
{
    "HostArn": "arn:aws:codestar-connections:us-east-1:123456789012:host/My-Host-28aef605"
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[Create a host (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-create.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[CreateHost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/create-host.html)」を参照してください。

### `delete-connection`
<a name="codestar-connections_DeleteConnection_cli_topic"></a>

次のコード例は、`delete-connection` を使用する方法を示しています。

**AWS CLI**  
**接続を削除するには**  
次の `delete-connection` の例では、接続を削除する方法を示しています。  

```
aws codestar-connections delete-connection \
    --connection-arn arn:aws:codestar-connections:us-west-2:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
```
このコマンドでは何も出力されません。  
詳細については、「*Developer Tools console ユーザーガイド*」の「[Delete a connection (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-delete.html#connections-delete-cli)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DeleteConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/delete-connection.html)」を参照してください。

### `delete-host`
<a name="codestar-connections_DeleteHost_cli_topic"></a>

次のコード例は、`delete-host` を使用する方法を示しています。

**AWS CLI**  
**ホストを削除するには**  
次の `delete-host` の例では、ホストを削除する方法を示しています。ホストを削除する前に、ホストに関連付けられたすべての接続を削除する必要があります。  

```
aws codestar-connections delete-host \
    --host-arn "arn:aws:codestar-connections:us-east-1 :123456789012:host/My-Host-28aef605"
```
このコマンドでは何も出力されません。  
詳細については、「*Developer Tools console ユーザーガイド*」の「[Delete a host (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-delete.html#connections-host-delete-cli)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[DeleteHost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/delete-host.html)」を参照してください。

### `get-connection`
<a name="codestar-connections_GetConnection_cli_topic"></a>

次のコード例は、`get-connection` を使用する方法を示しています。

**AWS CLI**  
**接続に関する情報を取得するには**  
次の `get-connection` の例では、接続の詳細を示します。  

```
aws codestar-connections get-connection \
    --connection-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
```
出力:  

```
{
    "Connection": {
        "ConnectionName": "MyConnection",
        "ConnectionArn": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
        "ProviderType": "Bitbucket",
        "OwnerAccountId": "123456789012",
        "ConnectionStatus": "AVAILABLE"
    }
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[View connection details](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-view-details.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[GetConnection](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/get-connection.html)」を参照してください。

### `get-host`
<a name="codestar-connections_GetHost_cli_topic"></a>

次のコード例は、`get-host` を使用する方法を示しています。

**AWS CLI**  
**ホストに関する情報を取得するには**  
次の `get-host` の例では、ホストの詳細を示します。  

```
aws codestar-connections get-host \
    --host-arn arn:aws:codestar-connections:us-east-1:123456789012:host/MyHost-28aef605
```
出力:  

```
{
    "Name": "MyHost",
    "Status": "AVAILABLE",
    "ProviderType": "GitHubEnterpriseServer",
    "ProviderEndpoint": "https://test-instance-1.dev/"
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[View host details (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-view.html#connections-host-view-cli)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[GetHost](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/get-host.html)」を参照してください。

### `list-connections`
<a name="codestar-connections_ListConnections_cli_topic"></a>

次のコード例は、`list-connections` を使用する方法を示しています。

**AWS CLI**  
**接続を一覧表示するには**  
次の `list-connections` の例では、Bitbucket プロバイダタイプのアカウント内のすべての接続のリストを取得しています。  

```
aws codestar-connections list-connections \
--provider-type Bitbucket \
--max-results 5 \
--next-token: next-token
```
出力:  

```
{
    "Connections": [
        {
            "ConnectionName": "my-connection",
            "ProviderType": "Bitbucket",
            "Status": "PENDING",
            "ARN": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
            "OwnerAccountId": "123456789012"
        },
        {
            "ConnectionName": "my-other-connection",
            "ProviderType": "Bitbucket",
            "Status": "AVAILABLE",
            "ARN": "arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f",
            "OwnerAccountId": "123456789012"
        },
    ],
    "NextToken": "next-token"
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[List connections (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-list.html#connections-list-cli)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListConnections](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/list-connections.html)」を参照してください。

### `list-hosts`
<a name="codestar-connections_ListHosts_cli_topic"></a>

次のコード例は、`list-hosts` を使用する方法を示しています。

**AWS CLI**  
**ホストを一覧表示するには**  
次の `list-hosts` の例では、アカウント内のすべてのホストのリストを取得しています。  

```
aws codestar-connections list-hosts
```
出力:  

```
{
    "Hosts": [
        {
            "Name": "My-Host",
            "HostArn": "arn:aws:codestar-connections:us-east-1:123456789012:host/My-Host-28aef605",
            "ProviderType": "GitHubEnterpriseServer",
            "ProviderEndpoint": "https://my-instance.test.dev",
            "Status": "AVAILABLE"
        }
    ]
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[List hosts (CLI)](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-host-list.html)」を参照してください。  
+  API の詳細については、「*AWS CLI コマンドリファレンス*」の「[ListHosts](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/list-hosts.html)」を参照してください。

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

次のコード例は、`list-tags-for-resource` を使用する方法を示しています。

**AWS CLI**  
**タグを一覧表示するには**  
次の `list-tags-for-resource` の例では、指定された接続リソースにアタッチされたすべてのタグのリストを取得しています。  

```
aws codestar-connections list-tags-for-resource \
    --resource-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
```
出力:  

```
{
    "Tags": [
        {
            "Key": "Project",
            "Value": "ProjectA"
        },
        {
            "Key": "ReadOnly",
            "Value": "true"
        }
    ]
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[View tags for a connections resource](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-tag.html#connections-tag-view)」を参照してください。  
+  API の詳細については、「AWS CLI コマンドリファレンス」の「[ListTagsForResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/list-tags-for-resource.html)」を参照してください。

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

次のコード例は、`tag-resource` を使用する方法を示しています。

**AWS CLI**  
**リソースにタグを付けるには**  
次の `tag-resource` の例では、提供されたタグのセットを接続に関連付けています。このコマンドを使用して、タグを追加または編集します。  

```
aws codestar-connections tag-resource \
    --resource-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f \
    --tags Key=Project,Value=ProjectA Key=IscontainerBased,Value=true
```
このコマンドでは何も出力されません。  
詳細については、「*Developer Tools console ユーザーガイド*」の「[Add tags to a connections resource](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-tag.html#connections-tag-add)」を参照してください。  
+  API の詳細については、AWS CLI コマンドリファレンス**の「[TagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/tag-resource.html)」を参照してください。

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

次のコード例は、`untag-resource` を使用する方法を示しています。

**AWS CLI**  
**接続リソースから AWS のタグを削除するには**  
次の `untag-resource` では、指定されたリソースからタグを削除しています。  

```
aws codestar-connections untag-resource \
    --resource-arn arn:aws:codestar-connections:us-east-1:123456789012:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f \
    --tag-keys Project ReadOnly
```
出力:  

```
{
    "Tags": []
}
```
詳細については、「*Developer Tools console ユーザーガイド*」の「[Remove tags from a connections resource](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-tag.html#connections-tag-delete)」を参照してください。  
+  API の詳細については、「AWS CLI コマンドリファレンス」の「[UntagResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/codestar-connections/untag-resource.html)」を参照してください。