blob: fc2717a9c0882e35d698d0205a3905a89289aa8e [file] [log] [blame]
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08001package identity
2
3import "github.com/mitchellh/mapstructure"
4
5type ServiceCatalogDesc struct {
6 serviceDescriptions []interface{}
7}
8
9type CatalogEntry struct {
10 Name string
11 Type string
12 Endpoints []Endpoint
13}
14
15type Endpoint struct {
16 TenantId string
17 PublicURL string
18 InternalURL string
19 Region string
20 VersionId string
21 VersionInfo string
22 VersionList string
23}
24
25func ServiceCatalog(ar AuthResults) (*ServiceCatalogDesc, error) {
26 access := ar["access"].(map[string]interface{})
27 sds := access["serviceCatalog"].([]interface{})
28 sc := &ServiceCatalogDesc{
29 serviceDescriptions: sds,
30 }
31 return sc, nil
32}
33
34func (sc *ServiceCatalogDesc) NumberOfServices() int {
35 return len(sc.serviceDescriptions)
36}
37
38func (sc *ServiceCatalogDesc) CatalogEntries() ([]CatalogEntry, error) {
39 var err error
40 ces := make([]CatalogEntry, sc.NumberOfServices())
41 for i, sd := range sc.serviceDescriptions {
42 d := sd.(map[string]interface{})
43 eps, err := parseEndpoints(d["endpoints"].([]interface{}))
44 if err != nil {
45 return ces, err
46 }
47 ces[i] = CatalogEntry{
48 Name: d["name"].(string),
49 Type: d["type"].(string),
50 Endpoints: eps,
51 }
52 }
53 return ces, err
54}
55
56func parseEndpoints(eps []interface{}) ([]Endpoint, error) {
57 var err error
58 result := make([]Endpoint, len(eps))
59 for i, ep := range eps {
60 e := Endpoint{}
61 err = mapstructure.Decode(ep, &e)
62 if err != nil {
63 return result, err
64 }
65 result[i] = e
66 }
67 return result, err
68}