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