| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 1 | package configurations | 
 | 2 |  | 
 | 3 | import ( | 
| Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 4 | 	"time" | 
 | 5 |  | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 6 | 	"github.com/mitchellh/mapstructure" | 
 | 7 | 	"github.com/rackspace/gophercloud" | 
 | 8 | 	"github.com/rackspace/gophercloud/pagination" | 
 | 9 | ) | 
 | 10 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 11 | // Config represents a configuration group API resource. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 12 | type Config struct { | 
| Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 13 | 	Created              time.Time `mapstructure:"-"` | 
 | 14 | 	Updated              time.Time `mapstructure:"-"` | 
 | 15 | 	DatastoreName        string    `mapstructure:"datastore_name"` | 
 | 16 | 	DatastoreVersionID   string    `mapstructure:"datastore_version_id"` | 
 | 17 | 	DatastoreVersionName string    `mapstructure:"datastore_version_name"` | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 18 | 	Description          string | 
 | 19 | 	ID                   string | 
 | 20 | 	Name                 string | 
 | 21 | 	Values               map[string]interface{} | 
 | 22 | } | 
 | 23 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 24 | // ConfigPage contains a page of Config resources in a paginated collection. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 25 | type ConfigPage struct { | 
 | 26 | 	pagination.SinglePageBase | 
 | 27 | } | 
 | 28 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 29 | // IsEmpty indicates whether a ConfigPage is empty. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 30 | func (r ConfigPage) IsEmpty() (bool, error) { | 
 | 31 | 	is, err := ExtractConfigs(r) | 
 | 32 | 	if err != nil { | 
 | 33 | 		return true, err | 
 | 34 | 	} | 
 | 35 | 	return len(is) == 0, nil | 
 | 36 | } | 
 | 37 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 38 | // ExtractConfigs will retrieve a slice of Config structs from a page. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 39 | func ExtractConfigs(page pagination.Page) ([]Config, error) { | 
 | 40 | 	casted := page.(ConfigPage).Body | 
 | 41 |  | 
 | 42 | 	var resp struct { | 
 | 43 | 		Configs []Config `mapstructure:"configurations" json:"configurations"` | 
 | 44 | 	} | 
 | 45 |  | 
 | 46 | 	err := mapstructure.Decode(casted, &resp) | 
| Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 47 |  | 
 | 48 | 	var vals []interface{} | 
 | 49 | 	switch (casted).(type) { | 
 | 50 | 	case interface{}: | 
 | 51 | 		vals = casted.(map[string]interface{})["configurations"].([]interface{}) | 
 | 52 | 	} | 
 | 53 |  | 
 | 54 | 	for i, v := range vals { | 
 | 55 | 		val := v.(map[string]interface{}) | 
 | 56 |  | 
 | 57 | 		if t, ok := val["created"].(string); ok && t != "" { | 
 | 58 | 			creationTime, err := time.Parse(time.RFC3339, t) | 
 | 59 | 			if err != nil { | 
 | 60 | 				return resp.Configs, err | 
 | 61 | 			} | 
 | 62 | 			resp.Configs[i].Created = creationTime | 
 | 63 | 		} | 
 | 64 |  | 
 | 65 | 		if t, ok := val["updated"].(string); ok && t != "" { | 
 | 66 | 			updatedTime, err := time.Parse(time.RFC3339, t) | 
 | 67 | 			if err != nil { | 
 | 68 | 				return resp.Configs, err | 
 | 69 | 			} | 
 | 70 | 			resp.Configs[i].Updated = updatedTime | 
 | 71 | 		} | 
 | 72 | 	} | 
 | 73 |  | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 74 | 	return resp.Configs, err | 
 | 75 | } | 
 | 76 |  | 
 | 77 | type commonResult struct { | 
 | 78 | 	gophercloud.Result | 
 | 79 | } | 
 | 80 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 81 | // Extract will retrieve a Config resource from an operation result. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 82 | func (r commonResult) Extract() (*Config, error) { | 
 | 83 | 	if r.Err != nil { | 
 | 84 | 		return nil, r.Err | 
 | 85 | 	} | 
 | 86 |  | 
 | 87 | 	var response struct { | 
 | 88 | 		Config Config `mapstructure:"configuration"` | 
 | 89 | 	} | 
 | 90 |  | 
 | 91 | 	err := mapstructure.Decode(r.Body, &response) | 
| Jamie Hannaford | e65ad95 | 2015-11-16 14:05:11 +0100 | [diff] [blame] | 92 | 	val := r.Body.(map[string]interface{})["configuration"].(map[string]interface{}) | 
 | 93 |  | 
 | 94 | 	if t, ok := val["created"].(string); ok && t != "" { | 
 | 95 | 		creationTime, err := time.Parse(time.RFC3339, t) | 
 | 96 | 		if err != nil { | 
 | 97 | 			return &response.Config, err | 
 | 98 | 		} | 
 | 99 | 		response.Config.Created = creationTime | 
 | 100 | 	} | 
 | 101 |  | 
 | 102 | 	if t, ok := val["updated"].(string); ok && t != "" { | 
 | 103 | 		updatedTime, err := time.Parse(time.RFC3339, t) | 
 | 104 | 		if err != nil { | 
 | 105 | 			return &response.Config, err | 
 | 106 | 		} | 
 | 107 | 		response.Config.Updated = updatedTime | 
 | 108 | 	} | 
 | 109 |  | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 110 | 	return &response.Config, err | 
 | 111 | } | 
 | 112 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 113 | // GetResult represents the result of a Get operation. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 114 | type GetResult struct { | 
 | 115 | 	commonResult | 
 | 116 | } | 
 | 117 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 118 | // CreateResult represents the result of a Create operation. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 119 | type CreateResult struct { | 
 | 120 | 	commonResult | 
 | 121 | } | 
 | 122 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 123 | // UpdateResult represents the result of an Update operation. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 124 | type UpdateResult struct { | 
 | 125 | 	gophercloud.ErrResult | 
 | 126 | } | 
 | 127 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 128 | // ReplaceResult represents the result of a Replace operation. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 129 | type ReplaceResult struct { | 
 | 130 | 	gophercloud.ErrResult | 
 | 131 | } | 
 | 132 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 133 | // DeleteResult represents the result of a Delete operation. | 
| Jamie Hannaford | ed7f453 | 2015-02-17 14:56:30 +0100 | [diff] [blame] | 134 | type DeleteResult struct { | 
 | 135 | 	gophercloud.ErrResult | 
 | 136 | } | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 137 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 138 | // Param represents a configuration parameter API resource. | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 139 | type Param struct { | 
 | 140 | 	Max             int | 
 | 141 | 	Min             int | 
 | 142 | 	Name            string | 
 | 143 | 	RestartRequired bool `mapstructure:"restart_required" json:"restart_required"` | 
 | 144 | 	Type            string | 
 | 145 | } | 
 | 146 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 147 | // ParamPage contains a page of Param resources in a paginated collection. | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 148 | type ParamPage struct { | 
 | 149 | 	pagination.SinglePageBase | 
 | 150 | } | 
 | 151 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 152 | // IsEmpty indicates whether a ParamPage is empty. | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 153 | func (r ParamPage) IsEmpty() (bool, error) { | 
 | 154 | 	is, err := ExtractParams(r) | 
 | 155 | 	if err != nil { | 
 | 156 | 		return true, err | 
 | 157 | 	} | 
 | 158 | 	return len(is) == 0, nil | 
 | 159 | } | 
 | 160 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 161 | // ExtractParams will retrieve a slice of Param structs from a page. | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 162 | func ExtractParams(page pagination.Page) ([]Param, error) { | 
 | 163 | 	casted := page.(ParamPage).Body | 
 | 164 |  | 
 | 165 | 	var resp struct { | 
 | 166 | 		Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"` | 
 | 167 | 	} | 
 | 168 |  | 
 | 169 | 	err := mapstructure.Decode(casted, &resp) | 
 | 170 | 	return resp.Params, err | 
 | 171 | } | 
 | 172 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 173 | // ParamResult represents the result of an operation which retrieves details | 
 | 174 | // about a particular configuration param. | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 175 | type ParamResult struct { | 
 | 176 | 	gophercloud.Result | 
 | 177 | } | 
 | 178 |  | 
| Jamie Hannaford | b0d267b | 2015-02-19 11:59:53 +0100 | [diff] [blame] | 179 | // Extract will retrieve a param from an operation result. | 
| Jamie Hannaford | 23867bb | 2015-02-17 15:56:48 +0100 | [diff] [blame] | 180 | func (r ParamResult) Extract() (*Param, error) { | 
 | 181 | 	if r.Err != nil { | 
 | 182 | 		return nil, r.Err | 
 | 183 | 	} | 
 | 184 |  | 
 | 185 | 	var param Param | 
 | 186 |  | 
 | 187 | 	err := mapstructure.Decode(r.Body, ¶m) | 
 | 188 | 	return ¶m, err | 
 | 189 | } |