@@ -2,6 +2,7 @@ package google
2
2
3
3
import (
4
4
"fmt"
5
+ "github.com/hashicorp/terraform/helper/validation"
5
6
"log"
6
7
"regexp"
7
8
"strconv"
@@ -39,6 +40,27 @@ func resourceKmsCryptoKey() *schema.Resource {
39
40
Optional : true ,
40
41
ValidateFunc : validateKmsCryptoKeyRotationPeriod ,
41
42
},
43
+ "version_template" : {
44
+ Type : schema .TypeList ,
45
+ MaxItems : 1 ,
46
+ Optional : true ,
47
+ Computed : true ,
48
+ Elem : & schema.Resource {
49
+ Schema : map [string ]* schema.Schema {
50
+ "algorithm" : {
51
+ Type : schema .TypeString ,
52
+ Required : true ,
53
+ },
54
+ "protection_level" : {
55
+ Type : schema .TypeString ,
56
+ Optional : true ,
57
+ ForceNew : true ,
58
+ Default : "SOFTWARE" ,
59
+ ValidateFunc : validation .StringInSlice ([]string {"SOFTWARE" , "HSM" , "" }, false ),
60
+ },
61
+ },
62
+ },
63
+ },
42
64
"self_link" : {
43
65
Type : schema .TypeString ,
44
66
Computed : true ,
@@ -84,7 +106,10 @@ func resourceKmsCryptoKeyCreate(d *schema.ResourceData, meta interface{}) error
84
106
Name : d .Get ("name" ).(string ),
85
107
}
86
108
87
- key := cloudkms.CryptoKey {Purpose : "ENCRYPT_DECRYPT" }
109
+ key := cloudkms.CryptoKey {
110
+ Purpose : "ENCRYPT_DECRYPT" ,
111
+ VersionTemplate : expandVersionTemplate (d .Get ("version_template" ).([]interface {})),
112
+ }
88
113
89
114
if d .Get ("rotation_period" ) != "" {
90
115
rotationPeriod := d .Get ("rotation_period" ).(string )
@@ -133,6 +158,10 @@ func resourceKmsCryptoKeyUpdate(d *schema.ResourceData, meta interface{}) error
133
158
key .RotationPeriod = rotationPeriod
134
159
}
135
160
161
+ if d .HasChange ("version_template" ) {
162
+ key .VersionTemplate = expandVersionTemplate (d .Get ("version_template" ).([]interface {}))
163
+ }
164
+
136
165
cryptoKey , err := config .clientKms .Projects .Locations .KeyRings .CryptoKeys .Patch (cryptoKeyId .cryptoKeyId (), & key ).UpdateMask ("rotation_period,next_rotation_time" ).Do ()
137
166
138
167
if err != nil {
@@ -165,6 +194,10 @@ func resourceKmsCryptoKeyRead(d *schema.ResourceData, meta interface{}) error {
165
194
d .Set ("rotation_period" , cryptoKey .RotationPeriod )
166
195
d .Set ("self_link" , cryptoKey .Name )
167
196
197
+ if err = d .Set ("version_template" , flattenVersionTemplate (cryptoKey .VersionTemplate )); err != nil {
198
+ return fmt .Errorf ("Error setting version_tempalte in state: %s" , err .Error ())
199
+ }
200
+
168
201
d .SetId (cryptoKeyId .cryptoKeyId ())
169
202
170
203
return nil
@@ -219,6 +252,33 @@ and all its CryptoKeyVersions will be destroyed, but it will still be present on
219
252
return nil
220
253
}
221
254
255
+ func expandVersionTemplate (configured []interface {}) * cloudkms.CryptoKeyVersionTemplate {
256
+ if configured == nil || len (configured ) == 0 {
257
+ return nil
258
+ }
259
+
260
+ data := configured [0 ].(map [string ]interface {})
261
+ return & cloudkms.CryptoKeyVersionTemplate {
262
+ Algorithm : data ["algorithm" ].(string ),
263
+ ProtectionLevel : data ["protection_level" ].(string ),
264
+ }
265
+ }
266
+
267
+ func flattenVersionTemplate (versionTemplate * cloudkms.CryptoKeyVersionTemplate ) []map [string ]interface {} {
268
+ if versionTemplate == nil {
269
+ return nil
270
+ }
271
+
272
+ versionTemplateSchema := make ([]map [string ]interface {}, 0 , 1 )
273
+ data := map [string ]interface {}{
274
+ "algorithm" : versionTemplate .Algorithm ,
275
+ "protection_level" : versionTemplate .ProtectionLevel ,
276
+ }
277
+
278
+ versionTemplateSchema = append (versionTemplateSchema , data )
279
+ return versionTemplateSchema
280
+ }
281
+
222
282
func validateKmsCryptoKeyRotationPeriod (value interface {}, _ string ) (ws []string , errors []error ) {
223
283
period := value .(string )
224
284
pattern := regexp .MustCompile ("^([0-9.]*\\ d)s$" )
0 commit comments