This topic describes how to create a Global Traffic Manager instance by calling API operations.
Requirement
Instance creation and renewal use the Alibaba Cloud Billing API, so you need to follow the Alibaba Cloud Billing API specifications when making calls.
Instance creation service
For more information, see Instance creation service.
SDK
Maven dependency:
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-bssopenapi</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
</dependencies>For other languages, see SDK list.
For client initialization, see: Install the new Java SDK.
For the China site (aliyun.com), the RegionId is fixed as cn-hangzhou. For the international site (alibabacloud.com), the RegionId is fixed as ap-southeast-1.
Create a Global Traffic Manager instance
Description: Use CreateInstance to create a Global Traffic Manager instance.
API operation name: CreateInstance
API reference document: Instance creation service.
Parameters
Name | Type | Required | Description | Details |
ProductCode | String | Yes | Product code | Product code, value: dns |
ProductType | String | Yes | Product type | Product code, values:
|
SubscriptionType | String | Yes | Billing method | The billing method. Valid value:
|
Period | Integer | Yes | Subscription period | When creating a subscription instance, this parameter is required. Unit: month. For annual payment products, enter multiples of 12. Examples:
|
RenewalStatus | String | No | Auto-renewal status |
|
RenewPeriod | Integer | No | The auto-renewal period. Unit: months. | This parameter is required when RenewalStatus is set to AutoRenewal. Examples:
|
Parameter.1.Value | String | Yes | Value of the purchased package version |
|
Parameter.1.Code | String | Yes | Package version | PackageEdition |
Parameter.2.Value | String | Yes | Number of additional health check task packages | The instance version already includes 100 health check tasks by default. Generally, there is no need to purchase additional tasks. Each health check task package contains 100 health check tasks. Examples:
Note Minimum value: 0, Step: 100, Maximum value: 100000. |
Parameter.2.Code | String | Yes | Probing task package | HealthcheckTaskCount |
Parameter.4.Value | String | Yes | Value of the purchased package version | Currently, both old and new versions of instances are available for purchase in the control tower. We recommend purchasing Global Traffic Manager 3.0, which offers more comprehensive capabilities. Valid values:
|
Parameter.4.Code | String | Yes | Instance version | PaymentMode |
Code examples
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* description# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
from typing import List
from alibabacloud_bssopenapi20171214.client import Client as BssOpenApi20171214Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_bssopenapi20171214 import models as bss_open_api_20171214_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client() -> BssOpenApi20171214Client:
"""
Use your AccessKey ID and AccessKey secret to initialize the client.
@return: Client
@throws Exception
"""
# If the project code is leaked, the AccessKey pair could be exposed, potentially compromising the security of all resources in your account. The following code is for reference only.
# For security purposes, we recommend that you use temporary access credentials that are provided by Security Token Service (STS). For more information, see https://www.alibabacloud.com/help/en/sdk/developer-reference/v2-manage-python-access-credentials.
config = open_api_models.Config(
# Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
access_key_id=os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'],
# Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
access_key_secret=os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
)
# For the endpoint, see https://api.aliyun.com/product/BssOpenApi
config.endpoint = f'business.aliyuncs.com'
return BssOpenApi20171214Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
client = Sample.create_client()
parameter_0 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='standard',
code='PackageEdition'
)
parameter_1 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='100',
code='HealthcheckTaskCount'
)
parameter_2 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='1000',
code='SmsNotificationCount'
)
parameter_3 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='subscription3.0',
code='PaymentMode'
)
create_instance_request = bss_open_api_20171214_models.CreateInstanceRequest(
product_code='dns',
product_type='dns_gtm_public_cn',
subscription_type='Subscription',
period=12,
renewal_status='AutoRenewal',
parameter=[
parameter_0,
parameter_1,
parameter_2,
parameter_3
],
renew_period=12
)
runtime = util_models.RuntimeOptions()
try:
# After you copy and run the sample code, obtain the return value of the operation.
client.create_instance_with_options(create_instance_request, runtime)
except Exception as error:
# Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, an error message is printed for reference only.
# The error message.
print(error.message)
# The URL of the corresponding error diagnostics page.
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
client = Sample.create_client()
parameter_0 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='standard',
code='PackageEdition'
)
parameter_1 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='100',
code='HealthcheckTaskCount'
)
parameter_2 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='1000',
code='SmsNotificationCount'
)
parameter_3 = bss_open_api_20171214_models.CreateInstanceRequestParameter(
value='subscription3.0',
code='PaymentMode'
)
create_instance_request = bss_open_api_20171214_models.CreateInstanceRequest(
product_code='dns',
product_type='dns_gtm_public_cn',
subscription_type='Subscription',
period=12,
renewal_status='AutoRenewal',
parameter=[
parameter_0,
parameter_1,
parameter_2,
parameter_3
],
renew_period=12
)
runtime = util_models.RuntimeOptions()
try:
# After you copy and run the sample code, obtain the return value of the operation.
await client.create_instance_with_options_async(create_instance_request, runtime)
except Exception as error:
# Handle exceptions with caution in your actual business scenario and do not ignore exceptions in your project. In this example, an error message is printed for reference only.
# The error message.
print(error.message)
# The URL of the corresponding error diagnostics page.
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])// This file is auto-generated, don't edit it. Thanks.
package main
import (
"encoding/json"
"strings"
"fmt"
"os"
bssopenapi20171214 "github.com/alibabacloud-go/bssopenapi-20171214/v5/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
// Description:
//
// Use your AccessKey ID and AccessKey secret to initialize the client
//
// @return Client
//
// @throws Exception
func CreateClient () (_result *bssopenapi20171214.Client, _err error) {
// If the project code is leaked, the AccessKey pair could be exposed, potentially compromising the security of all resources in your account. The following code is for reference only.
// For enhanced security, we recommend that you use temporary access credentials issued by Security Token Service (STS). For more information, visit https://www.alibabacloud.com/help/en/sdk/developer-reference/v2-manage-access-credentials.
config := &openapi.Config{
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured in the code runtime environment.
AccessKeyId: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured in the code runtime environment.
AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
}
// For the endpoint, see https://api.aliyun.com/product/BssOpenApi
config.Endpoint = tea.String("business.aliyuncs.com")
_result = &bssopenapi20171214.Client{}
_result, _err = bssopenapi20171214.NewClient(config)
return _result, _err
}
func _main (args []*string) (_err error) {
client, _err := CreateClient()
if _err != nil {
return _err
}
parameter0 := &bssopenapi20171214.CreateInstanceRequestParameter{
Value: tea.String("standard"),
Code: tea.String("PackageEdition"),
}
parameter1 := &bssopenapi20171214.CreateInstanceRequestParameter{
Value: tea.String("100"),
Code: tea.String("HealthcheckTaskCount"),
}
parameter2 := &bssopenapi20171214.CreateInstanceRequestParameter{
Value: tea.String("1000"),
Code: tea.String("SmsNotificationCount"),
}
parameter3 := &bssopenapi20171214.CreateInstanceRequestParameter{
Value: tea.String("subscription3.0"),
Code: tea.String("PaymentMode"),
}
createInstanceRequest := &bssopenapi20171214.CreateInstanceRequest{
ProductCode: tea.String("dns"),
ProductType: tea.String("dns_gtm_public_cn"),
SubscriptionType: tea.String("Subscription"),
Period: tea.Int32(12),
RenewalStatus: tea.String("AutoRenewal"),
Parameter: []*bssopenapi20171214.CreateInstanceRequestParameter{parameter0, parameter1, parameter2, parameter3},
RenewPeriod: tea.Int32(12),
}
runtime := &util.RuntimeOptions{}
tryErr := func()(_e error) {
defer func() {
if r := tea.Recover(recover()); r != nil {
_e = r
}
}()
// If you copy and run the sample code, write your own code to display the response of the API operation if necessary.
_, _err = client.CreateInstanceWithOptions(createInstanceRequest, runtime)
if _err != nil {
return _err
}
return nil
}()
if tryErr != nil {
var error = &tea.SDKError{}
if _t, ok := tryErr.(*tea.SDKError); ok {
error = _t
} else {
error.Message = tea.String(tryErr.Error())
}
// Handle exceptions with caution in actual business scenarios and do not ignore the exceptions in your project. The exceptions in this example are for illustrative purposes only.
// The error message
fmt.Println(tea.StringValue(error.Message))
// Provide the URL for troubleshooting.
var data interface{}
d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
d.Decode(&data)
if m, ok := data.(map[string]interface{}); ok {
recommend, _ := m["Recommend"]
fmt.Println(recommend)
}
_, _err = util.AssertAsString(error.Message)
if _err != nil {
return _err
}
}
return _err
}
func main() {
err := _main(tea.StringSlice(os.Args[1:]))
if err != nil {
panic(err)
}
}