blob: cb766f87eb94448135da240f61b6b391e2e5310a [file] [log] [blame]
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08001package main
2
3import (
4 "fmt"
5 "github.com/rackspace/gophercloud/openstack/identity"
6 "github.com/rackspace/gophercloud/openstack/utils"
7)
8
9type extractor func(*identity.TokenDesc) string
10
11func main() {
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080012 // Create an initialized set of authentication options based on available OS_*
13 // environment variables.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080014 ao, err := utils.AuthOptions()
15 if err != nil {
16 panic(err)
17 }
18
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080019 // Attempt to authenticate with them.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080020 r, err := identity.Authenticate(ao)
21 if err != nil {
22 panic(err)
23 }
24
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080025 // We're authenticated; now let's grab our authentication token.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080026 t, err := identity.Token(r)
27 if err != nil {
28 panic(err)
29 }
30
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080031 // Authentication tokens have a variety of fields which might be of some interest.
32 // Let's print a few of them out.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080033 table := map[string]extractor{
34 "ID": func(t *identity.TokenDesc) string { return t.Id() },
35 "Expires": func(t *identity.TokenDesc) string { return t.Expires() },
36 }
37
38 for attr, fn := range table {
39 fmt.Printf("Your token's %s is %s\n", attr, fn(t))
40 }
41
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080042 // With each authentication, you receive a master directory of all the services
43 // your account can access. This "service catalog", as OpenStack calls it,
44 // provides you the means to exploit other OpenStack services.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080045 sc, err := identity.ServiceCatalog(r)
46 if err != nil {
47 panic(err)
48 }
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080049
50 // Different providers will provide different services. Let's print them
51 // in summary.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080052 ces, err := sc.CatalogEntries()
53 fmt.Printf("Service Catalog Summary:\n %32s %-16s\n", "Name", "Type")
54 for _, ce := range ces {
55 fmt.Printf(" %32s | %-16s\n", ce.Name, ce.Type)
56 }
57
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080058 // Now let's print them in greater detail.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080059 for _, ce := range ces {
60 fmt.Printf("Endpoints for %s/%s\n", ce.Name, ce.Type)
61 for _, ep := range ce.Endpoints {
62 fmt.Printf(" Version: %s\n", ep.VersionId)
63 fmt.Printf(" Region: %s\n", ep.Region)
64 fmt.Printf(" Tenant: %s\n", ep.TenantId)
65 fmt.Printf(" Public URL: %s\n", ep.PublicURL)
66 fmt.Printf(" Internal URL: %s\n", ep.InternalURL)
67 }
68 }
69}
Samuel A. Falvo II41822a72014-02-08 16:43:09 -080070