Samuel A. Falvo II | b5d93f2 | 2014-02-21 15:00:20 -0800 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 3 | package openstack |
| 4 | |
| 5 | import ( |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 6 | "fmt" |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 7 | "os" |
| 8 | "testing" |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 9 | "text/tabwriter" |
Ash Wilson | 51dff8a | 2014-09-09 12:30:21 -0400 | [diff] [blame^] | 10 | |
| 11 | "github.com/rackspace/gophercloud" |
| 12 | identity "github.com/rackspace/gophercloud/openstack/identity/v2" |
| 13 | "github.com/rackspace/gophercloud/openstack/utils" |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | type extractor func(*identity.Token) string |
| 17 | |
| 18 | func 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 Wilson | 51dff8a | 2014-09-09 12:30:21 -0400 | [diff] [blame^] | 28 | client := &gophercloud.ServiceClient{Endpoint: ao.IdentityEndpoint + "/"} |
| 29 | r, err := identity.Authenticate(client, ao) |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 30 | 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 Wilson | 51dff8a | 2014-09-09 12:30:21 -0400 | [diff] [blame^] | 45 | "ID": func(t *identity.Token) string { return tok.ID }, |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 46 | "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 Wilson | 51dff8a | 2014-09-09 12:30:21 -0400 | [diff] [blame^] | 81 | 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 II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 82 | } |
| 83 | w.Flush() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func 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 Wilson | 51dff8a | 2014-09-09 12:30:21 -0400 | [diff] [blame^] | 97 | client := &gophercloud.ServiceClient{Endpoint: ao.IdentityEndpoint + "/"} |
| 98 | exts, err := identity.GetExtensions(client, ao) |
Samuel A. Falvo II | 8030050 | 2014-02-13 14:54:31 -0800 | [diff] [blame] | 99 | 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 | } |