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