blob: edb24ab557254171b65b16a1527ba03d5933dddd [file] [log] [blame]
Samuel A. Falvo II80300502014-02-13 14:54:31 -08001package openstack
2
3import (
4 "testing"
5 "fmt"
6 "os"
7 "github.com/rackspace/gophercloud/openstack/identity"
8 "github.com/rackspace/gophercloud/openstack/utils"
9 "text/tabwriter"
10)
11
12type extractor func(*identity.Token) string
13
14func TestAuthentication(t *testing.T) {
15 // Create an initialized set of authentication options based on available OS_*
16 // environment variables.
17 ao, err := utils.AuthOptions()
18 if err != nil {
19 t.Error(err)
20 return
21 }
22
23 // Attempt to authenticate with them.
24 r, err := identity.Authenticate(ao)
25 if err != nil {
26 t.Error(err)
27 return
28 }
29
30 // We're authenticated; now let's grab our authentication token.
31 tok, err := identity.GetToken(r)
32 if err != nil {
33 t.Error(err)
34 return
35 }
36
37 // Authentication tokens have a variety of fields which might be of some interest.
38 // Let's print a few of them out.
39 table := map[string]extractor{
40 "ID": func(t *identity.Token) string { return tok.Id },
41 "Expires": func(t *identity.Token) string { return tok.Expires },
42 }
43
44 for attr, fn := range table {
45 fmt.Printf("Your token's %s is %s\n", attr, fn(tok))
46 }
47
48 // With each authentication, you receive a master directory of all the services
49 // your account can access. This "service catalog", as OpenStack calls it,
50 // provides you the means to exploit other OpenStack services.
51 sc, err := identity.GetServiceCatalog(r)
52 if err != nil {
53 t.Error(err)
54 return
55 }
56
57 // Prepare our elastic tabstopped writer for our table.
58 w := new(tabwriter.Writer)
59 w.Init(os.Stdout, 2, 8, 2, ' ', 0)
60
61 // Different providers will provide different services. Let's print them
62 // in summary.
63 ces, err := sc.CatalogEntries()
64 fmt.Println("Service Catalog Summary:")
65 fmt.Fprintln(w, "Name\tType\t")
66 for _, ce := range ces {
67 fmt.Fprintf(w, "%s\t%s\t\n", ce.Name, ce.Type)
68 }
69 w.Flush()
70
71 // Now let's print them in greater detail.
72 for _, ce := range ces {
73 fmt.Printf("Endpoints for %s/%s\n", ce.Name, ce.Type)
74 fmt.Fprintln(w, "Version\tRegion\tTenant\tPublic URL\tInternal URL\t")
75 for _, ep := range ce.Endpoints {
76 fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t\n", ep.VersionId, ep.Region, ep.TenantId, ep.PublicURL, ep.InternalURL)
77 }
78 w.Flush()
79 }
80}
81
82func TestExtensions(t *testing.T) {
83 // Create an initialized set of authentication options based on available OS_*
84 // environment variables.
85 ao, err := utils.AuthOptions()
86 if err != nil {
87 t.Error(err)
88 return
89 }
90
91 // Attempt to query extensions.
92 exts, err := identity.GetExtensions(ao)
93 if err != nil {
94 t.Error(err)
95 return
96 }
97
98 // Print out a summary of supported extensions
99 aliases, err := exts.Aliases()
100 if err != nil {
101 t.Error(err)
102 return
103 }
104 fmt.Println("Extension Aliases:")
105 for _, alias := range aliases {
106 fmt.Printf(" %s\n", alias)
107 }
108}
109