在这篇文章中:

    创建云服务器

    功能介绍

    创建一台按需云服务器。

    URI

    POST /v2.1/servers

    接口约束

    1. 网络的三个参数(port、uuid和fixed_ip)中,port优先级最高;指定fixed_ip时必须指明uuid。
    2. 云服务器的系统盘会占用云硬盘的配额。
    3. 用户创建弹性云服务器时在block_device_mapping_v2设置的device_name字段不会生效,系统会默认生成一个device_name。

    请求消息

    请求参数

    参数 是否必选 参数类型 描述
    server Object 云服务器信息,参见表1。

    表1 server参数信息

    参数 是否必选 参数类型 描述
    flavorRef String 规格ID或URL。规格ID示例:standard.4C.8G
    name String 云服务器名称,长度大于0小于256字节。
    block_device_mapping_v2 Array of objects 扩展属性,指定云服务器存储设备的v2接口,参见表3。
    metadata Object 云服务器元数据,参见表2。
    security_groups Array of objects 扩展属性,指定云服务器的安全组,默认为default,参见表4。
    networks Array of objects 扩展属性,指定弹性云服务器的网卡信息,参见表5。
    key_name String 扩展属性,指定keypair的名称。
    description String 扩展属性,表示弹性云服务器描述信息,默认为空字符串。

    表2 metadata字段数据结构说明

    参数 是否必选 参数类型 描述
    cinder_img_volume_type String 云服务器使用镜像的存储类型,目前都为:容量型。
    volume_type String 存储类型,在source_type是image,destination_type是volume时建议填写

    表3 block_device_mapping_v2参数

    参数 参数类型 是否必选 描述
    source_type String 卷设备的源头类型,当前只支持image、snapshot类型。当使用镜像创建云服务器时,source_type设置为image;当使用快照创建云服务器时,source_type设置为snapshot。说明:**当卷设备的源头类型为snapshot时,且boot_index为0,则该快照对应的云硬盘必须为系统盘。
    destination_type String 卷设备的目标类型,当前仅支持volume类型。
    delete_on_termination Boolean 删除云服务器时,是否删除卷,默认值false。true:删除云服务器时,删除卷;false:删除弹性云服务器时,不删除卷
    boot_index String 启动标识,“0”代表启动盘,“-1“代表非启动盘。
    uuid String 当source_type值是snapshot时,uuid为快照的uuid;当source_type值是image时,uuid为镜像的uuid;
    volume_size Integer 卷大小,整数,在source_type是image,destination_type是volume的时候必选。单位为GB。

    表4 security_groups参数

    参数 是否必选 参数类型 描述
    name String 安全组名称或者uuid。

    表5 networks参数

    参数 是否必选 参数类型 描述
    port String 网络port uuid。没有指定网络uuid时必须指定。
    uuid String 网络uuid。没有指定网络port时必须指定。
    fixed_ip String 指定的IP地址。网络的三个参数(port、uuid和fixed_ip)中,port优先级最高;指定fixed_ip时必须指明uuid。

    响应消息

    响应参数

    参数 参数类型 描述
    server Object 云服务器信息,详情请参见表6。

    表6 server字段数据结构说明

    参数 参数类型 描述
    id String 云服务器ID,UUID格式。
    links Array of objects 云服务器URI自描述信息,详情参见表7。
    security_groups Array of objects 云服务器所在安全组,详情参见表8。
    OS-DCF:diskConfig String diskConfig方式。MANUAL,镜像空间不会扩展。AUTO,系统盘镜像空间会自动扩展为与flavor大小一致。
    adminPass String 云服务器的用户密码。

    表7 links字段数据结构说明

    参数 参数类型 描述
    rel String 快捷链接标记名称。
    href String 对应快捷链接。

    表8 security_groups字段数据结构说明

    参数 参数类型 描述
    name String 安全组名称。

    请求示例

    请求URL示例

    1
    POST https://{endpoint}/v2.1/servers

    示例:通过block_device_mapping_v2使用镜像创建弹性云服务器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    {
    "server" : {
    "name" : "guest-create-by-api",
    "flavorRef" : "02723f5f-5518-4031-bd5e-dfecae675606",
    "networks" : [{ "uuid" : "a7e9d02e-0783-4686-bb51-f3496cfea4c6" }],
    "block_device_mapping_v2": [{
    "boot_index": 0,
    "source_type": "image",
    "uuid": "08a865d4-3714-46ff-a633-0a4d38fcffe2",
    "destination_type": "volume",
    "volume_size": "100",
    "delete_on_termination": true
    }],
    "metadata": {
    "cinder_img_volume_type": "bbfdc5ea-f9ca-41f7-96b3-e3c473d20903",
    "volume_type": "bbfdc5ea-f9ca-41f7-96b3-e3c473d20903"
    }
    }
    }

    响应示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    {
    "server": {
    "security_groups": [{
    "name": "default"
    }],
    "OS-DCF:diskConfig": "MANUAL",
    "id": "86212972-a179-4248-b3a1-7e14a8ad821f",
    "links": [{
    "href": "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/86212972-a179-4248-b3a1-7e14a8ad821f",
    "rel": "self"
    }, {
    "href": "https://home.jcloud.sjtu.edu.cn:8774/servers/86212972-a179-4248-b3a1-7e14a8ad821f",
    "rel": "bookmark"
    }],
    "adminPass": "mz2CATAnXXEg"
    }
    }

    返回值

    请参考通用请求返回值