A series of examples of Gophercloud usage.
Refer to https://that.guru/blog/gophercloud-hello-world/ for more information.
A series of examples of Gophercloud usage.
Refer to https://that.guru/blog/gophercloud-hello-world/ for more information.
package main | |
import ( | |
"os" | |
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips" | |
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks" | |
"github.com/gophercloud/utils/openstack/clientconfig" | |
) | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Println("usage: create-fip <NETWORK>") | |
return | |
} | |
network := os.Args[1] | |
opts := new(clientconfig.ClientOpts) | |
// We could configure the cloud manually but we don't. Instead we'll leave it unset causing gophercloud to load the | |
// cloud from the 'OS_CLOUD' environment variable. We could also simplify this further and pass 'nil' below instead | |
// of generating and passing a 'ClientOpts' object but this shows how you _could_ configure things if you so chose. | |
// opts.Cloud = "devstack-admin" | |
client, err := clientconfig.NewServiceClient("network", opts) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
pager, err := networks.List(client, networks.ListOpts{ | |
Name: network, | |
}).AllPages() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
networks, err := networks.ExtractNetworks(pager) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
if len(networks) > 1 { | |
fmt.Println("Found more than one match!") | |
return | |
} | |
if len(networks) < 1 { | |
fmt.Println("Found no matches!") | |
return | |
} | |
floatingIP, err := floatingips.Create(client, floatingips.CreateOpts{ | |
Description: "stephenfin hello-world FIP", | |
FloatingNetworkID: networks[0].ID, | |
}).Extract() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println("Created FIP") | |
fmt.Printf("floating IP: id=%s, tags=%s\n", floatingIP.ID, floatingIP.Tags) | |
tag := fmt.Sprintf("fooBar=%s", "wow") | |
err = attributestags.Add(client, "floatingips", floatingIP.ID, tag).ExtractErr() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
floatingIP, err = floatingips.Get(client, floatingIP.ID).Extract() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println("Tagged FIP") | |
fmt.Printf("floating IP: id=%s, tags=%s\n", floatingIP.ID, floatingIP.Tags) | |
err = floatingips.Delete(client, floatingIP.ID).ExtractErr() | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println("Deleted FIP") | |
} |