Skip to content

Commit 69750bc

Browse files
author
jiangong
committed
Support for moving Service Gateway resource across Compartments
1 parent e982683 commit 69750bc

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
### Added
55
- Support for scheduling KMS key deletion
66
- Support for moving Volumes, Volume groups, Boot Volumes and corresponding Backups across compartments
7+
- Support for moving Service Gateway resource across Compartments
78

89
## 3.29.0 (June 12, 2019)
910

oci/core_service_gateway_resource.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func CoreServiceGatewayResource() *schema.Resource {
2828
"compartment_id": {
2929
Type: schema.TypeString,
3030
Required: true,
31-
ForceNew: true,
3231
},
3332
"services": {
3433
Type: schema.TypeSet,
@@ -239,6 +238,15 @@ func (s *CoreServiceGatewayResourceCrud) Get() error {
239238
}
240239

241240
func (s *CoreServiceGatewayResourceCrud) Update() error {
241+
if compartment, ok := s.D.GetOkExists("compartment_id"); ok && s.D.HasChange("compartment_id") {
242+
oldRaw, newRaw := s.D.GetChange("compartment_id")
243+
if newRaw != "" && oldRaw != "" {
244+
err := s.updateCompartment(compartment)
245+
if err != nil {
246+
return err
247+
}
248+
}
249+
}
242250
request := oci_core.UpdateServiceGatewayRequest{}
243251

244252
if blockTraffic, ok := s.D.GetOkExists("block_traffic"); ok {
@@ -377,3 +385,20 @@ func servicesHashCodeForSets(v interface{}) int {
377385
}
378386
return hashcode.String(buf.String())
379387
}
388+
func (s *CoreServiceGatewayResourceCrud) updateCompartment(compartment interface{}) error {
389+
changeCompartmentRequest := oci_core.ChangeServiceGatewayCompartmentRequest{}
390+
391+
compartmentTmp := compartment.(string)
392+
changeCompartmentRequest.CompartmentId = &compartmentTmp
393+
394+
idTmp := s.D.Id()
395+
changeCompartmentRequest.ServiceGatewayId = &idTmp
396+
397+
changeCompartmentRequest.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "core")
398+
399+
_, err := s.Client.ChangeServiceGatewayCompartment(context.Background(), changeCompartmentRequest)
400+
if err != nil {
401+
return err
402+
}
403+
return nil
404+
}

oci/core_service_gateway_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ func TestCoreServiceGatewayResource_basic(t *testing.T) {
5959
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
6060
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
6161

62+
compartmentIdU := getEnvSettingWithDefault("compartment_id_for_update", compartmentId)
63+
compartmentIdUVariableStr := fmt.Sprintf("variable \"compartment_id_for_update\" { default = \"%s\" }\n", compartmentIdU)
64+
6265
resourceName := "oci_core_service_gateway.test_service_gateway"
6366
datasourceName := "data.oci_core_service_gateways.test_service_gateways"
6467

@@ -122,6 +125,39 @@ func TestCoreServiceGatewayResource_basic(t *testing.T) {
122125
),
123126
},
124127

128+
// verify update to the compartment (the compartment will be switched back in the next step)
129+
{
130+
Config: config + compartmentIdVariableStr + compartmentIdUVariableStr + ServiceGatewayResourceDependencies +
131+
generateResourceFromRepresentationMap("oci_core_service_gateway", "test_service_gateway", Optional, Create,
132+
representationCopyWithNewProperties(serviceGatewayRepresentation, map[string]interface{}{
133+
"compartment_id": Representation{repType: Required, create: `${var.compartment_id_for_update}`},
134+
})),
135+
Check: resource.ComposeAggregateTestCheckFunc(
136+
resource.TestCheckResourceAttrSet(resourceName, "block_traffic"),
137+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentIdU),
138+
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
139+
resource.TestCheckResourceAttr(resourceName, "display_name", "displayName"),
140+
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
141+
resource.TestCheckResourceAttrSet(resourceName, "id"),
142+
resource.TestCheckResourceAttr(resourceName, "services.#", "1"),
143+
CheckResourceSetContainsElementWithProperties(resourceName, "services", map[string]string{},
144+
[]string{
145+
"service_id",
146+
"service_name",
147+
}),
148+
resource.TestCheckResourceAttrSet(resourceName, "state"),
149+
resource.TestCheckResourceAttrSet(resourceName, "vcn_id"),
150+
151+
func(s *terraform.State) (err error) {
152+
resId2, err = fromInstanceState(s, resourceName, "id")
153+
if resId != resId2 {
154+
return fmt.Errorf("resource recreated when it was supposed to be updated")
155+
}
156+
return err
157+
},
158+
),
159+
},
160+
125161
// verify updates to updatable parameters
126162
{
127163
Config: config + compartmentIdVariableStr + ServiceGatewayResourceDependencies +

website/docs/d/core_service_gateways.html.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ data "oci_core_service_gateways" "test_service_gateways" {
3030

3131
The following arguments are supported:
3232

33-
* `compartment_id` - (Required) The OCID of the compartment.
33+
* `compartment_id` - (Required) The [OCID](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/identifiers.htm) of the compartment.
3434
* `state` - (Optional) A filter to return only resources that match the given lifecycle state. The state value is case-insensitive.
35-
* `vcn_id` - (Optional) The OCID of the VCN.
35+
* `vcn_id` - (Optional) The [OCID](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/identifiers.htm) of the VCN.
3636

3737

3838
## Attributes Reference

website/docs/r/core_service_gateway.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ resource "oci_core_service_gateway" "test_service_gateway" {
4343

4444
The following arguments are supported:
4545

46-
* `compartment_id` - (Required) The [OCID] (https://docs.cloud.oracle.com/iaas/Content/General/Concepts/identifiers.htm) of the compartment to contain the service gateway.
46+
* `compartment_id` - (Required) (Updatable) The [OCID] (https://docs.cloud.oracle.com/iaas/Content/General/Concepts/identifiers.htm) of the compartment to contain the service gateway.
4747
* `defined_tags` - (Optional) (Updatable) Defined tags for this resource. Each key is predefined and scoped to a namespace. For more information, see [Resource Tags](https://docs.cloud.oracle.com/iaas/Content/General/Concepts/resourcetags.htm). Example: `{"Operations.CostCenter": "42"}`
4848
* `display_name` - (Optional) (Updatable) A user-friendly name. Does not have to be unique, and it's changeable. Avoid entering confidential information.
4949
* `freeform_tags` - (Optional) (Updatable) 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"}`

0 commit comments

Comments
 (0)