Skip to content

Commit 0fc9196

Browse files
Raghav Ravishankarravinitp
authored andcommitted
BugFix - Add zero-value check for kms_key during instance updation
1 parent fd61494 commit 0fc9196

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

internal/integrationtest/core_instance_test.go

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,12 @@ var (
331331
"source_type": acctest.Representation{RepType: acctest.Required, Create: `bootVolume`},
332332
"is_preserve_boot_volume_enabled": acctest.Representation{RepType: acctest.Optional, Create: `false`},
333333
}
334-
CoreInstanceSourceDetailsUpdateImageIdRepresentation = map[string]interface{}{
335-
"source_id": acctest.Representation{RepType: acctest.Required, Create: `${var.InstanceImageOCIDShieldedCompatible[var.region]}`},
334+
CoreInstanceSourceDetailsUpdateImageIdRequiredRepresentation = map[string]interface{}{
335+
"source_id": acctest.Representation{RepType: acctest.Required, Create: `${var.InstanceImageOCIDShieldedCompatible[var.region]}`},
336+
"source_type": acctest.Representation{RepType: acctest.Required, Create: `image`},
337+
}
338+
CoreInstanceSourceDetailsUpdateImageIdOptionalRepresentation = map[string]interface{}{
339+
"source_id": acctest.Representation{RepType: acctest.Required, Create: `${var.InstanceImageOCID[var.region]}`},
336340
"source_type": acctest.Representation{RepType: acctest.Required, Create: `image`},
337341
"is_preserve_boot_volume_enabled": acctest.Representation{RepType: acctest.Optional, Create: `false`},
338342
"kms_key_id": acctest.Representation{RepType: acctest.Optional, Create: `${lookup(data.oci_kms_keys.test_keys_dependency.keys[0], "id")}`},
@@ -2896,6 +2900,7 @@ func TestAccResourceCoreInstance_UpdateSourceDetails(t *testing.T) {
28962900

28972901
config := `
28982902
provider oci {
2903+
alias = "boot-volume-replacement"
28992904
test_time_maintenance_reboot_due = "2030-01-01 00:00:00"
29002905
}
29012906
` + acctest.CommonTestVariables()
@@ -2918,7 +2923,11 @@ func TestAccResourceCoreInstance_UpdateSourceDetails(t *testing.T) {
29182923
{
29192924
Config: acctest.ProviderTestConfig() + compartmentIdVariableStr + CoreInstanceBootVolumeSwapResourceDependencies +
29202925
acctest.GenerateResourceFromRepresentationMap("oci_core_instance", "test_instance", acctest.Optional, acctest.Create,
2921-
acctest.RepresentationCopyWithRemovedProperties(CoreInstanceRepresentation, []string{"dedicated_vm_host_id", "launch_options", "image"})),
2926+
acctest.RepresentationCopyWithRemovedProperties(
2927+
// SourceDetails w/o kms_key
2928+
acctest.GetUpdatedRepresentationCopy("source_details", acctest.RepresentationGroup{RepType: acctest.Required,
2929+
Group: instanceSourceDetailsSansKmsRepresentation}, CoreInstanceRepresentation),
2930+
[]string{"dedicated_vm_host_id", "launch_options", "image"})),
29222931
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
29232932
resource.TestCheckResourceAttrSet(resourceName, "availability_domain"),
29242933
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
@@ -2936,13 +2945,43 @@ func TestAccResourceCoreInstance_UpdateSourceDetails(t *testing.T) {
29362945
),
29372946
},
29382947

2939-
// Step 1: verify updates to existing source_details property does not destroy the resource - Update the imageId associated with source details
2948+
// Step 1: verify updates to existing source_details property does not destroy the resource - Update only imageId associated with source details
29402949
{
29412950
Config: config + compartmentIdVariableStr + CoreInstanceBootVolumeSwapResourceDependencies + utils.DefinedShieldedImageOCIDs +
29422951
acctest.GenerateResourceFromRepresentationMap("oci_core_instance", "test_instance", acctest.Optional, acctest.Create,
29432952
acctest.RepresentationCopyWithRemovedProperties(
2944-
acctest.GetUpdatedRepresentationCopy("source_details", acctest.RepresentationGroup{RepType: acctest.Optional, Group: CoreInstanceSourceDetailsUpdateImageIdRepresentation},
2945-
CoreInstanceRepresentation),
2953+
// Updating source_details with only required properties
2954+
acctest.GetUpdatedRepresentationCopy("source_details", acctest.RepresentationGroup{RepType: acctest.Required,
2955+
Group: CoreInstanceSourceDetailsUpdateImageIdRequiredRepresentation}, CoreInstanceRepresentation),
2956+
[]string{"dedicated_vm_host_id", "launch_options", "image"})),
2957+
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
2958+
resource.TestCheckResourceAttrSet(resourceName, "availability_domain"),
2959+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
2960+
resource.TestCheckResourceAttr(resourceName, "shape", "VM.Standard2.1"),
2961+
resource.TestCheckResourceAttrSet(resourceName, "subnet_id"),
2962+
resource.TestCheckResourceAttr(resourceName, "source_details.#", "1"),
2963+
resource.TestCheckResourceAttrSet(resourceName, "source_details.0.source_id"),
2964+
resource.TestCheckResourceAttr(resourceName, "source_details.0.source_type", "image"),
2965+
resource.TestCheckResourceAttr(resourceName, "source_details.0.boot_volume_size_in_gbs", "60"),
2966+
2967+
func(s *terraform.State) (err error) {
2968+
resId2, err = acctest.FromInstanceState(s, resourceName, "id")
2969+
if resId != resId2 {
2970+
return fmt.Errorf("Resource recreated when it was supposed to be updated.")
2971+
}
2972+
return err
2973+
},
2974+
),
2975+
},
2976+
2977+
// Step 2: verify updates to all optional properties associated with source_details - kmsKey, bootVolumeSize etc.
2978+
{
2979+
Config: config + compartmentIdVariableStr + CoreInstanceBootVolumeSwapResourceDependencies + utils.DefinedShieldedImageOCIDs +
2980+
acctest.GenerateResourceFromRepresentationMap("oci_core_instance", "test_instance", acctest.Optional, acctest.Create,
2981+
acctest.RepresentationCopyWithRemovedProperties(
2982+
// Updating source_details with only required properties
2983+
acctest.GetUpdatedRepresentationCopy("source_details", acctest.RepresentationGroup{RepType: acctest.Optional,
2984+
Group: CoreInstanceSourceDetailsUpdateImageIdOptionalRepresentation}, CoreInstanceRepresentation),
29462985
[]string{"dedicated_vm_host_id", "launch_options", "image"})),
29472986
Check: acctest.ComposeAggregateTestCheckFuncWrapper(
29482987
resource.TestCheckResourceAttrSet(resourceName, "availability_domain"),
@@ -2965,7 +3004,7 @@ func TestAccResourceCoreInstance_UpdateSourceDetails(t *testing.T) {
29653004
),
29663005
},
29673006

2968-
// Step 2: verify updates to existing source_details property does not destroy the resource - Update the bootVolumeId associated with the source details
3007+
// Step 3: verify updates to existing source_details property does not destroy the resource - Update the bootVolumeId associated with the source details
29693008
{
29703009
Config: config + compartmentIdVariableStr + CoreInstanceBootVolumeSwapResourceDependencies +
29713010
acctest.GenerateResourceFromRepresentationMap("oci_core_instance", "test_instance", acctest.Optional, acctest.Create,

internal/service/core/core_instance_resource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,9 @@ func (s *CoreInstanceResourceCrud) mapToUpdateInstanceSourceDetails(fieldKeyForm
22502250
}
22512251
details.BootVolumeSizeInGBs = &tmpInt64
22522252
}
2253-
if kmsKeyId, ok := s.D.GetOkExists(fmt.Sprintf(fieldKeyFormat, "kms_key_id")); ok {
2253+
// Use getOk for kmsKeyId as it is validated at the spec layer to ensure non-zero value; GetOk checks
2254+
// for non-zero value: https://pkg.go.dev/github.com/hashicorp/terraform/helper/schema#section-readme
2255+
if kmsKeyId, ok := s.D.GetOk(fmt.Sprintf(fieldKeyFormat, "kms_key_id")); ok {
22542256
tmp := kmsKeyId.(string)
22552257
details.KmsKeyId = &tmp
22562258
}

0 commit comments

Comments
 (0)