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