• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. Python
  6. MaaS Python SDK Demo

MaaS Python SDK Demo

Object Storage Migration Service (MaaSOBS) online migrates OBS of another cloud service provider to the destination cloud platform. This migration service offers secure object authentication, encrypted data transmission, and reliable interruption recovery.

Obtaining the Service Version

You can obtain the current MaaS service version using the Python OpenStack SDK based on the following code:

def get_version(conn):
    fip_dversion=conn.maas.versions()

Creating a Migration Task

You can create a JSON character string consisting of the migration parameters and then use the JSON string to create a migration task using the Python OpenStack SDK based on the following code. After the migration task is created, it is added to the task queue and waits for execution.

def create_task(conn):
    task_dict = {
        "src_node":
        {
            "region": "us-east-1",
            "ak": "AKIAIwww72JCUBV6Addd",
            "sk": " SKIAIwww72JCUBV6Addd123fuxcnk5",
            "object_key":
            {
                "path": "folder-2/",
                "keys": ["transFiles.file"]
            },
            "bucket": "maas-bucket"
        },
        "thread_num": 5,
        "enableKMS": False,
        "dst_node":
        {
            "region": "dst-region01",
            "ak": "asdusac13UFDHASDK1",
            "sk": "yHid7HDJKajdjsyf87658Ih7DFuHDI",
            "object_key": "destination/",
            "bucket": "dst-bucket"
        },
    }
    g_task = conn.maas.create_task(**task_dict)
Table 1 Parameter description

Parameter

Mandatory

Type

Description

Example Value

region

Yes

String

Specifies the region ID. Note that this parameter is not the region name.

"region": "us-east-1"

ak

Yes

String

Specifies the access key ID. In most cases, the value is a string of 20 characters consisting of digits and letters.

"ak":"AKTAI72RCUBV4AHQO46C"

sk

Yes

String

Specifies the secret access key ID. In most cases, the value is a string of 40 characters consisting of digits and letters.

"sk":"32o6vpFgj76zII1HOad7SrbygHChx9TbwWpDzsHo"

object_key

Yes

JsonString

Specifies the JSON character string of the migration object. In most cases, the value is in the path+keys array.

path indicates the parent path of the object on the source end.

keys indicates the object array in the path.

"object_key":

{

path: "test-01/",

keys: ["10000-files/","rmb001"]

}

bucket

Yes

String

Specifies the bucket name. The value is the name of a bucket created by the provider of the public cloud supporting service migration.

"bucket": "maas-bucket"

src_node

Yes

JsonString

Specifies the JSON character string for encapsulating the source-end information.

"src_node":{}

dst_node

Yes

JsonString

Specifies the JSON character string for encapsulating the destination-end information.

"dst_node":{}

thread_num

No

Integer

Specifies the number of threads used for migration. The default value is 5.

"thread_num":3

description

No

String

Provides supplementary information about the migration task.

"description":"test"

enableKMS

Yes

Boolean

Specifies whether to enable the KMS function to encrypt the object data after the migration.

"enableKMS":True

Pausing a Migration Task

You can pause a migration task using the Python OpenStack SDK based on the following code by specifying the task ID:

def stop_task(conn,taskid):
    conn.maas.stop_task(taskid)
Table 2 Parameter description

Parameter

Mandatory

Type

Description

Example Value

taskid

Yes

String

Specifies the ID of the migration task to be paused. The value is a string of digits.

12345125163

Continuing a Migration Task

You can continue a paused or failed migration task using the Python OpenStack SDK based on the following code by specifying required parameters, including the task ID and the respective AKs and SKs on the source end and destination end:

def start_task(conn,taskid):
    srcak="F8ISHCFJ28DK5KV"
    srcsk="AFASFFQTAS2342566SSDfsfd"
    dstak="SDASVNV8ASYSCJASLF"
    dstsk="Qn6FBZFG1Qf6lo17DASDS234SDVJHAFASFFQ"
    conn.maas.start_task(taskid,srcak,srcsk,dstak,dstsk) 
Table 3 Parameter description

Parameter

Mandatory

Type

Description

Example Value

taskid

Yes

String

Specifies the ID of the migration task to be continued. The value is a string of digits.

12345125163

srcak

Yes

String

Specifies the access key ID on the source end for the migration task. In most cases, the value is a string of 20 characters consisting of digits and letters.

AKTAI72RCUBV4AHQO46C

srcsk

Yes

String

Specifies the secret access key ID on the source end for the migration task. In most cases, the value is a string of 40 characters consisting of digits and letters.

32o6vpFgj76zII1HOad7SrbygHChx9TbwWpDzsHo

dstak

Yes

String

Specifies the access key ID on the destination end for the migration task. In most cases, the value is a string of 20 characters consisting of digits and letters.

KEIIFOFUCVDQJ0E0NW4S

dstsk

Yes

String

Specifies the secret access key ID on the destination end for the migration task. In most cases, the value is a string of 40 characters consisting of digits and letters.

32o6vpOg376zII1HOad7SrbyfHChx9TbwwprzsHN

Deleting a Migration Task

You can delete a migration task that is in the wait state using the Python OpenStack SDK based on the following code by specifying the task ID. When isExpr is set to False, the openstack.exceptions.ResourceNotFound exception will be reported if the task does not exist. On the contrary, the exception will not be reported when isExpr is set to True. The default value is False.

def delete_task(conn,taskid):
    conn.maas.delete_task(taskid,isExpr)
Table 4 Parameter description

Parameter

Mandatory

Type

Description

Example Value

taskid

Yes

String

Specifies the ID of the migration task to be deleted. The value is a string of digits.

12345125163

isExpr

Yes

Boolean

False: If the task does not exist, the openstack.exceptions.ResourceNotFound exception will be reported.

True: The openstack.exceptions.ResourceNotFound exception will not be reported.

False

Querying Details of a Migration Task

You can query details of a migration task using the Python OpenStack SDK based on the following code by specifying the task ID:

def get_task_by_id(conn,taskid):
    task=conn.maas.get_task(taskid)
Table 5 Parameter description

Parameter

Mandatory

Type

Description

Example Value

taskid

Yes

String

Specifies the ID of the migration task to be queried. The value is a string of digits.

12345125163

Querying All Tasks of a Tenant

You can query details of a specified number of migration tasks using the Python OpenStack SDK based on the following code by specifying a JSON character string that consists of three parameters. The first parameter specifies the start number of the migration tasks to be queried, the second parameter specifies the total number of migration tasks to be queried, and the third parameter specifies the status of the migration tasks to be queried. If the task status is not specified, migration tasks of all states will be queried. The following provides an example of querying details 10 latest tasks in the waiting state:

def query_tasks(conn):
    query = {
    'start': '0',
    'limit': '10',
    'state':'3'}
    task[] tasklist = conn.maas.tasks(**query):
Table 6 Parameter description

Parameter

Mandatory

Type

Description

Example Value

start

Yes

Integer

Specifies the start serial number.

"start":1

limit

Yes

Integer

Specifies the maximum number of returned tasks, which cannot exceed 100. Otherwise, the query fails.

"limit":5

state

No

Integer

Specifies the status of the tasks to be queried. If this parameter is left blank, the tasks of all states will be queried. The value can be:

0: indicates initialized tasks.

1 indicates waiting tasks.

2 indicates executed tasks.

3 indicates paused tasks.

4 indicates failed tasks.

5 indicates successful tasks.

"state":'1'

"state":'2'

"state":'3'

"state":'4'

"state":'5'

Querying the Total Number of Migration Tasks

You can query either the total number of migration tasks in a specified state or the total number of migration tasks in all states using the Python OpenStack SDK based on the following code.

Querying the total number of migration tasks in a specified state:

def task_count_by_state(conn,state):
    maas_count=conn.maas.task_count(state)

Querying the total number of migration tasks in all states:

def task_count(conn):
    all_count=conn.maas.task_count()
Table 7 Parameter description

Parameter

Mandatory

Type

Description

Example Value

state

Yes

Integer

Specifies the status of the tasks to be queried. If this parameter is left blank, the tasks of all states will be queried. The value can be:

0: indicates initialized tasks.

1 indicates waiting tasks.

2 indicates executed tasks.

3 indicates paused tasks.

4 indicates failed tasks.

5 indicates successful tasks.

"state":'1'

"state":'2'

"state":'3'

"state":'4'

"state":'5'