blob: df05a364b8c8055afd00649257bb77ce4975686f [file] [log] [blame]
Ash Wilson31844f22014-09-08 15:32:58 -04001package v2
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08002
3import (
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -08004 "github.com/mitchellh/mapstructure"
5)
6
Samuel A. Falvo II2b963212014-02-09 02:12:30 -08007// ExtensionDetails provides the details offered by the OpenStack Identity V2 extensions API
8// for a named extension.
9//
10// Name provides the name, presumably the same as that used to query the API with.
11//
12// Updated provides, in a sense, the version of the extension supported. It gives the timestamp
13// of the most recent extension deployment.
14//
15// Description provides a more customer-oriented description of the extension.
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080016type ExtensionDetails struct {
17 Name string
18 Namespace string
19 Updated string
20 Description string
21}
22
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080023// ExtensionsResult encapsulates the raw data returned by a call to
24// GetExtensions(). As OpenStack extensions may freely alter the response
25// bodies of structures returned to the client, you may only safely access the
26// data provided through separate, type-safe accessors or methods.
27type ExtensionsResult map[string]interface{}
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080028
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080029// IsExtensionAvailable returns true if and only if the provider supports the named extension.
30func (er ExtensionsResult) IsExtensionAvailable(alias string) bool {
31 e, err := extensions(er)
32 if err != nil {
33 return false
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080034 }
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080035 _, err = extensionIndexByAlias(e, alias)
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080036 return err == nil
37}
38
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080039// ExtensionDetailsByAlias returns more detail than the mere presence of an extension by the provider.
40// See the ExtensionDetails structure.
41func (er ExtensionsResult) ExtensionDetailsByAlias(alias string) (*ExtensionDetails, error) {
42 e, err := extensions(er)
43 if err != nil {
44 return nil, err
45 }
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080046 i, err := extensionIndexByAlias(e, alias)
47 if err != nil {
48 return nil, err
49 }
50 ed := &ExtensionDetails{}
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080051 err = mapstructure.Decode(e[i], ed)
Samuel A. Falvo II8a549ef2014-01-24 15:20:54 -080052 return ed, err
53}
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080054
55func extensionIndexByAlias(records []interface{}, alias string) (int, error) {
56 for i, er := range records {
57 extensionRecord := er.(map[string]interface{})
58 if extensionRecord["alias"] == alias {
59 return i, nil
60 }
61 }
62 return 0, ErrNotImplemented
63}
64
65func extensions(er ExtensionsResult) ([]interface{}, error) {
Jon Perritte76ade72014-08-08 20:43:58 -050066 ei, ok := er["extensions"]
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080067 if !ok {
68 return nil, ErrNotImplemented
69 }
Jon Perritte76ade72014-08-08 20:43:58 -050070 e := ei.(map[string]interface{})
Jon Perritte76ade72014-08-08 20:43:58 -050071 vi, ok := e["values"]
Jon Perritte76ade72014-08-08 20:43:58 -050072 if !ok {
73 return nil, ErrNotImplemented
74 }
75 v := vi.([]interface{})
76 return v, nil
Samuel A. Falvo II2b963212014-02-09 02:12:30 -080077}
78
79// Aliases returns the set of extension handles, or "aliases" as OpenStack calls them.
80// These are not the names of the extensions, but rather opaque, symbolic monikers for their corresponding extension.
81// Use the ExtensionDetailsByAlias() method to query more information for an extension if desired.
82func (er ExtensionsResult) Aliases() ([]string, error) {
83 e, err := extensions(er)
84 if err != nil {
85 return nil, err
86 }
87 aliases := make([]string, len(e))
88 for i, ex := range e {
89 ext := ex.(map[string]interface{})
90 extn, ok := ext["alias"]
91 if ok {
92 aliases[i] = extn.(string)
93 }
94 }
95 return aliases, nil
96}