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) }

 

posted @ 2018-11-15 12:07  丨Mars  阅读(...)  评论(... 编辑 收藏