5
5
"fmt"
6
6
"log"
7
7
8
- "github.com/hashicorp/terraform/helper/customdiff"
9
8
"github.com/hashicorp/terraform/helper/schema"
10
9
"github.com/hashicorp/terraform/helper/validation"
11
10
@@ -16,13 +15,7 @@ func resourceBigtableInstance() *schema.Resource {
16
15
return & schema.Resource {
17
16
Create : resourceBigtableInstanceCreate ,
18
17
Read : resourceBigtableInstanceRead ,
19
- // TODO: Update is only needed because we're doing forcenew in customizediff
20
- // when we're done with the deprecation, we can drop customizediff and make cluster forcenew
21
- Update : schema .Noop ,
22
18
Delete : resourceBigtableInstanceDestroy ,
23
- CustomizeDiff : customdiff .All (
24
- resourceBigTableInstanceClusterCustomizeDiff ,
25
- ),
26
19
27
20
Schema : map [string ]* schema.Schema {
28
21
"name" : {
@@ -31,28 +24,20 @@ func resourceBigtableInstance() *schema.Resource {
31
24
ForceNew : true ,
32
25
},
33
26
34
- "cluster_id" : {
35
- Type : schema .TypeString ,
36
- Optional : true ,
37
- Deprecated : "Use cluster instead." ,
38
- ConflictsWith : []string {"cluster" },
39
- },
40
-
41
27
"cluster" : {
42
- Type : schema .TypeSet ,
43
- Optional : true ,
44
- MaxItems : 1 ,
45
- ConflictsWith : [] string { "cluster_id" , "zone" , "num_nodes" , "storage_type" } ,
28
+ Type : schema .TypeSet ,
29
+ Required : true ,
30
+ ForceNew : true ,
31
+ MaxItems : 2 ,
46
32
Elem : & schema.Resource {
47
33
Schema : map [string ]* schema.Schema {
48
34
"cluster_id" : {
49
35
Type : schema .TypeString ,
50
- Optional : true ,
36
+ Required : true ,
51
37
},
52
38
"zone" : {
53
39
Type : schema .TypeString ,
54
- Optional : true ,
55
- Computed : true ,
40
+ Required : true ,
56
41
},
57
42
"num_nodes" : {
58
43
Type : schema .TypeInt ,
@@ -68,29 +53,13 @@ func resourceBigtableInstance() *schema.Resource {
68
53
},
69
54
},
70
55
71
- "zone" : {
72
- Type : schema .TypeString ,
73
- Optional : true ,
74
- Computed : true ,
75
- ForceNew : true ,
76
- Deprecated : "Use cluster instead." ,
77
- ConflictsWith : []string {"cluster" },
78
- },
79
-
80
56
"display_name" : {
81
57
Type : schema .TypeString ,
82
58
Optional : true ,
83
59
ForceNew : true ,
84
60
Computed : true ,
85
61
},
86
62
87
- "num_nodes" : {
88
- Type : schema .TypeInt ,
89
- Optional : true ,
90
- Deprecated : "Use cluster instead." ,
91
- ConflictsWith : []string {"cluster" },
92
- },
93
-
94
63
"instance_type" : {
95
64
Type : schema .TypeString ,
96
65
Optional : true ,
@@ -99,67 +68,42 @@ func resourceBigtableInstance() *schema.Resource {
99
68
ValidateFunc : validation .StringInSlice ([]string {"DEVELOPMENT" , "PRODUCTION" }, false ),
100
69
},
101
70
102
- "storage_type" : {
103
- Type : schema .TypeString ,
104
- Optional : true ,
105
- Default : "SSD" ,
106
- ValidateFunc : validation .StringInSlice ([]string {"SSD" , "HDD" }, false ),
107
- Deprecated : "Use cluster instead." ,
108
- ConflictsWith : []string {"cluster" },
109
- },
110
-
111
71
"project" : {
112
72
Type : schema .TypeString ,
113
73
Optional : true ,
114
74
Computed : true ,
115
75
ForceNew : true ,
116
76
},
117
- },
118
- }
119
- }
120
77
121
- func resourceBigTableInstanceClusterCustomizeDiff (d * schema.ResourceDiff , meta interface {}) error {
122
- if d .Get ("cluster_id" ).(string ) == "" && d .Get ("cluster.#" ).(int ) == 0 {
123
- return fmt .Errorf ("At least one cluster must be set." )
124
- }
125
- if ! d .HasChange ("cluster_id" ) && ! d .HasChange ("zone" ) && ! d .HasChange ("num_nodes" ) &&
126
- ! d .HasChange ("storage_type" ) && ! d .HasChange ("cluster" ) {
127
- return nil
128
- }
129
- if d .Get ("cluster.#" ).(int ) == 1 {
130
- // if we have exactly one cluster, and it has the same values as the old top-level
131
- // values, we can assume the user is trying to go from the deprecated values to the
132
- // new values, and we shouldn't ForceNew. We know that the top-level values aren't
133
- // set, because they ConflictWith cluster.
134
- oldID , _ := d .GetChange ("cluster_id" )
135
- oldNodes , _ := d .GetChange ("num_nodes" )
136
- oldZone , _ := d .GetChange ("zone" )
137
- oldStorageType , _ := d .GetChange ("storage_type" )
138
- new := d .Get ("cluster" ).(* schema.Set ).List ()[0 ].(map [string ]interface {})
139
-
140
- if oldID .(string ) == new ["cluster_id" ].(string ) &&
141
- oldNodes .(int ) == new ["num_nodes" ].(int ) &&
142
- oldZone .(string ) == new ["zone" ].(string ) &&
143
- oldStorageType .(string ) == new ["storage_type" ].(string ) {
144
- return nil
145
- }
146
- }
147
- if d .HasChange ("cluster_id" ) {
148
- d .ForceNew ("cluster_id" )
149
- }
150
- if d .HasChange ("cluster" ) {
151
- d .ForceNew ("cluster" )
152
- }
153
- if d .HasChange ("zone" ) {
154
- d .ForceNew ("zone" )
155
- }
156
- if d .HasChange ("num_nodes" ) {
157
- d .ForceNew ("num_nodes" )
158
- }
159
- if d .HasChange ("storage_type" ) {
160
- d .ForceNew ("storage_type" )
78
+ "cluster_id" : {
79
+ Type : schema .TypeString ,
80
+ Optional : true ,
81
+ Computed : true ,
82
+ Removed : "Use cluster instead." ,
83
+ },
84
+
85
+ "zone" : {
86
+ Type : schema .TypeString ,
87
+ Optional : true ,
88
+ Computed : true ,
89
+ Removed : "Use cluster instead." ,
90
+ },
91
+
92
+ "num_nodes" : {
93
+ Type : schema .TypeInt ,
94
+ Optional : true ,
95
+ Computed : true ,
96
+ Removed : "Use cluster instead." ,
97
+ },
98
+
99
+ "storage_type" : {
100
+ Type : schema .TypeString ,
101
+ Optional : true ,
102
+ Computed : true ,
103
+ Removed : "Use cluster instead." ,
104
+ },
105
+ },
161
106
}
162
- return nil
163
107
}
164
108
165
109
func resourceBigtableInstanceCreate (d * schema.ResourceData , meta interface {}) error {
@@ -188,31 +132,9 @@ func resourceBigtableInstanceCreate(d *schema.ResourceData, meta interface{}) er
188
132
conf .InstanceType = bigtable .PRODUCTION
189
133
}
190
134
191
- if d .Get ("cluster.#" ).(int ) > 0 {
192
- // expand cluster
193
- conf .Clusters = expandBigtableClusters (d .Get ("cluster" ).(* schema.Set ).List (), conf .InstanceID , config .Zone )
194
- if err != nil {
195
- return fmt .Errorf ("error expanding clusters: %s" , err .Error ())
196
- }
197
- } else {
198
- // TODO: remove this when we're done with the deprecation period
199
- zone , err := getZone (d , config )
200
- if err != nil {
201
- return err
202
- }
203
- cluster := bigtable.ClusterConfig {
204
- InstanceID : conf .InstanceID ,
205
- NumNodes : int32 (d .Get ("num_nodes" ).(int )),
206
- Zone : zone ,
207
- ClusterID : d .Get ("cluster_id" ).(string ),
208
- }
209
- switch d .Get ("storage_type" ).(string ) {
210
- case "HDD" :
211
- cluster .StorageType = bigtable .HDD
212
- case "SSD" :
213
- cluster .StorageType = bigtable .SSD
214
- }
215
- conf .Clusters = append (conf .Clusters , cluster )
135
+ conf .Clusters = expandBigtableClusters (d .Get ("cluster" ).(* schema.Set ).List (), conf .InstanceID )
136
+ if err != nil {
137
+ return fmt .Errorf ("error expanding clusters: %s" , err .Error ())
216
138
}
217
139
218
140
c , err := config .bigtableClientFactory .NewInstanceAdminClient (project )
@@ -256,37 +178,27 @@ func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) erro
256
178
}
257
179
258
180
d .Set ("project" , project )
259
- if d .Get ("cluster.#" ).(int ) > 0 {
260
- clusters := d .Get ("cluster" ).(* schema.Set ).List ()
261
- clusterState := []map [string ]interface {}{}
262
- for _ , cl := range clusters {
263
- cluster := cl .(map [string ]interface {})
264
- clus , err := c .GetCluster (ctx , instance .Name , cluster ["cluster_id" ].(string ))
265
- if err != nil {
266
- if isGoogleApiErrorWithCode (err , 404 ) {
267
- log .Printf ("[WARN] Cluster %q not found, not setting it in state" , cluster ["cluster_id" ].(string ))
268
- continue
269
- }
270
- return fmt .Errorf ("Error retrieving cluster %q: %s" , cluster ["cluster_id" ].(string ), err .Error ())
271
- }
272
- clusterState = append (clusterState , flattenBigtableCluster (clus , cluster ["storage_type" ].(string )))
273
- }
274
- err = d .Set ("cluster" , clusterState )
275
- if err != nil {
276
- return fmt .Errorf ("Error setting clusters in state: %s" , err .Error ())
277
- }
278
- d .Set ("cluster_id" , "" )
279
- d .Set ("zone" , "" )
280
- d .Set ("num_nodes" , 0 )
281
- d .Set ("storage_type" , "SSD" )
282
- } else {
283
- // TODO remove this when we're done with our deprecation period
284
- zone , err := getZone (d , config )
181
+
182
+ clusters := d .Get ("cluster" ).(* schema.Set ).List ()
183
+ clusterState := []map [string ]interface {}{}
184
+ for _ , cl := range clusters {
185
+ cluster := cl .(map [string ]interface {})
186
+ clus , err := c .GetCluster (ctx , instance .Name , cluster ["cluster_id" ].(string ))
285
187
if err != nil {
286
- return err
188
+ if isGoogleApiErrorWithCode (err , 404 ) {
189
+ log .Printf ("[WARN] Cluster %q not found, not setting it in state" , cluster ["cluster_id" ].(string ))
190
+ continue
191
+ }
192
+ return fmt .Errorf ("Error retrieving cluster %q: %s" , cluster ["cluster_id" ].(string ), err .Error ())
287
193
}
288
- d .Set ("zone" , zone )
194
+ clusterState = append (clusterState , flattenBigtableCluster (clus , cluster ["storage_type" ].(string )))
195
+ }
196
+
197
+ err = d .Set ("cluster" , clusterState )
198
+ if err != nil {
199
+ return fmt .Errorf ("Error setting clusters in state: %s" , err .Error ())
289
200
}
201
+
290
202
d .Set ("name" , instance .Name )
291
203
d .Set ("display_name" , instance .DisplayName )
292
204
@@ -329,14 +241,11 @@ func flattenBigtableCluster(c *bigtable.ClusterInfo, storageType string) map[str
329
241
}
330
242
}
331
243
332
- func expandBigtableClusters (clusters []interface {}, instanceID string , defaultZone string ) []bigtable.ClusterConfig {
244
+ func expandBigtableClusters (clusters []interface {}, instanceID string ) []bigtable.ClusterConfig {
333
245
results := make ([]bigtable.ClusterConfig , 0 , len (clusters ))
334
246
for _ , c := range clusters {
335
247
cluster := c .(map [string ]interface {})
336
- zone := defaultZone
337
- if confZone , ok := cluster ["zone" ]; ok {
338
- zone = confZone .(string )
339
- }
248
+ zone := cluster ["zone" ].(string )
340
249
var storageType bigtable.StorageType
341
250
switch cluster ["storage_type" ].(string ) {
342
251
case "SSD" :
0 commit comments