forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
76 lines (68 loc) · 2.18 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package acsengine
import (
"encoding/json"
"strings"
)
// PrettyPrintArmTemplate will pretty print the arm template ensuring ordered by params, vars, resources, and outputs
func PrettyPrintArmTemplate(template string) (string, error) {
translateParams := [][]string{
{"\"parameters\"", "\"dparameters\""},
{"\"variables\"", "\"evariables\""},
{"\"resources\"", "\"fresources\""},
{"\"outputs\"", "\"zoutputs\""},
// there is a bug in ARM where it doesn't correctly translate back '\u003e' (>)
{">", "GREATERTHAN"},
{"<", "LESSTHAN"},
{"&", "AMPERSAND"},
}
template = translateJSON(template, translateParams, false)
var err error
if template, err = PrettyPrintJSON(template); err != nil {
return "", err
}
template = translateJSON(template, translateParams, true)
return template, nil
}
// PrettyPrintJSON will pretty print the json into
func PrettyPrintJSON(content string) (string, error) {
var data map[string]interface{}
if err := json.Unmarshal([]byte(content), &data); err != nil {
return "", err
}
prettyprint, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", err
}
return string(prettyprint), nil
}
// BuildAzureParametersFile will add the correct schema and contentversion information
func BuildAzureParametersFile(content string) (string, error) {
var parametersMap map[string]interface{}
if err := json.Unmarshal([]byte(content), ¶metersMap); err != nil {
return "", err
}
parametersAll := map[string]interface{}{}
parametersAll["$schema"] = "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#"
parametersAll["contentVersion"] = "1.0.0.0"
parametersAll["parameters"] = parametersMap
prettyprint, err := json.MarshalIndent(parametersAll, "", " ")
if err != nil {
return "", err
}
return string(prettyprint), nil
}
func translateJSON(content string, translateParams [][]string, reverseTranslate bool) string {
for _, tuple := range translateParams {
if len(tuple) != 2 {
panic("string tuples must be of size 2")
}
a := tuple[0]
b := tuple[1]
if reverseTranslate {
content = strings.Replace(content, b, a, -1)
} else {
content = strings.Replace(content, a, b, -1)
}
}
return content
}