开发环境搭建
创建一台云主机,并登录此云服务器,基于云服务器 Python3.6.8 运行环境,安装 SDK 依赖库。 通过华为云控制台,选择北京四区域,创建一台 x86 架构,“按需计费”的 2 核,4G, 系统盘 50G 的云实例,实例名为 chinaskill,选择镜像为 CentOS 7.9 64bit(40GB),分配独 立的公网 IP,带宽选择按使用流量计费 5M。登录此云服务器,在云服务器 Python3.6.8 环 境上,安装华为云最新版本 SDK 的依赖库,包括弹性云服务、
虚拟私有云、镜像服务、容 器云引擎、云数据库的 python 库。
https://console.huaweicloud.com/apiexplorer/#/sdkcenter?language=Python
密钥对管理
编写 Python 代码,实现密钥对的创建。 在云服务器的/root/huawei/目录下编写 create_keypair.py 文件,使用 SDK 编写 Python 代码,创建华为云的秘钥对,具体要求为: (1)秘钥对名称:chinaskills_keypair; (2)如果秘钥对已经存在,代码中需要先删除; (3)输出此秘钥对详细信息。
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.kps.v3.region.KpsRegion;
import com.huaweicloud.sdk.kps.v3.*;
import com.huaweicloud.sdk.kps.v3.model.*;
public class AssociateKeypairSolution {
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");
ICredential auth = new BasicCredentials()
.withAk(ak)
.withSk(sk);
KpsClient client = KpsClient.newBuilder()
.withCredential(auth)
.withRegion(KpsRegion.valueOf("af-south-1"))
.build();
AssociateKeypairRequest request = new AssociateKeypairRequest();
AssociateKeypairRequestBody body = new AssociateKeypairRequestBody();
request.withBody(body);
try {
AssociateKeypairResponse response = client.associateKeypair(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());
}
}
}
云硬盘管理
调用 SDK 云硬盘管理的方法,实现云硬盘的的增删查改。 在/root/huawei 目录下编写 create_block_store.py 文件,使用 SDK 编写 Python 代码,调 用创建华为云的云硬盘,具体要求如下: (1)云硬盘可用区域:cn-north-4a (2)云硬盘名称:chinaskills_volume (3)云硬盘规格和大小:超高 IO,100G (4)设置云硬盘共享 (5)设置云硬盘加密,加密秘钥为默认的 KMS 密钥 (6)如果云硬盘已经存在,代码中需要先删除 (7)输出此云硬盘的详细信息(状态要求为 available)
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.evs.v2.region.EvsRegion;
import com.huaweicloud.sdk.evs.v2.*;
import com.huaweicloud.sdk.evs.v2.model.*;
public class ChangeVolumeChargeModeSolution {
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");
ICredential auth = new BasicCredentials()
.withAk(ak)
.withSk(sk);
EvsClient client = EvsClient.newBuilder()
.withCredential(auth)
.withRegion(EvsRegion.valueOf("af-north-1"))
.build();
ChangeVolumeChargeModeRequest request = new ChangeVolumeChargeModeRequest();
ChangeVolumeChargeModeRequestBody body = new ChangeVolumeChargeModeRequestBody();
request.withBody(body);
try {
ChangeVolumeChargeModeResponse response = client.changeVolumeChargeMode(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());
}
}
}
云主机管理
调用 SDK 云主机管理的方法,实现云主机的的增删查改。 使用已建好的运维开发环境,在/root/huawei 目录下创建 ecs_manager.py 脚本,完成 ECS 云主机管理,ecs_manager.py 程序支持命令行参数执行。 提示说明:华为云主机支持安装所需 Python 库。提交前答案前,需安装所开发程序所依赖的 Python 库。 (1)程序支持根据命令行参数,创建 1 个云主机。 位置参数“create”,表示创建; 参数“-i 或--input”,格式为 json 格式文本的云主机的名称、镜像名称 2 个信息。其他参 数同上述开发环境云主机一致。 创建待成功,再返回查询该云主机的信息,结果以 json 格式输出到控制台。 参考执行实例如下: python3 /root/huawei/ecs_manager.py create --input '{ "name": " chinaskill001", " imagename": "imageid"} ' (2)支持查询给定具体名称的 ECS 云主机查询。 位置参数“get”,表示查询 ECS; 参数“-n 或 --name”支持指定名称 ECS 查询,类型为 string。 参数“-o 或 --output”支持查询该 ECS 信息输出到文件,格式为 json 格式。 (3)程序支持查询目前区域账号下所有的 ECS 云主机。 位置参数“getall”,表示查询所有 ECS 云主机; 参数“-o 或--output”支持输出到文件,格式为 yaml 格式。 (4)支持删除指定名称的云主机。 位置参数“delete”,表示删除一个 ECS 云主机;返回 response,通过控制台输出。 参数“-n 或--name”支持指定名称查询,类型为 string。
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.ecs.v2.region.EcsRegion;
import com.huaweicloud.sdk.ecs.v2.*;
import com.huaweicloud.sdk.ecs.v2.model.*;
public class CreatePostPaidServersSolution {
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");
ICredential auth = new BasicCredentials()
.withAk(ak)
.withSk(sk);
EcsClient client = EcsClient.newBuilder()
.withCredential(auth)
.withRegion(EcsRegion.valueOf("af-north-1"))
.build();
CreatePostPaidServersRequest request = new CreatePostPaidServersRequest();
CreatePostPaidServersRequestBody body = new CreatePostPaidServersRequestBody();
request.withBody(body);
try {
CreatePostPaidServersResponse response = client.createPostPaidServers(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());
}
}
}
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)