blob: 4a861ff63d055d39841f057c3232945c2d021a74 [file] [log] [blame]
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08001package identity
2
3import (
4 "fmt"
5 "github.com/mitchellh/mapstructure"
6)
7
8var (
9 ErrNotFound = fmt.Errorf("Identity extension not found")
10)
11
12type 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.
21type ExtensionsDesc struct {
22 extensions []interface{}
23}
24
25func Extensions(m map[string]interface{}) *ExtensionsDesc {
26 return &ExtensionsDesc{extensions: m["extensions"].([]interface{})}
27}
28
29func 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
39func (e *ExtensionsDesc) IsExtensionAvailable(alias string) bool {
40 _, err := extensionIndexByAlias(e, alias)
41 return err == nil
42}
43
44func (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}