Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/rackspace/gophercloud/openstack/identity" |
| 6 | "github.com/rackspace/gophercloud/openstack/utils" |
| 7 | ) |
| 8 | |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame^] | 9 | type extractor func(*identity.Token) string |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 10 | |
| 11 | func main() { |
Samuel A. Falvo II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 12 | // Create an initialized set of authentication options based on available OS_* |
| 13 | // environment variables. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 14 | ao, err := utils.AuthOptions() |
| 15 | if err != nil { |
| 16 | panic(err) |
| 17 | } |
| 18 | |
Samuel A. Falvo II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 19 | // Attempt to authenticate with them. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 20 | r, err := identity.Authenticate(ao) |
| 21 | if err != nil { |
| 22 | panic(err) |
| 23 | } |
| 24 | |
Samuel A. Falvo II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 25 | // We're authenticated; now let's grab our authentication token. |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame^] | 26 | t, err := identity.GetToken(r) |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 27 | if err != nil { |
| 28 | panic(err) |
| 29 | } |
| 30 | |
Samuel A. Falvo II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 31 | // 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 II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 33 | table := map[string]extractor{ |
Samuel A. Falvo II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame^] | 34 | "ID": func(t *identity.Token) string { return t.Id }, |
| 35 | "Expires": func(t *identity.Token) string { return t.Expires }, |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 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 II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 42 | // 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 II | 2b96321 | 2014-02-09 02:12:30 -0800 | [diff] [blame^] | 45 | sc, err := identity.GetServiceCatalog(r) |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
Samuel A. Falvo II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 49 | |
| 50 | // Different providers will provide different services. Let's print them |
| 51 | // in summary. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 52 | 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 II | 41822a7 | 2014-02-08 16:43:09 -0800 | [diff] [blame] | 58 | // Now let's print them in greater detail. |
Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 59 | 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 | } |