forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_runtimeconfig_variable.go
196 lines (160 loc) · 5.68 KB
/
resource_runtimeconfig_variable.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/runtimeconfig/v1beta1"
"regexp"
)
func resourceRuntimeconfigVariable() *schema.Resource {
return &schema.Resource{
Create: resourceRuntimeconfigVariableCreate,
Read: resourceRuntimeconfigVariableRead,
Update: resourceRuntimeconfigVariableUpdate,
Delete: resourceRuntimeconfigVariableDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"parent": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"text"},
},
"text": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"value"},
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceRuntimeconfigVariableCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
variable, parent, err := newRuntimeconfigVariableFromResourceData(d, project)
if err != nil {
return err
}
createdVariable, err := config.clientRuntimeconfig.Projects.Configs.Variables.Create(resourceRuntimeconfigFullName(project, parent), variable).Do()
if err != nil {
return err
}
d.SetId(createdVariable.Name)
return setRuntimeConfigVariableToResourceData(d, project, *createdVariable)
}
func resourceRuntimeconfigVariableRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
fullName := d.Id()
createdVariable, err := config.clientRuntimeconfig.Projects.Configs.Variables.Get(fullName).Do()
if err != nil {
return err
}
return setRuntimeConfigVariableToResourceData(d, project, *createdVariable)
}
func resourceRuntimeconfigVariableUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
// Update works more like an 'overwrite' method - we build a new runtimeconfig.Variable struct and it becomes the
// new config. This means our Update logic looks an awful lot like Create (and hence, doesn't use
// schema.ResourceData.hasChange()).
variable, _, err := newRuntimeconfigVariableFromResourceData(d, project)
if err != nil {
return err
}
createdVariable, err := config.clientRuntimeconfig.Projects.Configs.Variables.Update(variable.Name, variable).Do()
if err != nil {
return err
}
return setRuntimeConfigVariableToResourceData(d, project, *createdVariable)
}
func resourceRuntimeconfigVariableDelete(d *schema.ResourceData, meta interface{}) error {
fullName := d.Id()
config := meta.(*Config)
_, err := config.clientRuntimeconfig.Projects.Configs.Variables.Delete(fullName).Do()
if err != nil {
return err
}
d.SetId("")
return nil
}
// resourceRuntimeconfigVariableFullName turns a given project, runtime config name, and a 'short name' for a runtime
// config variable into a full name (e.g. projects/my-project/configs/my-config/variables/my-variable).
func resourceRuntimeconfigVariableFullName(project, config, name string) string {
return fmt.Sprintf("projects/%s/configs/%s/variables/%s", project, config, name)
}
// resourceRuntimeconfigVariableParseFullName parses a full name
// (e.g. projects/my-project/configs/my-config/variables/my-variable) by parsing out the
// project, runtime config name, and the short name. Returns "", "", "", err upon error.
func resourceRuntimeconfigVariableParseFullName(fullName string) (project, config, name string, err error) {
re := regexp.MustCompile("^projects/([^/]+)/configs/([^/]+)/variables/(.+)$")
matches := re.FindStringSubmatch(fullName)
if matches == nil {
return "", "", "", fmt.Errorf("Given full name doesn't match expected regexp; fullname = '%s'", fullName)
}
return matches[1], matches[2], matches[3], nil
}
// newRuntimeconfigVariableFromResourceData builds a new runtimeconfig.Variable struct from the data stored in a
// schema.ResourceData. Also returns the full name of the parent. Returns nil, "", err upon error.
func newRuntimeconfigVariableFromResourceData(d *schema.ResourceData, project string) (variable *runtimeconfig.Variable, parent string, err error) {
// Validate that both text and value are not set
text, textSet := d.GetOk("text")
value, valueSet := d.GetOk("value")
if !textSet && !valueSet {
return nil, "", fmt.Errorf("You must specify one of value or text.")
}
// TODO(selmanj) here we assume it's a simple name, not a full name. Should probably support full name as well
parent = d.Get("parent").(string)
name := d.Get("name").(string)
fullName := resourceRuntimeconfigVariableFullName(project, parent, name)
variable = &runtimeconfig.Variable{
Name: fullName,
}
if textSet {
variable.Text = text.(string)
} else {
variable.Value = value.(string)
}
return variable, parent, nil
}
// setRuntimeConfigVariableToResourceData stores a provided runtimeconfig.Variable struct inside a schema.ResourceData.
func setRuntimeConfigVariableToResourceData(d *schema.ResourceData, project string, variable runtimeconfig.Variable) error {
varProject, parent, name, err := resourceRuntimeconfigVariableParseFullName(variable.Name)
if err != nil {
return err
}
d.Set("name", name)
d.Set("parent", parent)
if varProject != project {
d.Set("project", varProject)
}
d.Set("value", variable.Value)
d.Set("text", variable.Text)
d.Set("update_time", variable.UpdateTime)
return nil
}