Skip to content

Commit 40ac113

Browse files
committed
Add support for moving instances across compartments
1 parent 805c0ca commit 40ac113

10 files changed

+89
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Support for moving `ons_notification_topic`, `ons_subscription` resources across compartments
1111
- Support for moving `oci_load_balancer` across compartments
1212
- Support for moving `oci_kms_key` and `oci_kms_vault` Across Compartments
13+
- Support for moving `core_instance` resources across compartments
1314
- Support for LBaaS Cookie Insertion (Sticky Cookie)
1415

1516
## 3.33.0 (July 10, 2019)

oci/core_instance_resource.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ func CoreInstanceResource() *schema.Resource {
4444
"compartment_id": {
4545
Type: schema.TypeString,
4646
Required: true,
47-
ForceNew: true,
4847
},
4948
"shape": {
5049
Type: schema.TypeString,
@@ -648,6 +647,15 @@ func (s *CoreInstanceResourceCrud) Get() error {
648647
}
649648

650649
func (s *CoreInstanceResourceCrud) Update() error {
650+
if compartment, ok := s.D.GetOkExists("compartment_id"); ok && s.D.HasChange("compartment_id") {
651+
oldRaw, newRaw := s.D.GetChange("compartment_id")
652+
if newRaw != "" && oldRaw != "" {
653+
err := s.updateCompartment(compartment)
654+
if err != nil {
655+
return err
656+
}
657+
}
658+
}
651659
request := oci_core.UpdateInstanceRequest{}
652660

653661
if agentConfig, ok := s.D.GetOkExists("agent_config"); ok {
@@ -1276,3 +1284,21 @@ func (s *CoreInstanceResourceCrud) getBootVolume() (*oci_core.BootVolume, error)
12761284

12771285
return &bootVolumeResponse.BootVolume, nil
12781286
}
1287+
1288+
func (s *CoreInstanceResourceCrud) updateCompartment(compartment interface{}) error {
1289+
changeCompartmentRequest := oci_core.ChangeInstanceCompartmentRequest{}
1290+
1291+
compartmentTmp := compartment.(string)
1292+
changeCompartmentRequest.CompartmentId = &compartmentTmp
1293+
1294+
idTmp := s.D.Id()
1295+
changeCompartmentRequest.InstanceId = &idTmp
1296+
1297+
changeCompartmentRequest.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "core")
1298+
1299+
_, err := s.Client.ChangeInstanceCompartment(context.Background(), changeCompartmentRequest)
1300+
if err != nil {
1301+
return err
1302+
}
1303+
return nil
1304+
}

oci/core_instance_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ func TestCoreInstanceResource_basic(t *testing.T) {
128128
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
129129
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
130130

131+
compartmentIdU := getEnvSettingWithDefault("compartment_id_for_update", compartmentId)
132+
compartmentIdUVariableStr := fmt.Sprintf("variable \"compartment_id_for_update\" { default = \"%s\" }\n", compartmentIdU)
133+
131134
resourceName := "oci_core_instance.test_instance"
132135
datasourceName := "data.oci_core_instances.test_instances"
133136
singularDatasourceName := "data.oci_core_instance.test_instance"
@@ -230,6 +233,57 @@ func TestCoreInstanceResource_basic(t *testing.T) {
230233
),
231234
},
232235

236+
// verify update to the compartment (the compartment will be switched back in the next step)
237+
{
238+
Config: config + compartmentIdVariableStr + compartmentIdUVariableStr + InstanceResourceDependencies +
239+
generateResourceFromRepresentationMap("oci_core_instance", "test_instance", Optional, Create,
240+
representationCopyWithNewProperties(instanceRepresentation, map[string]interface{}{
241+
"compartment_id": Representation{repType: Required, create: `${var.compartment_id_for_update}`},
242+
})),
243+
Check: resource.ComposeAggregateTestCheckFunc(
244+
resource.TestCheckResourceAttr(resourceName, "agent_config.#", "1"),
245+
resource.TestCheckResourceAttr(resourceName, "agent_config.0.is_monitoring_disabled", "false"),
246+
resource.TestCheckResourceAttrSet(resourceName, "availability_domain"),
247+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentIdU),
248+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.#", "1"),
249+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.assign_public_ip", "true"),
250+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.defined_tags.%", "1"),
251+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.display_name", "displayName"),
252+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.freeform_tags.%", "1"),
253+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.hostname_label", "hostnamelabel"),
254+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.private_ip", "10.0.0.5"),
255+
resource.TestCheckResourceAttr(resourceName, "create_vnic_details.0.skip_source_dest_check", "false"),
256+
resource.TestCheckResourceAttrSet(resourceName, "create_vnic_details.0.subnet_id"),
257+
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
258+
resource.TestCheckResourceAttr(resourceName, "display_name", "displayName"),
259+
resource.TestCheckResourceAttr(resourceName, "extended_metadata.%", "2"),
260+
resource.TestCheckResourceAttr(resourceName, "fault_domain", "FAULT-DOMAIN-2"),
261+
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
262+
resource.TestCheckResourceAttr(resourceName, "hostname_label", "hostnamelabel"),
263+
resource.TestCheckResourceAttrSet(resourceName, "id"),
264+
resource.TestCheckResourceAttrSet(resourceName, "image"),
265+
resource.TestCheckResourceAttr(resourceName, "ipxe_script", "ipxeScript"),
266+
resource.TestCheckResourceAttr(resourceName, "is_pv_encryption_in_transit_enabled", "false"),
267+
resource.TestCheckResourceAttr(resourceName, "metadata.%", "1"),
268+
resource.TestCheckResourceAttrSet(resourceName, "region"),
269+
resource.TestCheckResourceAttr(resourceName, "shape", "VM.Standard2.1"),
270+
resource.TestCheckResourceAttr(resourceName, "source_details.#", "1"),
271+
resource.TestCheckResourceAttrSet(resourceName, "source_details.0.source_id"),
272+
resource.TestCheckResourceAttr(resourceName, "source_details.0.source_type", "image"),
273+
resource.TestCheckResourceAttr(resourceName, "state", "STOPPED"),
274+
resource.TestCheckResourceAttrSet(resourceName, "subnet_id"),
275+
resource.TestCheckResourceAttrSet(resourceName, "time_created"),
276+
277+
func(s *terraform.State) (err error) {
278+
resId2, err = fromInstanceState(s, resourceName, "id")
279+
if resId != resId2 {
280+
return fmt.Errorf("resource recreated when it was supposed to be updated")
281+
}
282+
return err
283+
},
284+
),
285+
},
286+
233287
// verify updates to updatable parameters
234288
{
235289
Config: config + compartmentIdVariableStr + InstanceResourceDependencies +

website/docs/d/core_boot_volume_backup.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ The following attributes are exported:
3939
* `freeform_tags` - Free-form tags for this resource. Each tag is a simple key-value pair with no predefined name, type, or namespace. For more information, see [Resource Tags](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
4040
* `id` - The OCID of the boot volume backup.
4141
* `image_id` - The image OCID used to create the boot volume the backup is taken from.
42-
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the boot volume backup.
42+
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the boot volume backup. For more information about the Key Management service and encryption keys, see [Overview of Key Management](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Concepts/keyoverview.htm) and [Using Keys](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Tasks/usingkeys.htm).
4343
* `size_in_gbs` - The size of the boot volume, in GBs.
4444
* `source_type` - Specifies whether the backup was created manually, or via scheduled backup policy.
4545
* `state` - The current state of a boot volume backup.

website/docs/d/core_boot_volume_backups.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The following attributes are exported:
5454
* `freeform_tags` - Free-form tags for this resource. Each tag is a simple key-value pair with no predefined name, type, or namespace. For more information, see [Resource Tags](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
5555
* `id` - The OCID of the boot volume backup.
5656
* `image_id` - The image OCID used to create the boot volume the backup is taken from.
57-
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the boot volume backup.
57+
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the boot volume backup. For more information about the Key Management service and encryption keys, see [Overview of Key Management](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Concepts/keyoverview.htm) and [Using Keys](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Tasks/usingkeys.htm).
5858
* `size_in_gbs` - The size of the boot volume, in GBs.
5959
* `source_type` - Specifies whether the backup was created manually, or via scheduled backup policy.
6060
* `state` - The current state of a boot volume backup.

website/docs/d/core_volume_backups.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The following attributes are exported:
5454
* `expiration_time` - The date and time the volume backup will expire and be automatically deleted. Format defined by RFC3339. This parameter will always be present for backups that were created automatically by a scheduled-backup policy. For manually created backups, it will be absent, signifying that there is no expiration time and the backup will last forever until manually deleted.
5555
* `freeform_tags` - Free-form tags for this resource. Each tag is a simple key-value pair with no predefined name, type, or namespace. For more information, see [Resource Tags](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
5656
* `id` - The OCID of the volume backup.
57-
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the backup.
57+
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the volume backup. For more information about the Key Management service and encryption keys, see [Overview of Key Management](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Concepts/keyoverview.htm) and [Using Keys](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Tasks/usingkeys.htm).
5858
* `size_in_gbs` - The size of the volume, in GBs.
5959
* `size_in_mbs` - The size of the volume in MBs. The value must be a multiple of 1024. This field is deprecated. Please use `size_in_gbs`.
6060
* `source_type` - Specifies whether the backup was created manually, or via scheduled backup policy.

website/docs/r/core_boot_volume_backup.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The following attributes are exported:
5959
* `freeform_tags` - Free-form tags for this resource. Each tag is a simple key-value pair with no predefined name, type, or namespace. For more information, see [Resource Tags](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
6060
* `id` - The OCID of the boot volume backup.
6161
* `image_id` - The image OCID used to create the boot volume the backup is taken from.
62-
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the boot volume backup.
62+
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the boot volume backup. For more information about the Key Management service and encryption keys, see [Overview of Key Management](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Concepts/keyoverview.htm) and [Using Keys](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Tasks/usingkeys.htm).
6363
* `size_in_gbs` - The size of the boot volume, in GBs.
6464
* `source_type` - Specifies whether the backup was created manually, or via scheduled backup policy.
6565
* `state` - The current state of a boot volume backup.

website/docs/r/core_instance.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ The following arguments are supported:
106106
* `agent_config` - (Optional) (Updatable)
107107
* `is_monitoring_disabled` - (Optional) (Updatable) Whether the agent running on the instance can gather performance metrics and monitor the instance. Default value is false.
108108
* `availability_domain` - (Required) The availability domain of the instance. Example: `Uocm:PHX-AD-1`
109-
* `compartment_id` - (Required) The OCID of the compartment.
109+
* `compartment_id` - (Required) (Updatable) The OCID of the compartment.
110110
* `create_vnic_details` - (Optional) Details for the primary VNIC, which is automatically created and attached when the instance is launched.
111111
* `assign_public_ip` - (Optional) Whether the VNIC should be assigned a public IP address. Defaults to true. If left blank or set to true and `prohibitPublicIpOnVnic` = true, an error is returned.
112112

website/docs/r/core_virtual_circuit.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ The following arguments are supported:
9292
* `public_prefixes` - (Optional) For a public virtual circuit. The public IP prefixes (CIDRs) the customer wants to advertise across the connection.
9393
* `cidr_block` - (Required) An individual public IP prefix (CIDR) to add to the public virtual circuit. Must be /31 or less specific.
9494
* `region` - (Optional) The Oracle Cloud Infrastructure region where this virtual circuit is located. Example: `phx`
95-
* `type` - (Required) The type of IP addresses used in this virtual circuit. PRIVATE means [RFC 1918](https://tools.ietf.org/html/rfc1918) addresses (10.0.0.0/8, 172.16/12, and 192.168/16). Only PRIVATE is supported.
95+
* `type` - (Required) The type of IP addresses used in this virtual circuit. PRIVATE means [RFC 1918](https://tools.ietf.org/html/rfc1918) addresses (10.0.0.0/8, 172.16/12, and 192.168/16).
9696

9797

9898
** IMPORTANT **

website/docs/r/core_volume_backup.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The following attributes are exported:
6161
* `expiration_time` - The date and time the volume backup will expire and be automatically deleted. Format defined by RFC3339. This parameter will always be present for backups that were created automatically by a scheduled-backup policy. For manually created backups, it will be absent, signifying that there is no expiration time and the backup will last forever until manually deleted.
6262
* `freeform_tags` - Free-form tags for this resource. Each tag is a simple key-value pair with no predefined name, type, or namespace. For more information, see [Resource Tags](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
6363
* `id` - The OCID of the volume backup.
64-
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the backup.
64+
* `kms_key_id` - The OCID of the KMS key which is the master encryption key for the volume backup. For more information about the Key Management service and encryption keys, see [Overview of Key Management](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Concepts/keyoverview.htm) and [Using Keys](https://docs.cloud.oracle.com/iaas/Content/KeyManagement/Tasks/usingkeys.htm).
6565
* `size_in_gbs` - The size of the volume, in GBs.
6666
* `size_in_mbs` - The size of the volume in MBs. The value must be a multiple of 1024. This field is deprecated. Please use `size_in_gbs`.
6767
* `source_type` - Specifies whether the backup was created manually, or via scheduled backup policy.

0 commit comments

Comments
 (0)