Help Center/ ServiceStage/ API Reference/ Examples/ Creating and Managing an Environment
Updated on 2024-10-16 GMT+08:00

Creating and Managing an Environment

Scenarios

An environment is a collection of basic compute (such as CCE and ECS), network (such as ELB and EIP), and middleware (such as DCS and RDS) resources, used for component deployment and running. ServiceStage combines multiple basic resources into an environment, including development, test, pre-production, and production environments. Managing resources and deploying components by environment simplifies O&M management.

The following describes how to call the APIs for Creating an Environment, Modifying an Environment Resource Based on the Environment ID, and Obtaining Environment Details Based on the Environment ID.

For details about how to call APIs, see Calling APIs.

Prerequisites

  • You have obtained the IAM user token by calling the API for obtaining a user token, and set the token to an environment variable, that is, export Token={token}.
  • You have obtained the ServiceStage endpoint to replace {servicestage_endpoint} in the following procedure. For details, see Regions and Endpoints.

Procedure

  1. Call the API for Creating an Environment to create an environment. The following is a request example:

    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"
    }'
    Information similar to the following is displayed.
    {
        "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": []
    }

    Record the environment ID in bold in the command output to replace {environment_id} in subsequent examples.

  2. Call the API for Modifying an Environment Resource Based on the Environment ID to add resources to the environment.

    You can add multiple types of resources, such as computing, network, and middleware resources, in the same VPC to the environment. The following is a request example:
    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"
            }
        ]
    }'
    Information similar to the following is displayed.
    {
        "resources": [
            {
                "id": "d2917177-3436-4e53-a5bc-706c3529e0e7",
                "type": "elb",
                "name": "elb-pu6a"
            }
        ]
    }

  3. Call the API for Obtaining Environment Details Based on the Environment ID to query environment details based on the environment ID. The following is a request example:

    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}"

    Information similar to the following is displayed.

    {
        "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"
            }
        ]
    }