Skip to content

Commit 216223a

Browse files
jiangongabhilash-av
authored andcommitted
kms vaults change compartment
kms key change compartment
1 parent 95869de commit 216223a

File tree

7 files changed

+132
-14
lines changed

7 files changed

+132
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- Support for Permitted Methods Feature in LBaaS
99
- Support for VCN access control lists via `load_balancer_rule_set`
1010
- Support for moving `ons_notification_topic`, `ons_subscription` resources across compartments
11+
- Support for moving `oci_load_balancer` across compartments
12+
- Support for moving `oci_kms_key` and `oci_kms_vault` Across Compartments
1113

1214
## 3.33.0 (July 10, 2019)
1315

@@ -19,7 +21,6 @@
1921
- Support for Granular Security Lists in Load Balancer
2022
- Support for Network Security Groups in databases
2123
- Support in autonomous database and object data sources for encoding downloaded binary content as base64. This works around behavior in Terraform v0.12 that could cause binary content to be corrupted if written directly to state.
22-
- Support for moving `oci_load_balancer` across compartments
2324

2425
### Fixed
2526
- Address panics caused by invalid type assertions in object map conversion. This could potentially affect attributes

oci/kms_key_resource.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func KmsKeyResource() *schema.Resource {
3333
"compartment_id": {
3434
Type: schema.TypeString,
3535
Required: true,
36-
ForceNew: true,
3736
},
3837
"display_name": {
3938
Type: schema.TypeString,
@@ -318,6 +317,15 @@ func (s *KmsKeyResourceCrud) Get() error {
318317
}
319318

320319
func (s *KmsKeyResourceCrud) Update() error {
320+
if compartment, ok := s.D.GetOkExists("compartment_id"); ok && s.D.HasChange("compartment_id") {
321+
oldRaw, newRaw := s.D.GetChange("compartment_id")
322+
if newRaw != "" && oldRaw != "" {
323+
err := s.updateCompartment(compartment)
324+
if err != nil {
325+
return err
326+
}
327+
}
328+
}
321329
request := oci_kms.UpdateKeyRequest{}
322330

323331
if definedTags, ok := s.D.GetOkExists("defined_tags"); ok {
@@ -470,3 +478,21 @@ func KeyShapeToMap(obj *oci_kms.KeyShape) map[string]interface{} {
470478

471479
return result
472480
}
481+
482+
func (s *KmsKeyResourceCrud) updateCompartment(compartment interface{}) error {
483+
changeCompartmentRequest := oci_kms.ChangeKeyCompartmentRequest{}
484+
485+
compartmentTmp := compartment.(string)
486+
changeCompartmentRequest.CompartmentId = &compartmentTmp
487+
488+
idTmp := s.D.Id()
489+
changeCompartmentRequest.KeyId = &idTmp
490+
491+
changeCompartmentRequest.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "kms")
492+
493+
_, err := s.Client.ChangeKeyCompartment(context.Background(), changeCompartmentRequest)
494+
if err != nil {
495+
return err
496+
}
497+
return nil
498+
}

oci/kms_key_test.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var (
3030
}
3131

3232
keyDataSourceRepresentation = map[string]interface{}{
33-
"compartment_id": Representation{repType: Required, create: `${var.tenancy_ocid}`},
33+
"compartment_id": Representation{repType: Required, create: `${var.compartment_id}`},
3434
"management_endpoint": Representation{repType: Required, create: `${data.oci_kms_vault.test_vault.management_endpoint}`},
3535
"filter": RepresentationGroup{Required, keyDataSourceFilterRepresentation}}
3636
keyDataSourceFilterRepresentation = map[string]interface{}{
@@ -41,7 +41,7 @@ var (
4141
deletionTime = time.Now().UTC().AddDate(0, 0, 8).Truncate(time.Millisecond)
4242

4343
keyRepresentation = map[string]interface{}{
44-
"compartment_id": Representation{repType: Required, create: `${var.tenancy_ocid}`},
44+
"compartment_id": Representation{repType: Required, create: `${var.compartment_id}`},
4545
"display_name": Representation{repType: Required, create: `Key C`, update: `displayName2`},
4646
"key_shape": RepresentationGroup{Required, keyKeyShapeRepresentation},
4747
"management_endpoint": Representation{repType: Required, create: `${data.oci_kms_vault.test_vault.management_endpoint}`},
@@ -90,7 +90,9 @@ func TestKmsKeyResource_basic(t *testing.T) {
9090

9191
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
9292
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
93-
tenancyId := getEnvSettingWithBlankDefault("tenancy_ocid")
93+
94+
compartmentIdU := getEnvSettingWithDefault("compartment_id_for_update", compartmentId)
95+
compartmentIdUVariableStr := fmt.Sprintf("variable \"compartment_id_for_update\" { default = \"%s\" }\n", compartmentIdU)
9496

9597
resourceName := "oci_kms_key.test_key"
9698
datasourceName := "data.oci_kms_keys.test_keys"
@@ -110,7 +112,7 @@ func TestKmsKeyResource_basic(t *testing.T) {
110112
Config: config + compartmentIdVariableStr + KeyResourceDependencies + DefinedTagsDependencies +
111113
generateResourceFromRepresentationMap("oci_kms_key", "test_key", Required, Create, keyRepresentation),
112114
Check: resource.ComposeAggregateTestCheckFunc(
113-
resource.TestCheckResourceAttr(resourceName, "compartment_id", tenancyId),
115+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
114116
resource.TestCheckResourceAttr(resourceName, "display_name", "Key C"),
115117
resource.TestCheckResourceAttr(resourceName, "key_shape.#", "1"),
116118
resource.TestCheckResourceAttr(resourceName, "key_shape.0.algorithm", "AES"),
@@ -132,7 +134,7 @@ func TestKmsKeyResource_basic(t *testing.T) {
132134
Config: config + compartmentIdVariableStr + KeyResourceDependencies + DefinedTagsDependencies +
133135
generateResourceFromRepresentationMap("oci_kms_key", "test_key", Optional, Create, keyRepresentation),
134136
Check: resource.ComposeAggregateTestCheckFunc(
135-
resource.TestCheckResourceAttr(resourceName, "compartment_id", tenancyId),
137+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
136138
resource.TestCheckResourceAttrSet(resourceName, "current_key_version"),
137139
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
138140
resource.TestCheckResourceAttr(resourceName, "display_name", "Key C"),
@@ -153,12 +155,43 @@ func TestKmsKeyResource_basic(t *testing.T) {
153155
),
154156
},
155157

158+
// verify update to the compartment (the compartment will be switched back in the next step)
159+
{
160+
Config: config + compartmentIdVariableStr + compartmentIdUVariableStr + KeyResourceDependencies + DefinedTagsDependencies +
161+
generateResourceFromRepresentationMap("oci_kms_key", "test_key", Optional, Create,
162+
representationCopyWithNewProperties(keyRepresentation, map[string]interface{}{
163+
"compartment_id": Representation{repType: Required, create: `${var.compartment_id_for_update}`},
164+
})),
165+
Check: resource.ComposeAggregateTestCheckFunc(
166+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentIdU),
167+
resource.TestCheckResourceAttrSet(resourceName, "current_key_version"),
168+
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
169+
resource.TestCheckResourceAttr(resourceName, "display_name", "Key C"),
170+
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
171+
resource.TestCheckResourceAttrSet(resourceName, "id"),
172+
resource.TestCheckResourceAttr(resourceName, "key_shape.#", "1"),
173+
resource.TestCheckResourceAttr(resourceName, "key_shape.0.algorithm", "AES"),
174+
resource.TestCheckResourceAttr(resourceName, "key_shape.0.length", "16"),
175+
resource.TestCheckResourceAttrSet(resourceName, "state"),
176+
resource.TestCheckResourceAttrSet(resourceName, "time_created"),
177+
resource.TestCheckResourceAttrSet(resourceName, "vault_id"),
178+
179+
func(s *terraform.State) (err error) {
180+
resId2, err = fromInstanceState(s, resourceName, "id")
181+
if resId != resId2 {
182+
return fmt.Errorf("resource recreated when it was supposed to be updated")
183+
}
184+
return err
185+
},
186+
),
187+
},
188+
156189
// verify updates to updatable parameters
157190
{
158191
Config: config + compartmentIdVariableStr + KeyResourceDependencies + DefinedTagsDependencies +
159192
generateResourceFromRepresentationMap("oci_kms_key", "test_key", Optional, Update, keyRepresentation),
160193
Check: resource.ComposeAggregateTestCheckFunc(
161-
resource.TestCheckResourceAttr(resourceName, "compartment_id", tenancyId),
194+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentId),
162195
resource.TestCheckResourceAttrSet(resourceName, "current_key_version"),
163196
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
164197
resource.TestCheckResourceAttr(resourceName, "display_name", "displayName2"),
@@ -187,10 +220,10 @@ func TestKmsKeyResource_basic(t *testing.T) {
187220
compartmentIdVariableStr + KeyResourceDependencies + DefinedTagsDependencies +
188221
generateResourceFromRepresentationMap("oci_kms_key", "test_key", Optional, Update, keyRepresentation),
189222
Check: resource.ComposeAggregateTestCheckFunc(
190-
resource.TestCheckResourceAttr(datasourceName, "compartment_id", tenancyId),
223+
resource.TestCheckResourceAttr(datasourceName, "compartment_id", compartmentId),
191224

192225
resource.TestCheckResourceAttr(datasourceName, "keys.#", "1"),
193-
resource.TestCheckResourceAttr(datasourceName, "keys.0.compartment_id", tenancyId),
226+
resource.TestCheckResourceAttr(datasourceName, "keys.0.compartment_id", compartmentId),
194227
resource.TestCheckResourceAttr(datasourceName, "keys.0.defined_tags.%", "1"),
195228
resource.TestCheckResourceAttr(datasourceName, "keys.0.display_name", "displayName2"),
196229
resource.TestCheckResourceAttr(datasourceName, "keys.0.freeform_tags.%", "1"),
@@ -208,7 +241,7 @@ func TestKmsKeyResource_basic(t *testing.T) {
208241
Check: resource.ComposeAggregateTestCheckFunc(
209242
resource.TestCheckResourceAttrSet(singularDatasourceName, "key_id"),
210243

211-
resource.TestCheckResourceAttr(singularDatasourceName, "compartment_id", tenancyId),
244+
resource.TestCheckResourceAttr(singularDatasourceName, "compartment_id", compartmentId),
212245
resource.TestCheckResourceAttrSet(singularDatasourceName, "current_key_version"),
213246
resource.TestCheckResourceAttr(singularDatasourceName, "defined_tags.%", "1"),
214247
resource.TestCheckResourceAttr(singularDatasourceName, "display_name", "displayName2"),

oci/kms_vault_resource.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func KmsVaultResource() *schema.Resource {
2525
"compartment_id": {
2626
Type: schema.TypeString,
2727
Required: true,
28-
ForceNew: true,
2928
},
3029
"display_name": {
3130
Type: schema.TypeString,
@@ -204,6 +203,15 @@ func (s *KmsVaultResourceCrud) Get() error {
204203
}
205204

206205
func (s *KmsVaultResourceCrud) Update() error {
206+
if compartment, ok := s.D.GetOkExists("compartment_id"); ok && s.D.HasChange("compartment_id") {
207+
oldRaw, newRaw := s.D.GetChange("compartment_id")
208+
if newRaw != "" && oldRaw != "" {
209+
err := s.updateCompartment(compartment)
210+
if err != nil {
211+
return err
212+
}
213+
}
214+
}
207215
request := oci_kms.UpdateVaultRequest{}
208216

209217
if definedTags, ok := s.D.GetOkExists("defined_tags"); ok {
@@ -286,3 +294,21 @@ func (s *KmsVaultResourceCrud) SetData() error {
286294

287295
return nil
288296
}
297+
298+
func (s *KmsVaultResourceCrud) updateCompartment(compartment interface{}) error {
299+
changeCompartmentRequest := oci_kms.ChangeVaultCompartmentRequest{}
300+
301+
compartmentTmp := compartment.(string)
302+
changeCompartmentRequest.CompartmentId = &compartmentTmp
303+
304+
idTmp := s.D.Id()
305+
changeCompartmentRequest.VaultId = &idTmp
306+
307+
changeCompartmentRequest.RequestMetadata.RetryPolicy = getRetryPolicy(s.DisableNotFoundRetries, "kms")
308+
309+
_, err := s.Client.ChangeVaultCompartment(context.Background(), changeCompartmentRequest)
310+
if err != nil {
311+
return err
312+
}
313+
return nil
314+
}

oci/kms_vault_test.go

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

60+
compartmentIdU := getEnvSettingWithDefault("compartment_id_for_update", compartmentId)
61+
compartmentIdUVariableStr := fmt.Sprintf("variable \"compartment_id_for_update\" { default = \"%s\" }\n", compartmentIdU)
62+
6063
resourceName := "oci_kms_vault.test_vault"
6164
datasourceName := "data.oci_kms_vaults.test_vaults"
6265
singularDatasourceName := "data.oci_kms_vault.test_vault"
@@ -113,6 +116,35 @@ func TestKmsVaultResource_basic(t *testing.T) {
113116
),
114117
},
115118

119+
// verify update to the compartment (the compartment will be switched back in the next step)
120+
{
121+
Config: config + compartmentIdVariableStr + compartmentIdUVariableStr + VaultResourceDependencies +
122+
generateResourceFromRepresentationMap("oci_kms_vault", "test_vault", Optional, Create,
123+
representationCopyWithNewProperties(vaultRepresentation, map[string]interface{}{
124+
"compartment_id": Representation{repType: Required, create: `${var.compartment_id_for_update}`},
125+
})),
126+
Check: resource.ComposeAggregateTestCheckFunc(
127+
resource.TestCheckResourceAttr(resourceName, "compartment_id", compartmentIdU),
128+
resource.TestCheckResourceAttrSet(resourceName, "crypto_endpoint"),
129+
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
130+
resource.TestCheckResourceAttr(resourceName, "display_name", "Vault 1"),
131+
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
132+
resource.TestCheckResourceAttrSet(resourceName, "id"),
133+
resource.TestCheckResourceAttrSet(resourceName, "management_endpoint"),
134+
resource.TestCheckResourceAttrSet(resourceName, "state"),
135+
resource.TestCheckResourceAttrSet(resourceName, "time_created"),
136+
resource.TestCheckResourceAttr(resourceName, "vault_type", "VIRTUAL_PRIVATE"),
137+
138+
func(s *terraform.State) (err error) {
139+
resId2, err = fromInstanceState(s, resourceName, "id")
140+
if resId != resId2 {
141+
return fmt.Errorf("resource recreated when it was supposed to be updated")
142+
}
143+
return err
144+
},
145+
),
146+
},
147+
116148
// verify updates to updatable parameters
117149
{
118150
Config: config + compartmentIdVariableStr + VaultResourceDependencies +

website/docs/r/kms_key.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ resource "oci_kms_key" "test_key" {
3535

3636
The following arguments are supported:
3737

38-
* `compartment_id` - (Required) The OCID of the compartment that contains this key.
38+
* `compartment_id` - (Required) (Updatable) The OCID of the compartment that contains this key.
3939
* `defined_tags` - (Optional) (Updatable) Usage of predefined tag keys. These predefined keys are scoped to namespaces. Example: `{"foo-namespace.bar-key": "foo-value"}`
4040
* `desired_state` - (Optional) (Updatable) Desired state of the key. Possible values : `ENABLED` or `DISABLED`
4141
* `display_name` - (Required) (Updatable) A user-friendly name for the key. It does not have to be unique, and it is changeable. Avoid entering confidential information.

website/docs/r/kms_vault.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ resource "oci_kms_vault" "test_vault" {
3434

3535
The following arguments are supported:
3636

37-
* `compartment_id` - (Required) The OCID of the compartment where you want to create this vault.
37+
* `compartment_id` - (Required) (Updatable) The OCID of the compartment where you want to create this vault.
3838
* `defined_tags` - (Optional) (Updatable) Usage of predefined tag keys. These predefined keys are scoped to namespaces. Example: `{"foo-namespace.bar-key": "foo-value"}`
3939
* `display_name` - (Required) (Updatable) A user-friendly name for the vault. It does not have to be unique, and it is changeable. Avoid entering confidential information.
4040
* `freeform_tags` - (Optional) (Updatable) Simple key-value pair that is applied without any predefined name, type, or scope. Exists for cross-compatibility only. Example: `{"bar-key": "value"}`

0 commit comments

Comments
 (0)