Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“azurerm_spring_cloud_java_deployment” – the cpu and memory_in_gb properties have been deprecated in favour of the quota block #12924

Merged
merged 16 commits into from
Oct 29, 2021
Merged
2 changes: 1 addition & 1 deletion internal/services/springcloud/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
cosmosValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
mysqlValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/mysql/validate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
redisValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/redis/validate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/springcloud/parse"
Expand Down Expand Up @@ -50,11 +50,30 @@ func resourceSpringCloudJavaDeployment() *pluginsdk.Resource {
ValidateFunc: validate.SpringCloudAppID,
},

// TODO: Remove in 3.0
// The value returned in GET will be recalculated by the service if "cpu_v2" is honored, so make this property as Computed.
"cpu": {
Type: pluginsdk.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 4),
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 4),
ConflictsWith: []string{"cpu_v2"},
Deprecated: "This field has been deprecated in favour of `cpu_v2` and will be removed in a future version of the provider",
},

// The value returned in GET will be recalculated by the service if "cpu" is honored, so make this property as Computed.
"cpu_v2": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"500m",
"1",
"2",
"3",
"4",
}, false),
ConflictsWith: []string{"cpu"},
},

"environment_variables": {
Expand All @@ -77,21 +96,44 @@ func resourceSpringCloudJavaDeployment() *pluginsdk.Resource {
Optional: true,
},

// TODO: Remove in 3.0
// The value returned in GET will be recalculated by the service if "memory_v2" is honored, so make this property as Computed.
"memory_in_gb": {
Type: pluginsdk.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 8),
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 8),
ConflictsWith: []string{"memory_v2"},
Deprecated: "This field has been deprecated in favour of `memory_v2` and will be removed in a future version of the provider",
},

// The value returned in GET will be recalculated by the service if "memory_in_gb" is honored, so make this property as Computed.
"memory_v2": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"512Mi",
"1Gi",
"2Gi",
"3Gi",
"4Gi",
"5Gi",
"6Gi",
"7Gi",
"8Gi",
}, false),
ConflictsWith: []string{"memory_in_gb"},
},

"runtime_version": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(appplatform.Java8),
string(appplatform.Java11),
string(appplatform.RuntimeVersionJava8),
string(appplatform.RuntimeVersionJava11),
}, false),
Default: string(appplatform.Java8),
Default: string(appplatform.RuntimeVersionJava8),
},
},
}
Expand Down Expand Up @@ -136,14 +178,15 @@ func resourceSpringCloudJavaDeploymentCreate(d *pluginsdk.ResourceData, meta int
},
Properties: &appplatform.DeploymentResourceProperties{
Source: &appplatform.UserSourceInfo{
Type: appplatform.Jar,
Type: appplatform.UserSourceTypeJar,
RelativePath: utils.String("<default>"),
},
DeploymentSettings: &appplatform.DeploymentSettings{
CPU: utils.Int32(int32(d.Get("cpu").(int))),
MemoryInGB: utils.Int32(int32(d.Get("memory_in_gb").(int))),
JvmOptions: utils.String(d.Get("jvm_options").(string)),
EnvironmentVariables: expandSpringCloudDeploymentEnvironmentVariables(d.Get("environment_variables").(map[string]interface{})),
ResourceRequests: expandSpringCloudDeploymentResourceRequests(d.Get("cpu").(int), d.Get("cpu_v2").(string), d.Get("memory_in_gb").(int), d.Get("memory_v2").(string)),
RuntimeVersion: appplatform.RuntimeVersion(d.Get("runtime_version").(string)),
},
},
Expand Down Expand Up @@ -187,6 +230,11 @@ func resourceSpringCloudJavaDeploymentUpdate(d *pluginsdk.ResourceData, meta int

if d.HasChange("cpu") {
existing.Properties.DeploymentSettings.CPU = utils.Int32(int32(d.Get("cpu").(int)))

// "cpu_v2" that takes precedence of "cpu" should be ignored in this situation where users explicitly update the legacy "cpu" that conflicts with "cpu_v2"
if existing.Properties.DeploymentSettings.ResourceRequests != nil {
existing.Properties.DeploymentSettings.ResourceRequests.CPU = utils.String("")
}
}

if d.HasChange("environment_variables") {
Expand All @@ -199,6 +247,19 @@ func resourceSpringCloudJavaDeploymentUpdate(d *pluginsdk.ResourceData, meta int

if d.HasChange("memory_in_gb") {
existing.Properties.DeploymentSettings.MemoryInGB = utils.Int32(int32(d.Get("memory_in_gb").(int)))

// "memory_v2" 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_v2"
if existing.Properties.DeploymentSettings.ResourceRequests != nil {
existing.Properties.DeploymentSettings.ResourceRequests.Memory = utils.String("")
}
}

if d.HasChange("cpu_v2") || d.HasChange("memory_v2") {
if existing.Properties.DeploymentSettings.ResourceRequests == nil {
return fmt.Errorf("nil `properties.deploymentSettings.resourceRequests` for %s: %+v", id, err)
}

existing.Properties.DeploymentSettings.ResourceRequests = expandSpringCloudDeploymentResourceRequests(d.Get("cpu").(int), d.Get("cpu_v2").(string), d.Get("memory_in_gb").(int), d.Get("memory_v2").(string))
}

if d.HasChange("runtime_version") {
Expand Down Expand Up @@ -249,6 +310,12 @@ func resourceSpringCloudJavaDeploymentRead(d *pluginsdk.ResourceData, meta inter
d.Set("jvm_options", settings.JvmOptions)
d.Set("environment_variables", flattenSpringCloudDeploymentEnvironmentVariables(settings.EnvironmentVariables))
d.Set("runtime_version", settings.RuntimeVersion)

if settings.ResourceRequests != nil {
resourceRequests := settings.ResourceRequests
d.Set("cpu_v2", resourceRequests.CPU)
d.Set("memory_v2", resourceRequests.Memory)
}
}

return nil
Expand Down Expand Up @@ -291,3 +358,27 @@ func flattenSpringCloudDeploymentEnvironmentVariables(envMap map[string]*string)
}
return output
}

func expandSpringCloudDeploymentResourceRequests(cpu int, cpuV2 string, mem int, memV2 string) *appplatform.ResourceRequests {
cpuResult := "1" // default value that's aligned with previous behavior used to be defined in schema.
memResult := "1Gi" // default value that's aligned with previous behavior used to be defined in schema.

if cpuV2 != "" {
cpuResult = cpuV2
} else if cpu != 0 {
cpuResult = "" // Take legacy property as precedence with setting "" to new property, otherwise the new property that's not set by users always takes precedence.
}

if memV2 != "" {
memResult = memV2
} else if mem != 0 {
memResult = "" // Take legacy property as precedence with setting "" to new property, otherwise the new property that's not set by users always takes precedence.
}

result := appplatform.ResourceRequests{
CPU: utils.String(cpuResult),
Memory: utils.String(memResult),
}

return &result
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ func TestAccSpringCloudJavaDeployment_update(t *testing.T) {
})
}

func TestAccSpringCloudJavaDeployment_updateHalfCpuMemory(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_spring_cloud_java_deployment", "test")
r := SpringCloudJavaDeploymentResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.halfCpuMemory(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.nonHalfCpuMemory(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r SpringCloudJavaDeploymentResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.SpringCloudDeploymentID(state.ID)
if err != nil {
Expand Down Expand Up @@ -147,6 +176,32 @@ resource "azurerm_spring_cloud_java_deployment" "test" {
`, r.template(data), data.RandomString)
}

func (r SpringCloudJavaDeploymentResource) halfCpuMemory(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_spring_cloud_java_deployment" "test" {
name = "acctest-scjd%s"
spring_cloud_app_id = azurerm_spring_cloud_app.test.id
cpu_v2 = "500m"
memory_v2 = "512Mi"
}
`, r.template(data), data.RandomString)
}

func (r SpringCloudJavaDeploymentResource) nonHalfCpuMemory(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_spring_cloud_java_deployment" "test" {
name = "acctest-scjd%s"
spring_cloud_app_id = azurerm_spring_cloud_app.test.id
cpu_v2 = "2"
memory_v2 = "4Gi"
}
`, r.template(data), data.RandomString)
}

func (SpringCloudJavaDeploymentResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2020-11-01-preview/appplatform"
"github.com/Azure/azure-sdk-for-go/services/preview/appplatform/mgmt/2021-06-01-preview/appplatform"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading