
# 创建并管理环境
#### 操作场景
环境是用于组件部署和运行的计算（如集群、弹性云服务器 ECS等）、网络（如弹性负载均衡 ELB、弹性IP EIP等）和中间件（如分布式缓存 DCS、云数据库 RDS等）等基础资源的集合。ServiceStage把多种基础资源组合为一个环境，如：开发环境、测试环境、预生产环境和生产环境。按环境维度来管理资源、创建部署组件，可以减少具体基础资源运维管理的复杂性。
下面介绍如何调用[创建环境 - CreateEnvironment](https://support.huaweicloud.com/api-servicestage/servicestage_06_0056.html)API创建环境，再调用[根据环境ID修改环境资源 - ModifyEnvironmentResourceBasedOnTheEnvironmentID](https://support.huaweicloud.com/api-servicestage/servicestage_06_0061.html)API为环境添加其他需要的资源，最后调用[根据环境ID获取环境详细信息 - ObtainEnvironmentDetailsBasedOnTheEnvironmentID](https://support.huaweicloud.com/api-servicestage/servicestage_06_0060.html)API根据环境ID查询环境详细信息。
API的调用方法请参见[如何调用API](https://support.huaweicloud.com/api-servicestage/servicestage_06_0008.html)。
#### 前提条件
- 已调用[获取用户Token](https://support.huaweicloud.com/api-iam/iam_30_0001.html)获取IAM用户的Token，并将token值设置为环境变量：export Token={token}。
- 已获取ServiceStage的终端节点，详细信息请参见[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/ServiceStage)，并用于替换以下操作步骤示例中的{servicestage_endpoint}。
 
#### 操作步骤
1. 调用[创建环境 - CreateEnvironment](https://support.huaweicloud.com/api-servicestage/servicestage_06_0056.html)API创建环境，请求示例如下：
   
   ```
   curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$Token" -X POST "https://{servicestage_endpoint}/v3/{project_id}/cas/environments" -d ' 
   {
       "name": "env-Kubernetes",
       "description": "test environment",
       "enterprise_project_id": "0",
       "charge_mode": "provided",
       "vpc_id": "081c539d-ed09-43b0-81d7-a87603e528b7",
       "labels": [
           {
               "key": "testKey",
               "value": "testValue"
           }
       ],
       "deploy_mode": "container"
   }'
   ```
   返回结果：
   ```
   {
       "id": "9cd33350-5d7f-46ec-ba6d-40ed793f0eb7",
       "name": "env-Kubernetes",
       "description": "test environment",
       "creator": "test_user",
       "enterprise_project_id": "0",
       "vpc_id": "081c539d-ed09-43b0-81d7-a87603e528b7",
       "deploy_mode": "container",
       "vm_cluster_size": 50,
       "labels": [
           {
               "key": "testKey",
               "value": "testValue"
           }
       ],
       "create_time": 1689564968283,
       "update_time": 1689564968283,
       "resources": []
   }
   ```
   记录返回结果中加粗显示的环境ID，用于替换后续示例中的{environment_id}。
   
   
2. 调用[根据环境ID修改环境资源 - ModifyEnvironmentResourceBasedOnTheEnvironmentID](https://support.huaweicloud.com/api-servicestage/servicestage_06_0061.html)API为环境添加资源。
   
   根据您已有的资源和需求，可以将和环境相同VPC下的计算、网络、中间件等多种类型的资源添加到环境中。请求示例如下：
   ```
   curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$Token" -X PUT "https://{servicestage_endpoint}/v3/{project_id}/cas/environments/{environment_id}/resources" -d ' 
   {
       "resources": [
           {
               "id": "d2917177-3436-4e53-a5bc-706c3529e0e7",
               "name": "elb-pu6a",
               "type": "elb"
           }
       ]
   }'
   ```
   返回结果：
   ```
   {
       "resources": [
           {
               "id": "d2917177-3436-4e53-a5bc-706c3529e0e7",
               "type": "elb",
               "name": "elb-pu6a"
           }
       ]
   }
   ```
   
   
3. 调用[根据环境ID获取环境详细信息 - ObtainEnvironmentDetailsBasedOnTheEnvironmentID](https://support.huaweicloud.com/api-servicestage/servicestage_06_0060.html)API根据环境ID查询环境详细信息，请求示例如下：
   
   ```
   curl -k -H "Content-Type:application/json;charset=utf8" -H "X-Auth-Token:$Token" -X GET "https://{servicestage_endpoint}/v3/{project_id}/cas/environments/{environment_id}"
   ```
   返回结果：
   ```
   {
       "id": "9cd33350-5d7f-46ec-ba6d-40ed793f0eb7",
       "name": "env-Kubernetes",
       "description": "test environment",
       "creator": "test_user",
       "enterprise_project_id": "0",
       "vpc_id": "081c539d-ed09-43b0-81d7-a87603e528b7",
       "deploy_mode": "container",
       "vm_cluster_size": 50,
       "labels": [
           {
               "key": "testKey",
               "value": "testValue"
           }
       ],
       "create_time": 1689564968283,
       "update_time": 1689574181240,
       "resources": [
           {
               "id": "d2917177-3436-4e53-a5bc-706c3529e0e7",
               "type": "elb",
               "name": "elb-pu6a"
           }
       ]
   }
   ```
   
   
 
