MyFirstApp: Block storage section for gophercloud
Adding the code for interact with the block storage service with go. Change-Id: Ie148af56ca1bc811a01eabe4d9fe2fdd1f92aa52
This commit is contained in:
parent
68f51b3a0e
commit
0d8e283152
135
firstapp/samples/gophercloud/block_storage.go
Normal file
135
firstapp/samples/gophercloud/block_storage.go
Normal file
@ -0,0 +1,135 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gophercloud/gophercloud"
|
||||
"github.com/gophercloud/gophercloud/openstack"
|
||||
"github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"
|
||||
"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/extensions/volumeattach"
|
||||
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
||||
"github.com/gophercloud/gophercloud/pagination"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// step-1
|
||||
authOpts, err := openstack.AuthOptionsFromEnv()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
provider, err := openstack.AuthenticatedClient(authOpts)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
var regionName = os.Getenv("OS_REGION_NAME")
|
||||
volumeClient, err := openstack.NewBlockStorageV1(provider, gophercloud.EndpointOpts{
|
||||
Region: regionName,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// step-2
|
||||
volumeOpts := volumes.CreateOpts{Size: 1, Name: "test"}
|
||||
volume, err := volumes.Create(volumeClient, volumeOpts).Extract()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// step-3
|
||||
_ = volumes.List(volumeClient, nil).EachPage(func(page pagination.Page) (bool, error) {
|
||||
volumeList, _ := volumes.ExtractVolumes(page)
|
||||
for _, vol := range volumeList {
|
||||
fmt.Println(vol)
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
// step-4
|
||||
computeClient, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
|
||||
Region: regionName,
|
||||
Type: "computev21",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
securityGroupName := "database"
|
||||
databaseSecurityGroup, _ := secgroups.Create(computeClient, secgroups.CreateOpts{
|
||||
Name: securityGroupName,
|
||||
Description: "for database service",
|
||||
}).Extract()
|
||||
secgroups.CreateRule(computeClient, secgroups.CreateRuleOpts{
|
||||
ParentGroupID: databaseSecurityGroup.ID,
|
||||
FromPort: 22,
|
||||
ToPort: 22,
|
||||
IPProtocol: "TCP",
|
||||
CIDR: "0.0.0.0/0",
|
||||
}).Extract()
|
||||
secgroups.CreateRule(computeClient, secgroups.CreateRuleOpts{
|
||||
ParentGroupID: databaseSecurityGroup.ID,
|
||||
FromPort: 3306,
|
||||
ToPort: 3306,
|
||||
IPProtocol: "TCP",
|
||||
CIDR: "0.0.0.0/0",
|
||||
}).Extract()
|
||||
|
||||
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`
|
||||
|
||||
imageID := "41ba40fd-e801-4639-a842-e3a2e5a2ebdd"
|
||||
flavorID := "3"
|
||||
networkID := "aba7a6f8-6ec9-4666-8c42-ac2d00707010"
|
||||
|
||||
serverOpts := servers.CreateOpts{
|
||||
Name: "app-database",
|
||||
ImageRef: imageID,
|
||||
FlavorRef: flavorID,
|
||||
Networks: []servers.Network{servers.Network{UUID: networkID}},
|
||||
SecurityGroups: []string{securityGroupName},
|
||||
UserData: []byte(userData),
|
||||
}
|
||||
|
||||
server, err := servers.Create(computeClient, keypairs.CreateOptsExt{
|
||||
CreateOptsBuilder: serverOpts,
|
||||
KeyName: "demokey",
|
||||
}).Extract()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
servers.WaitForStatus(computeClient, server.ID, "ACTIVE", 300)
|
||||
|
||||
// step-5
|
||||
volumeAttachOptions := volumeattach.CreateOpts{
|
||||
VolumeID: volume.ID,
|
||||
}
|
||||
|
||||
volumeAttach, err := volumeattach.Create(computeClient, server.ID, volumeAttachOptions).Extract()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
volumes.WaitForStatus(volumeClient, volumeAttach.ID, "available", 60)
|
||||
|
||||
// step-6
|
||||
err = volumeattach.Delete(computeClient, server.ID, volume.ID).ExtractErr()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
volumes.WaitForStatus(volumeClient, volume.ID, "available", 60)
|
||||
volumes.Delete(volumeClient, volume.ID)
|
||||
}
|
@ -83,6 +83,13 @@ Connect to the API endpoint:
|
||||
:start-after: step-1
|
||||
:end-before: step-2
|
||||
|
||||
.. only:: gophercloud
|
||||
|
||||
.. literalinclude:: ../samples/gophercloud/block_storage.go
|
||||
:language: go
|
||||
:start-after: step-1
|
||||
:end-before: step-2
|
||||
|
||||
To try it out, make a 1GB volume called 'test'.
|
||||
|
||||
.. only:: libcloud
|
||||
@ -112,6 +119,15 @@ To try it out, make a 1GB volume called 'test'.
|
||||
|
||||
.. note:: The parameter :code:`size` is in gigabytes.
|
||||
|
||||
.. only:: gophercloud
|
||||
|
||||
.. literalinclude:: ../samples/gophercloud/block_storage.go
|
||||
:language: go
|
||||
:start-after: step-2
|
||||
:end-before: step-3
|
||||
|
||||
.. note:: The parameter :code:`Size` is in gigabytes.
|
||||
|
||||
To see if the volume creation was successful, list all volumes:
|
||||
|
||||
.. only:: libcloud
|
||||
@ -139,7 +155,12 @@ To see if the volume creation was successful, list all volumes:
|
||||
:start-after: step-3
|
||||
:end-before: step-4
|
||||
|
||||
Attach the storage volume to a running instance.
|
||||
.. only:: gophercloud
|
||||
|
||||
.. literalinclude:: ../samples/gophercloud/block_storage.go
|
||||
:language: go
|
||||
:start-after: step-3
|
||||
:end-before: step-4
|
||||
|
||||
Use Block Storage for the Fractal database server
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
@ -172,6 +193,13 @@ MySQL, port 3306) from the network:
|
||||
:start-after: step-4
|
||||
:end-before: step-5
|
||||
|
||||
.. only:: gophercloud
|
||||
|
||||
.. literalinclude:: ../samples/gophercloud/block_storage.go
|
||||
:language: go
|
||||
:start-after: step-4
|
||||
:end-before: step-5
|
||||
|
||||
Create a volume object by using the unique identifier (UUID) for the
|
||||
volume. Then, use the server object from the previous code snippet to
|
||||
attach the volume to it at :code:`/dev/vdb`:
|
||||
@ -197,6 +225,13 @@ attach the volume to it at :code:`/dev/vdb`:
|
||||
:start-after: step-5
|
||||
:end-before: step-6
|
||||
|
||||
.. only:: gophercloud
|
||||
|
||||
.. literalinclude:: ../samples/gophercloud/block_storage.go
|
||||
:language: go
|
||||
:start-after: step-5
|
||||
:end-before: step-6
|
||||
|
||||
Log in to the server to run the following steps.
|
||||
|
||||
.. note:: Replace :code:`IP_DATABASE` with the IP address of the
|
||||
@ -307,6 +342,12 @@ To detach and delete a volume:
|
||||
:language: python
|
||||
:start-after: step-6
|
||||
|
||||
.. only:: gophercloud
|
||||
|
||||
.. literalinclude:: ../samples/gophercloud/block_storage.go
|
||||
:language: go
|
||||
:start-after: step-6
|
||||
|
||||
.. only:: libcloud
|
||||
|
||||
Other features, such as creating volume snapshots, are useful for backups:
|
||||
|
Loading…
x
Reference in New Issue
Block a user