All Products
Search
Document Center

Data Online Migration:Manage agents

Last Updated:Dec 11, 2025

This topic describes how to use Data Online Migration SDK to manage agents.

Create an agent

Note

Data Online Migration SDK does not automatically deploy an agent. Before you use an agent, you must deploy the agent. For more information, see Manage agents. An agent is required only when you migrate data over an Express Connect circuit or a VPN gateway. You do not need to create agents when you migrate data over the Internet.

The following sample code shows how to create an agent:

package main

import (
    "log"
    "os"

    openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
)

/** Do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey pair leak and compromise the security of all resources in your account. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

/** Enter the ID of your Alibaba Cloud account. */
var userId = "11470***876***55"

func main() {
    // Enter the channel ID.
    tunnelId := "ab31d1f9-****-4f62-****-914e4b2f78c7"
    // This example uses the China (Beijing) region.
    endpoint := "cn-beijing.mgw.aliyuncs.com"
    // Enter the agent name.
    agentName := "exampleagent"
    // Specify the network type. Enter public for the public network. Enter vpc for an Express Connect circuit or VPN.
    agentEndpoint := "public"
    // Specify the deployment method. Currently, only default is supported.
    deployMethod := "default"
    // Valid values: HTTPS and HTTP. If you do not specify this parameter, HTTPS is used by default.
    protocol := "http"

    config := openapipackage.Config{
        AccessKeyId:     &accessKeyId,
        AccessKeySecret: &accessKeySecret,
        Endpoint:        &endpoint,
        Protocol:        &protocol,
    }
    client, err := mgwpackage.NewClient(&config)
    if err != nil {
        log.Printf("create client failed: %v", err)
        return
    }
    info := mgwpackage.CreateAgentInfo{
        AgentEndpoint: &agentEndpoint,
        DeployMethod:  &deployMethod,
        Name:          &agentName,
        TunnelId:      &tunnelId,
    }
    _, err = client.CreateAgent(&userId, &mgwpackage.CreateAgentRequest{ImportAgent: &info})
    if err != nil {
        log.Printf("create agent failed: %v", err)
        return
    }
}

Query the status of an agent

The following sample code shows how to query the status of an agent:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "os"

    openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
)

/** Do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey pair leak and compromise the security of all resources in your account. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

/** Enter the ID of your Alibaba Cloud account. */
var userId = "11470***876***55"

func main() {
    // This example uses the China (Beijing) region.
    endpoint := "cn-beijing.mgw.aliyuncs.com"
    // Enter the agent name.
    agentName := "exampleagent"
    // Valid values: HTTPS and HTTP. If you do not specify this parameter, HTTPS is used by default.
    protocol := "http"
    config := openapipackage.Config{
        AccessKeyId:     &accessKeyId,
        AccessKeySecret: &accessKeySecret,
        Endpoint:        &endpoint,
        Protocol:        &protocol,
    }
    client, err := mgwpackage.NewClient(&config)
    if err != nil {
        log.Printf("create client failed: %v", err)
        return
    }
    resp, err := client.GetAgentStatus(&userId, &agentName)
    if err != nil {
        log.Printf("get client status failed: %v", err)
        return
    }
    jsonBytes, err := json.Marshal(resp)
    if err != nil {
        log.Printf("covert failed: %v", err)
        return
    }
    // If the status field is OK, the agent is running as expected. Any other value indicates that the agent is abnormal.
    fmt.Println(string(jsonBytes))
}

Sample success responses

{
  "ImportAgentStatus": {
    "Status": "OK",
    "AgentIP": "192.168.0.2",
    "AgentVersion": "1.6.0"
  }
}

Query the information about an agent

The following sample code shows how to query the information about an agent:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "os"

    openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
)

/** Do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey pair leak and compromise the security of all resources in your account. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

/** Enter the ID of your Alibaba Cloud account. */
var userId = "11470***876***55"

func main() {
    // This example uses the China (Beijing) region.
    endpoint := "cn-beijing.mgw.aliyuncs.com"
    // Enter the agent name.
    agentName := "exampleagent"
     // Valid values: HTTPS and HTTP. If you do not specify this parameter, HTTPS is used by default.
    protocol := "http"
    config := openapipackage.Config{
        AccessKeyId:     &accessKeyId,
        AccessKeySecret: &accessKeySecret,
        Endpoint:        &endpoint,
        Protocol:        &protocol,
    }
    client, err := mgwpackage.NewClient(&config)
    if err != nil {
        log.Printf("create client failed: %v", err)
        return
    }
    resp, err := client.GetAgent(&userId, &agentName)
    if err != nil {
        log.Printf("get agent failed: %v", err)
        return
    }
    jsonBytes, err := json.Marshal(resp)
    if err != nil {
        log.Printf("covert failed: %v", err)
        return
    }
    fmt.Println(string(jsonBytes))
}

Sample success responses

{
  "ImportAgent": {
    "Owner": "test_owner",
    "Name": "test_name",
    "CreateTime": "2024-05-01T12:00:00.000Z",
    "ModifyTime": "2024-05-01T12:00:00.000Z",
    "DeployMethod": "default",
    "AgentEndpoint": "vpc",
    "ActivationKey": "6af62558-970d-4f44-8663-4e297170fd6a",
    "Tags": "K1:V1,K2:V2",
    "Version": "test_agent_id",
    "TunnelId": "test_tunnel_id"
  }
}

List agents

The following sample code shows how to list all the agents within an Alibaba Cloud account:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "os"

    openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
)

/** Do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey pair leak and compromise the security of all resources in your account. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

/** Enter the ID of your Alibaba Cloud account. */
var userId = "11470***876***55"

func main() {
    // This example uses the China (Beijing) region.
    endpoint := "cn-beijing.mgw.aliyuncs.com"
    // Valid values: HTTPS and HTTP. If you do not specify this parameter, HTTPS is used by default.
    protocol := "http"
    config := openapipackage.Config{
        AccessKeyId:     &accessKeyId,
        AccessKeySecret: &accessKeySecret,
        Endpoint:        &endpoint,
        Protocol:        &protocol,
    }
    client, err := mgwpackage.NewClient(&config)
    if err != nil {
        log.Printf("create client failed: %v", err)
        return
    }
    // Enter the marker and count as needed.
    count := int32(1)
    marker := ""
    resp, err := client.ListAgent(&userId, &mgwpackage.ListAgentRequest{
        Count: &count, Marker: &marker,
    })
    if err != nil {
        log.Printf("list agent failed: %v", err)
        return
    }
    jsonBytes, err := json.Marshal(resp)
    if err != nil {
        log.Printf("covert failed: %v", err)
        return
    }
    fmt.Println(string(jsonBytes))
}

Sample success responses

{
  "ImportAgentList": {
    "Truncated": true,
    "NextMarker": "test_next_marker",
    "ImportAgent": [
      {
        "Owner": "test_owner",
        "Name": "test_name",
        "CreateTime": "2024-05-01T12:00:00.000Z",
        "ModifyTime": "2024-05-01T12:00:00.000Z",
        "DeployMethod": "default",
        "AgentEndpoint": "vpc",
        "ActivationKey": "6af62558-970d-4f44-8663-4e297170fd6a",
        "Tags": "K1:V1,K2:V2",
        "Version": "test_agent_id",
        "TunnelId": "test_tunnel_id"
      }
    ]
  }
}

Delete an agent

The following sample code shows how to delete an agent:

package main

import (
    "log"
    "os"

    openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
)

/** Do not save the AccessKey ID and AccessKey secret in your project code. This can lead to an AccessKey pair leak and compromise the security of all resources in your account. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")

/** Enter the ID of your Alibaba Cloud account. */
var userId = "11470***876***55"

func main() {
    // This example uses the China (Beijing) region.
    endpoint := "cn-beijing.mgw.aliyuncs.com"
    // Enter the agent name.
    agentName := "exampleagent"
    // Valid values: HTTPS and HTTP. If you do not specify this parameter, HTTPS is used by default.
    protocol := "http"
    config := openapipackage.Config{
        AccessKeyId:     &accessKeyId,
        AccessKeySecret: &accessKeySecret,
        Endpoint:        &endpoint,
        Protocol:        &protocol,
    }
    client, err := mgwpackage.NewClient(&config)
    if err != nil {
        log.Printf("create client failed: %v", err)
        return
    }
    _, err = client.DeleteAgent(&userId, &agentName)
    if err != nil {
        log.Printf("delete agent failed: %v", err)
        return
    }
}

What to do next

After you create an agent, you can create a data address. For more information, see Data addresses.