
# 示例三：创建VPN双连接
#### 操作场景
本章节指导用户通过调用API来创建VPN双连接。
#### 前提条件
- 已创建VPN网关。具体请参见[创建VPN网关](https://support.huaweicloud.com/api-vpn/vpn_api_0014.html)。

- 已创建对端网关。具体请参见[创建对端网关](https://support.huaweicloud.com/api-vpn/vpn_api_0021.html)。

- 当您使用Token认证方式完成认证鉴权时，需要获取用户Token并在调用接口时增加"X-Auth-Token"到业务接口请求消息头中。Token认证，具体操作请参考[认证鉴权](https://support.huaweicloud.com/api-vpn/vpn_api_0010.html)章节。
  ![](https://support.huaweicloud.com/api-vpn/public_sys-resources/note_3.0-zh-cn.png)
  通过IAM服务获取到的Token有效期为24小时，需要使用同一个Token鉴权时，可以先将Token缓存，避免频繁调用。
  
 
#### 数据准备
表1创建VPN连接请求关键参数 
| 参数           | 说明            | 取值样例                                 |
|:---|:---|:---|
| vgw_id       | VPN网关ID。      | 8030f6d6-demo-4d20-a7f8-50a7a826e2f8 |
| vgw_ip1      | VPN网关EIP1的ID。 | e4d7930f-demo-4cbf-b78a-b004416c7485 |
| vgw_ip2      | VPN网关EIP2的ID。 | 1fb97767-demo-4d8b-83bb-6f878f662005 |
| cgw_id       | 对端网关ID。       | 8916effb-demo-42d8-83d7-4517567d3d26 |
| peer_subnets | 对端子网。         | 192.168.44.0/24                      |
| psk          | 预共享密钥。        | abcd\*\*\*\*                         |
   
#### 操作步骤
1. 基于关联VPC的VPN网关，创建静态路由模式的VPN连接。
   1. 发送"POST https://{endpoint}/v5/{project_id}/vpn-connections/batch-create"，project_id为项目ID。
   
   2. 在Request Header中增加"X-Auth-Token"。
   
   3. 在Request Body中传入参数如下。
      ```
      {
          "vpn_connections": [
              {
                  "vgw_ip": "e4d7930f-demo-4cbf-b78a-b004416c7485",
                  "cgw_id": "8916effb-demo-42d8-83d7-4517567d3d26",
                  "vgw_id": "8030f6d6-demo-4d20-a7f8-50a7a826e2f8",
                  "peer_subnets": [
                      "192.168.44.0/24"
                  ],
                  "psk": "abcd****"
              },
              {
                  "vgw_ip": "1fb97767-demo-4d8b-83bb-6f878f662005",
                  "cgw_id": "8916effb-demo-42d8-83d7-4517567d3d26",
                  "vgw_id": "8030f6d6-demo-4d20-a7f8-50a7a826e2f8",
                  "peer_subnets": [
                      "192.168.44.0/24"
                  ],
                  "psk": "abcd****"
              }
          ]
      }
      ```
      
   
   4. 查看请求响应结果。
      - 请求成功时，响应参数如下，id就是VPN连接的ID。
        ```
        {
            "vpn_connections": [
                {
                    "id": "18be2aa1-demo-410f-832e-4d8ba13b4c5d",
                    "name": "vpn-22b6",
                    "vgw_id": "8030f6d6-demo-4d20-a7f8-50a7a826e2f8",
                    "vgw_ip": "e4d7930f-demo-4cbf-b78a-b004416c7485",
                    "style": "STATIC",
                    "cgw_id": "8916effb-demo-42d8-83d7-4517567d3d26",
                    "peer_subnets": [
                        "192.168.44.0/24"
                    ],
                    "tunnel_local_address": "169.254.135.49/30",
                    "tunnel_peer_address": "169.254.135.50/30",
                    "enable_nqa": false,
                    "policy_rules": [],
                    "ikepolicy": {
                        "ike_version": "v2",
                        "authentication_algorithm": "sha2-256",
                        "encryption_algorithm": "aes-128",
                        "dh_group": "group15",
                        "authentication_method": "pre-share",
                        "lifetime_seconds": 86400,
                        "local_id_type": "ip",
                        "local_id": "10.***.***.128",
                        "peer_id_type": "ip",
                        "peer_id": "188.***.***.189",
                        "dpd": {
                            "interval": 30,
                            "timeout": 15,
                            "msg": "seq-hash-notify"
                        }
                    },
                    "ipsecpolicy": {
                        "authentication_algorithm": "sha2-256",
                        "encryption_algorithm": "aes-128",
                        "pfs": "group15",
                        "transform_protocol": "esp",
                        "lifetime_seconds": 3600,
                        "encapsulation_mode": "tunnel"
                    },
                    "created_at": "2025-03-17T12:25:21.369Z",
                    "updated_at": "2025-03-17T12:25:21.369Z",
                    "enterprise_project_id": "0",
                    "ha_role": "master",
                    "tags": [],
                    "policy_rules_v6": []
                },
                {
                    "id": "c7e617bd-877f-demo-8af0-44b5f8598116",
                    "name": "vpn-e41c",
                    "vgw_id": "8030f6d6-demo-4d20-a7f8-50a7a826e2f8",
                    "vgw_ip": "1fb97767-demo-4d8b-83bb-6f878f662005",
                    "style": "STATIC",
                    "cgw_id": "8916effb-demo-42d8-83d7-4517567d3d26",
                    "peer_subnets": [
                        "192.168.44.0/24"
                    ],
                    "tunnel_local_address": "169.254.73.253/30",
                    "tunnel_peer_address": "169.254.73.254/30",
                    "enable_nqa": false,
                    "policy_rules": [],
                    "ikepolicy": {
                        "ike_version": "v2",
                        "authentication_algorithm": "sha2-256",
                        "encryption_algorithm": "aes-128",
                        "dh_group": "group15",
                        "authentication_method": "pre-share",
                        "lifetime_seconds": 86400,
                        "local_id_type": "ip",
                        "local_id": "215.***.***.55",
                        "peer_id_type": "ip",
                        "peer_id": "188.***.***.189",
                        "dpd": {
                            "interval": 30,
                            "timeout": 15,
                            "msg": "seq-hash-notify"
                        }
                    },
                    "ipsecpolicy": {
                        "authentication_algorithm": "sha2-256",
                        "encryption_algorithm": "aes-128",
                        "pfs": "group15",
                        "transform_protocol": "esp",
                        "lifetime_seconds": 3600,
                        "encapsulation_mode": "tunnel"
                    },
                    "created_at": "2025-03-17T12:25:21.678Z",
                    "updated_at": "2025-03-17T12:25:21.678Z",
                    "enterprise_project_id": "0",
                    "ha_role": "master",
                    "tags": [],
                    "policy_rules_v6": []
                }
            ],
            "request_id": "a923f31456941e12c5fc9a663a6e630e"
        }
        ```
        
       
    
2. 查询VPN连接详情。
   1. 发送"GET https://{endpoint}/v5/{project_id}/vpn-connection/{vpn_connection_id}"，project_id为项目ID。
   
   2. 在Request Header中增加"X-Auth-Token"。
   
   3. 查看请求响应结果。
      - 请求成功时，响应参数如下，id就是VPN连接的ID。
        ```
        {
            "vpn_connection": {
                "id": "98c5af8a-demo-a8df-va86-ae2280a6f4c3",
                "name": "vpn-1655",
                "status": "DOWN",
                "vgw_id": "b32d91a4-demo-a8df-va86-e907174eb11d",
                "vgw_ip": "0c464dad-demo-a8df-va86-c22bb0eb0bde",
                "style": "STATIC",
                "cgw_id": "5247ae10-demo-a8df-va86-dd36659a7f5d",
                "peer_subnets": ["192.168.44.0/24"],
                "tunnel_local_address": "169.254.56.225/30",
                "tunnel_peer_address": "169.254.56.226/30",
                "enable_nqa": false,
                "ikepolicy": {
                    "ike_version": "v2",
                    "authentication_algorithm": "sha2-256",
                    "encryption_algorithm": "aes-128",
                    "dh_group": "group15",
                    "authentication_method": "pre-share",
                    "lifetime_seconds": 86400,
                    "local_id_type": "ip",
                    "local_id": "10.***.***.134",
                    "peer_id_type": "ip",
                    "peer_id": "88.***.***.164",
                    "dpd": {
                        "timeout": 15,
                        "interval": 30,
                        "msg": "seq-hash-notify"
                    }
                },
                "ipsecpolicy": {
                    "authentication_algorithm": "sha2-256",
                    "encryption_algorithm": "aes-128",
                    "pfs": "group15",
                    "transform_protocol": "esp",
                    "lifetime_seconds": 3600,
                    "encapsulation_mode": "tunnel"
                },
                "created_at": "2025-06-26T13:41:34.626Z",
                "updated_at": "2025-06-26T13:41:34.626Z",
                "enterprise_project_id": "0",
            },
            "request_id": "104c5608-b68b-462c-af17-ead2fb5ccee4"
        }
        {
            "vpn_connection": {
                "id": "18be2aa1-demo-410f-832e-4d8ba13b4c5d",
                "name": "vpn-22b6",
                "status": "DOWN",
                "vgw_id": "8030f6d6-demo-4d20-a7f8-50a7a826e2f8",
                "vgw_ip": "e4d7930f-demo-4cbf-b78a-b004416c7485",
                "style": "STATIC",
                "cgw_id": "8916effb-demo-42d8-83d7-4517567d3d26",
                "peer_subnets": [
                    "192.168.44.0/24"
                ],
                "tunnel_local_address": "169.254.135.49/30",
                "tunnel_peer_address": "169.254.135.50/30",
                "enable_nqa": false,
                "policy_rules": [],
                "ikepolicy": {
                    "ike_version": "v2",
                    "authentication_algorithm": "sha2-256",
                    "encryption_algorithm": "aes-128",
                    "dh_group": "group15",
                    "authentication_method": "pre-share",
                    "lifetime_seconds": 86400,
                    "local_id_type": "ip",
                    "local_id": "10.***.***.128",
                    "peer_id_type": "ip",
                    "peer_id": "188.***.***.189",
                    "dpd": {
                        "interval": 30,
                        "timeout": 15,
                        "msg": "seq-hash-notify"
                    }
                },
                "ipsecpolicy": {
                    "authentication_algorithm": "sha2-256",
                    "encryption_algorithm": "aes-128",
                    "pfs": "group15",
                    "transform_protocol": "esp",
                    "lifetime_seconds": 3600,
                    "encapsulation_mode": "tunnel"
                },
                "created_at": "2025-03-17T12:25:21.369Z",
                "updated_at": "2025-03-17T12:25:21.369Z",
                "enterprise_project_id": "0",
                "ha_role": "master",
                "tags": [],
                "policy_rules_v6": [],
                "eip_id": "e4d7930f-7038-4cbf-b78a-b004416c7485",
                "type": "ROUTE",
                "route_mode": "STATIC"
            },
            "request_id": "62dc155a7353037f0a1ccc569016a3e9"
        }
        ```
        
       
    
 
