Samuel A. Falvo II | 8a549ef | 2014-01-24 15:20:54 -0800 | [diff] [blame] | 1 | package identity |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/mitchellh/mapstructure" |
| 6 | ) |
| 7 | |
| 8 | var ( |
| 9 | ErrNotFound = fmt.Errorf("Identity extension not found") |
| 10 | ) |
| 11 | |
| 12 | type ExtensionDetails struct { |
| 13 | Name string |
| 14 | Namespace string |
| 15 | Updated string |
| 16 | Description string |
| 17 | } |
| 18 | |
| 19 | // ExtensionsDesc structures are returned by the Extensions() function for valid input. |
| 20 | // This structure implements the ExtensionInquisitor interface. |
| 21 | type ExtensionsDesc struct { |
| 22 | extensions []interface{} |
| 23 | } |
| 24 | |
| 25 | func Extensions(m map[string]interface{}) *ExtensionsDesc { |
| 26 | return &ExtensionsDesc{extensions: m["extensions"].([]interface{})} |
| 27 | } |
| 28 | |
| 29 | func extensionIndexByAlias(e *ExtensionsDesc, alias string) (int, error) { |
| 30 | for i, ee := range e.extensions { |
| 31 | extensionRecord := ee.(map[string]interface{}) |
| 32 | if extensionRecord["alias"] == alias { |
| 33 | return i, nil |
| 34 | } |
| 35 | } |
| 36 | return 0, ErrNotFound |
| 37 | } |
| 38 | |
| 39 | func (e *ExtensionsDesc) IsExtensionAvailable(alias string) bool { |
| 40 | _, err := extensionIndexByAlias(e, alias) |
| 41 | return err == nil |
| 42 | } |
| 43 | |
| 44 | func (e *ExtensionsDesc) ExtensionDetailsByAlias(alias string) (*ExtensionDetails, error) { |
| 45 | i, err := extensionIndexByAlias(e, alias) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | ed := &ExtensionDetails{} |
| 50 | err = mapstructure.Decode(e.extensions[i], ed) |
| 51 | return ed, err |
| 52 | } |