Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame^] | 1 | package identity |
| 2 | |
| 3 | import "github.com/mitchellh/mapstructure" |
| 4 | |
| 5 | type ServiceCatalogDesc struct { |
| 6 | serviceDescriptions []interface{} |
| 7 | } |
| 8 | |
| 9 | type CatalogEntry struct { |
| 10 | Name string |
| 11 | Type string |
| 12 | Endpoints []Endpoint |
| 13 | } |
| 14 | |
| 15 | type 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 | |
| 25 | func 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 | |
| 34 | func (sc *ServiceCatalogDesc) NumberOfServices() int { |
| 35 | return len(sc.serviceDescriptions) |
| 36 | } |
| 37 | |
| 38 | func (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 | |
| 56 | func 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 | } |