Go:HuaweiCloudSDK

时间:May 29, 2020 分类:

目录:

SDK获取

# unzip source code
unzip -d $GOPATH/src xxxxxx.zip
#run go build 
cd $GOPATH/src/github.com/gophercloud/gophercloud
go build 

使用示例

package main

import (
    "github.com/gophercloud/gophercloud/auth/token"
    "github.com/gophercloud/gophercloud"
    "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    "github.com/gophercloud/gophercloud/openstack"
    "fmt"
)

func main() {
    //设置认证参数
    tokenOpts := token.TokenOptions{
        IdentityEndpoint: "https://iam.example.com/v3",
        Username:         "{username}",
        Password:         "{password}",
        DomainID:         "{domainid}",
        ProjectID:        "{projectid}",
    }
    //初始化provider client
    provider, providerErr := openstack.AuthenticatedClient(tokenOpts)
    if providerErr != nil {
        fmt.Println("init provider client error:", providerErr)
        panic(providerErr)
    }

    //初始化service client
    sc, serviceErr := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{})
    if serviceErr != nil {
        fmt.Println("init compute service client error:", serviceErr)
        panic(serviceErr)
    }

    //列出所有服务器
    allPages, err := servers.List(sc, servers.ListOpts{}).AllPages()

    if err != nil {
        fmt.Println("request server list error:", err)
        panic(err)
    }
    //解析返回值
    allServers, err := servers.ExtractServers(allPages)
    if err != nil {
        fmt.Println("extract response data error:", err)
        if ue, ok := err.(*gophercloud.UnifiedError); ok {
            fmt.Println("ErrCode:", ue.ErrorCode())
            fmt.Println("Message:", ue.Message())
        }
        return
    }
    //打印信息
    fmt.Println("List Servers:")
    for _, s := range allServers {
        fmt.Println("server ID is :", s.ID)
        fmt.Println("server name is :", s.Name)
        fmt.Println("server Status is :", s.Status)
        fmt.Println("server AvailbiltyZone is :", s.AvailbiltyZone)
    }
}

查询方法

参考github的示例

因为有些数据是空接口类型,需要进行转化

示例

package main

import (
    "fmt"
    "github.com/gophercloud/gophercloud"
    //"github.com/gophercloud/gophercloud/auth/aksk"
    "github.com/gophercloud/gophercloud/auth/token"
    "github.com/gophercloud/gophercloud/openstack"
    "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
    "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
)

func main() {
    //设置认证参数
    //akskOpts := aksk.AKSKOptions{
    //  IdentityEndpoint: "https://iam.example.com/v3",
    //  DomainID:         "{domainid}",
    //  ProjectID:        "{projectid}",
    //  Cloud:            "myhuaweicloud.com",
    //  Region:           "cn-north-1",
    //  AccessKey:        "{your AK string}",
    //  SecretKey:        "{your SK string}",
    //}
    tokenOpts := token.TokenOptions{
        IdentityEndpoint: "https://iam.cn-north-1.myhuaweicloud.com/v3",
        Username:         "xxxxxx",
        Password:         "xxxxxx",
        DomainID:         "xxxxxx",
        ProjectID:        "xxxxxx",
    }
    //初始化provider client
    provider, providerErr := openstack.AuthenticatedClient(tokenOpts)
    if providerErr != nil {
        fmt.Println("init provider client error:", providerErr)
        panic(providerErr)
    }

    //初始化ecs client
    ecsClient, ecsErr := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{})
    if ecsErr != nil {
        fmt.Println("init ecs client error:", ecsErr)
        panic(ecsErr)
    }
    //初始化evs client
    evsClient, evsErr := openstack.NewBlockStorageV2(provider, gophercloud.EndpointOpts{})
    if evsErr != nil {
        fmt.Println("init evs client error:", evsErr)
        panic(evsErr)
    }

    //列出所有服务器
    allPages, err := servers.List(ecsClient, servers.ListOpts{}).AllPages()
    if err != nil {
        fmt.Println("request server list error:", err)
        panic(err)
    }
    //解析返回值
    allServers, err := servers.ExtractServers(allPages)
    if err != nil {
        fmt.Println("extract response data error:", err)
        if ue, ok := err.(*gophercloud.UnifiedError); ok {
            fmt.Println("ErrCode:", ue.ErrorCode())
            fmt.Println("Message:", ue.Message())
        }
        return
    }
    //打印信息
    fmt.Println("List Servers:")
    for _, s := range allServers {
        fmt.Println("server ID is :", s.ID)
        fmt.Println("server name is :", s.Name)
        // 机型
        flavor, flavorsGetErr := flavors.Get(ecsClient, s.Flavor["id"].(string)).Extract()
        if flavorsGetErr != nil {
            fmt.Println("flavorsGetErr", flavorsGetErr)
            if ue, ok := flavorsGetErr.(*gophercloud.UnifiedError); ok {
                fmt.Println("ErrCode:", ue.ErrorCode())
                fmt.Println("Message:", ue.Message())
            }
            return
        }
        fmt.Println("flavor is :", flavor.VCPUs)
        fmt.Println("flavor is :", flavor.RAM)

        // 网络
        for _, addressListMapTmp := range s.Addresses {
            addressListMap := addressListMapTmp.([]interface{})
            for _, addressMapTmp := range addressListMap{
                addressMap := addressMapTmp.(map[string]interface{})
                if addressMap["OS-EXT-IPS:type"].(string) == "fixed"{
                    // private_ip
                    fmt.Println("private_ip: ", addressMap["addr"].(string))
                }
                if addressMap["OS-EXT-IPS:type"].(string) == "floating"{
                    // public_ip
                    fmt.Println("public_ip: ",addressMap["addr"].(string))
                }
            }
        }
        // 磁盘相关
        for _, v := range s.VolumeAttached{
            vID := v["id"].(string)
            volume, err := volumes.Get(evsClient, vID).Extract()
            if err != nil {
                fmt.Println(err)
                if ue, ok := err.(*gophercloud.UnifiedError); ok {
                    fmt.Println("ErrCode", ue.ErrCode)
                    fmt.Println("ErrMessage", ue.ErrMessage)
                }
                return
            }
            fmt.Println("volume: ", volume.ID, volume.Size, volume.VolumeType)

        }
        fmt.Println("server AvailbiltyZone is :", s.AvailbiltyZone)
    }
}