zoukankan      html  css  js  c++  java
  • openstack golang sdk使用

    1. go get github.com/gophercloud/gophercloud

    
    
    import (
    	"github.com/gophercloud/gophercloud"
    	"github.com/gophercloud/gophercloud/openstack"
    	"fmt"
    	"crypto/rand"
    
    	"github.com/gophercloud/gophercloud/pagination"
    	"golang.org/x/crypto/ssh"
    	"time"
    	"net"
    	"github.com/golang/glog"
    	"os"
    	"github.com/gophercloud/gophercloud/openstack/identity/v3/projects"
    	"encoding/json"
    	"github.com/gophercloud/gophercloud/openstack/identity/v3/domains"
    	"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
    	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
    	"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
    	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
    )
    

    //认证 scop := gophercloud.AuthScope{ ProjectName:"xx", DomainName: "xx", } opts := gophercloud.AuthOptions{ IdentityEndpoint: "http://xx:5000/v3", Username: "xx", Password: "xxxxxxx", DomainName: "default", Scope:&scop, } provider, err := openstack.AuthenticatedClient(opts) if err != nil { fmt.Printf("AuthenticatedClient : %v" , err) return } func ListZones(provider *gophercloud.ProviderClient) (result []availabilityzones.AvailabilityZone) { method := "ListZones" client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) fmt.Println(client) if err != nil { fmt.Printf("%s : %v" , method, err) return result } pager,_ := availabilityzones.List(client).AllPages() result, err = availabilityzones.ExtractAvailabilityZones(pager) return result } func ListProject(provider *gophercloud.ProviderClient) (result []projects.Project) { method := "ListProject" client, err := openstack.NewIdentityV3(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) fmt.Println(client) if err != nil { fmt.Printf("%s : %v" , method, err) return result } pager := projects.List(client, projects.ListOpts{}) fmt.Println(pager) err = pager.EachPage(func(page pagination.Page) (bool, error) { extract, _ := projects.ExtractProjects(page) result = extract return true, nil }) return result } func ListDomain(provider *gophercloud.ProviderClient) (result []domains.Domain) { method := "ListProject" client, err := openstack.NewIdentityV3(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) fmt.Println(client) if err != nil { fmt.Printf("%s : %v" , method, err) return result } pager := domains.List(client, domains.ListOpts{}) fmt.Println(pager) err = pager.EachPage(func(page pagination.Page) (bool, error) { extract, _ := domains.ExtractDomains(page) result = extract return true, nil }) return result } func ListImage(provider *gophercloud.ProviderClient) (result []images.Image) { method := "ListProject" client, err := openstack.NewImageServiceV2(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) fmt.Println(client) if err != nil { fmt.Printf("%s : %v" , method, err) return result } pager := images.List(client, images.ListOpts{}) fmt.Println(pager) err = pager.EachPage(func(page pagination.Page) (bool, error) { extract, _ := images.ExtractImages(page) result = extract return true, nil }) return result } func ListNetwork(provider *gophercloud.ProviderClient) (result []networks.Network) { method := "ListNetwork" client, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) fmt.Println(client) if err != nil { fmt.Printf("%s : %v" , method, err) return result } pager := networks.List(client, networks.ListOpts{}) fmt.Println(pager) err = pager.EachPage(func(page pagination.Page) (bool, error) { extract, _ := networks.ExtractNetworks(page) result = extract return true, nil }) return result } func ListFlavor(provider *gophercloud.ProviderClient) (result []flavors.Flavor) { method := "ListNetwork" client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) fmt.Println(client) if err != nil { fmt.Printf("%s : %v" , method, err) return result } pager := flavors.ListDetail(client, flavors.ListOpts{}) fmt.Println(pager) err = pager.EachPage(func(page pagination.Page) (bool, error) { extract, _ := flavors.ExtractFlavors(page) result = extract return true, nil }) return result } func CreateInstance(provider *gophercloud.ProviderClient, name string) { fmt.Println("create instance..........") client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ Region: "RegionOne", }) ss, err := servers.Create(client, servers.CreateOpts{ Name: name, FlavorRef: "64", ImageRef: "aba55c25-60b5-4097-882d-9625fbc8fc11", AvailabilityZone:"nova", Networks: []servers.Network{ servers.Network{UUID:"79e9b0c2-7e49-49f9-a7e2-0e03dd2055b3"}, }, AdminPass:"root", }).Extract() if err != nil { fmt.Printf("Create : %v", err) return } fmt.Println(ss) }
    欢迎指正,交流沟通,共同进步!对您有帮助的话点下推荐~~
  • 相关阅读:
    实现a标签按钮完全禁用【转】
    input 内容改变的触发事件【转】
    PHP字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、切割成数组等)【转】
    数列分块入门 4(涉及区间加法,区间求和)
    #6279. 数列分块入门 3(询问区间内小于某个值 xx 的前驱(比其小的最大元素))
    #6278. 数列分块入门 2(询问区间内小于某个值 xx 的元素个数)
    A Simple Problem with Integers POJ
    bzoj3262: 陌上花开(CDQ+树状数组处理三维偏序问题)
    P3806 【模板】点分治1(CDQ分治)
    Toy Train(贪心)
  • 原文地址:https://www.cnblogs.com/gaoyawei/p/9962758.html
Copyright © 2011-2022 走看看