Skip to content

Commit c9c57d7

Browse files
ccushingrcohenma
authored andcommitted
Support nesting and deleting compartments
1 parent b640d8e commit c9c57d7

11 files changed

+486
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 3.6.0 (November 01, 2018)
1+
## 3.6.0 (Unreleased)
22

33
### Added
44
- New parameters `db_name` and `state` in `oci_database_database` data source
@@ -12,6 +12,7 @@
1212
- Support for specifying a Key Management Service key when backing up or restoring a block storage volume in the Block Volume service
1313
- Support enabling cost tracking for tags using `is_cost_tracking` field
1414
- Support returning maintenance reboot time for compute instances using `time_maintenance_reboot_due` field
15+
- Support nesting and deleting compartments. Compartment delete requires opt in, see compartment documentation
1516

1617
### Fixed
1718
- Data type for properties with type as TypeSet to TypeList in following datasources: `oci_core_route_tables`, `oci_core_security_lists`, `oci_core_volume`, and `oci_core_service_gateways` to allow referencing by indexes in Terraform configs.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* This example demonstrates how to work with nested compartments.
3+
*/
4+
5+
variable "tenancy_ocid" {}
6+
variable "user_ocid" {}
7+
variable "fingerprint" {}
8+
variable "private_key_path" {}
9+
variable "compartment_ocid" {}
10+
variable "region" {}
11+
12+
provider "oci" {
13+
region = "${var.region}"
14+
tenancy_ocid = "${var.tenancy_ocid}"
15+
user_ocid = "${var.user_ocid}"
16+
fingerprint = "${var.fingerprint}"
17+
private_key_path = "${var.private_key_path}"
18+
}
19+
20+
resource "oci_identity_compartment" "parent-compartment" {
21+
name = "parent-compartment"
22+
description = "compartment that holds a compartment"
23+
compartment_id = "${var.tenancy_ocid}"
24+
}
25+
26+
resource "oci_identity_compartment" "child-compartment" {
27+
name = "child-compartment"
28+
description = "compartment inside another compartment"
29+
compartment_id = "${oci_identity_compartment.parent-compartment.id}"
30+
}
31+
32+
data "oci_identity_compartments" "all-compartments" {
33+
compartment_id = "${oci_identity_compartment.parent-compartment.compartment_id}"
34+
compartment_id_in_subtree = "true"
35+
access_level = "ANY"
36+
37+
filter {
38+
name = "name"
39+
values = ["parent-compartment", "child-compartment"]
40+
}
41+
}
42+
43+
data "oci_identity_compartment" "child-compartment" {
44+
id = "${oci_identity_compartment.child-compartment.id}"
45+
}
46+
47+
output "print-child-compartment" {
48+
value = <<EOF
49+
50+
id = ${data.oci_identity_compartment.child-compartment.id}
51+
compartment_id = ${data.oci_identity_compartment.child-compartment.compartment_id}
52+
name = ${data.oci_identity_compartment.child-compartment.name}
53+
description = ${data.oci_identity_compartment.child-compartment.description}
54+
EOF
55+
}
56+
57+
output "print-all-compartments" {
58+
value = "${data.oci_identity_compartments.all-compartments.compartments}"
59+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"context"
7+
"strconv"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
oci_identity "github.com/oracle/oci-go-sdk/identity"
11+
)
12+
13+
func CompartmentDataSource() *schema.Resource {
14+
return &schema.Resource{
15+
Read: readSingularCompartment,
16+
Schema: map[string]*schema.Schema{
17+
"id": {
18+
Type: schema.TypeString,
19+
Required: true,
20+
},
21+
"compartment_id": {
22+
Type: schema.TypeString,
23+
Computed: true,
24+
},
25+
// Computed
26+
"defined_tags": {
27+
Type: schema.TypeMap,
28+
Computed: true,
29+
Elem: schema.TypeString,
30+
},
31+
"description": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"freeform_tags": {
36+
Type: schema.TypeMap,
37+
Computed: true,
38+
Elem: schema.TypeString,
39+
},
40+
"inactive_state": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
},
44+
"is_accessible": {
45+
Type: schema.TypeBool,
46+
Computed: true,
47+
},
48+
"name": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"state": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
},
56+
"time_created": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
},
60+
},
61+
}
62+
}
63+
64+
func readSingularCompartment(d *schema.ResourceData, m interface{}) error {
65+
sync := &CompartmentDataSourceCrud{}
66+
sync.D = d
67+
sync.Client = m.(*OracleClients).identityClient
68+
69+
return ReadResource(sync)
70+
}
71+
72+
type CompartmentDataSourceCrud struct {
73+
D *schema.ResourceData
74+
Client *oci_identity.IdentityClient
75+
Res *oci_identity.GetCompartmentResponse
76+
}
77+
78+
func (s *CompartmentDataSourceCrud) VoidState() {
79+
s.D.SetId("")
80+
}
81+
82+
func (s *CompartmentDataSourceCrud) Get() error {
83+
request := oci_identity.GetCompartmentRequest{}
84+
85+
// API ambiguity: the GET identifier for a single compartment is "compartmentId", but on the returned object
86+
// it's "id", and "compartmentId" holds the _parent_ compartment id.
87+
if id, ok := s.D.GetOkExists("id"); ok {
88+
tmp := id.(string)
89+
request.CompartmentId = &tmp
90+
}
91+
92+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity")
93+
94+
response, err := s.Client.GetCompartment(context.Background(), request)
95+
if err != nil {
96+
return err
97+
}
98+
99+
s.Res = &response
100+
return nil
101+
}
102+
103+
func (s *CompartmentDataSourceCrud) SetData() error {
104+
if s.Res == nil {
105+
return nil
106+
}
107+
108+
s.D.SetId(*s.Res.Id)
109+
110+
if s.Res.DefinedTags != nil {
111+
s.D.Set("defined_tags", definedTagsToMap(s.Res.DefinedTags))
112+
}
113+
114+
if s.Res.CompartmentId != nil {
115+
s.D.Set("compartment_id", *s.Res.CompartmentId)
116+
}
117+
118+
if s.Res.Description != nil {
119+
s.D.Set("description", *s.Res.Description)
120+
}
121+
122+
s.D.Set("freeform_tags", s.Res.FreeformTags)
123+
124+
if s.Res.InactiveStatus != nil {
125+
s.D.Set("inactive_state", strconv.FormatInt(*s.Res.InactiveStatus, 10))
126+
}
127+
128+
if s.Res.IsAccessible != nil {
129+
s.D.Set("is_accessible", *s.Res.IsAccessible)
130+
}
131+
132+
if s.Res.Name != nil {
133+
s.D.Set("name", *s.Res.Name)
134+
}
135+
136+
s.D.Set("state", s.Res.LifecycleState)
137+
138+
if s.Res.TimeCreated != nil {
139+
s.D.Set("time_created", s.Res.TimeCreated.String())
140+
}
141+
142+
return nil
143+
}

0 commit comments

Comments
 (0)