Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 1 | package stacks |
| 2 | |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 3 | import "strings" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 4 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 5 | // Environment is a structure that represents stack environments |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 6 | type Environment struct { |
| 7 | TE |
| 8 | } |
| 9 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 10 | // EnvironmentSections is a map containing allowed sections in a stack environment file |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 11 | var EnvironmentSections = map[string]bool{ |
| 12 | "parameters": true, |
| 13 | "parameter_defaults": true, |
| 14 | "resource_registry": true, |
| 15 | } |
| 16 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 17 | // Validate validates the contents of the Environment |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 18 | func (e *Environment) Validate() error { |
| 19 | if e.Parsed == nil { |
| 20 | if err := e.Parse(); err != nil { |
| 21 | return err |
| 22 | } |
| 23 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 24 | for key := range e.Parsed { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 25 | if _, ok := EnvironmentSections[key]; !ok { |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 26 | return ErrInvalidEnvironment{Section: key} |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | return nil |
| 30 | } |
| 31 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 32 | // Parse environment file to resolve the URL's of the resources. This is done by |
| 33 | // reading from the `Resource Registry` section, which is why the function is |
| 34 | // named GetRRFileContents. |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 35 | func (e *Environment) getRRFileContents(ignoreIf igFunc) error { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 36 | // initialize environment if empty |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 37 | if e.Files == nil { |
| 38 | e.Files = make(map[string]string) |
| 39 | } |
| 40 | if e.fileMaps == nil { |
| 41 | e.fileMaps = make(map[string]string) |
| 42 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 43 | |
| 44 | // get the resource registry |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 45 | rr := e.Parsed["resource_registry"] |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 46 | |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 47 | // search the resource registry for URLs |
| 48 | switch rr.(type) { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 49 | // process further only if the resource registry is a map |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 50 | case map[string]interface{}, map[interface{}]interface{}: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 51 | rrMap, err := toStringKeys(rr) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 52 | if err != nil { |
| 53 | return err |
| 54 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 55 | // the resource registry might contain a base URL for the resource. If |
| 56 | // such a field is present, use it. Otherwise, use the default base URL. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 57 | var baseURL string |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 58 | if val, ok := rrMap["base_url"]; ok { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 59 | baseURL = val.(string) |
| 60 | } else { |
| 61 | baseURL = e.baseURL |
| 62 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 63 | |
| 64 | // The contents of the resource may be located in a remote file, which |
| 65 | // will be a template. Instantiate a temporary template to manage the |
| 66 | // contents. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 67 | tempTemplate := new(Template) |
| 68 | tempTemplate.baseURL = baseURL |
| 69 | tempTemplate.client = e.client |
| 70 | |
Pratik Mallya | e3086ae | 2015-09-21 15:25:16 -0500 | [diff] [blame] | 71 | // Fetch the contents of remote resource URL's |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 72 | if err = tempTemplate.getFileContents(rr, ignoreIf, false); err != nil { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 73 | return err |
| 74 | } |
Pratik Mallya | e3086ae | 2015-09-21 15:25:16 -0500 | [diff] [blame] | 75 | // check the `resources` section (if it exists) for more URL's. Note that |
| 76 | // the previous call to GetFileContents was (deliberately) not recursive |
| 77 | // as we want more control over where to look for URL's |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 78 | if val, ok := rrMap["resources"]; ok { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 79 | switch val.(type) { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 80 | // process further only if the contents are a map |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 81 | case map[string]interface{}, map[interface{}]interface{}: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 82 | resourcesMap, err := toStringKeys(val) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 83 | if err != nil { |
| 84 | return err |
| 85 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 86 | for _, v := range resourcesMap { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 87 | switch v.(type) { |
| 88 | case map[string]interface{}, map[interface{}]interface{}: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 89 | resourceMap, err := toStringKeys(v) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | var resourceBaseURL string |
| 94 | // if base_url for the resource type is defined, use it |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 95 | if val, ok := resourceMap["base_url"]; ok { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 96 | resourceBaseURL = val.(string) |
| 97 | } else { |
| 98 | resourceBaseURL = baseURL |
| 99 | } |
| 100 | tempTemplate.baseURL = resourceBaseURL |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 101 | if err := tempTemplate.getFileContents(v, ignoreIf, false); err != nil { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 102 | return err |
| 103 | } |
| 104 | } |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 105 | } |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 106 | } |
| 107 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 108 | // if the resource registry contained any URL's, store them. This can |
| 109 | // then be passed as parameter to api calls to Heat api. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 110 | e.Files = tempTemplate.Files |
| 111 | return nil |
| 112 | default: |
| 113 | return nil |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // function to choose keys whose values are other environment files |
| 118 | func ignoreIfEnvironment(key string, value interface{}) bool { |
| 119 | // base_url and hooks refer to components which cannot have urls |
| 120 | if key == "base_url" || key == "hooks" { |
| 121 | return true |
| 122 | } |
| 123 | // if value is not string, it cannot be a URL |
| 124 | valueString, ok := value.(string) |
| 125 | if !ok { |
| 126 | return true |
| 127 | } |
| 128 | // if value contains `::`, it must be a reference to another resource type |
| 129 | // e.g. OS::Nova::Server : Rackspace::Cloud::Server |
| 130 | if strings.Contains(valueString, "::") { |
| 131 | return true |
| 132 | } |
| 133 | return false |
| 134 | } |