@@ -21,145 +21,10 @@ import (
21
21
"strconv"
22
22
"time"
23
23
24
- "github.com/hashicorp/terraform/helper/customdiff"
25
24
"github.com/hashicorp/terraform/helper/schema"
26
25
compute "google.golang.org/api/compute/v1"
27
26
)
28
27
29
- func customDiffComputeSnapshotSnapshotEncryptionKeys (diff * schema.ResourceDiff , meta interface {}) error {
30
- oldConvenience , newConvenience := diff .GetChange ("snapshot_encryption_key_raw" )
31
- oldNewField , newNewField := diff .GetChange ("snapshot_encryption_key.0.raw_key" )
32
-
33
- if newConvenience != "" && newNewField != "" {
34
- return fmt .Errorf ("can't use snapshot_encryption_key_raw and snapshot_encryption_key.0.raw_key at the same time." +
35
- "If you're removing snapshot_encryption_key.0.raw_key, set the value to \" \" instead. This is due to limitations in Terraform." )
36
- }
37
-
38
- // Either field (convenience or new) has a value
39
- // and then has another different value, so we ForceNew.
40
- // We need to handle _EVERY_ ForceNew case in this diff
41
- if oldConvenience != "" && newConvenience != "" && oldConvenience != newConvenience {
42
- return diff .ForceNew ("snapshot_encryption_key_raw" )
43
- }
44
-
45
- if oldNewField != "" && newNewField != "" && oldNewField != newNewField {
46
- return diff .ForceNew ("snapshot_encryption_key.0.raw_key" )
47
- }
48
-
49
- // Our resource isn't using either field, then uses one;
50
- // ForceNew on whichever one is now using it.
51
- if (oldConvenience == "" && oldNewField == "" && newConvenience != "" ) || (oldConvenience == "" && oldNewField == "" && newNewField != "" ) {
52
- if oldConvenience == "" && newConvenience != "" {
53
- return diff .ForceNew ("snapshot_encryption_key_raw" )
54
- } else {
55
- return diff .ForceNew ("snapshot_encryption_key.0.raw_key" )
56
- }
57
- }
58
-
59
- // convenience no longer used
60
- if oldConvenience != "" && newConvenience == "" {
61
- if newNewField == "" {
62
- // convenience is being nulled, and the new field is empty as well
63
- // we've stopped using the field altogether
64
- return diff .ForceNew ("snapshot_encryption_key_raw" )
65
- } else if oldConvenience != newNewField {
66
- // convenience is being nulled, and the new field has a new value
67
- // so we ForceNew on either field
68
- return diff .ForceNew ("snapshot_encryption_key_raw" )
69
- } else {
70
- // If we reach it here, we're using the same value in the new field as we had in the convenience field
71
- }
72
- }
73
-
74
- // new no longer used
75
- // note that it will remain _set_ because of how Computed fields work
76
- // unset fields will have their values kept in state as a non-zero value
77
- if oldNewField != "" && newNewField == "" {
78
- if newConvenience == "" {
79
- // new field is being nulled, and the convenience field is empty as well
80
- // we've stopped using the field altogether
81
- return diff .ForceNew ("snapshot_encryption_key.0.raw_key" )
82
- } else if oldNewField != newConvenience {
83
- // new is being nulled, and the convenience field has a new value
84
- // so we ForceNew on either field
85
-
86
- // This stops a really opaque diffs don't match during apply error. Without this, wee see
87
- // a diff from the old state -> new state with a ForceNew at plan time (as expected!)
88
- // But during apply time the entire nested object is nil in old state unexpectedly.
89
- // So we just force the diff to match more by nilling it here, which is unclear why it
90
- // works, and probably a worse UX with some real ugly diff, but also makes the tests pass.
91
- // Computed nested fields are hard.
92
- err := diff .SetNew ("snapshot_encryption_key" , nil )
93
- if err != nil {
94
- return err
95
- }
96
-
97
- return diff .ForceNew ("snapshot_encryption_key.0.raw_key" )
98
- } else {
99
- // If we reach it here, we're using the same value in the convenience field as we had in the new field
100
- }
101
- }
102
-
103
- return nil
104
- }
105
-
106
- func customDiffComputeSnapshotSourceDiskEncryptionKeys (diff * schema.ResourceDiff , meta interface {}) error {
107
- oldConvenience , newConvenience := diff .GetChange ("source_disk_encryption_key_raw" )
108
- oldNewField , newNewField := diff .GetChange ("source_disk_encryption_key.0.raw_key" )
109
-
110
- // Either field has a value and then has another value
111
- // We need to handle _EVERY_ ForceNew case in this diff
112
- if oldConvenience != "" && newConvenience != "" && oldConvenience != newConvenience {
113
- return diff .ForceNew ("source_disk_encryption_key_raw" )
114
- }
115
-
116
- if oldNewField != "" && newNewField != "" && oldNewField != newNewField {
117
- return diff .ForceNew ("source_disk_encryption_key.0.raw_key" )
118
- }
119
-
120
- // Our resource isn't using either field, then uses one;
121
- // ForceNew on whichever one is now using it.
122
- if (oldConvenience == "" && oldNewField == "" && newConvenience != "" ) || (oldConvenience == "" && oldNewField == "" && newNewField != "" ) {
123
- if oldConvenience == "" && newConvenience != "" {
124
- return diff .ForceNew ("source_disk_encryption_key_raw" )
125
- } else {
126
- return diff .ForceNew ("source_disk_encryption_key.0.raw_key" )
127
- }
128
- }
129
-
130
- // convenience no longer used
131
- if oldConvenience != "" && newConvenience == "" {
132
- if newNewField == "" {
133
- // convenience is being nulled, and the new field is empty as well
134
- // we've stopped using the field altogether
135
- return diff .ForceNew ("source_disk_encryption_key_raw" )
136
- } else if oldConvenience != newNewField {
137
- // convenience is being nulled, and the new field has a new value
138
- // so we ForceNew on either field
139
- return diff .ForceNew ("source_disk_encryption_key_raw" )
140
- } else {
141
- // If we reach it here, we're using the same value in the new field as we had in the convenience field
142
- }
143
- }
144
-
145
- // new no longer used
146
- if oldNewField != "" && newNewField == "" {
147
- if newConvenience == "" {
148
- // new field is being nulled, and the convenience field is empty as well
149
- // we've stopped using the field altogether
150
- return diff .ForceNew ("source_disk_encryption_key.0.raw_key" )
151
- } else if newConvenience != oldNewField {
152
- // new is being nulled, and the convenience field has a new value
153
- // so we ForceNew on either field
154
- return diff .ForceNew ("source_disk_encryption_key.0.raw_key" )
155
- } else {
156
- // If we reach it here, we're using the same value in the convenience field as we had in the new field
157
- }
158
- }
159
-
160
- return nil
161
- }
162
-
163
28
func resourceComputeSnapshot () * schema.Resource {
164
29
return & schema.Resource {
165
30
Create : resourceComputeSnapshotCreate ,
@@ -177,11 +42,6 @@ func resourceComputeSnapshot() *schema.Resource {
177
42
Delete : schema .DefaultTimeout (300 * time .Second ),
178
43
},
179
44
180
- CustomizeDiff : customdiff .All (
181
- customDiffComputeSnapshotSnapshotEncryptionKeys ,
182
- customDiffComputeSnapshotSourceDiskEncryptionKeys ,
183
- ),
184
-
185
45
Schema : map [string ]* schema.Schema {
186
46
"name" : {
187
47
Type : schema .TypeString ,
@@ -206,14 +66,15 @@ func resourceComputeSnapshot() *schema.Resource {
206
66
},
207
67
"snapshot_encryption_key" : {
208
68
Type : schema .TypeList ,
209
- Computed : true ,
210
69
Optional : true ,
70
+ ForceNew : true ,
211
71
MaxItems : 1 ,
212
72
Elem : & schema.Resource {
213
73
Schema : map [string ]* schema.Schema {
214
74
"raw_key" : {
215
75
Type : schema .TypeString ,
216
76
Optional : true ,
77
+ ForceNew : true ,
217
78
Sensitive : true ,
218
79
},
219
80
"sha256" : {
@@ -226,12 +87,14 @@ func resourceComputeSnapshot() *schema.Resource {
226
87
"source_disk_encryption_key" : {
227
88
Type : schema .TypeList ,
228
89
Optional : true ,
90
+ ForceNew : true ,
229
91
MaxItems : 1 ,
230
92
Elem : & schema.Resource {
231
93
Schema : map [string ]* schema.Schema {
232
94
"raw_key" : {
233
95
Type : schema .TypeString ,
234
96
Optional : true ,
97
+ ForceNew : true ,
235
98
Sensitive : true ,
236
99
},
237
100
},
@@ -278,29 +141,29 @@ func resourceComputeSnapshot() *schema.Resource {
278
141
},
279
142
280
143
"snapshot_encryption_key_raw" : {
281
- Type : schema .TypeString ,
282
- Optional : true ,
283
- Sensitive : true ,
284
- Deprecated : "Use snapshot_encryption_key.raw_key instead." ,
144
+ Type : schema .TypeString ,
145
+ Optional : true ,
146
+ Sensitive : true ,
147
+ Removed : "Use snapshot_encryption_key.raw_key instead." ,
285
148
},
286
149
287
150
"snapshot_encryption_key_sha256" : {
288
- Type : schema .TypeString ,
289
- Computed : true ,
290
- Deprecated : "Use snapshot_encryption_key.sha256 instead." ,
151
+ Type : schema .TypeString ,
152
+ Computed : true ,
153
+ Removed : "Use snapshot_encryption_key.sha256 instead." ,
291
154
},
292
155
293
156
"source_disk_encryption_key_raw" : {
294
- Type : schema .TypeString ,
295
- Optional : true ,
296
- Sensitive : true ,
297
- Deprecated : "Use source_disk_encryption_key.raw_key instead." ,
157
+ Type : schema .TypeString ,
158
+ Optional : true ,
159
+ Sensitive : true ,
160
+ Removed : "Use source_disk_encryption_key.raw_key instead." ,
298
161
},
299
162
300
163
"source_disk_encryption_key_sha256" : {
301
- Type : schema .TypeString ,
302
- Computed : true ,
303
- Deprecated : "Use source_disk_encryption_key.sha256 instead." ,
164
+ Type : schema .TypeString ,
165
+ Computed : true ,
166
+ Removed : "Use source_disk_encryption_key.sha256 instead." ,
304
167
},
305
168
"project" : {
306
169
Type : schema .TypeString ,
@@ -713,47 +576,62 @@ func expandComputeSnapshotZone(v interface{}, d *schema.ResourceData, config *Co
713
576
714
577
func expandComputeSnapshotSnapshotEncryptionKey (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
715
578
l := v .([]interface {})
716
- req := make ([]interface {}, 0 , 1 )
717
- if len (l ) == 1 && l [0 ].(map [string ]interface {})["raw_key" ] != "" {
718
- // There is a value
719
- outMap := make (map [string ]interface {})
720
- outMap ["rawKey" ] = l [0 ].(map [string ]interface {})["raw_key" ]
721
- req = append (req , outMap )
722
- } else {
723
- // Check alternative setting?
724
- if altV , ok := d .GetOk ("snapshot_encryption_key_raw" ); ok && altV != "" {
725
- outMap := make (map [string ]interface {})
726
- outMap ["rawKey" ] = altV
727
- req = append (req , outMap )
728
- }
579
+ if len (l ) == 0 || l [0 ] == nil {
580
+ return nil , nil
581
+ }
582
+ raw := l [0 ]
583
+ original := raw .(map [string ]interface {})
584
+ transformed := make (map [string ]interface {})
585
+
586
+ transformedRawKey , err := expandComputeSnapshotSnapshotEncryptionKeyRawKey (original ["raw_key" ], d , config )
587
+ if err != nil {
588
+ return nil , err
589
+ } else if val := reflect .ValueOf (transformedRawKey ); val .IsValid () && ! isEmptyValue (val ) {
590
+ transformed ["rawKey" ] = transformedRawKey
729
591
}
730
- return req , nil
592
+
593
+ transformedSha256 , err := expandComputeSnapshotSnapshotEncryptionKeySha256 (original ["sha256" ], d , config )
594
+ if err != nil {
595
+ return nil , err
596
+ } else if val := reflect .ValueOf (transformedSha256 ); val .IsValid () && ! isEmptyValue (val ) {
597
+ transformed ["sha256" ] = transformedSha256
598
+ }
599
+
600
+ return transformed , nil
601
+ }
602
+
603
+ func expandComputeSnapshotSnapshotEncryptionKeyRawKey (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
604
+ return v , nil
605
+ }
606
+
607
+ func expandComputeSnapshotSnapshotEncryptionKeySha256 (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
608
+ return v , nil
731
609
}
732
610
733
611
func expandComputeSnapshotSourceDiskEncryptionKey (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
734
612
l := v .([]interface {})
735
- req := make ([]interface {}, 0 , 1 )
736
- if len (l ) == 1 {
737
- // There is a value
738
- outMap := make (map [string ]interface {})
739
- outMap ["rawKey" ] = l [0 ].(map [string ]interface {})["raw_key" ]
740
- req = append (req , outMap )
741
- } else {
742
- // Check alternative setting?
743
- if altV , ok := d .GetOk ("source_disk_encryption_key_raw" ); ok && altV != "" {
744
- outMap := make (map [string ]interface {})
745
- outMap ["rawKey" ] = altV
746
- req = append (req , outMap )
747
- }
613
+ if len (l ) == 0 || l [0 ] == nil {
614
+ return nil , nil
748
615
}
749
- return req , nil
616
+ raw := l [0 ]
617
+ original := raw .(map [string ]interface {})
618
+ transformed := make (map [string ]interface {})
619
+
620
+ transformedRawKey , err := expandComputeSnapshotSourceDiskEncryptionKeyRawKey (original ["raw_key" ], d , config )
621
+ if err != nil {
622
+ return nil , err
623
+ } else if val := reflect .ValueOf (transformedRawKey ); val .IsValid () && ! isEmptyValue (val ) {
624
+ transformed ["rawKey" ] = transformedRawKey
625
+ }
626
+
627
+ return transformed , nil
628
+ }
629
+
630
+ func expandComputeSnapshotSourceDiskEncryptionKeyRawKey (v interface {}, d * schema.ResourceData , config * Config ) (interface {}, error ) {
631
+ return v , nil
750
632
}
751
633
752
634
func resourceComputeSnapshotDecoder (d * schema.ResourceData , meta interface {}, res map [string ]interface {}) (map [string ]interface {}, error ) {
753
635
d .Set ("source_disk_link" , ConvertSelfLinkToV1 (res ["sourceDisk" ].(string )))
754
- if snapshotEncryptionKey := res ["snapshotEncryptionKey" ]; snapshotEncryptionKey != nil {
755
- d .Set ("snapshot_encryption_key_sha256" , snapshotEncryptionKey .((map [string ]interface {}))["sha256" ])
756
- }
757
-
758
636
return res , nil
759
637
}
0 commit comments