[MyFirstApp] Gophercloud library migration
Migrating from rackspace/gophercloud to gophercloud/gophercloud library. Change-Id: Ia72a4c548e23b4225b788520d924be8e04ce5c6f
This commit is contained in:
parent
f03fcbdc62
commit
ef14eaaf5e
@ -2,38 +2,44 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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{
|
// step-1
|
||||||
IdentityEndpoint: authUrl,
|
authOpts, err := openstack.AuthOptionsFromEnv()
|
||||||
Username: authUsername,
|
if err != nil {
|
||||||
Password: authPassword,
|
fmt.Println(err)
|
||||||
TenantID: projectName,
|
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,
|
Region: regionName,
|
||||||
|
Type: "computev21",
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// step-2
|
// step-2
|
||||||
pager := images.ListDetail(client, images.ListOpts{})
|
pager := images.ListDetail(client, images.ListOpts{})
|
||||||
@ -59,11 +65,15 @@ func main() {
|
|||||||
|
|
||||||
// step-6
|
// step-6
|
||||||
instanceName := "testing"
|
instanceName := "testing"
|
||||||
testingInstance, _ := servers.Create(client, servers.CreateOpts{
|
testingInstance, err := servers.Create(client, servers.CreateOpts{
|
||||||
Name: instanceName,
|
Name: instanceName,
|
||||||
ImageRef: imageID,
|
ImageRef: imageID,
|
||||||
FlavorRef: flavorID,
|
FlavorRef: flavorID,
|
||||||
}).Extract()
|
}).Extract()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
fmt.Println(testingInstance)
|
fmt.Println(testingInstance)
|
||||||
|
|
||||||
// step-7
|
// step-7
|
||||||
@ -154,9 +164,8 @@ func main() {
|
|||||||
|
|
||||||
// step-11
|
// step-11
|
||||||
userData := `#!/usr/bin/env bash
|
userData := `#!/usr/bin/env bash
|
||||||
curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.sh | bash -s -- \
|
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
|
// step-12
|
||||||
fmt.Println("Checking for existing instance...")
|
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},
|
SecurityGroups: []string{securityGroupName},
|
||||||
UserData: []byte(userData),
|
UserData: []byte(userData),
|
||||||
}
|
}
|
||||||
testingInstance, _ = servers.Create(client, keypairs.CreateOptsExt{
|
testingInstance, err = servers.Create(client, keypairs.CreateOptsExt{
|
||||||
CreateOptsBuilder: opts,
|
CreateOptsBuilder: opts,
|
||||||
KeyName: keyPairName,
|
KeyName: keyPairName,
|
||||||
}).Extract()
|
}).Extract()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
servers.WaitForStatus(client, testingInstance.ID, "ACTIVE", 300)
|
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
|
// step-15
|
||||||
fmt.Println("Checking for unused Floating IP...")
|
fmt.Println("Checking for unused Floating IP...")
|
||||||
var unusedFloatingIP string
|
var unusedFloatingIP string
|
||||||
pager = floatingip.List(client)
|
pager = floatingips.List(client)
|
||||||
page, _ = pager.AllPages()
|
page, _ = pager.AllPages()
|
||||||
floatingIPList, _ := floatingip.ExtractFloatingIPs(page)
|
floatingIPList, _ := floatingips.ExtractFloatingIPs(page)
|
||||||
for _, ip := range floatingIPList {
|
for _, ip := range floatingIPList {
|
||||||
if ip.InstanceID == "" {
|
if ip.InstanceID == "" {
|
||||||
unusedFloatingIP = ip.IP
|
unusedFloatingIP = ip.IP
|
||||||
@ -269,7 +282,7 @@ curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println("Allocating new Floating IP from pool: " + pool.Name)
|
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
|
unusedFloatingIP = f.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,7 +290,10 @@ curl -L -s https://git.openstack.org/cgit/openstack/faafo/plain/contrib/install.
|
|||||||
if len(publicIP) != 0 {
|
if len(publicIP) != 0 {
|
||||||
fmt.Println("Instance " + testingInstance.Name + " already has a public ip. Skipping attachment.")
|
fmt.Println("Instance " + testingInstance.Name + " already has a public ip. Skipping attachment.")
|
||||||
} else {
|
} else {
|
||||||
floatingip.Associate(client, testingInstance.ID, unusedFloatingIP)
|
opts := floatingips.AssociateOpts{
|
||||||
|
FloatingIP: unusedFloatingIP,
|
||||||
|
}
|
||||||
|
floatingips.AssociateInstance(client, testingInstance.ID, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// step-17
|
// step-17
|
||||||
|
@ -33,3 +33,24 @@ Specify a network during instance build
|
|||||||
image=image_id,
|
image=image_id,
|
||||||
flavor=flavor_id,
|
flavor=flavor_id,
|
||||||
network=network_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()
|
||||||
|
@ -97,9 +97,9 @@ and toolkits with the OpenStack cloud:
|
|||||||
Use it to write C++ or C# code for Microsoft applications.
|
Use it to write C++ or C# code for Microsoft applications.
|
||||||
- https://www.nuget.org/packages/openstack.net
|
- https://www.nuget.org/packages/openstack.net
|
||||||
* - Go
|
* - Go
|
||||||
- `gophercloud <https://github.com/rackspace/gophercloud>`_
|
- `gophercloud <https://github.com/gophercloud/gophercloud>`_
|
||||||
- A go-based SDK.
|
- A go-based SDK.
|
||||||
Use it with multiple clouds.
|
Use it to write Golang code that works with OpenStack clouds.
|
||||||
- http://gophercloud.io/
|
- http://gophercloud.io/
|
||||||
|
|
||||||
For a list of available SDKs, see `Software Development Kits <https://wiki.openstack.org/wiki/SDKs>`_.
|
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
|
.. 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:
|
Obtain the following information from your cloud provider:
|
||||||
@ -383,14 +383,14 @@ to run code snippets in your language of choice.
|
|||||||
|
|
||||||
.. only:: gophercloud
|
.. 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
|
.. literalinclude:: ../samples/gophercloud/getting_started.go
|
||||||
:language: go
|
:language: go
|
||||||
:start-after: step-1
|
:start-after: step-1
|
||||||
:end-before: step-2
|
: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.
|
so that version is in this tutorial.
|
||||||
|
|
||||||
Flavors and images
|
Flavors and images
|
||||||
|
Loading…
Reference in New Issue
Block a user