Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 1 | package stacks |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/rackspace/gophercloud" |
| 13 | "gopkg.in/yaml.v2" |
| 14 | ) |
| 15 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 16 | // Client is an interface that expects a Get method similar to http.Get. This |
| 17 | // is needed for unit testing, since we can mock an http client. Thus, the |
| 18 | // client will usually be an http.Client EXCEPT in unit tests. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 19 | type Client interface { |
| 20 | Get(string) (*http.Response, error) |
| 21 | } |
| 22 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 23 | // TE is a base structure for both Template and Environment |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 24 | type TE struct { |
| 25 | // Bin stores the contents of the template or environment. |
| 26 | Bin []byte |
| 27 | // URL stores the URL of the template. This is allowed to be a 'file://' |
| 28 | // for local files. |
| 29 | URL string |
| 30 | // Parsed contains a parsed version of Bin. Since there are 2 different |
| 31 | // fields referring to the same value, you must be careful when accessing |
| 32 | // this filed. |
| 33 | Parsed map[string]interface{} |
| 34 | // Files contains a mapping between the urls in templates to their contents. |
| 35 | Files map[string]string |
| 36 | // fileMaps is a map used internally when determining Files. |
| 37 | fileMaps map[string]string |
| 38 | // baseURL represents the location of the template or environment file. |
| 39 | baseURL string |
| 40 | // client is an interface which allows TE to fetch contents from URLS |
| 41 | client Client |
| 42 | } |
| 43 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 44 | // Fetch fetches the contents of a TE from its URL. Once a TE structure has a |
| 45 | // URL, call the fetch method to fetch the contents. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 46 | func (t *TE) Fetch() error { |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 47 | // if the baseURL is not provided, use the current directors as the base URL |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 48 | if t.baseURL == "" { |
| 49 | u, err := getBasePath() |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | t.baseURL = u |
| 54 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 55 | |
| 56 | // if the contents are already present, do nothing. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 57 | if t.Bin != nil { |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 58 | return nil |
| 59 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 60 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 61 | // get a fqdn from the URL using the baseURL of the TE. For local files, |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 62 | // the URL's will have the `file` scheme. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 63 | u, err := gophercloud.NormalizePathURL(t.baseURL, t.URL) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | t.URL = u |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 68 | |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 69 | // get an HTTP client if none present |
| 70 | if t.client == nil { |
| 71 | t.client = getHTTPClient() |
| 72 | } |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 73 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 74 | // use the client to fetch the contents of the TE |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 75 | resp, err := t.client.Get(t.URL) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | defer resp.Body.Close() |
| 80 | body, err := ioutil.ReadAll(resp.Body) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | t.Bin = body |
| 85 | return nil |
| 86 | } |
| 87 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 88 | // get the basepath of the TE |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 89 | func getBasePath() (string, error) { |
| 90 | basePath, err := filepath.Abs(".") |
| 91 | if err != nil { |
| 92 | return "", err |
| 93 | } |
| 94 | u, err := gophercloud.NormalizePathURL("", basePath) |
| 95 | if err != nil { |
| 96 | return "", err |
| 97 | } |
| 98 | return u, nil |
| 99 | } |
| 100 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 101 | // get a an HTTP client to retrieve URL's. This client allows the use of `file` |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 102 | // scheme since we may need to fetch files from users filesystem |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 103 | func getHTTPClient() Client { |
| 104 | transport := &http.Transport{} |
| 105 | transport.RegisterProtocol("file", http.NewFileTransport(http.Dir("/"))) |
| 106 | return &http.Client{Transport: transport} |
| 107 | } |
| 108 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 109 | // Parse will parse the contents and then validate. The contents MUST be either JSON or YAML. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 110 | func (t *TE) Parse() error { |
| 111 | if err := t.Fetch(); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | if jerr := json.Unmarshal(t.Bin, &t.Parsed); jerr != nil { |
| 115 | if yerr := yaml.Unmarshal(t.Bin, &t.Parsed); yerr != nil { |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 116 | return fmt.Errorf("Data in neither json nor yaml format.") |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return t.Validate() |
| 120 | } |
| 121 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 122 | // Validate validates the contents of TE |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 123 | func (t *TE) Validate() error { |
| 124 | return nil |
| 125 | } |
| 126 | |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 127 | // igfunc is a parameter used by GetFileContents and GetRRFileContents to check |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 128 | // for valid URL's. |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 129 | type igFunc func(string, interface{}) bool |
| 130 | |
| 131 | // convert map[interface{}]interface{} to map[string]interface{} |
| 132 | func toStringKeys(m interface{}) (map[string]interface{}, error) { |
| 133 | switch m.(type) { |
| 134 | case map[string]interface{}, map[interface{}]interface{}: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 135 | typedMap := make(map[string]interface{}) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 136 | if _, ok := m.(map[interface{}]interface{}); ok { |
| 137 | for k, v := range m.(map[interface{}]interface{}) { |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 138 | typedMap[k.(string)] = v |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 139 | } |
| 140 | } else { |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 141 | typedMap = m.(map[string]interface{}) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 142 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 143 | return typedMap, nil |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 144 | default: |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 145 | return nil, fmt.Errorf("Expected a map of type map[string]interface{} or map[interface{}]interface{}, actual type: %v", reflect.TypeOf(m)) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 146 | |
| 147 | } |
| 148 | } |
| 149 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 150 | // fix the reference to files by replacing relative URL's by absolute |
Pratik Mallya | bfc6eda | 2015-09-21 15:01:18 -0500 | [diff] [blame] | 151 | // URL's |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 152 | func (t *TE) fixFileRefs() { |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 153 | tStr := string(t.Bin) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 154 | if t.fileMaps == nil { |
| 155 | return |
| 156 | } |
| 157 | for k, v := range t.fileMaps { |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 158 | tStr = strings.Replace(tStr, k, v, -1) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 159 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 160 | t.Bin = []byte(tStr) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 161 | } |