This topic describes how to use Data Online Migration SDK to manage agents.
Create an agent
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"
)
/** We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of resources within your account may be 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 endpoint. In this example, the endpoint of the China (Beijing) region is used.
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 {
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"
)
/** We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of resources within your account may be 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 endpoint. In this example, the endpoint of 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 {
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
}
// An agent runs as expected only if the value of the status parameter is OK.
fmt.Println(string(jsonBytes))
}
Sample success responses
{
"ImportAgentStatus": {
"Status": "OK"
}
}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"
)
/** We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of resources within your account may be 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 endpoint. In this example, the endpoint of 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 {
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"
)
/** We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of resources within your account may be 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 endpoint. In this example, the endpoint of 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 {
log.Printf("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: &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"
)
/** We recommend that you do not save the AccessKey ID and the AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of resources within your account may be 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 endpoint. In this example, the endpoint of 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 {
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 an agent is created, you can create a data address. For more information, see Manage data addresses.