forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization_helpers.go
104 lines (96 loc) · 3.32 KB
/
serialization_helpers.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"encoding/json"
"fmt"
"regexp"
"strings"
cloudresourcemanager "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudresourcemanager"
cloudresourcemanagerBeta "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudresourcemanager/beta"
cloudresourcemanagerAlpha "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/cloudresourcemanager/alpha"
)
func serializeAlphaProjectToHCL(r cloudresourcemanagerAlpha.Project, hasGAEquivalent bool) (string, error) {
b, err := json.Marshal(r)
if err != nil {
return "", err
}
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
return "", err
}
return serializeProjectToHCL(m, hasGAEquivalent)
}
func serializeBetaProjectToHCL(r cloudresourcemanagerBeta.Project, hasGAEquivalent bool) (string, error) {
b, err := json.Marshal(r)
if err != nil {
return "", err
}
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
return "", err
}
return serializeProjectToHCL(m, hasGAEquivalent)
}
func serializeGAProjectToHCL(r cloudresourcemanager.Project, hasGAEquivalent bool) (string, error) {
b, err := json.Marshal(r)
if err != nil {
return "", err
}
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
return "", err
}
return serializeProjectToHCL(m, hasGAEquivalent)
}
func serializeProjectToHCL(m map[string]interface{}, hasGAEquivalent bool) (string, error) {
outputConfig := "resource \"google_project\" \"output\" {\n"
if name, ok := m["name"]; ok {
outputConfig += fmt.Sprintf("\tproject_id = %#v\n", name)
outputConfig += fmt.Sprintf("\tname = %#v\n", name)
} else {
return "", fmt.Errorf("project id was not provided")
}
if parentInterface, ok := m["parent"]; ok {
parent, ok := parentInterface.(string)
if !ok {
return "", fmt.Errorf("non-string parent %v", parentInterface)
}
if strings.HasPrefix(parent, "folders/") {
outputConfig += fmt.Sprintf("\tfolder_id = %#v\n", strings.TrimPrefix(parent, "folders/"))
} else if strings.HasPrefix(parent, "organizations/") {
outputConfig += fmt.Sprintf("\torg_id = %#v\n", strings.TrimPrefix(parent, "organizations/"))
} else {
return "", fmt.Errorf("unknown parent type %v", parent)
}
} else {
return "", fmt.Errorf("parent was not provided")
}
formatted, err := formatHCL(outputConfig + "}")
if err != nil {
// The formatter will not accept the google-beta symbol because it is injected during testing.
return "", err
}
if !hasGAEquivalent {
return withProviderLine(formatted), nil
}
return formatted, nil
}
// Returns the terraform representation of a three-state boolean value represented by a pointer to bool in DCL.
func serializeEnumBool(v interface{}) string {
b, ok := v.(*bool)
if !ok || b == nil {
return ""
}
if *b {
return "TRUE"
}
return "FALSE"
}
// Returns the given formatted hcl with the provider = google-beta line added at the end.
func withProviderLine(hcl string) string {
// Count the number of characters before the first = to determine how to space the provider line.
equalsPosition := len(regexp.MustCompile(".*=").FindString(hcl)) - 1
if equalsPosition < 11 {
equalsPosition = 11
}
return hcl[0:len(hcl)-2] + " provider" + strings.Repeat(" ", equalsPosition-10) + "= google-beta\n}"
}