5
5
"log"
6
6
"time"
7
7
8
- "github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11 -01-preview/appplatform"
8
+ "github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06 -01-preview/appplatform"
9
9
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
10
10
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
11
11
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"
@@ -50,11 +50,15 @@ func resourceSpringCloudJavaDeployment() *pluginsdk.Resource {
50
50
ValidateFunc : validate .SpringCloudAppID ,
51
51
},
52
52
53
+ // TODO: Remove in 3.0
54
+ // The value returned in GET will be recalculated by the service if "cpu" within "quota" is honored, so make this property as Computed.
53
55
"cpu" : {
54
- Type : pluginsdk .TypeInt ,
55
- Optional : true ,
56
- Default : 1 ,
57
- ValidateFunc : validation .IntBetween (1 , 4 ),
56
+ Type : pluginsdk .TypeInt ,
57
+ Optional : true ,
58
+ Computed : true ,
59
+ ValidateFunc : validation .IntBetween (1 , 4 ),
60
+ ConflictsWith : []string {"quota.0.cpu" },
61
+ Deprecated : "This field has been deprecated in favour of `cpu` within `quota` and will be removed in a future version of the provider" ,
58
62
},
59
63
60
64
"environment_variables" : {
@@ -77,21 +81,69 @@ func resourceSpringCloudJavaDeployment() *pluginsdk.Resource {
77
81
Optional : true ,
78
82
},
79
83
84
+ // TODO: Remove in 3.0
85
+ // The value returned in GET will be recalculated by the service if "memory" is honored, so make this property as Computed.
80
86
"memory_in_gb" : {
81
- Type : pluginsdk .TypeInt ,
82
- Optional : true ,
83
- Default : 1 ,
84
- ValidateFunc : validation .IntBetween (1 , 8 ),
87
+ Type : pluginsdk .TypeInt ,
88
+ Optional : true ,
89
+ Computed : true ,
90
+ ValidateFunc : validation .IntBetween (1 , 8 ),
91
+ ConflictsWith : []string {"quota.0.memory" },
92
+ Deprecated : "This field has been deprecated in favour of `memory` within `quota` and will be removed in a future version of the provider" ,
93
+ },
94
+
95
+ "quota" : {
96
+ Type : pluginsdk .TypeList ,
97
+ Optional : true ,
98
+ Computed : true ,
99
+ MaxItems : 1 ,
100
+ Elem : & pluginsdk.Resource {
101
+ Schema : map [string ]* pluginsdk.Schema {
102
+ // The value returned in GET will be recalculated by the service if the deprecated "cpu" is honored, so make this property as Computed.
103
+ "cpu" : {
104
+ Type : pluginsdk .TypeString ,
105
+ Optional : true ,
106
+ Computed : true ,
107
+ ValidateFunc : validation .StringInSlice ([]string {
108
+ "500m" ,
109
+ "1" ,
110
+ "2" ,
111
+ "3" ,
112
+ "4" ,
113
+ }, false ),
114
+ ConflictsWith : []string {"cpu" },
115
+ },
116
+
117
+ // The value returned in GET will be recalculated by the service if the deprecated "memory_in_gb" is honored, so make this property as Computed.
118
+ "memory" : {
119
+ Type : pluginsdk .TypeString ,
120
+ Optional : true ,
121
+ Computed : true ,
122
+ ValidateFunc : validation .StringInSlice ([]string {
123
+ "512Mi" ,
124
+ "1Gi" ,
125
+ "2Gi" ,
126
+ "3Gi" ,
127
+ "4Gi" ,
128
+ "5Gi" ,
129
+ "6Gi" ,
130
+ "7Gi" ,
131
+ "8Gi" ,
132
+ }, false ),
133
+ ConflictsWith : []string {"memory_in_gb" },
134
+ },
135
+ },
136
+ },
85
137
},
86
138
87
139
"runtime_version" : {
88
140
Type : pluginsdk .TypeString ,
89
141
Optional : true ,
90
142
ValidateFunc : validation .StringInSlice ([]string {
91
- string (appplatform .Java8 ),
92
- string (appplatform .Java11 ),
143
+ string (appplatform .RuntimeVersionJava8 ),
144
+ string (appplatform .RuntimeVersionJava11 ),
93
145
}, false ),
94
- Default : string (appplatform .Java8 ),
146
+ Default : string (appplatform .RuntimeVersionJava8 ),
95
147
},
96
148
},
97
149
}
@@ -136,14 +188,15 @@ func resourceSpringCloudJavaDeploymentCreate(d *pluginsdk.ResourceData, meta int
136
188
},
137
189
Properties : & appplatform.DeploymentResourceProperties {
138
190
Source : & appplatform.UserSourceInfo {
139
- Type : appplatform .Jar ,
191
+ Type : appplatform .UserSourceTypeJar ,
140
192
RelativePath : utils .String ("<default>" ),
141
193
},
142
194
DeploymentSettings : & appplatform.DeploymentSettings {
143
195
CPU : utils .Int32 (int32 (d .Get ("cpu" ).(int ))),
144
196
MemoryInGB : utils .Int32 (int32 (d .Get ("memory_in_gb" ).(int ))),
145
197
JvmOptions : utils .String (d .Get ("jvm_options" ).(string )),
146
198
EnvironmentVariables : expandSpringCloudDeploymentEnvironmentVariables (d .Get ("environment_variables" ).(map [string ]interface {})),
199
+ ResourceRequests : expandSpringCloudDeploymentResourceRequests (d .Get ("cpu" ).(int ), d .Get ("memory_in_gb" ).(int ), d .Get ("quota" ).([]interface {})),
147
200
RuntimeVersion : appplatform .RuntimeVersion (d .Get ("runtime_version" ).(string )),
148
201
},
149
202
},
@@ -187,6 +240,11 @@ func resourceSpringCloudJavaDeploymentUpdate(d *pluginsdk.ResourceData, meta int
187
240
188
241
if d .HasChange ("cpu" ) {
189
242
existing .Properties .DeploymentSettings .CPU = utils .Int32 (int32 (d .Get ("cpu" ).(int )))
243
+
244
+ // "cpu" within "quota" that takes precedence of deprecated "cpu" should be ignored in this situation where users explicitly update the deprecated "cpu" that conflicts with "cpu" within "quota"
245
+ if existing .Properties .DeploymentSettings .ResourceRequests != nil {
246
+ existing .Properties .DeploymentSettings .ResourceRequests .CPU = utils .String ("" )
247
+ }
190
248
}
191
249
192
250
if d .HasChange ("environment_variables" ) {
@@ -199,6 +257,19 @@ func resourceSpringCloudJavaDeploymentUpdate(d *pluginsdk.ResourceData, meta int
199
257
200
258
if d .HasChange ("memory_in_gb" ) {
201
259
existing .Properties .DeploymentSettings .MemoryInGB = utils .Int32 (int32 (d .Get ("memory_in_gb" ).(int )))
260
+
261
+ // "memory" that takes precedence of "memory_in_gb" should be ignored in this situation where users explicitly update the legacy "memory_in_gb" that conflicts with "memory"
262
+ if existing .Properties .DeploymentSettings .ResourceRequests != nil {
263
+ existing .Properties .DeploymentSettings .ResourceRequests .Memory = utils .String ("" )
264
+ }
265
+ }
266
+
267
+ if d .HasChange ("quota" ) {
268
+ if existing .Properties .DeploymentSettings .ResourceRequests == nil {
269
+ return fmt .Errorf ("nil `properties.deploymentSettings.resourceRequests` for %s: %+v" , id , err )
270
+ }
271
+
272
+ existing .Properties .DeploymentSettings .ResourceRequests = expandSpringCloudDeploymentResourceRequests (d .Get ("cpu" ).(int ), d .Get ("memory_in_gb" ).(int ), d .Get ("quota" ).([]interface {}))
202
273
}
203
274
204
275
if d .HasChange ("runtime_version" ) {
@@ -249,6 +320,9 @@ func resourceSpringCloudJavaDeploymentRead(d *pluginsdk.ResourceData, meta inter
249
320
d .Set ("jvm_options" , settings .JvmOptions )
250
321
d .Set ("environment_variables" , flattenSpringCloudDeploymentEnvironmentVariables (settings .EnvironmentVariables ))
251
322
d .Set ("runtime_version" , settings .RuntimeVersion )
323
+ if err := d .Set ("quota" , flattenSpringCloudDeploymentResourceRequests (settings .ResourceRequests )); err != nil {
324
+ return fmt .Errorf ("setting `quota`: %+v" , err )
325
+ }
252
326
}
253
327
254
328
return nil
@@ -291,3 +365,73 @@ func flattenSpringCloudDeploymentEnvironmentVariables(envMap map[string]*string)
291
365
}
292
366
return output
293
367
}
368
+
369
+ func expandSpringCloudDeploymentResourceRequests (cpu int , mem int , input []interface {}) * appplatform.ResourceRequests {
370
+ cpuResult := "1" // default value that's aligned with previous behavior used to be defined in schema.
371
+ memResult := "1Gi" // default value that's aligned with previous behavior used to be defined in schema.
372
+
373
+ if len (input ) == 0 || input [0 ] == nil {
374
+ // Take legacy property as precedence with setting "" to new property, otherwise the new property that's not set by users always takes precedence.
375
+ // The above explanation applies to left similar sections within this function.
376
+ if cpu != 0 {
377
+ cpuResult = ""
378
+ }
379
+
380
+ if mem != 0 {
381
+ memResult = ""
382
+ }
383
+ } else {
384
+ v := input [0 ].(map [string ]interface {})
385
+ if v == nil {
386
+ if cpu != 0 {
387
+ cpuResult = ""
388
+ }
389
+
390
+ if mem != 0 {
391
+ memResult = ""
392
+ }
393
+ } else {
394
+ if cpuNew := v ["cpu" ].(string ); cpuNew != "" {
395
+ cpuResult = cpuNew
396
+ } else if cpu != 0 {
397
+ cpuResult = ""
398
+ }
399
+
400
+ if memoryNew := v ["memory" ].(string ); memoryNew != "" {
401
+ memResult = memoryNew
402
+ } else if mem != 0 {
403
+ memResult = ""
404
+ }
405
+ }
406
+ }
407
+
408
+ result := appplatform.ResourceRequests {
409
+ CPU : utils .String (cpuResult ),
410
+ Memory : utils .String (memResult ),
411
+ }
412
+
413
+ return & result
414
+ }
415
+
416
+ func flattenSpringCloudDeploymentResourceRequests (input * appplatform.ResourceRequests ) []interface {} {
417
+ if input == nil {
418
+ return []interface {}{}
419
+ }
420
+
421
+ cpu := ""
422
+ if input .CPU != nil {
423
+ cpu = * input .CPU
424
+ }
425
+
426
+ memory := ""
427
+ if input .Memory != nil {
428
+ memory = * input .Memory
429
+ }
430
+
431
+ return []interface {}{
432
+ map [string ]interface {}{
433
+ "cpu" : cpu ,
434
+ "memory" : memory ,
435
+ },
436
+ }
437
+ }
0 commit comments