Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 1 | package identity |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | ) |
| 6 | |
| 7 | type Client struct { |
| 8 | Endpoint string |
| 9 | Authority AuthResults |
| 10 | Options AuthOptions |
| 11 | } |
| 12 | |
| 13 | type ClientOpts struct { |
| 14 | Type string |
| 15 | Name string |
| 16 | Region string |
| 17 | URLType string |
| 18 | } |
| 19 | |
| 20 | func (ao AuthOptions) NewClient(opts ClientOpts) (Client, error) { |
| 21 | client := Client{ |
| 22 | Options: ao, |
| 23 | } |
| 24 | |
| 25 | ar, err := Authenticate(ao) |
| 26 | if err != nil { |
| 27 | return client, err |
| 28 | } |
| 29 | |
| 30 | client.Authority = ar |
| 31 | |
| 32 | sc, err := GetServiceCatalog(ar) |
| 33 | if err != nil { |
| 34 | return client, err |
| 35 | } |
| 36 | |
| 37 | ces, err := sc.CatalogEntries() |
| 38 | if err != nil { |
| 39 | return client, err |
| 40 | } |
| 41 | |
| 42 | var eps []Endpoint |
| 43 | |
| 44 | if opts.Name != "" { |
| 45 | for _, ce := range ces { |
| 46 | if ce.Type == opts.Type && ce.Name == opts.Name { |
| 47 | eps = ce.Endpoints |
| 48 | } |
| 49 | } |
| 50 | } else { |
| 51 | for _, ce := range ces { |
| 52 | if ce.Type == opts.Type { |
| 53 | eps = ce.Endpoints |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | region := os.Getenv("OS_REGION_NAME") |
| 59 | if opts.Region != "" { |
| 60 | region = opts.Region |
| 61 | } |
| 62 | |
| 63 | var rep string |
| 64 | for _, ep := range eps { |
| 65 | if ep.Region == region { |
| 66 | switch opts.URLType { |
| 67 | case "public": |
| 68 | rep = ep.PublicURL |
| 69 | case "private": |
| 70 | rep = ep.InternalURL |
| 71 | default: |
| 72 | rep = ep.PublicURL |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | client.Endpoint = rep |
| 78 | |
| 79 | return client, nil |
| 80 | } |