Skip to content

Commit 127754e

Browse files
authored
“azurerm_spring_cloud_java_deployment” – the cpu and memory_in_gb properties have been deprecated in favour of the quota block (#12924)
Co-authored-by: mybayern1974 <nanxu@microsoft.com> Now the spring cloud Java deployment resource supports users to set half cpu and memory values. The current cpu and memory properties whose types are int are unable to support that. This PR introduces new cpu and memory properties that take precedence of corresponding legacy properties. On the service side, new properties supporting setting half values also take precedence of their corresponding legacy properties. Service api reference: Azure/azure-rest-api-specs@master/specification/appplatform/resource-manager/Microsoft.AppPlatform/preview/2021-06-01-preview/appplatform.json#L4111
1 parent 09dfa18 commit 127754e

30 files changed

+420
-161
lines changed

internal/services/springcloud/client/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package client
22

33
import (
4-
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
4+
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
55
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
66
)
77

internal/services/springcloud/spring_cloud_active_deployment_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1010
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1111
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"

internal/services/springcloud/spring_cloud_app_cosmosdb_association_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1010
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1111
cosmosValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate"

internal/services/springcloud/spring_cloud_app_mysql_association_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1010
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1111
mysqlValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/mysql/validate"

internal/services/springcloud/spring_cloud_app_redis_association_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77
"time"
88

9-
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
9+
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
1010
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1111
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1212
redisValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/redis/validate"

internal/services/springcloud/spring_cloud_app_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
1010
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1111
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"

internal/services/springcloud/spring_cloud_certificate_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
1010
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1111
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"

internal/services/springcloud/spring_cloud_custom_domain_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1010
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1111
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"

internal/services/springcloud/spring_cloud_java_deployment_resource.go

+157-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log"
66
"time"
77

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"
99
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1010
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1111
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"
@@ -50,11 +50,15 @@ func resourceSpringCloudJavaDeployment() *pluginsdk.Resource {
5050
ValidateFunc: validate.SpringCloudAppID,
5151
},
5252

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.
5355
"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",
5862
},
5963

6064
"environment_variables": {
@@ -77,21 +81,69 @@ func resourceSpringCloudJavaDeployment() *pluginsdk.Resource {
7781
Optional: true,
7882
},
7983

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.
8086
"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+
},
85137
},
86138

87139
"runtime_version": {
88140
Type: pluginsdk.TypeString,
89141
Optional: true,
90142
ValidateFunc: validation.StringInSlice([]string{
91-
string(appplatform.Java8),
92-
string(appplatform.Java11),
143+
string(appplatform.RuntimeVersionJava8),
144+
string(appplatform.RuntimeVersionJava11),
93145
}, false),
94-
Default: string(appplatform.Java8),
146+
Default: string(appplatform.RuntimeVersionJava8),
95147
},
96148
},
97149
}
@@ -136,14 +188,15 @@ func resourceSpringCloudJavaDeploymentCreate(d *pluginsdk.ResourceData, meta int
136188
},
137189
Properties: &appplatform.DeploymentResourceProperties{
138190
Source: &appplatform.UserSourceInfo{
139-
Type: appplatform.Jar,
191+
Type: appplatform.UserSourceTypeJar,
140192
RelativePath: utils.String("<default>"),
141193
},
142194
DeploymentSettings: &appplatform.DeploymentSettings{
143195
CPU: utils.Int32(int32(d.Get("cpu").(int))),
144196
MemoryInGB: utils.Int32(int32(d.Get("memory_in_gb").(int))),
145197
JvmOptions: utils.String(d.Get("jvm_options").(string)),
146198
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{})),
147200
RuntimeVersion: appplatform.RuntimeVersion(d.Get("runtime_version").(string)),
148201
},
149202
},
@@ -187,6 +240,11 @@ func resourceSpringCloudJavaDeploymentUpdate(d *pluginsdk.ResourceData, meta int
187240

188241
if d.HasChange("cpu") {
189242
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+
}
190248
}
191249

192250
if d.HasChange("environment_variables") {
@@ -199,6 +257,19 @@ func resourceSpringCloudJavaDeploymentUpdate(d *pluginsdk.ResourceData, meta int
199257

200258
if d.HasChange("memory_in_gb") {
201259
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{}))
202273
}
203274

204275
if d.HasChange("runtime_version") {
@@ -249,6 +320,9 @@ func resourceSpringCloudJavaDeploymentRead(d *pluginsdk.ResourceData, meta inter
249320
d.Set("jvm_options", settings.JvmOptions)
250321
d.Set("environment_variables", flattenSpringCloudDeploymentEnvironmentVariables(settings.EnvironmentVariables))
251322
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+
}
252326
}
253327

254328
return nil
@@ -291,3 +365,73 @@ func flattenSpringCloudDeploymentEnvironmentVariables(envMap map[string]*string)
291365
}
292366
return output
293367
}
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+
}

internal/services/springcloud/spring_cloud_java_deployment_resource_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ func TestAccSpringCloudJavaDeployment_update(t *testing.T) {
9090
})
9191
}
9292

93+
func TestAccSpringCloudJavaDeployment_updateHalfCpuMemory(t *testing.T) {
94+
data := acceptance.BuildTestData(t, "azurerm_spring_cloud_java_deployment", "test")
95+
r := SpringCloudJavaDeploymentResource{}
96+
97+
data.ResourceTest(t, r, []acceptance.TestStep{
98+
{
99+
Config: r.complete(data),
100+
Check: acceptance.ComposeTestCheckFunc(
101+
check.That(data.ResourceName).ExistsInAzure(r),
102+
),
103+
},
104+
data.ImportStep(),
105+
{
106+
Config: r.halfCpuMemory(data),
107+
Check: acceptance.ComposeTestCheckFunc(
108+
check.That(data.ResourceName).ExistsInAzure(r),
109+
),
110+
},
111+
data.ImportStep(),
112+
{
113+
Config: r.nonHalfCpuMemory(data),
114+
Check: acceptance.ComposeTestCheckFunc(
115+
check.That(data.ResourceName).ExistsInAzure(r),
116+
),
117+
},
118+
data.ImportStep(),
119+
})
120+
}
121+
93122
func (r SpringCloudJavaDeploymentResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
94123
id, err := parse.SpringCloudDeploymentID(state.ID)
95124
if err != nil {
@@ -147,6 +176,36 @@ resource "azurerm_spring_cloud_java_deployment" "test" {
147176
`, r.template(data), data.RandomString)
148177
}
149178

179+
func (r SpringCloudJavaDeploymentResource) halfCpuMemory(data acceptance.TestData) string {
180+
return fmt.Sprintf(`
181+
%s
182+
183+
resource "azurerm_spring_cloud_java_deployment" "test" {
184+
name = "acctest-scjd%s"
185+
spring_cloud_app_id = azurerm_spring_cloud_app.test.id
186+
quota {
187+
cpu = "500m"
188+
memory = "512Mi"
189+
}
190+
}
191+
`, r.template(data), data.RandomString)
192+
}
193+
194+
func (r SpringCloudJavaDeploymentResource) nonHalfCpuMemory(data acceptance.TestData) string {
195+
return fmt.Sprintf(`
196+
%s
197+
198+
resource "azurerm_spring_cloud_java_deployment" "test" {
199+
name = "acctest-scjd%s"
200+
spring_cloud_app_id = azurerm_spring_cloud_app.test.id
201+
quota {
202+
cpu = "2"
203+
memory = "4Gi"
204+
}
205+
}
206+
`, r.template(data), data.RandomString)
207+
}
208+
150209
func (SpringCloudJavaDeploymentResource) template(data acceptance.TestData) string {
151210
return fmt.Sprintf(`
152211
provider "azurerm" {

internal/services/springcloud/spring_cloud_service_resource.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
10+
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
1111
"github.com/gofrs/uuid"
1212
"github.com/hashicorp/go-azure-helpers/response"
1313
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"

0 commit comments

Comments
 (0)