更新时间:2024-09-03 GMT+08:00
分享

创建缓存实例

功能介绍

创建缓存实例,该接口创建的缓存实例支持按需和包周期两种计费方式。

调用方法

请参见如何调用API

URI

POST /v2/{project_id}/instances

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

项目ID。获取方法请参见获取项目ID

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

name

String

实例名称。

由英文字符开头,只能由英文字母、数字、中划线和下划线组成。

创建单个实例时,名称长度为4到64位的字符串。批量创建实例时,名称长度为4到56位的字符串,且实例名称格式为“自定义名称-n”,其中n从000开始,依次递增。例如,批量创建两个实例,自定义名称为dcs_demo,则两个实例的名称为dcs_demo-000和dcs_demo-001。

engine

String

缓存引擎:Redis。

engine_version

String

缓存版本。

Redis缓存引擎,取值为4.0、5.0、6.0。

capacity

Float

缓存容量(G Byte)

  • Redis4.0、Redis5.0和Redis6.0基础版:单机和主备类型实例取值:0.125、0.25、0.5、1、2、4、8、16、24、32、48、64。集群实例规格支持4、8、16、24、32、48、64、96、128、192、256、384、512、768、1024。读写分离实例规格8、16、32。

spec_code

String

产品规格编码。具体查询方法如下:

  • 方法一:登录分布式缓存的控制台,在创建缓存实例页面,查找对应的实例规格名称。

  • 方法二:调用查询产品规格接口查询。

az_codes

Array of strings

创建缓存节点到指定且有资源的可用区Code。具体查询方法,请参考查询可用区信息,在查询时,请注意查看该可用区是否有资源。

如果是创建主备、Proxy集群、Cluster集群实例,支持跨可用区部署,可以为备节点指定备可用区。在为节点指定可用区时,用逗号分隔开,具体请查看示例。

vpc_id

String

虚拟私有云ID。

获取方法如下:

  • 方法1:登录虚拟私有云服务的控制台界面,在虚拟私有云的详情页面查找VPC ID。

  • 方法2:通过虚拟私有云服务的查询VPC列表API接口查询。

subnet_id

String

子网的网络ID。

获取方法如下:

  • 方法1:登录虚拟私有云服务的控制台界面,单击VPC下的子网,进入子网详情页面,查找网络ID。

  • 方法2:通过虚拟私有云服务的查询子网列表API接口查询。

security_group_id

String

指定实例所属的安全组。

当engine为Redis且engine_version为4.0/5.0/6.0时,该参数为可选。Redis4.0、Redis5.0、Redis6.0版本基础版实例不支持安全组控制访问(会设置为默认参数值securityGroupId),只支持白名单控制。

获取方法如下:

  • 方法1:登录虚拟私有云服务的控制台界面,在安全组的详情页面查找安全组ID。

  • 方法2:通过虚拟私有云服务的查询安全组列表API接口查询。

publicip_id

String

Redis缓存实例绑定的弹性IP地址的id。

如果开启了公网访问功能(即enable_publicip为true),该字段为必选。

enterprise_project_id

String

企业项目ID。

enterprise_project_name

String

企业项目名称。

description

String

实例的描述信息。

长度不超过1024的字符串。

说明:

\与"在json报文中属于特殊字符,如果参数值中需要显示\或者"字符,请在字符前增加转义字符\,比如\或者"。

enable_ssl

Boolean

Redis 3.0缓存实例开启公网访问功能时,是否选择支持ssl。该参数仅针对Redis 3.0实例,其他版本不涉及。

  • true:开启

  • false:不开启

private_ip

String

创建缓存实例手动指定的IP地址。

instance_num

Integer

表示批量创建缓存实例时,创建的实例个数。

默认值:1

取值范围:1-100

maintain_begin

String

维护时间窗开始时间,为UTC时间,格式为HH:mm:ss

  • 维护时间窗开始和结束时间必须为指定的时间段,可参考查询维护时间窗时间段获取。

  • 开始时间必须为18:00:00、19:00:00等。

  • 该参数不能单独为空,若该值为空,则结束时间也为空。系统分配一个默认开始时间18:00:00。

maintain_end

String

维护时间窗结束时间,为UTC时间,格式为HH:mm:ss。

  • 维护时间窗开始和结束时间必须为指定的时间段,可参考查询维护时间窗时间段获取。

  • 结束时间在开始时间基础上加一个小时,即当开始时间为18:00:00时,结束时间为19:00:00。

  • 该参数不能单独为空,若该值为空,则开始时间也为空,系统分配一个默认结束时间19:00:00。

password

String

缓存实例的认证信息。

说明:

当“no_password_access”配置为“false”或未配置时,请求消息中须包含password参数。

Redis类型的缓存实例密码复杂度要求:

  • 输入长度为8到64位的字符串。

  • 不能使用正序或逆序用户名作为密码。

  • 必须包含如下四种字符中的三种组合:

    • 小写字母

    • 大写字母

    • 数字

    • 特殊字符包括(`~!@#$^&*()-_=+\|{}'",<.>/?)

no_password_access

Boolean

是否允许免密码访问缓存实例。

  • true:该实例无需密码即可访问。

  • false:该实例必须通过密码认证才能访问。

    若未配置该参数则默认值为“false”。

bss_param

BssParam object

表示计费模式的相关参数。置空时,默认计费模式为按需计费;否则是包周期方式。

instance_backup_policy

BackupPolicy object

备份策略,实例类型为主备和集群时支持。

tags

Array of ResourceTag objects

实例标签键值。

access_user

String

缓存类型为Redis时,不需要设置,保持为空即可。

enable_publicip

Boolean

Redis 3.0缓存实例是否开启公网访问功能。

  • true:开启

  • false:不开启

port

Integer

实例自定义端口。只有创建Redis4.0及以上版本的基础版实例才支持自定义端口,企业版实例不支持自定义端口,默认端口为6379。

创建Redis实例时,如果没发送该参数或该参数为空,表示实例使用默认端口6379。如果自定义端口,端口范围为1~65535的任意数字。

rename_commands

Object

支持自定义重命名高危命令。仅创建Redis4.0及以上版本的实例支持重命名高危命令。

创建Redis实例,如果没发送该参数或该参数为空,表示没有需要重命名的命令。当前支持重命名的高危命令有command、keys、flushdb、flushall、hgetall、scan、hscan、sscan、和zscan,Proxy集群实例还支持dbsize和dbstats命令重命名,其他命令暂不支持重命名。

template_id

String

参数模板ID,不配置时使用默认参数模板,如需指定自定义参数模板,请参见查询参数模板列表获取模板ID。

表3 BssParam

参数

是否必选

参数类型

描述

is_auto_renew

String

当选择包年包月时,该字段为必选,表示是否自动续订资源。

取值范围:

  • false:不自动续订;

  • true:自动续订;

    默认值为:false

    约束:

    如果设置为自动续订,到期后,会自动续订一个月(自动续订时间后续可能会变化),详情可联系客服咨询。

charging_mode

String

功能说明:付费方式(预付费、按需付费;预付费,即包周期付费)。

取值范围:

  • prePaid:预付费,即包年包月;

  • postPaid:后付费,即按需付费;

    默认值是postPaid。

    后付费的场景下,bss_param参数的其他字段都会被忽略。

is_auto_pay

String

功能说明:下单订购后,是否自动从客户的账户中支付;默认是“不自动支付” 。

取值范围:

  • true:是(自动支付,从账户余额自动扣费)

  • false:否(默认值,只提交订单不支付,需要客户手动去支付)

约束:

自动支付时,只能使用账户的现金支付;如果要使用代金券,请选择不自动支付,然后在用户费用中心,选择代金券支付。

如果包周期购买,没有设置成自动支付,即设置为false时,在变更规格或创建实例之后,用户必须在“费用中心 > 我的订单”,完成订单支付,否则订单一直在支付中,实例没有变更或者创建成功

period_type

String

当选择包年包月时,该字段为必选,表示订购资源的周期类型。

取值范围如下:

  • month:表示包月

  • year:表示包年

period_num

Integer

功能说明:订购周期数。

当选择按需时,该字段非必选。

[取值范围:(后续会随运营策略变化)

  • period_type为month时,为[1,9],

  • period_type为year时,为[1,3]

约束:同period_type约束。

表4 BackupPolicy

参数

是否必选

参数类型

描述

backup_type

String

备份类型。

  • auto:自动备份

  • manual:手动备份

save_days

Integer

当backup_type设置为auto时,该参数为必填。

保留天数,单位:天,取值范围:1-7。由自动切换为手动时,保留自动备份策略信息。

periodical_backup_plan

BackupPlan object

备份计划,当backup_type设置为auto时,该参数为必填。由自动切换为手动时,保留自动备份策略信息。

表5 BackupPlan

参数

是否必选

参数类型

描述

timezone_offset

String

备份的时区。该参数已废弃,没有实际作用。

backup_at

Array of integers

每周的周几开始备份,取值1-7,1代表周一,7代表周日。由自动切换为手动时,保留自动备份策略信息。

period_type

String

备份周期类型,目前支持“weekly”。由自动切换为手动时,保留自动备份策略信息。

begin_at

String

备份开始执行时间,该时间为UTC时间,例如“00:00-01:00”代表UTC时间0点开始执行备份。只能是整点时间段,间隔时间限定为一个小时。由自动切换为手动时,保留自动备份策略信息。

表6 ResourceTag

参数

是否必选

参数类型

描述

key

String

标签键。

  • 不能为空。

  • 对于同一资源键值唯一。

  • 长度不超过36个字符。

  • 标签的键必须唯一且输入不能为空。

value

String

标签值。

  • action为create时必选。action为delete时非必选。

  • 长度不超过43个字符。

响应参数

状态码: 200

表7 响应Body参数

参数

参数类型

描述

order_id

String

订单ID,仅在创建包周期实例时返回。

instances

Array of Instances objects

缓存实例ID和名称,如果批量创建实例,则会返回多个。

表8 Instances

参数

参数类型

描述

instance_id

String

缓存实例ID。

instance_name

String

缓存实例名称。

状态码: 400

表9 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息(暂未使用,赋值为null)。

状态码: 401

表10 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息(暂未使用,赋值为null)。

状态码: 403

表11 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息(暂未使用,赋值为null)。

状态码: 404

表12 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息(暂未使用,赋值为null)。

状态码: 500

表13 响应Body参数

参数

参数类型

描述

error_msg

String

错误信息。

error_code

String

错误码。

error_ext_msg

String

扩展错误信息(暂未使用,赋值为null)。

请求示例

  • 创建一个按需计费的Redis单机实例,缓存版本是5.0,缓存容量是0.125GB。

    POST https://{dcs_endpoint}/v2/{project_id}/instances
    
    {
      "name" : "dcs-APITest",
      "description" : "Test",
      "engine" : "Redis",
      "engine_version" : 5.0,
      "enterprise_project_id" : 0,
      "enterprise_project_name" : "default",
      "vpc_id" : "5e37b3be-950a-48e1-b498-65b63d336481",
      "security_group_id" : "securityGroupId",
      "enable_ssl" : false,
      "private_ip" : "",
      "az_codes" : [ "region01" ],
      "capacity" : 0,
      "access_user" : "",
      "maintain_begin" : "18:00:00",
      "maintain_end" : "19:00:00",
      "password" : "********",
      "enable_publicip" : false,
      "spec_code" : "redis.single.xu1.tiny.128",
      "subnet_id" : "a4112635-3ec0-471c-95c3-5cf49b9533af",
      "no_password_access" : false,
      "template_id" : "b3ecc9c7-1133-4c47-a06a-21366e227d61"
    }
  • 创建一个按需计费的Redis主备实例,缓存容量2GB,设置了标签、自定义端口、高危命令重命名和跨可用区部署。

    {
      "enterprise_project_name" : "default",
      "vpc_id" : "c5cd009b-9ba9-41f2-9a26-2b8c3f3ffd39",
      "description" : "Test",
      "security_group_id" : "d57fad27-e3e9-43b7-9498-0a698ab63b27",
      "port" : 4040,
      "maintain_end" : "19:00:00",
      "enable_ssl" : false,
      "private_ip" : "",
      "az_codes" : [ "region01", "region02" ],
      "capacity" : 2,
      "access_user" : "",
      "instance_num" : 1,
      "maintain_begin" : "18:00:00",
      "enterprise_project_id" : 0,
      "password" : "********",
      "enable_publicip" : false,
      "engine" : "Redis",
      "spec_code" : "redis.ha.au1.large.2",
      "name" : "dcs-demo",
      "subnet_id" : "7e95c4d6-d7af-424c-a3cf-2be10968fe81",
      "engine_version" : 4.0,
      "no_password_access" : false,
      "rename_commands" : {
        "keys" : "keys001",
        "flushdb" : "flushdb001",
        "flushall" : "flushall001"
      },
      "tags" : [ {
        "key" : "dcs001",
        "value" : 2
      }, {
        "key" : "dcs003",
        "value" : 4
      } ]
    }
  • 创建一个包周期的Redis主备实例,缓存版本是4.0,缓存容量是0.125GB,设置了备份策略和跨可用区部署。

    {
      "publicip_id" : "",
      "enterprise_project_name" : "default",
      "vpc_id" : "5e37b3be-950a-48e1-b498-65b63d336481",
      "description" : "Test",
      "security_group_id" : "securityGroupId",
      "maintain_begin" : "18:00:00",
      "maintain_end" : "19:00:00",
      "enable_ssl" : false,
      "private_ip" : "",
      "az_codes" : [ "region01", "region02" ],
      "capacity" : 0,
      "access_user" : "",
      "enterprise_project_id" : 0,
      "password" : "********",
      "enable_publicip" : false,
      "engine" : "Redis",
      "spec_code" : "redis.ha.xu1.tiny.r2.128",
      "name" : "dcs-APITest1",
      "subnet_id" : "a4112635-3ec0-471c-95c3-5cf49b9533af",
      "engine_version" : 4.0,
      "no_password_access" : false,
      "instance_backup_policy" : {
        "save_days" : 3,
        "backup_type" : "auto",
        "periodical_backup_plan" : {
          "begin_at" : "16:00-17:00",
          "period_type" : "weekly",
          "backup_at" : [ 1, 2, 3, 4 ]
        }
      },
      "bss_param" : {
        "charging_mode" : "prePaid",
        "is_auto_pay" : true,
        "period_type" : "month",
        "period_num" : 1,
        "is_auto_renew" : false
      }
    }
  • 创建一个包周期的Redis Cluster集群实例,缓存版本是4.0,缓存容量是4GB,设置了备份策略。

    {
      "publicip_id" : "",
      "enterprise_project_name" : "default",
      "vpc_id" : "5e37b3be-950a-48e1-b498-65b63d336481",
      "description" : "Test",
      "security_group_id" : "securityGroupId",
      "maintain_end" : "19:00:00",
      "enable_ssl" : false,
      "private_ip" : "",
      "az_codes" : [ "region01" ],
      "capacity" : 4,
      "access_user" : "",
      "maintain_begin" : "18:00:00",
      "enterprise_project_id" : 0,
      "password" : "********",
      "enable_publicip" : false,
      "engine" : "Redis",
      "spec_code" : "redis.cluster.xu1.large.r2.4",
      "name" : "dcs-APITest2",
      "subnet_id" : "a4112635-3ec0-471c-95c3-5cf49b9533af",
      "engine_version" : 4.0,
      "no_password_access" : false,
      "bss_param" : {
        "charging_mode" : "prePaid",
        "is_auto_pay" : true,
        "period_type" : "month",
        "period_num" : 1,
        "is_auto_renew" : false
      },
      "instance_backup_policy" : {
        "save_days" : 3,
        "backup_type" : "auto",
        "periodical_backup_plan" : {
          "begin_at" : "16:00-17:00",
          "period_type" : "weekly",
          "backup_at" : [ 1, 2, 3, 4, 5, 6, 7 ]
        }
      }
    }

响应示例

状态码: 200

下发创建缓存实例任务成功。

查看实例状态为RUNNING时,即缓存实例创建成功。

  • 示例 1

    {
      "instances" : [ {
        "instance_id" : "3c49fd6b-fc7c-419e-9644-b6cce008653f",
        "instance_name" : "dcs-test005"
      } ]
    }
  • 示例 2

    {
      "order_id" : "CS2005091653L2DAM",
      "instances" : [ {
        "instance_id" : "77284d69-cd51-4bf1-aa30-2e60f055800c",
        "instance_name" : "dcs-APITest1"
      } ]
    }

SDK代码示例

SDK代码示例如下。

  • 创建一个按需计费的Redis单机实例,缓存版本是5.0,缓存容量是0.125GB。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.dcs.v2.region.DcsRegion;
    import com.huaweicloud.sdk.dcs.v2.*;
    import com.huaweicloud.sdk.dcs.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateInstanceSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            DcsClient client = DcsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DcsRegion.valueOf("<YOUR REGION>"))
                    .build();
            CreateInstanceRequest request = new CreateInstanceRequest();
            CreateInstanceBody body = new CreateInstanceBody();
            List<String> listbodyAzCodes = new ArrayList<>();
            listbodyAzCodes.add("region01");
            body.withTemplateId("b3ecc9c7-1133-4c47-a06a-21366e227d61");
            body.withEnablePublicip(false);
            body.withAccessUser("");
            body.withNoPasswordAccess(false);
            body.withPassword("********");
            body.withMaintainEnd("19:00:00");
            body.withMaintainBegin("18:00:00");
            body.withPrivateIp("");
            body.withEnableSsl(false);
            body.withDescription("Test");
            body.withEnterpriseProjectName("default");
            body.withEnterpriseProjectId("0");
            body.withSecurityGroupId("securityGroupId");
            body.withSubnetId("a4112635-3ec0-471c-95c3-5cf49b9533af");
            body.withVpcId("5e37b3be-950a-48e1-b498-65b63d336481");
            body.withAzCodes(listbodyAzCodes);
            body.withSpecCode("redis.single.xu1.tiny.128");
            body.withCapacity(0f);
            body.withEngineVersion("5.0");
            body.withEngine("Redis");
            body.withName("dcs-APITest");
            request.withBody(body);
            try {
                CreateInstanceResponse response = client.createInstance(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 创建一个按需计费的Redis主备实例,缓存容量2GB,设置了标签、自定义端口、高危命令重命名和跨可用区部署。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.dcs.v2.region.DcsRegion;
    import com.huaweicloud.sdk.dcs.v2.*;
    import com.huaweicloud.sdk.dcs.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateInstanceSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            DcsClient client = DcsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DcsRegion.valueOf("<YOUR REGION>"))
                    .build();
            CreateInstanceRequest request = new CreateInstanceRequest();
            CreateInstanceBody body = new CreateInstanceBody();
            List<ResourceTag> listbodyTags = new ArrayList<>();
            listbodyTags.add(
                new ResourceTag()
                    .withKey("dcs001")
                    .withValue("2")
            );
            listbodyTags.add(
                new ResourceTag()
                    .withKey("dcs003")
                    .withValue("4")
            );
            List<String> listbodyAzCodes = new ArrayList<>();
            listbodyAzCodes.add("region01");
            listbodyAzCodes.add("region02");
            body.withRenameCommands("{\"keys\":\"keys001\",\"flushall\":\"flushall001\",\"flushdb\":\"flushdb001\"}");
            body.withPort(4040);
            body.withEnablePublicip(false);
            body.withAccessUser("");
            body.withTags(listbodyTags);
            body.withNoPasswordAccess(false);
            body.withPassword("********");
            body.withMaintainEnd("19:00:00");
            body.withMaintainBegin("18:00:00");
            body.withInstanceNum(1);
            body.withPrivateIp("");
            body.withEnableSsl(false);
            body.withDescription("Test");
            body.withEnterpriseProjectName("default");
            body.withEnterpriseProjectId("0");
            body.withSecurityGroupId("d57fad27-e3e9-43b7-9498-0a698ab63b27");
            body.withSubnetId("7e95c4d6-d7af-424c-a3cf-2be10968fe81");
            body.withVpcId("c5cd009b-9ba9-41f2-9a26-2b8c3f3ffd39");
            body.withAzCodes(listbodyAzCodes);
            body.withSpecCode("redis.ha.au1.large.2");
            body.withCapacity(2f);
            body.withEngineVersion("4.0");
            body.withEngine("Redis");
            body.withName("dcs-demo");
            request.withBody(body);
            try {
                CreateInstanceResponse response = client.createInstance(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 创建一个包周期的Redis主备实例,缓存版本是4.0,缓存容量是0.125GB,设置了备份策略和跨可用区部署。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.dcs.v2.region.DcsRegion;
    import com.huaweicloud.sdk.dcs.v2.*;
    import com.huaweicloud.sdk.dcs.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateInstanceSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            DcsClient client = DcsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DcsRegion.valueOf("<YOUR REGION>"))
                    .build();
            CreateInstanceRequest request = new CreateInstanceRequest();
            CreateInstanceBody body = new CreateInstanceBody();
            List<Integer> listPeriodicalBackupPlanBackupAt = new ArrayList<>();
            listPeriodicalBackupPlanBackupAt.add(1);
            listPeriodicalBackupPlanBackupAt.add(2);
            listPeriodicalBackupPlanBackupAt.add(3);
            listPeriodicalBackupPlanBackupAt.add(4);
            BackupPlan periodicalBackupPlanInstanceBackupPolicy = new BackupPlan();
            periodicalBackupPlanInstanceBackupPolicy.withBackupAt(listPeriodicalBackupPlanBackupAt)
                .withPeriodType("weekly")
                .withBeginAt("16:00-17:00");
            BackupPolicy instanceBackupPolicybody = new BackupPolicy();
            instanceBackupPolicybody.withBackupType("auto")
                .withSaveDays(3)
                .withPeriodicalBackupPlan(periodicalBackupPlanInstanceBackupPolicy);
            BssParam bssParambody = new BssParam();
            bssParambody.withIsAutoRenew(BssParam.IsAutoRenewEnum.fromValue("false"))
                .withChargingMode(BssParam.ChargingModeEnum.fromValue("prePaid"))
                .withIsAutoPay(BssParam.IsAutoPayEnum.fromValue("true"))
                .withPeriodType(BssParam.PeriodTypeEnum.fromValue("month"))
                .withPeriodNum(1);
            List<String> listbodyAzCodes = new ArrayList<>();
            listbodyAzCodes.add("region01");
            listbodyAzCodes.add("region02");
            body.withEnablePublicip(false);
            body.withAccessUser("");
            body.withInstanceBackupPolicy(instanceBackupPolicybody);
            body.withBssParam(bssParambody);
            body.withNoPasswordAccess(false);
            body.withPassword("********");
            body.withMaintainEnd("19:00:00");
            body.withMaintainBegin("18:00:00");
            body.withPrivateIp("");
            body.withEnableSsl(false);
            body.withDescription("Test");
            body.withEnterpriseProjectName("default");
            body.withEnterpriseProjectId("0");
            body.withPublicipId("");
            body.withSecurityGroupId("securityGroupId");
            body.withSubnetId("a4112635-3ec0-471c-95c3-5cf49b9533af");
            body.withVpcId("5e37b3be-950a-48e1-b498-65b63d336481");
            body.withAzCodes(listbodyAzCodes);
            body.withSpecCode("redis.ha.xu1.tiny.r2.128");
            body.withCapacity(0f);
            body.withEngineVersion("4.0");
            body.withEngine("Redis");
            body.withName("dcs-APITest1");
            request.withBody(body);
            try {
                CreateInstanceResponse response = client.createInstance(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 创建一个包周期的Redis Cluster集群实例,缓存版本是4.0,缓存容量是4GB,设置了备份策略。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.dcs.v2.region.DcsRegion;
    import com.huaweicloud.sdk.dcs.v2.*;
    import com.huaweicloud.sdk.dcs.v2.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateInstanceSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            DcsClient client = DcsClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(DcsRegion.valueOf("<YOUR REGION>"))
                    .build();
            CreateInstanceRequest request = new CreateInstanceRequest();
            CreateInstanceBody body = new CreateInstanceBody();
            List<Integer> listPeriodicalBackupPlanBackupAt = new ArrayList<>();
            listPeriodicalBackupPlanBackupAt.add(1);
            listPeriodicalBackupPlanBackupAt.add(2);
            listPeriodicalBackupPlanBackupAt.add(3);
            listPeriodicalBackupPlanBackupAt.add(4);
            listPeriodicalBackupPlanBackupAt.add(5);
            listPeriodicalBackupPlanBackupAt.add(6);
            listPeriodicalBackupPlanBackupAt.add(7);
            BackupPlan periodicalBackupPlanInstanceBackupPolicy = new BackupPlan();
            periodicalBackupPlanInstanceBackupPolicy.withBackupAt(listPeriodicalBackupPlanBackupAt)
                .withPeriodType("weekly")
                .withBeginAt("16:00-17:00");
            BackupPolicy instanceBackupPolicybody = new BackupPolicy();
            instanceBackupPolicybody.withBackupType("auto")
                .withSaveDays(3)
                .withPeriodicalBackupPlan(periodicalBackupPlanInstanceBackupPolicy);
            BssParam bssParambody = new BssParam();
            bssParambody.withIsAutoRenew(BssParam.IsAutoRenewEnum.fromValue("false"))
                .withChargingMode(BssParam.ChargingModeEnum.fromValue("prePaid"))
                .withIsAutoPay(BssParam.IsAutoPayEnum.fromValue("true"))
                .withPeriodType(BssParam.PeriodTypeEnum.fromValue("month"))
                .withPeriodNum(1);
            List<String> listbodyAzCodes = new ArrayList<>();
            listbodyAzCodes.add("region01");
            body.withEnablePublicip(false);
            body.withAccessUser("");
            body.withInstanceBackupPolicy(instanceBackupPolicybody);
            body.withBssParam(bssParambody);
            body.withNoPasswordAccess(false);
            body.withPassword("********");
            body.withMaintainEnd("19:00:00");
            body.withMaintainBegin("18:00:00");
            body.withPrivateIp("");
            body.withEnableSsl(false);
            body.withDescription("Test");
            body.withEnterpriseProjectName("default");
            body.withEnterpriseProjectId("0");
            body.withPublicipId("");
            body.withSecurityGroupId("securityGroupId");
            body.withSubnetId("a4112635-3ec0-471c-95c3-5cf49b9533af");
            body.withVpcId("5e37b3be-950a-48e1-b498-65b63d336481");
            body.withAzCodes(listbodyAzCodes);
            body.withSpecCode("redis.cluster.xu1.large.r2.4");
            body.withCapacity(4f);
            body.withEngineVersion("4.0");
            body.withEngine("Redis");
            body.withName("dcs-APITest2");
            request.withBody(body);
            try {
                CreateInstanceResponse response = client.createInstance(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 创建一个按需计费的Redis单机实例,缓存版本是5.0,缓存容量是0.125GB。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdcs.v2.region.dcs_region import DcsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdcs.v2 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = DcsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DcsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = CreateInstanceRequest()
            listAzCodesbody = [
                "region01"
            ]
            request.body = CreateInstanceBody(
                template_id="b3ecc9c7-1133-4c47-a06a-21366e227d61",
                enable_publicip=False,
                access_user="",
                no_password_access=False,
                password="********",
                maintain_end="19:00:00",
                maintain_begin="18:00:00",
                private_ip="",
                enable_ssl=False,
                description="Test",
                enterprise_project_name="default",
                enterprise_project_id="0",
                security_group_id="securityGroupId",
                subnet_id="a4112635-3ec0-471c-95c3-5cf49b9533af",
                vpc_id="5e37b3be-950a-48e1-b498-65b63d336481",
                az_codes=listAzCodesbody,
                spec_code="redis.single.xu1.tiny.128",
                capacity=0,
                engine_version="5.0",
                engine="Redis",
                name="dcs-APITest"
            )
            response = client.create_instance(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建一个按需计费的Redis主备实例,缓存容量2GB,设置了标签、自定义端口、高危命令重命名和跨可用区部署。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdcs.v2.region.dcs_region import DcsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdcs.v2 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = DcsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DcsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = CreateInstanceRequest()
            listTagsbody = [
                ResourceTag(
                    key="dcs001",
                    value="2"
                ),
                ResourceTag(
                    key="dcs003",
                    value="4"
                )
            ]
            listAzCodesbody = [
                "region01",
                "region02"
            ]
            request.body = CreateInstanceBody(
                rename_commands="{\"keys\":\"keys001\",\"flushall\":\"flushall001\",\"flushdb\":\"flushdb001\"}",
                port=4040,
                enable_publicip=False,
                access_user="",
                tags=listTagsbody,
                no_password_access=False,
                password="********",
                maintain_end="19:00:00",
                maintain_begin="18:00:00",
                instance_num=1,
                private_ip="",
                enable_ssl=False,
                description="Test",
                enterprise_project_name="default",
                enterprise_project_id="0",
                security_group_id="d57fad27-e3e9-43b7-9498-0a698ab63b27",
                subnet_id="7e95c4d6-d7af-424c-a3cf-2be10968fe81",
                vpc_id="c5cd009b-9ba9-41f2-9a26-2b8c3f3ffd39",
                az_codes=listAzCodesbody,
                spec_code="redis.ha.au1.large.2",
                capacity=2,
                engine_version="4.0",
                engine="Redis",
                name="dcs-demo"
            )
            response = client.create_instance(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建一个包周期的Redis主备实例,缓存版本是4.0,缓存容量是0.125GB,设置了备份策略和跨可用区部署。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdcs.v2.region.dcs_region import DcsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdcs.v2 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = DcsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DcsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = CreateInstanceRequest()
            listBackupAtPeriodicalBackupPlan = [
                1,
                2,
                3,
                4
            ]
            periodicalBackupPlanInstanceBackupPolicy = BackupPlan(
                backup_at=listBackupAtPeriodicalBackupPlan,
                period_type="weekly",
                begin_at="16:00-17:00"
            )
            instanceBackupPolicybody = BackupPolicy(
                backup_type="auto",
                save_days=3,
                periodical_backup_plan=periodicalBackupPlanInstanceBackupPolicy
            )
            bssParambody = BssParam(
                is_auto_renew="false",
                charging_mode="prePaid",
                is_auto_pay="true",
                period_type="month",
                period_num=1
            )
            listAzCodesbody = [
                "region01",
                "region02"
            ]
            request.body = CreateInstanceBody(
                enable_publicip=False,
                access_user="",
                instance_backup_policy=instanceBackupPolicybody,
                bss_param=bssParambody,
                no_password_access=False,
                password="********",
                maintain_end="19:00:00",
                maintain_begin="18:00:00",
                private_ip="",
                enable_ssl=False,
                description="Test",
                enterprise_project_name="default",
                enterprise_project_id="0",
                publicip_id="",
                security_group_id="securityGroupId",
                subnet_id="a4112635-3ec0-471c-95c3-5cf49b9533af",
                vpc_id="5e37b3be-950a-48e1-b498-65b63d336481",
                az_codes=listAzCodesbody,
                spec_code="redis.ha.xu1.tiny.r2.128",
                capacity=0,
                engine_version="4.0",
                engine="Redis",
                name="dcs-APITest1"
            )
            response = client.create_instance(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建一个包周期的Redis Cluster集群实例,缓存版本是4.0,缓存容量是4GB,设置了备份策略。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkdcs.v2.region.dcs_region import DcsRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkdcs.v2 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = DcsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(DcsRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = CreateInstanceRequest()
            listBackupAtPeriodicalBackupPlan = [
                1,
                2,
                3,
                4,
                5,
                6,
                7
            ]
            periodicalBackupPlanInstanceBackupPolicy = BackupPlan(
                backup_at=listBackupAtPeriodicalBackupPlan,
                period_type="weekly",
                begin_at="16:00-17:00"
            )
            instanceBackupPolicybody = BackupPolicy(
                backup_type="auto",
                save_days=3,
                periodical_backup_plan=periodicalBackupPlanInstanceBackupPolicy
            )
            bssParambody = BssParam(
                is_auto_renew="false",
                charging_mode="prePaid",
                is_auto_pay="true",
                period_type="month",
                period_num=1
            )
            listAzCodesbody = [
                "region01"
            ]
            request.body = CreateInstanceBody(
                enable_publicip=False,
                access_user="",
                instance_backup_policy=instanceBackupPolicybody,
                bss_param=bssParambody,
                no_password_access=False,
                password="********",
                maintain_end="19:00:00",
                maintain_begin="18:00:00",
                private_ip="",
                enable_ssl=False,
                description="Test",
                enterprise_project_name="default",
                enterprise_project_id="0",
                publicip_id="",
                security_group_id="securityGroupId",
                subnet_id="a4112635-3ec0-471c-95c3-5cf49b9533af",
                vpc_id="5e37b3be-950a-48e1-b498-65b63d336481",
                az_codes=listAzCodesbody,
                spec_code="redis.cluster.xu1.large.r2.4",
                capacity=4,
                engine_version="4.0",
                engine="Redis",
                name="dcs-APITest2"
            )
            response = client.create_instance(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建一个按需计费的Redis单机实例,缓存版本是5.0,缓存容量是0.125GB。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dcs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := dcs.NewDcsClient(
            dcs.DcsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateInstanceRequest{}
    	var listAzCodesbody = []string{
            "region01",
        }
    	templateIdCreateInstanceBody:= "b3ecc9c7-1133-4c47-a06a-21366e227d61"
    	enablePublicipCreateInstanceBody:= false
    	accessUserCreateInstanceBody:= ""
    	noPasswordAccessCreateInstanceBody:= false
    	passwordCreateInstanceBody:= "********"
    	maintainEndCreateInstanceBody:= "19:00:00"
    	maintainBeginCreateInstanceBody:= "18:00:00"
    	privateIpCreateInstanceBody:= ""
    	enableSslCreateInstanceBody:= false
    	descriptionCreateInstanceBody:= "Test"
    	enterpriseProjectNameCreateInstanceBody:= "default"
    	enterpriseProjectIdCreateInstanceBody:= "0"
    	securityGroupIdCreateInstanceBody:= "securityGroupId"
    	engineVersionCreateInstanceBody:= "5.0"
    	request.Body = &model.CreateInstanceBody{
    		TemplateId: &templateIdCreateInstanceBody,
    		EnablePublicip: &enablePublicipCreateInstanceBody,
    		AccessUser: &accessUserCreateInstanceBody,
    		NoPasswordAccess: &noPasswordAccessCreateInstanceBody,
    		Password: &passwordCreateInstanceBody,
    		MaintainEnd: &maintainEndCreateInstanceBody,
    		MaintainBegin: &maintainBeginCreateInstanceBody,
    		PrivateIp: &privateIpCreateInstanceBody,
    		EnableSsl: &enableSslCreateInstanceBody,
    		Description: &descriptionCreateInstanceBody,
    		EnterpriseProjectName: &enterpriseProjectNameCreateInstanceBody,
    		EnterpriseProjectId: &enterpriseProjectIdCreateInstanceBody,
    		SecurityGroupId: &securityGroupIdCreateInstanceBody,
    		SubnetId: "a4112635-3ec0-471c-95c3-5cf49b9533af",
    		VpcId: "5e37b3be-950a-48e1-b498-65b63d336481",
    		AzCodes: listAzCodesbody,
    		SpecCode: "redis.single.xu1.tiny.128",
    		Capacity: float32(0),
    		EngineVersion: &engineVersionCreateInstanceBody,
    		Engine: "Redis",
    		Name: "dcs-APITest",
    	}
    	response, err := client.CreateInstance(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建一个按需计费的Redis主备实例,缓存容量2GB,设置了标签、自定义端口、高危命令重命名和跨可用区部署。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dcs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := dcs.NewDcsClient(
            dcs.DcsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateInstanceRequest{}
    	valueTags:= "2"
    	valueTags1:= "4"
    	var listTagsbody = []model.ResourceTag{
            {
                Key: "dcs001",
                Value: &valueTags,
            },
            {
                Key: "dcs003",
                Value: &valueTags1,
            },
        }
    	var listAzCodesbody = []string{
            "region01",
    	    "region02",
        }
    	var renameCommandsCreateInstanceBody interface{} = "{\"keys\":\"keys001\",\"flushall\":\"flushall001\",\"flushdb\":\"flushdb001\"}"
    	portCreateInstanceBody:= int32(4040)
    	enablePublicipCreateInstanceBody:= false
    	accessUserCreateInstanceBody:= ""
    	noPasswordAccessCreateInstanceBody:= false
    	passwordCreateInstanceBody:= "********"
    	maintainEndCreateInstanceBody:= "19:00:00"
    	maintainBeginCreateInstanceBody:= "18:00:00"
    	instanceNumCreateInstanceBody:= int32(1)
    	privateIpCreateInstanceBody:= ""
    	enableSslCreateInstanceBody:= false
    	descriptionCreateInstanceBody:= "Test"
    	enterpriseProjectNameCreateInstanceBody:= "default"
    	enterpriseProjectIdCreateInstanceBody:= "0"
    	securityGroupIdCreateInstanceBody:= "d57fad27-e3e9-43b7-9498-0a698ab63b27"
    	engineVersionCreateInstanceBody:= "4.0"
    	request.Body = &model.CreateInstanceBody{
    		RenameCommands: &renameCommandsCreateInstanceBody,
    		Port: &portCreateInstanceBody,
    		EnablePublicip: &enablePublicipCreateInstanceBody,
    		AccessUser: &accessUserCreateInstanceBody,
    		Tags: &listTagsbody,
    		NoPasswordAccess: &noPasswordAccessCreateInstanceBody,
    		Password: &passwordCreateInstanceBody,
    		MaintainEnd: &maintainEndCreateInstanceBody,
    		MaintainBegin: &maintainBeginCreateInstanceBody,
    		InstanceNum: &instanceNumCreateInstanceBody,
    		PrivateIp: &privateIpCreateInstanceBody,
    		EnableSsl: &enableSslCreateInstanceBody,
    		Description: &descriptionCreateInstanceBody,
    		EnterpriseProjectName: &enterpriseProjectNameCreateInstanceBody,
    		EnterpriseProjectId: &enterpriseProjectIdCreateInstanceBody,
    		SecurityGroupId: &securityGroupIdCreateInstanceBody,
    		SubnetId: "7e95c4d6-d7af-424c-a3cf-2be10968fe81",
    		VpcId: "c5cd009b-9ba9-41f2-9a26-2b8c3f3ffd39",
    		AzCodes: listAzCodesbody,
    		SpecCode: "redis.ha.au1.large.2",
    		Capacity: float32(2),
    		EngineVersion: &engineVersionCreateInstanceBody,
    		Engine: "Redis",
    		Name: "dcs-demo",
    	}
    	response, err := client.CreateInstance(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建一个包周期的Redis主备实例,缓存版本是4.0,缓存容量是0.125GB,设置了备份策略和跨可用区部署。

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dcs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := dcs.NewDcsClient(
            dcs.DcsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateInstanceRequest{}
    	var listBackupAtPeriodicalBackupPlan = []int32{
            int32(1),
    	    int32(2),
    	    int32(3),
    	    int32(4),
        }
    	periodicalBackupPlanInstanceBackupPolicy := &model.BackupPlan{
    		BackupAt: listBackupAtPeriodicalBackupPlan,
    		PeriodType: "weekly",
    		BeginAt: "16:00-17:00",
    	}
    	saveDaysInstanceBackupPolicy:= int32(3)
    	instanceBackupPolicybody := &model.BackupPolicy{
    		BackupType: "auto",
    		SaveDays: &saveDaysInstanceBackupPolicy,
    		PeriodicalBackupPlan: periodicalBackupPlanInstanceBackupPolicy,
    	}
    	isAutoRenewBssParam:= model.GetBssParamIsAutoRenewEnum().FALSE
    	isAutoPayBssParam:= model.GetBssParamIsAutoPayEnum().TRUE
    	periodTypeBssParam:= model.GetBssParamPeriodTypeEnum().MONTH
    	periodNumBssParam:= int32(1)
    	bssParambody := &model.BssParam{
    		IsAutoRenew: &isAutoRenewBssParam,
    		ChargingMode: model.GetBssParamChargingModeEnum().PRE_PAID,
    		IsAutoPay: &isAutoPayBssParam,
    		PeriodType: &periodTypeBssParam,
    		PeriodNum: &periodNumBssParam,
    	}
    	var listAzCodesbody = []string{
            "region01",
    	    "region02",
        }
    	enablePublicipCreateInstanceBody:= false
    	accessUserCreateInstanceBody:= ""
    	noPasswordAccessCreateInstanceBody:= false
    	passwordCreateInstanceBody:= "********"
    	maintainEndCreateInstanceBody:= "19:00:00"
    	maintainBeginCreateInstanceBody:= "18:00:00"
    	privateIpCreateInstanceBody:= ""
    	enableSslCreateInstanceBody:= false
    	descriptionCreateInstanceBody:= "Test"
    	enterpriseProjectNameCreateInstanceBody:= "default"
    	enterpriseProjectIdCreateInstanceBody:= "0"
    	publicipIdCreateInstanceBody:= ""
    	securityGroupIdCreateInstanceBody:= "securityGroupId"
    	engineVersionCreateInstanceBody:= "4.0"
    	request.Body = &model.CreateInstanceBody{
    		EnablePublicip: &enablePublicipCreateInstanceBody,
    		AccessUser: &accessUserCreateInstanceBody,
    		InstanceBackupPolicy: instanceBackupPolicybody,
    		BssParam: bssParambody,
    		NoPasswordAccess: &noPasswordAccessCreateInstanceBody,
    		Password: &passwordCreateInstanceBody,
    		MaintainEnd: &maintainEndCreateInstanceBody,
    		MaintainBegin: &maintainBeginCreateInstanceBody,
    		PrivateIp: &privateIpCreateInstanceBody,
    		EnableSsl: &enableSslCreateInstanceBody,
    		Description: &descriptionCreateInstanceBody,
    		EnterpriseProjectName: &enterpriseProjectNameCreateInstanceBody,
    		EnterpriseProjectId: &enterpriseProjectIdCreateInstanceBody,
    		PublicipId: &publicipIdCreateInstanceBody,
    		SecurityGroupId: &securityGroupIdCreateInstanceBody,
    		SubnetId: "a4112635-3ec0-471c-95c3-5cf49b9533af",
    		VpcId: "5e37b3be-950a-48e1-b498-65b63d336481",
    		AzCodes: listAzCodesbody,
    		SpecCode: "redis.ha.xu1.tiny.r2.128",
    		Capacity: float32(0),
    		EngineVersion: &engineVersionCreateInstanceBody,
    		Engine: "Redis",
    		Name: "dcs-APITest1",
    	}
    	response, err := client.CreateInstance(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建一个包周期的Redis Cluster集群实例,缓存版本是4.0,缓存容量是4GB,设置了备份策略。

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        dcs "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/dcs/v2/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := dcs.NewDcsClient(
            dcs.DcsClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.CreateInstanceRequest{}
    	var listBackupAtPeriodicalBackupPlan = []int32{
            int32(1),
    	    int32(2),
    	    int32(3),
    	    int32(4),
    	    int32(5),
    	    int32(6),
    	    int32(7),
        }
    	periodicalBackupPlanInstanceBackupPolicy := &model.BackupPlan{
    		BackupAt: listBackupAtPeriodicalBackupPlan,
    		PeriodType: "weekly",
    		BeginAt: "16:00-17:00",
    	}
    	saveDaysInstanceBackupPolicy:= int32(3)
    	instanceBackupPolicybody := &model.BackupPolicy{
    		BackupType: "auto",
    		SaveDays: &saveDaysInstanceBackupPolicy,
    		PeriodicalBackupPlan: periodicalBackupPlanInstanceBackupPolicy,
    	}
    	isAutoRenewBssParam:= model.GetBssParamIsAutoRenewEnum().FALSE
    	isAutoPayBssParam:= model.GetBssParamIsAutoPayEnum().TRUE
    	periodTypeBssParam:= model.GetBssParamPeriodTypeEnum().MONTH
    	periodNumBssParam:= int32(1)
    	bssParambody := &model.BssParam{
    		IsAutoRenew: &isAutoRenewBssParam,
    		ChargingMode: model.GetBssParamChargingModeEnum().PRE_PAID,
    		IsAutoPay: &isAutoPayBssParam,
    		PeriodType: &periodTypeBssParam,
    		PeriodNum: &periodNumBssParam,
    	}
    	var listAzCodesbody = []string{
            "region01",
        }
    	enablePublicipCreateInstanceBody:= false
    	accessUserCreateInstanceBody:= ""
    	noPasswordAccessCreateInstanceBody:= false
    	passwordCreateInstanceBody:= "********"
    	maintainEndCreateInstanceBody:= "19:00:00"
    	maintainBeginCreateInstanceBody:= "18:00:00"
    	privateIpCreateInstanceBody:= ""
    	enableSslCreateInstanceBody:= false
    	descriptionCreateInstanceBody:= "Test"
    	enterpriseProjectNameCreateInstanceBody:= "default"
    	enterpriseProjectIdCreateInstanceBody:= "0"
    	publicipIdCreateInstanceBody:= ""
    	securityGroupIdCreateInstanceBody:= "securityGroupId"
    	engineVersionCreateInstanceBody:= "4.0"
    	request.Body = &model.CreateInstanceBody{
    		EnablePublicip: &enablePublicipCreateInstanceBody,
    		AccessUser: &accessUserCreateInstanceBody,
    		InstanceBackupPolicy: instanceBackupPolicybody,
    		BssParam: bssParambody,
    		NoPasswordAccess: &noPasswordAccessCreateInstanceBody,
    		Password: &passwordCreateInstanceBody,
    		MaintainEnd: &maintainEndCreateInstanceBody,
    		MaintainBegin: &maintainBeginCreateInstanceBody,
    		PrivateIp: &privateIpCreateInstanceBody,
    		EnableSsl: &enableSslCreateInstanceBody,
    		Description: &descriptionCreateInstanceBody,
    		EnterpriseProjectName: &enterpriseProjectNameCreateInstanceBody,
    		EnterpriseProjectId: &enterpriseProjectIdCreateInstanceBody,
    		PublicipId: &publicipIdCreateInstanceBody,
    		SecurityGroupId: &securityGroupIdCreateInstanceBody,
    		SubnetId: "a4112635-3ec0-471c-95c3-5cf49b9533af",
    		VpcId: "5e37b3be-950a-48e1-b498-65b63d336481",
    		AzCodes: listAzCodesbody,
    		SpecCode: "redis.cluster.xu1.large.r2.4",
    		Capacity: float32(4),
    		EngineVersion: &engineVersionCreateInstanceBody,
    		Engine: "Redis",
    		Name: "dcs-APITest2",
    	}
    	response, err := client.CreateInstance(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

下发创建缓存实例任务成功。

说明:

查看实例状态为RUNNING时,即缓存实例创建成功。

400

非法请求。

401

认证信息非法。

403

请求被拒绝访问。

404

所请求的资源不存在。

500

内部服务错误。

错误码

请参见错误码

相关文档