更新时间:2024-03-21 GMT+08:00

创建迁移任务

功能介绍

根据源端服务器创建一个迁移任务。

调用方法

请参见如何调用API

URI

POST /v3/tasks

请求参数

表1 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户Token。通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。

最小长度:1

最大长度:16384

表2 请求Body参数

参数

是否必选

参数类型

描述

name

String

任务名称

最小长度:0

最大长度:255

type

String

任务类型 MIGRATE_FILE:文件级迁移 MIGRATE_BLOCK:块级迁移

枚举值:

  • MIGRATE_FILE

  • MIGRATE_BLOCK

start_target_server

Boolean

迁移后是否启动目的端虚拟机

缺省值:true

os_type

String

操作系统类型

最小长度:0

最大长度:255

source_server

SourceServerByTask object

源端服务器信息

target_server

TargetServerByTask object

目的端虚拟机信息

migration_ip

String

迁移IP,如果是自动创建虚拟机,不需要此参数

最小长度:0

最大长度:255

region_name

String

region的名称

最小长度:0

最大长度:255

region_id

String

region ID

最小长度:0

最大长度:255

project_name

String

项目名称

最小长度:0

最大长度:255

project_id

String

项目ID

最小长度:0

最大长度:255

vm_template_id

String

自动创建虚拟机使用模板

最小长度:0

最大长度:255

use_public_ip

Boolean

是否使用公网ip

缺省值:true

syncing

Boolean

复制或者同步后是否会继续持续同步,不添加则默认是false

缺省值:false

exist_server

Boolean

是否存在服务,如果存在,则创建任务

start_network_check

Boolean

是否开启网络检测

表3 SourceServerByTask

参数

是否必选

参数类型

描述

id

String

源端服务器ID

最小长度:0

最大长度:255

表4 TargetServerByTask

参数

是否必选

参数类型

描述

btrfs_list

Array of BtrfsFileSystem objects

btrfs信息,数据从源端获取

数组长度:0 - 65535

disks

Array of TargetDisks objects

磁盘信息

数组长度:0 - 65535

name

String

名称

最小长度:0

最大长度:255

vm_id

String

虚拟机ID

最小长度:0

最大长度:255

volume_groups

Array of VolumeGroups objects

卷组,数据从源端获取

数组长度:0 - 65535

表5 BtrfsFileSystem

参数

是否必选

参数类型

描述

name

String

文件系统名称

最小长度:0

最大长度:255

label

String

文件系统标签,若无标签为空字符串

最小长度:0

最大长度:255

uuid

String

文件系统的uuid

最小长度:0

最大长度:255

device

String

btrfs包含的设备名称

最小长度:0

最大长度:255

size

Long

文件系统数据占用大小

最小值:0

最大值:9223372036854775807

nodesize

Long

btrfs节点大小

最小值:0

最大值:9223372036854775807

sectorsize

Integer

扇区大小

最小值:0

最大值:2147483647

data_profile

String

数据配置(RAD)

最小长度:0

最大长度:255

system_profile

String

文件系统配置(RAD)

最小长度:0

最大长度:255

metadata_profile

String

元数据配置(RAD)

最小长度:0

最大长度:255

global_reserve1

String

Btrfs文件系统信息

最小长度:0

最大长度:255

g_vol_used_size

Long

Btrfs卷已使用空间大小

最小值:0

最大值:9223372036854775807

default_subvolid

String

默认子卷ID

最小长度:0

最大长度:255

default_subvol_name

String

默认子卷名称

最小长度:0

最大长度:255

default_subvol_mountpath

String

默认子卷挂载路径/BTRFS文件系统的挂载路径

最小长度:0

最大长度:255

subvolumn

Array of BtrfsSubvolumn objects

子卷信息

数组长度:0 - 65535

表6 BtrfsSubvolumn

参数

是否必选

参数类型

描述

uuid

String

父卷的uuid

最小长度:0

最大长度:255

is_snapshot

String

子卷是否为快照

最小长度:0

最大长度:255

subvol_id

String

子卷的ID

最小长度:0

最大长度:255

parent_id

String

父卷ID

最小长度:0

最大长度:255

subvol_name

String

子卷的名称

最小长度:0

最大长度:255

subvol_mount_path

String

子卷的挂载路径

最小长度:0

最大长度:255

表7 TargetDisks

参数

是否必选

参数类型

描述

device_use

String

磁盘类型,普通磁盘,OS所在磁盘,BOOT所在磁盘 BOOT:BOOT设备 OS:系统设备 NORMAL:平常

缺省值:NORMAL

枚举值:

  • NORMAL

  • OS

  • BOOT

disk_id

String

磁盘ID,自动创建虚拟机不用设置

最小长度:0

最大长度:255

name

String

名称,根据磁盘顺序设置为disk X

最小长度:0

最大长度:255

physical_volumes

Array of PhysicalVolumes objects

物理卷信息

数组长度:0 - 65535

size

Long

大小

最小值:0

最大值:9223372036854775807

used_size

Long

使用大小

最小值:0

最大值:9223372036854775807

表8 PhysicalVolumes

参数

是否必选

参数类型

描述

device_use

String

分区类型,普通分区,启动分区,系统分区

最小长度:0

最大长度:255

file_system

String

文件系统类型

最小长度:0

最大长度:255

index

Integer

顺序

最小值:0

最大值:2147483647

mount_point

String

挂载点

最小长度:0

最大长度:255

name

String

名称,windows表示盘符,Linux表示设备号

最小长度:0

最大长度:255

size

Long

大小

最小值:0

最大值:9223372036854775807

inode_size

Long

inode数量

最小值:0

最大值:9223372036854775807

used_size

Long

使用大小

最小值:0

最大值:9223372036854775807

uuid

String

GUID,可从源端查询

最小长度:0

最大长度:255

表9 VolumeGroups

参数

是否必选

参数类型

描述

components

String

Pv信息

最小长度:0

最大长度:255

free_size

Long

剩余空间

最小值:0

最大值:9223372036854775807

logical_volumes

Array of LogicalVolumes objects

lv信息

数组长度:0 - 255

name

String

名称

最小长度:0

最大长度:255

size

Long

大小

最小值:0

最大值:9223372036854775807

表10 LogicalVolumes

参数

是否必选

参数类型

描述

block_count

Integer

块数量

最小值:0

最大值:4294967296

缺省值:0

block_size

Long

块大小

最小值:0

最大值:1048576

缺省值:0

file_system

String

文件系统

最小长度:0

最大长度:255

inode_size

Integer

inode数量

最小值:0

最大值:4294967296

device_use

String

分区类型,普通分区,启动分区,系统分区

最小长度:0

最大长度:255

mount_point

String

挂载点

最小长度:0

最大长度:256

name

String

名称

最小长度:0

最大长度:1024

size

Long

大小

最小值:0

最大值:9223372036854775807

used_size

Long

使用大小

最小值:0

最大值:9223372036854775807

free_size

Long

剩余空间

最小值:0

最大值:9223372036854775807

响应参数

状态码: 200

表11 响应Body参数

参数

参数类型

描述

id

String

创建成功返回的任务ID

最小长度:0

最大长度:255

请求示例

创建一个迁移任务,迁移任务名称是MigrationTask,迁移类型是MIGRATE_FILE文件级迁移,源端操作系统类型是LINUX,使用公网迁移是true,迁移region名称是region_name,迁移region的ID是region_id。

POST https://{endpoint}/v3/tasks

{
  "name" : "MigrationTask",
  "type" : "MIGRATE_FILE",
  "os_type" : "LINUX",
  "start_target_server" : true,
  "use_public_ip" : true,
  "migration_ip" : "192.168.0.1",
  "region_name" : "region_name",
  "region_id" : "region_id",
  "project_name" : "project_name",
  "project_id" : "b276e6fb38c74aa2ba4598f049745ce9",
  "source_server" : {
    "id" : "16c03428-ff9a-4f36-84aa-583e41499bb2"
  },
  "target_server" : {
    "vm_id" : "1900aab8-90c4-462c-b4c5-acdafc8fbeca",
    "name" : "Auto-tar-name",
    "disks" : [ {
      "name" : "/dev/vda",
      "disk_id" : "76a59f9f-5901-4913-a9a5-f6742baa308c",
      "size" : 42949672960,
      "used_size" : 429496,
      "device_use" : "BOOT",
      "physical_volumes" : [ {
        "uuid" : null,
        "index" : 0,
        "name" : "/dev/vda1",
        "device_use" : "OS",
        "file_system" : "ext4",
        "mount_point" : "/",
        "size" : 42947575808,
        "used_size" : 5346484224
      } ]
    } ],
    "volume_groups" : [ ]
  }
}

响应示例

状态码: 200

OK

{
  "id" : "8abda8635e09d185015e09d188dd0001xx"
}

SDK代码示例

SDK代码示例如下。

创建一个迁移任务,迁移任务名称是MigrationTask,迁移类型是MIGRATE_FILE文件级迁移,源端操作系统类型是LINUX,使用公网迁移是true,迁移region名称是region_name,迁移region的ID是region_id。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.sms.v3.region.SmsRegion;
import com.huaweicloud.sdk.sms.v3.*;
import com.huaweicloud.sdk.sms.v3.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateTaskSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new GlobalCredentials()
                .withAk(ak)
                .withSk(sk);

        SmsClient client = SmsClient.newBuilder()
                .withCredential(auth)
                .withRegion(SmsRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateTaskRequest request = new CreateTaskRequest();
        PostTask body = new PostTask();
        List<PhysicalVolumes> listDisksPhysicalVolumes = new ArrayList<>();
        listDisksPhysicalVolumes.add(
            new PhysicalVolumes()
                .withDeviceUse("OS")
                .withFileSystem("ext4")
                .withIndex(0)
                .withMountPoint("/")
                .withName("/dev/vda1")
                .withSize(42947575808L)
                .withUsedSize(5346484224L)
        );
        List<TargetDisks> listTargetServerDisks = new ArrayList<>();
        listTargetServerDisks.add(
            new TargetDisks()
                .withDeviceUse(TargetDisks.DeviceUseEnum.fromValue("BOOT"))
                .withDiskId("76a59f9f-5901-4913-a9a5-f6742baa308c")
                .withName("/dev/vda")
                .withPhysicalVolumes(listDisksPhysicalVolumes)
                .withSize(42949672960L)
                .withUsedSize(429496L)
        );
        TargetServerByTask targetServerbody = new TargetServerByTask();
        targetServerbody.withDisks(listTargetServerDisks)
            .withName("Auto-tar-name")
            .withVmId("1900aab8-90c4-462c-b4c5-acdafc8fbeca");
        SourceServerByTask sourceServerbody = new SourceServerByTask();
        sourceServerbody.withId("16c03428-ff9a-4f36-84aa-583e41499bb2");
        body.withUsePublicIp(true);
        body.withProjectId("b276e6fb38c74aa2ba4598f049745ce9");
        body.withProjectName("project_name");
        body.withRegionId("region_id");
        body.withRegionName("region_name");
        body.withMigrationIp("192.168.0.1");
        body.withTargetServer(targetServerbody);
        body.withSourceServer(sourceServerbody);
        body.withOsType("LINUX");
        body.withStartTargetServer(true);
        body.withType(PostTask.TypeEnum.fromValue("MIGRATE_FILE"));
        body.withName("MigrationTask");
        request.withBody(body);
        try {
            CreateTaskResponse response = client.createTask(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

创建一个迁移任务,迁移任务名称是MigrationTask,迁移类型是MIGRATE_FILE文件级迁移,源端操作系统类型是LINUX,使用公网迁移是true,迁移region名称是region_name,迁移region的ID是region_id。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdksms.v3.region.sms_region import SmsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdksms.v3 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = __import__('os').getenv("CLOUD_SDK_AK")
    sk = __import__('os').getenv("CLOUD_SDK_SK")

    credentials = GlobalCredentials(ak, sk) \

    client = SmsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(SmsRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateTaskRequest()
        listPhysicalVolumesDisks = [
            PhysicalVolumes(
                device_use="OS",
                file_system="ext4",
                index=0,
                mount_point="/",
                name="/dev/vda1",
                size=42947575808,
                used_size=5346484224
            )
        ]
        listDisksTargetServer = [
            TargetDisks(
                device_use="BOOT",
                disk_id="76a59f9f-5901-4913-a9a5-f6742baa308c",
                name="/dev/vda",
                physical_volumes=listPhysicalVolumesDisks,
                size=42949672960,
                used_size=429496
            )
        ]
        targetServerbody = TargetServerByTask(
            disks=listDisksTargetServer,
            name="Auto-tar-name",
            vm_id="1900aab8-90c4-462c-b4c5-acdafc8fbeca"
        )
        sourceServerbody = SourceServerByTask(
            id="16c03428-ff9a-4f36-84aa-583e41499bb2"
        )
        request.body = PostTask(
            use_public_ip=True,
            project_id="b276e6fb38c74aa2ba4598f049745ce9",
            project_name="project_name",
            region_id="region_id",
            region_name="region_name",
            migration_ip="192.168.0.1",
            target_server=targetServerbody,
            source_server=sourceServerbody,
            os_type="LINUX",
            start_target_server=True,
            type="MIGRATE_FILE",
            name="MigrationTask"
        )
        response = client.create_task(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建一个迁移任务,迁移任务名称是MigrationTask,迁移类型是MIGRATE_FILE文件级迁移,源端操作系统类型是LINUX,使用公网迁移是true,迁移region名称是region_name,迁移region的ID是region_id。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
    sms "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/sms/v3"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/sms/v3/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/sms/v3/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := global.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := sms.NewSmsClient(
        sms.SmsClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateTaskRequest{}
	deviceUsePhysicalVolumes:= "OS"
	fileSystemPhysicalVolumes:= "ext4"
	indexPhysicalVolumes:= int32(0)
	mountPointPhysicalVolumes:= "/"
	namePhysicalVolumes:= "/dev/vda1"
	sizePhysicalVolumes:= int64(42947575808)
	usedSizePhysicalVolumes:= int64(5346484224)
	var listPhysicalVolumesDisks = []model.PhysicalVolumes{
        {
            DeviceUse: &deviceUsePhysicalVolumes,
            FileSystem: &fileSystemPhysicalVolumes,
            Index: &indexPhysicalVolumes,
            MountPoint: &mountPointPhysicalVolumes,
            Name: &namePhysicalVolumes,
            Size: &sizePhysicalVolumes,
            UsedSize: &usedSizePhysicalVolumes,
        },
    }
	deviceUseDisks:= model.GetTargetDisksDeviceUseEnum().BOOT
	diskIdDisks:= "76a59f9f-5901-4913-a9a5-f6742baa308c"
	var listDisksTargetServer = []model.TargetDisks{
        {
            DeviceUse: &deviceUseDisks,
            DiskId: &diskIdDisks,
            Name: "/dev/vda",
            PhysicalVolumes: listPhysicalVolumesDisks,
            Size: int64(42949672960),
            UsedSize: int64(429496),
        },
    }
	targetServerbody := &model.TargetServerByTask{
		Disks: listDisksTargetServer,
		Name: "Auto-tar-name",
		VmId: "1900aab8-90c4-462c-b4c5-acdafc8fbeca",
	}
	sourceServerbody := &model.SourceServerByTask{
		Id: "16c03428-ff9a-4f36-84aa-583e41499bb2",
	}
	usePublicIpPostTask:= true
	migrationIpPostTask:= "192.168.0.1"
	startTargetServerPostTask:= true
	request.Body = &model.PostTask{
		UsePublicIp: &usePublicIpPostTask,
		ProjectId: "b276e6fb38c74aa2ba4598f049745ce9",
		ProjectName: "project_name",
		RegionId: "region_id",
		RegionName: "region_name",
		MigrationIp: &migrationIpPostTask,
		TargetServer: targetServerbody,
		SourceServer: sourceServerbody,
		OsType: "LINUX",
		StartTargetServer: &startTargetServerPostTask,
		Type: model.GetPostTaskTypeEnum().MIGRATE_FILE,
		Name: "MigrationTask",
	}
	response, err := client.CreateTask(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK

错误码

请参见错误码