All Products
Search
Document Center

Data Online Migration:Manage agents

Last Updated:Nov 25, 2024

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.

The following sample code shows how to create an agent:

package main

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

/** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** Specify the ID of your Alibaba Cloud account. */
var userId = "11470***876***55";

func main()  {
   // Specify the tunnel ID. 
   tunnelId := "ab31d1f9-****-4f62-****-914e4b2f78c7"
   // Specify the region. In this example, the China (Beijing) region is selected. 
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // Specify the agent name. 
   agentName := "exampleagent"
   // Specify the network type. If you use the Internet, set the parameter to public. If you use Express Connect or VPN Gateway, set the parameter to vpc. 
   agentEndpoint := "public"
   // Specify the deployment method. Set the value to default. 
   deployMethod := "default"
   
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil {
      fmt.Errorf("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 {
      fmt.Errorf("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"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

/** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** Specify the ID of your Alibaba Cloud account. */
var userId = "11470***876***55";

func main()  {
   // In this example, the endpoint in the China (Beijing) region is used. 
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // Specify the agent name. 
   agentName := "exampleagent"
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   resp, err := client.GetAgentStatus(&userId, &agentName)
   if err != nil{
      fmt.Errorf("get client status failed, %v", err)
      return
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   // An agent runs as expected only if the value of the status parameter is OK. 
   fmt.Println(string(jsonBytes))
}

Sample success response

{
  "ImportAgentStatus": {
    "Status": "OK"
  }
}

Query the details of an agent

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

package main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

/** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** Specify the ID of your Alibaba Cloud account. */
var userId = "11470***876***55";

func main()  {
   // In this example, the endpoint in the China (Beijing) region is used. 
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // Specify the agent name. 
   agentName := "exampleagent"
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   resp, err := client.GetAgent(&userId, &agentName)
   if err != nil{
      fmt.Errorf("get agent failed, %v", err)
      return
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

Sample success response

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

Query a list of agents

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

package main

import (
   "encoding/json"
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

/** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** Specify the ID of your Alibaba Cloud account. */
var userId = "11470***876***55";

func main()  {
   // In this example, the endpoint in the China (Beijing) region is used. 
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   // Specify the marker and count parameters based on your business requirements. 
   count := int32(1)
   marker := ""
   resp, err := client.ListAgent(&userId, &mgwpackage.ListAgentRequest{
      &count, &marker,
   })
   if err != nil{
      fmt.Errorf("list agent failed, %v", err)
      return
   }
   jsonBytes, err := json.Marshal(resp)
   if err != nil {
      fmt.Println("covert failed,", err)
      return
   }
   fmt.Println(string(jsonBytes))
}

Sample success response

{
  "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 (
   "fmt"
   openapipackage "github.com/alibabacloud-go/darabonba-openapi/v2/client"
   mgwpackage "github.com/alibabacloud-go/hcs-mgw-20240626/client"
   "os"
)

/** We recommend that you do not hard-code the AccessKey ID or AccessKey secret into your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account is compromised. */
var accessKeyId = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
var accessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
/** Specify the ID of your Alibaba Cloud account. */
var userId = "11470***876***55";

func main()  {
   // In this example, the endpoint in the China (Beijing) region is used. 
   endpoint := "cn-beijing.mgw.aliyuncs.com"
   // Specify the agent name. 
   agentName := "exampleagent"
   config := openapipackage.Config{
      AccessKeyId:     &accessKeyId,
      AccessKeySecret: &accessKeySecret,
      Endpoint:        &endpoint,
   }
   client, err := mgwpackage.NewClient(&config)
   if err != nil{
      fmt.Errorf("create client failed, %v", err)
      return
   }
   _, err = client.DeleteAgent(&userId, &agentName)
   if err != nil{
      fmt.Errorf("delete agent failed, %v", err)
      return
   }
}

What to do next

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