[MyFirstApp] Gophercloud library migration

Migrating from rackspace/gophercloud to gophercloud/gophercloud library.

Change-Id: Ia72a4c548e23b4225b788520d924be8e04ce5c6f
This commit is contained in:
Marcela Bonell 2017-01-10 12:48:55 -06:00
parent f03fcbdc62
commit ef14eaaf5e
3 changed files with 300 additions and 263 deletions

View File

@ -2,38 +2,44 @@ package main
import (
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
"github.com/gophercloud/gophercloud/openstack/compute/v2/images"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/external"
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
"io/ioutil"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack"
"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
"github.com/rackspace/gophercloud/openstack/compute/v2/images"
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/floatingip"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/external"
"os"
)
func main() {
// step-1
var authUsername string = "your_auth_username"
var authPassword string = "your_auth_password"
var authUrl string = "http://controller:5000"
var projectName string = "your_project_id"
var regionName string = "your_region_name"
authOpts := gophercloud.AuthOptions{
IdentityEndpoint: authUrl,
Username: authUsername,
Password: authPassword,
TenantID: projectName,
// step-1
authOpts, err := openstack.AuthOptionsFromEnv()
if err != nil {
fmt.Println(err)
return
}
provider, _ := openstack.AuthenticatedClient(authOpts)
client, _ := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
provider, err := openstack.AuthenticatedClient(authOpts)
if err != nil {
fmt.Println(err)
return
}
var regionName = os.Getenv("OS_REGION_NAME")
client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
Region: regionName,
Type: "computev21",
})
if err != nil {
fmt.Println(err)
return
}
// step-2
pager := images.ListDetail(client, images.ListOpts{})
@ -59,11 +65,15 @@ func main() {
// step-6
instanceName := "testing"
testingInstance, _ := servers.Create(client, servers.CreateOpts{
testingInstance, err := servers.Create(client, servers.CreateOpts{
Name: instanceName,
ImageRef: imageID,
FlavorRef: flavorID,
}).Extract()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(testingInstance)
// step-7
@ -155,8 +165,7 @@ func main() {
// step-11
userData := `#!/usr/bin/env bash
curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
-i faafo -i messaging -r api -r worker -r demo
`
-i faafo -i messaging -r api -r worker -r demo`
// step-12
fmt.Println("Checking for existing instance...")
@ -184,10 +193,14 @@ curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.
SecurityGroups: []string{securityGroupName},
UserData: []byte(userData),
}
testingInstance, _ = servers.Create(client, keypairs.CreateOptsExt{
testingInstance, err = servers.Create(client, keypairs.CreateOptsExt{
CreateOptsBuilder: opts,
KeyName: keyPairName,
}).Extract()
if err != nil {
fmt.Println(err)
return
}
}
servers.WaitForStatus(client, testingInstance.ID, "ACTIVE", 300)
@ -247,9 +260,9 @@ curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.
// step-15
fmt.Println("Checking for unused Floating IP...")
var unusedFloatingIP string
pager = floatingip.List(client)
pager = floatingips.List(client)
page, _ = pager.AllPages()
floatingIPList, _ := floatingip.ExtractFloatingIPs(page)
floatingIPList, _ := floatingips.ExtractFloatingIPs(page)
for _, ip := range floatingIPList {
if ip.InstanceID == "" {
unusedFloatingIP = ip.IP
@ -269,7 +282,7 @@ curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.
continue
}
fmt.Println("Allocating new Floating IP from pool: " + pool.Name)
f, _ := floatingip.Create(client, floatingip.CreateOpts{Pool: pool.Name}).Extract()
f, _ := floatingips.Create(client, floatingips.CreateOpts{Pool: pool.Name}).Extract()
unusedFloatingIP = f.IP
}
@ -277,7 +290,10 @@ curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.
if len(publicIP) != 0 {
fmt.Println("Instance " + testingInstance.Name + " already has a public ip. Skipping attachment.")
} else {
floatingip.Associate(client, testingInstance.ID, unusedFloatingIP)
opts := floatingips.AssociateOpts{
FloatingIP: unusedFloatingIP,
}
floatingips.AssociateInstance(client, testingInstance.ID, opts)
}
// step-17

View File

@ -33,3 +33,24 @@ Specify a network during instance build
image=image_id,
flavor=flavor_id,
network=network_id)
.. only:: gophercloud
Add the option Networks and send its id to attach the instance to:
.. code-block:: go
opts := servers.CreateOpts {
Name: instanceName,
ImageRef: image.ID,
FlavorRef: flavor.ID,
SecurityGroups: []string{securityGroupName},
UserData: []byte(userData),
Networks: []servers.Network{servers.Network{UUID: networkID}},
}
testingInstance, _ = servers.Create(client, keypairs.CreateOptsExt {
CreateOptsBuilder: opts,
KeyName: keyPairName,
}).Extract()

View File

@ -97,9 +97,9 @@ and toolkits with the OpenStack cloud:
Use it to write C++ or C# code for Microsoft applications.
- https://www.nuget.org/packages/openstack.net
* - Go
- `gophercloud <https://github.com/rackspace/gophercloud>`_
- `gophercloud <https://github.com/gophercloud/gophercloud>`_
- A go-based SDK.
Use it with multiple clouds.
Use it to write Golang code that works with OpenStack clouds.
- http://gophercloud.io/
For a list of available SDKs, see `Software Development Kits <https://wiki.openstack.org/wiki/SDKs>`_.
@ -232,7 +232,7 @@ To interact with the cloud, you must also have
.. only:: gophercloud
`a recent version of gophercloud installed <https://godoc.org/github.com/rackspace/gophercloud>`_
`a recent version of gophercloud installed <https://godoc.org/github.com/gophercloud/gophercloud>`_
Obtain the following information from your cloud provider:
@ -383,14 +383,14 @@ to run code snippets in your language of choice.
.. only:: gophercloud
To try it, add the following code to go file
Use environment variables to set your cloud credentials
.. literalinclude:: ../samples/gophercloud/getting_started.go
:language: go
:start-after: step-1
:end-before: step-2
.. note:: The client object accesses the Compute v2.0 service,
.. note:: The client object accesses the Compute v2.0 service and type v2.1,
so that version is in this tutorial.
Flavors and images