Skip to content

Commit 13e7633

Browse files
ccushingrcohenma
authored andcommitted
Add Fault Domains data source and integrate with Instance resource
1 parent 4bf721d commit 13e7633

15 files changed

+352
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ All notable changes to this project are documented in this file.
33

44
The format is based on [Keep a Changelog](http://keepachangelog.com/).
55

6+
## 2.2.2 (Unreleased)
7+
8+
### Added
9+
- Support listing Fault Domains in an AD and specifying them when launching an Instance
10+
11+
612
## 2.2.1 - 2018-08-23
713

814
### Added

docs/Table of Contents.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ This lists all of the available OCI resources and/or data sources.
7979
* [Compartments](identity/compartments.md)
8080
* [Customer Secret Keys](identity/customer_secret_keys.md)
8181
* [Dynamic Groups](identity/dynamic_groups.md)
82+
* [Fault Domains](identity/fault_domains.md)
8283
* [Groups](identity/groups.md)
8384
* [Identity Providers](identity/identity_providers.md)
8485
* [IdpGroupMappings](identity/idp_group_mappings.md)

docs/core/instances.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ The following attributes are exported:
1111
* `compartment_id` - The OCID of the compartment that contains the instance.
1212
* `defined_tags` - Defined tags for this resource. Each key is predefined and scoped to a namespace. For more information, see [Resource Tags](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/resourcetags.htm). Example: `{"Operations.CostCenter": "42"}`
1313
* `display_name` - A user-friendly name. Does not have to be unique, and it's changeable. Avoid entering confidential information. Example: `My bare metal instance`
14-
* `extended_metadata` - Additional metadata key/value pairs that you provide. They serve a similar purpose and functionality from fields in the 'metadata' object.
14+
* `extended_metadata` - Additional metadata key/value pairs that you provide. They serve the same purpose and functionality as fields in the 'metadata' object.
1515

1616
They are distinguished from 'metadata' fields in that these can be nested JSON objects (whereas 'metadata' fields are string/string maps only).
17+
18+
If you don't need nested metadata values, it is strongly advised to avoid using this object and use the Metadata object instead.
19+
20+
Input in terraform is the same as metadata but allows nested metadata if you pass a valid JSON string as a value. See the example below.
21+
* `fault_domain` - The name of the fault domain the instance is running in.
1722

18-
If you don't need nested metadata values, it is strongly advised to avoid using this object and use the Metadata object instead.
23+
A fault domain is a grouping of hardware and infrastructure within an availability domain. Each availability domain contains three fault domains. Fault domains let you distribute your instances so that they are not on the same physical hardware within a single availability domain. A hardware failure or Compute hardware maintenance that affects one fault domain does not affect instances in other fault domains.
24+
25+
If you do not specify the fault domain, the system selects one for you. To change the fault domain for an instance, terminate it and launch a new instance in the preferred fault domain.
26+
27+
Example: `FAULT-DOMAIN-1`
1928
* `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.us-phoenix-1.oraclecloud.com/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
2029
* `id` - The OCID of the instance.
2130
* `image` - Deprecated. Use `sourceDetails` instead.
@@ -134,6 +143,7 @@ The following arguments are supported:
134143
If you don't need nested metadata values, it is strongly advised to avoid using this object and use the Metadata object instead.
135144

136145
Input in terraform is the same as metadata but allows nested metadata if you pass a valid JSON string as a value. See the example below.
146+
* `fault_domain` - (Optional) A fault domain is a grouping of hardware and infrastructure within an availability domain. Each availability domain contains three fault domains. Fault domains let you distribute your instances so that they are not on the same physical hardware within a single availability domain. A hardware failure or Compute hardware maintenance that affects one fault domain does not affect instances in other fault domains. If you do not specify the fault domain, the system selects one for you. To change the fault domain for an instance, terminate it and launch a new instance in the preferred fault domain. To get a list of fault domains, use the [ListFaultDomains](https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/20160918/faultDomains/ListFaultDomains) operation in the Identity and Access Management Service API. Example: `FAULT-DOMAIN-1`
137147
* `freeform_tags` - (Optional) 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.us-phoenix-1.oraclecloud.com/Content/General/Concepts/resourcetags.htm). Example: `{"Department": "Finance"}`
138148
* `hostname_label` - (Optional) Deprecated. Instead use `hostnameLabel` in [CreateVnicDetails](https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/20160918/CreateVnicDetails/). If you provide both, the values must match.
139149
* `image` - (Optional) Deprecated. Use `sourceDetails` with [InstanceSourceViaImageDetails](https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/latest/requests/InstanceSourceViaImageDetails) source type instead. If you specify values for both, the values must match.
@@ -220,6 +230,7 @@ resource "oci_core_instance" "test_instance" {
220230
some_string = "stringA"
221231
nested_object = "{\"some_string\": \"stringB\", \"object\": {\"some_string\": \"stringC\"}}"
222232
}
233+
fault_domain = "${var.instance_fault_domain}"
223234
freeform_tags = {"Department"= "Finance"}
224235
hostname_label = "${var.instance_hostname_label}"
225236
ipxe_script = "${var.instance_ipxe_script}"

docs/identity/fault_domains.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
# oci_identity_fault_domains
3+
4+
## FaultDomain DataSource
5+
6+
Gets a list of fault_domains.
7+
8+
### List Operation
9+
Lists the Fault Domains in your tenancy. Specify the OCID of either the tenancy or another
10+
of your compartments as the value for the compartment ID (remember that the tenancy is simply the root compartment).
11+
See [Where to Get the Tenancy's OCID and User's OCID](https://docs.us-phoenix-1.oraclecloud.com/Content/API/Concepts/apisigningkey.htm#five).
12+
13+
The following arguments are supported:
14+
15+
* `availability_domain` - (Required) The name of the availibilityDomain.
16+
* `compartment_id` - (Required) The OCID of the compartment (remember that the tenancy is simply the root compartment).
17+
18+
19+
The following attributes are exported:
20+
21+
* `fault_domains` - The list of fault_domains.
22+
23+
### Example Usage
24+
25+
```hcl
26+
data "oci_identity_fault_domains" "test_fault_domains" {
27+
#Required
28+
availability_domain = "${var.fault_domain_availability_domain}"
29+
compartment_id = "${var.compartment_id}"
30+
}
31+
```
32+
### FaultDomain Reference
33+
34+
The following attributes are exported:
35+
36+
* `availability_domain` - The name of the availabilityDomain where the Fault Domain belongs.
37+
* `compartment_id` - The OCID of the compartment. Currently only tenancy (root) compartment can be provided.
38+
* `id` - The OCID of the Fault Domain.
39+
* `name` - The name of the Fault Domain.

provider/core_instance_resource.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ func InstanceResource() *schema.Resource {
153153
ForceNew: true,
154154
Elem: schema.TypeString,
155155
},
156+
"fault_domain": {
157+
Type: schema.TypeString,
158+
Optional: true,
159+
Computed: true,
160+
ForceNew: true,
161+
},
156162
"freeform_tags": {
157163
Type: schema.TypeMap,
158164
Optional: true,
@@ -427,6 +433,11 @@ func (s *InstanceResourceCrud) Create() error {
427433
request.ExtendedMetadata = extendedMetadata
428434
}
429435

436+
if faultDomain, ok := s.D.GetOkExists("fault_domain"); ok {
437+
tmp := faultDomain.(string)
438+
request.FaultDomain = &tmp
439+
}
440+
430441
if freeformTags, ok := s.D.GetOkExists("freeform_tags"); ok {
431442
request.FreeformTags = objectMapToStringMap(freeformTags.(map[string]interface{}))
432443
}
@@ -608,6 +619,10 @@ func (s *InstanceResourceCrud) SetData() error {
608619
// s.D.Set("extended_metadata", []interface{}{objectToMap(s.Res.ExtendedMetadata)})
609620
// }
610621

622+
if s.Res.FaultDomain != nil {
623+
s.D.Set("fault_domain", *s.Res.FaultDomain)
624+
}
625+
611626
s.D.Set("freeform_tags", s.Res.FreeformTags)
612627

613628
if s.Res.ImageId != nil {

provider/core_instance_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ resource "oci_core_instance" "test_instance" {
6565
defined_tags = "${map("${oci_identity_tag_namespace.tag-namespace1.name}.${oci_identity_tag.tag1.name}", "${var.instance_defined_tags_value}")}"
6666
display_name = "${var.instance_display_name}"
6767
extended_metadata = "${var.instance_extended_metadata}"
68+
fault_domain = "${var.instance_fault_domain}"
6869
freeform_tags = "${var.instance_freeform_tags}"
6970
ipxe_script = "${var.instance_ipxe_script}"
7071
metadata = "${var.instance_metadata}"
@@ -104,6 +105,7 @@ variable "instance_extended_metadata" { default = {
104105
some_string = "stringA"
105106
nested_object = "{\"some_string\": \"stringB\", \"object\": {\"some_string\": \"stringC\"}}"
106107
} }
108+
variable "instance_fault_domain" { default = "FAULT-DOMAIN-2" }
107109
variable "instance_freeform_tags" { default = {"Department"= "Finance"} }
108110
variable "instance_hostname_label" { default = "hostnameLabel" }
109111
variable "instance_image" { default = "image" }
@@ -174,6 +176,7 @@ func TestCoreInstanceResource_basic(t *testing.T) {
174176
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
175177
resource.TestCheckResourceAttr(resourceName, "display_name", "displayName"),
176178
resource.TestCheckResourceAttr(resourceName, "extended_metadata.%", "2"),
179+
resource.TestCheckResourceAttr(resourceName, "fault_domain", "FAULT-DOMAIN-2"),
177180
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
178181
resource.TestCheckResourceAttrSet(resourceName, "id"),
179182
resource.TestCheckResourceAttrSet(resourceName, "image"),
@@ -210,6 +213,7 @@ variable "instance_extended_metadata" { default = {
210213
some_string = "stringA"
211214
nested_object = "{\"some_string\": \"stringB\", \"object\": {\"some_string\": \"stringC\"}}"
212215
} }
216+
variable "instance_fault_domain" { default = "FAULT-DOMAIN-2" }
213217
variable "instance_freeform_tags" { default = {"Department"= "Accounting"} }
214218
variable "instance_hostname_label" { default = "hostnameLabel" }
215219
variable "instance_image" { default = "image" }
@@ -234,6 +238,7 @@ variable "instance_state" { default = "RUNNING" }
234238
resource.TestCheckResourceAttr(resourceName, "defined_tags.%", "1"),
235239
resource.TestCheckResourceAttr(resourceName, "display_name", "displayName2"),
236240
resource.TestCheckResourceAttr(resourceName, "extended_metadata.%", "2"),
241+
resource.TestCheckResourceAttr(resourceName, "fault_domain", "FAULT-DOMAIN-2"),
237242
resource.TestCheckResourceAttr(resourceName, "freeform_tags.%", "1"),
238243
resource.TestCheckResourceAttrSet(resourceName, "id"),
239244
resource.TestCheckResourceAttrSet(resourceName, "image"),
@@ -272,6 +277,7 @@ variable "instance_extended_metadata" { default = {
272277
some_string = "stringA"
273278
nested_object = "{\"some_string\": \"stringB\", \"object\": {\"some_string\": \"stringC\"}}"
274279
} }
280+
variable "instance_fault_domain" { default = "FAULT-DOMAIN-2" }
275281
variable "instance_freeform_tags" { default = {"Department"= "Accounting"} }
276282
variable "instance_hostname_label" { default = "hostnameLabel" }
277283
variable "instance_image" { default = "image" }
@@ -308,6 +314,7 @@ data "oci_core_instances" "test_instances" {
308314
resource.TestCheckResourceAttr(datasourceName, "instances.0.defined_tags.%", "1"),
309315
resource.TestCheckResourceAttr(datasourceName, "instances.0.display_name", "displayName2"),
310316
resource.TestCheckResourceAttr(datasourceName, "instances.0.extended_metadata.%", "2"),
317+
resource.TestCheckResourceAttr(datasourceName, "instances.0.fault_domain", "FAULT-DOMAIN-2"),
311318
resource.TestCheckResourceAttr(datasourceName, "instances.0.freeform_tags.%", "1"),
312319
resource.TestCheckResourceAttrSet(datasourceName, "instances.0.id"),
313320
resource.TestCheckResourceAttrSet(datasourceName, "instances.0.image"),

provider/core_instances_data_source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func (s *InstancesDataSourceCrud) SetData() error {
142142
instance["extended_metadata"] = convertNestedMapToFlatMap(r.ExtendedMetadata)
143143
}
144144

145+
if r.FaultDomain != nil {
146+
instance["fault_domain"] = *r.FaultDomain
147+
}
148+
145149
instance["freeform_tags"] = r.FreeformTags
146150

147151
if r.Id != nil {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"regexp"
10+
11+
"github.com/hashicorp/terraform/helper/resource"
12+
"github.com/hashicorp/terraform/terraform"
13+
)
14+
15+
const (
16+
FaultDomainResourceConfig = FaultDomainResourceDependencies + `
17+
18+
`
19+
FaultDomainPropertyVariables = `
20+
21+
`
22+
FaultDomainResourceDependencies = AvailabilityDomainConfig
23+
)
24+
25+
func TestIdentityFaultDomainResource_basic(t *testing.T) {
26+
provider := testAccProvider
27+
config := testProviderConfig()
28+
29+
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
30+
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
31+
32+
datasourceName := "data.oci_identity_fault_domains.test_fault_domains"
33+
34+
resource.Test(t, resource.TestCase{
35+
PreCheck: func() { testAccPreCheck(t) },
36+
Providers: map[string]terraform.ResourceProvider{
37+
"oci": provider,
38+
},
39+
Steps: []resource.TestStep{
40+
// verify datasource
41+
{
42+
Config: config + `
43+
44+
data "oci_identity_fault_domains" "test_fault_domains" {
45+
#Required
46+
availability_domain = "${lookup(data.oci_identity_availability_domains.test_availability_domains.availability_domains[0],"name")}"
47+
compartment_id = "${var.compartment_id}"
48+
}
49+
` + compartmentIdVariableStr + FaultDomainResourceConfig,
50+
Check: resource.ComposeAggregateTestCheckFunc(
51+
resource.TestMatchResourceAttr(datasourceName, "availability_domain", regexp.MustCompile(`\w+-AD-\d+`)),
52+
resource.TestMatchResourceAttr(datasourceName, "compartment_id", regexp.MustCompile(`.*?(tenancy|compartment).*?`)),
53+
resource.TestCheckResourceAttr(datasourceName, "fault_domains.#", "3"), // more could be added in the future
54+
resource.TestMatchResourceAttr(datasourceName, "fault_domains.0.availability_domain", regexp.MustCompile(`\w+-AD-\d+`)),
55+
resource.TestMatchResourceAttr(datasourceName, "fault_domains.0.compartment_id", regexp.MustCompile(`.*?(tenancy|compartment).*?`)),
56+
resource.TestMatchResourceAttr(datasourceName, "fault_domains.0.id", regexp.MustCompile(`.*?faultdomain.*?`)),
57+
resource.TestCheckResourceAttr(datasourceName, "fault_domains.0.name", "FAULT-DOMAIN-1"),
58+
resource.TestCheckResourceAttr(datasourceName, "fault_domains.1.name", "FAULT-DOMAIN-2"),
59+
resource.TestCheckResourceAttr(datasourceName, "fault_domains.2.name", "FAULT-DOMAIN-3"),
60+
),
61+
},
62+
},
63+
})
64+
}

0 commit comments

Comments
 (0)