3. 安装SDK
云容器引擎 CCE SDK支持go 1.14及以上版本。执行go version检查当前Go的版本信息。
使用go get安装华为云Go SDK,执行如下命令安装华为云Go SDK库以及相关依赖库,具体的SDK版本号请参见SDK开发中心。
Copied!
# 安装华为云Go库
go get github.com/huaweicloud/huaweicloud-sdk-go-v3
4. 代码示例
以下代码展示如何删除指定CCE集群:
Copied!
package main
import (
"fmt"
"time"
"os"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model"
region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region"
)
func main() {
ak := os.Getenv("HUAWEICLOUD_SDK_AK")
sk := os.Getenv("HUAWEICLOUD_SDK_SK")
projectID := "<YOUR PROJECT ID>"
clusterID := "<YOUR ClUSTER ID>"
userRegion := "<YOUR REGION>"
fmt.Printf("This is a demo to delete cluster.\n")
auth := basic.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
WithProjectId(projectID).
Build()
fmt.Printf("Build auth successfully.\n")
client := cce.NewCceClient(
cce.CceClientBuilder().
WithRegion(region.ValueOf(userRegion)).
WithCredential(auth).
Build())
fmt.Printf("Build client successfully.\n")
request := &model.DeleteClusterRequest{}
request.ClusterId = clusterID
response, err := client.DeleteCluster(request)
if err == nil {
fmt.Printf("Start to delete cluster...\n")
} else {
fmt.Printf("Delete cluster failed...\n")
fmt.Printf("Error: %+v\n", err)
return
}
if response != nil && response.Status != nil && response.Status.JobID != nil {
waitJobReady(client, *response.Status.JobID)
} else {
fmt.Printf("Get cluster state failed...\n")
}
}
func waitJobReady(c *cce.CceClient, jobID string) {
req := &model.ShowJobRequest{
JobId: jobID,
}
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
timeout := time.After(30 * time.Minute)
for {
select {
case <-timeout:
fmt.Printf("Wait cluster state timed out")
return
case <-ticker.C:
resp, err := c.ShowJob(req)
if err != nil {
fmt.Printf("Show delete cluster job failed...\n")
fmt.Printf("Error: %+v\n", err)
return
}
if resp != nil && resp.Status != nil && resp.Status.Phase != nil {
fmt.Printf("Wait cluster deleted, job is %s...\n", *resp.Status.Phase)
if *resp.Status.Phase == "Success" {
fmt.Printf("Delete cluster successfully!")
return
} else if *resp.Status.Phase == "Failed" {
fmt.Printf("Delete cluster failed!")
return
}
}
}
}
}
0. 版本说明
本示例基于华为云SDK V3.0版本开发。
1. 示例简介
华为云提供了云容器引擎 CCE SDK,您可以直接集成SDK来调用相关API,从而实现对云容器引擎服务的快速操作。
该示例展示了如何在云容器引擎服务删除指定集群。
2. 开发前准备
3. 安装SDK
云容器引擎 CCE SDK支持go 1.14及以上版本。执行go version检查当前Go的版本信息。
使用go get安装华为云Go SDK,执行如下命令安装华为云Go SDK库以及相关依赖库,具体的SDK版本号请参见SDK开发中心。
4. 代码示例
以下代码展示如何删除指定CCE集群:
package main import ( "fmt" "time" "os" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region" ) func main() { /* 以下部分请替换成用户实际参数 */ // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak := os.Getenv("HUAWEICLOUD_SDK_AK") // 华为云账号Access Key sk := os.Getenv("HUAWEICLOUD_SDK_SK") // 华为云账号Secret Access Key projectID := "<YOUR PROJECT ID>" // 华为云账号项目ID clusterID := "<YOUR ClUSTER ID>" // 删除的集群的ID userRegion := "<YOUR REGION>" // 服务所在区域,eg: cn-north-4 /* 主流程 */ fmt.Printf("This is a demo to delete cluster.\n") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectID). Build() fmt.Printf("Build auth successfully.\n") client := cce.NewCceClient( cce.CceClientBuilder(). WithRegion(region.ValueOf(userRegion)). WithCredential(auth). Build()) fmt.Printf("Build client successfully.\n") request := &model.DeleteClusterRequest{} request.ClusterId = clusterID /* 删除集群 */ response, err := client.DeleteCluster(request) if err == nil { fmt.Printf("Start to delete cluster...\n") } else { fmt.Printf("Delete cluster failed...\n") fmt.Printf("Error: %+v\n", err) return } if response != nil && response.Status != nil && response.Status.JobID != nil { waitJobReady(client, *response.Status.JobID) } else { fmt.Printf("Get cluster state failed...\n") } } /* 查询任务执行状态,等待集群删除成功 */ func waitJobReady(c *cce.CceClient, jobID string) { req := &model.ShowJobRequest{ JobId: jobID, } ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() timeout := time.After(30 * time.Minute) for { select { case <-timeout: fmt.Printf("Wait cluster state timed out") return case <-ticker.C: resp, err := c.ShowJob(req) if err != nil { fmt.Printf("Show delete cluster job failed...\n") fmt.Printf("Error: %+v\n", err) return } if resp != nil && resp.Status != nil && resp.Status.Phase != nil { fmt.Printf("Wait cluster deleted, job is %s...\n", *resp.Status.Phase) if *resp.Status.Phase == "Success" { fmt.Printf("Delete cluster successfully!") return } else if *resp.Status.Phase == "Failed" { fmt.Printf("Delete cluster failed!") return } } } } }
5. 参考
更多信息请参考:云容器引擎 CCE 文档
6. 修订记录