Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 1 | package stacks |
| 2 | |
| 3 | import ( |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 4 | "fmt" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 5 | "reflect" |
| 6 | "strings" |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 7 | |
| 8 | "github.com/gophercloud/gophercloud" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 11 | // Template is a structure that represents OpenStack Heat templates |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 12 | type Template struct { |
| 13 | TE |
| 14 | } |
| 15 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 16 | // TemplateFormatVersions is a map containing allowed variations of the template format version |
| 17 | // Note that this contains the permitted variations of the _keys_ not the values. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 18 | var TemplateFormatVersions = map[string]bool{ |
| 19 | "HeatTemplateFormatVersion": true, |
| 20 | "heat_template_version": true, |
| 21 | "AWSTemplateFormatVersion": true, |
| 22 | } |
| 23 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 24 | // Validate validates the contents of the Template |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 25 | func (t *Template) Validate() error { |
| 26 | if t.Parsed == nil { |
| 27 | if err := t.Parse(); err != nil { |
| 28 | return err |
| 29 | } |
| 30 | } |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 31 | var invalid string |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 32 | for key := range t.Parsed { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 33 | if _, ok := TemplateFormatVersions[key]; ok { |
| 34 | return nil |
| 35 | } |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 36 | invalid = key |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 37 | } |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 38 | return ErrInvalidTemplateFormatVersion{Version: invalid} |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 41 | // GetFileContents recursively parses a template to search for urls. These urls |
| 42 | // are assumed to point to other templates (known in OpenStack Heat as child |
| 43 | // templates). The contents of these urls are fetched and stored in the `Files` |
| 44 | // parameter of the template structure. This is the only way that a user can |
| 45 | // use child templates that are located in their filesystem; urls located on the |
| 46 | // web (e.g. on github or swift) can be fetched directly by Heat engine. |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 47 | func (t *Template) getFileContents(te interface{}, ignoreIf igFunc, recurse bool) error { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 48 | // initialize template if empty |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 49 | if t.Files == nil { |
| 50 | t.Files = make(map[string]string) |
| 51 | } |
| 52 | if t.fileMaps == nil { |
| 53 | t.fileMaps = make(map[string]string) |
| 54 | } |
| 55 | switch te.(type) { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 56 | // if te is a map |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 57 | case map[string]interface{}, map[interface{}]interface{}: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 58 | teMap, err := toStringKeys(te) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 59 | if err != nil { |
| 60 | return err |
| 61 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 62 | for k, v := range teMap { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 63 | value, ok := v.(string) |
| 64 | if !ok { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 65 | // if the value is not a string, recursively parse that value |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 66 | if err := t.getFileContents(v, ignoreIf, recurse); err != nil { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 67 | return err |
| 68 | } |
| 69 | } else if !ignoreIf(k, value) { |
| 70 | // at this point, the k, v pair has a reference to an external template. |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 71 | // The assumption of heatclient is that value v is a reference |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 72 | // to a file in the users environment |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 73 | |
| 74 | // create a new child template |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 75 | childTemplate := new(Template) |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 76 | |
| 77 | // initialize child template |
| 78 | |
| 79 | // get the base location of the child template |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 80 | baseURL, err := gophercloud.NormalizePathURL(t.baseURL, value) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | childTemplate.baseURL = baseURL |
| 85 | childTemplate.client = t.client |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 86 | |
| 87 | // fetch the contents of the child template |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 88 | if err := childTemplate.Parse(); err != nil { |
| 89 | return err |
| 90 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 91 | |
| 92 | // process child template recursively if required. This is |
| 93 | // required if the child template itself contains references to |
| 94 | // other templates |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 95 | if recurse { |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 96 | if err := childTemplate.getFileContents(childTemplate.Parsed, ignoreIf, recurse); err != nil { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 97 | return err |
| 98 | } |
| 99 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 100 | // update parent template with current child templates' content. |
| 101 | // At this point, the child template has been parsed recursively. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 102 | t.fileMaps[value] = childTemplate.URL |
| 103 | t.Files[childTemplate.URL] = string(childTemplate.Bin) |
| 104 | |
| 105 | } |
| 106 | } |
| 107 | return nil |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 108 | // if te is a slice, call the function on each element of the slice. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 109 | case []interface{}: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 110 | teSlice := te.([]interface{}) |
| 111 | for i := range teSlice { |
| 112 | if err := t.getFileContents(teSlice[i], ignoreIf, recurse); err != nil { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 113 | return err |
| 114 | } |
| 115 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 116 | // if te is anything else, return |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 117 | case string, bool, float64, nil, int: |
| 118 | return nil |
| 119 | default: |
Jon Perritt | fea9073 | 2016-03-15 02:57:05 -0500 | [diff] [blame] | 120 | return gophercloud.ErrUnexpectedType{Actual: fmt.Sprintf("%v", reflect.TypeOf(te))} |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | // function to choose keys whose values are other template files |
| 126 | func ignoreIfTemplate(key string, value interface{}) bool { |
| 127 | // key must be either `get_file` or `type` for value to be a URL |
| 128 | if key != "get_file" && key != "type" { |
| 129 | return true |
| 130 | } |
| 131 | // value must be a string |
| 132 | valueString, ok := value.(string) |
| 133 | if !ok { |
| 134 | return true |
| 135 | } |
| 136 | // `.template` and `.yaml` are allowed suffixes for template URLs when referred to by `type` |
| 137 | if key == "type" && !(strings.HasSuffix(valueString, ".template") || strings.HasSuffix(valueString, ".yaml")) { |
| 138 | return true |
| 139 | } |
| 140 | return false |
| 141 | } |