Skip to content

Commit 2ac63f8

Browse files
author
ccushing
committed
Add Region and Tenancy data sources with example updates
* New concept example shows how to route identity calls to the home tenancy home region
1 parent 0876619 commit 2ac63f8

File tree

11 files changed

+429
-0
lines changed

11 files changed

+429
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This example demonstrates how to use tenancy and region data sources to route identity calls to the correct
3+
* home region.
4+
*/
5+
6+
variable "tenancy_ocid" {}
7+
variable "user_ocid" {}
8+
variable "fingerprint" {}
9+
variable "private_key_path" {}
10+
variable "compartment_ocid" {}
11+
variable "region" { default = "us-ashburn-1" }
12+
13+
14+
provider "oci" {
15+
region = "${var.region}"
16+
tenancy_ocid = "${var.tenancy_ocid}"
17+
user_ocid = "${var.user_ocid}"
18+
fingerprint = "${var.fingerprint}"
19+
private_key_path = "${var.private_key_path}"
20+
}
21+
22+
data "oci_identity_tenancy" "tenancy" {
23+
tenancy_id = "${var.tenancy_ocid}"
24+
}
25+
26+
data "oci_identity_regions" "home-region" {
27+
filter {
28+
name = "key"
29+
values = ["${data.oci_identity_tenancy.tenancy.home_region_key}"]
30+
}
31+
}
32+
33+
provider "oci" {
34+
alias = "home"
35+
region = "${lookup(data.oci_identity_regions.home-region.regions[0], "name")}"
36+
tenancy_ocid = "${var.tenancy_ocid}"
37+
user_ocid = "${var.user_ocid}"
38+
fingerprint = "${var.fingerprint}"
39+
private_key_path = "${var.private_key_path}"
40+
}
41+
42+
resource "oci_identity_user" "user1" {
43+
provider = "oci.home"
44+
name = "tf-example-user"
45+
description = "user created by terraform"
46+
}

docs/examples/identity/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
## Identity Resource and Datasource Examples
99

1010
This example demonstrates the following Identity concepts:
11+
* Show tenancy details and list OCI regions
1112
* List and filter Availability Domains
1213
* Create and list compartments
1314
* Create users, add an api key and access their password

docs/examples/identity/region.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* This example file shows how to list available OCI regions.
3+
*/
4+
5+
data "oci_identity_regions" "regions" {
6+
}
7+
8+
output "regions" {
9+
value = "${data.oci_identity_regions.regions.regions}"
10+
}

docs/examples/identity/tenancy.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This example file shows how to access information on the current tenancy.
3+
*/
4+
5+
data "oci_identity_tenancy" "tenancy" {
6+
tenancy_id = "${var.tenancy_ocid}"
7+
}
8+
9+
output "tenancy" {
10+
value = <<EOF
11+
12+
name = ${data.oci_identity_tenancy.tenancy.name}
13+
description = ${data.oci_identity_tenancy.tenancy.description}
14+
tenancy_id = ${data.oci_identity_tenancy.tenancy.tenancy_id}
15+
home_region_key = ${data.oci_identity_tenancy.tenancy.home_region_key}
16+
EOF
17+
}

docs/identity/regions.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# oci_identity_regions
3+
4+
## Region DataSource
5+
6+
Gets a list of regions.
7+
8+
### List Operation
9+
Lists all the regions offered by Oracle Cloud Infrastructure.
10+
11+
The following attributes are exported:
12+
13+
* `regions` - The list of regions.
14+
15+
### Example Usage
16+
17+
```
18+
data "oci_identity_regions" "test_regions" {
19+
}
20+
```
21+
### Region Reference
22+
23+
The following attributes are exported:
24+
25+
* `key` - The key of the region. Allowed values are: - `PHX` - `IAD` - `FRA` - `LHR`
26+
* `name` - The name of the region. Allowed values are: - `us-phoenix-1` - `us-ashburn-1` - `eu-frankfurt-1` - `uk-london-1`

docs/identity/tenancies.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# oci_identity_tenancies
3+
4+
## Tenancy DataSource
5+
6+
Gets a single tenancy
7+
8+
### Get Operation
9+
Get the specified tenancy's information.
10+
The following arguments are supported:
11+
12+
* `tenancy_id` - (Required) The OCID of the tenancy.
13+
14+
15+
The following attributes are exported:
16+
17+
* `description` - The description of the tenancy.
18+
* `home_region_key` - The region key for the tenancy's home region. For more information about regions, see [Regions and Availability Domains](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/regions.htm). Allowed values are: - `IAD` - `PHX` - `FRA` - `LHR`
19+
* `tenancy_id` - The OCID of the tenancy.
20+
* `name` - The name of the tenancy.
21+
22+
23+
### Example Usage
24+
25+
```
26+
data "oci_identity_tenancies" "test_tenancies" {
27+
#Required
28+
tenancy_id = "${var.tenancy_tenancy_id}"
29+
}
30+
```
31+
### Tenancy Reference
32+
33+
The following attributes are exported:
34+
35+
* `description` - The description of the tenancy.
36+
* `home_region_key` - The region key for the tenancy's home region. For more information about regions, see [Regions and Availability Domains](https://docs.us-phoenix-1.oraclecloud.com/Content/General/Concepts/regions.htm). Allowed values are: - `IAD` - `PHX` - `FRA` - `LHR`
37+
* `tenancy_id` - The OCID of the tenancy.
38+
* `name` - The name of the tenancy.

provider/identity_region_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
)
11+
12+
func TestIdentityRegionResource_basic(t *testing.T) {
13+
provider := testAccProvider
14+
config := testProviderConfig()
15+
16+
datasourceName := "data.oci_identity_regions.test_regions"
17+
18+
resource.Test(t, resource.TestCase{
19+
Providers: map[string]terraform.ResourceProvider{
20+
"oci": provider,
21+
},
22+
Steps: []resource.TestStep{
23+
// verify datasource
24+
{
25+
Config: config + `
26+
data "oci_identity_regions" "test_regions" {
27+
filter {
28+
name = "name"
29+
values = ["${var.region}"]
30+
}
31+
}
32+
`,
33+
Check: resource.ComposeTestCheckFunc(
34+
resource.TestCheckResourceAttr(datasourceName, "regions.#", "1"),
35+
),
36+
},
37+
},
38+
})
39+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package provider
4+
5+
import (
6+
"context"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
oci_identity "github.com/oracle/oci-go-sdk/identity"
10+
11+
"github.com/oracle/terraform-provider-oci/crud"
12+
)
13+
14+
func RegionsDataSource() *schema.Resource {
15+
return &schema.Resource{
16+
Read: readRegions,
17+
Schema: map[string]*schema.Schema{
18+
"filter": dataSourceFiltersSchema(),
19+
"regions": {
20+
Type: schema.TypeList,
21+
Computed: true,
22+
Elem: &schema.Resource{
23+
Schema: map[string]*schema.Schema{
24+
// Required
25+
26+
// Optional
27+
28+
// Computed
29+
"key": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"name": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
},
38+
},
39+
},
40+
},
41+
}
42+
}
43+
44+
func readRegions(d *schema.ResourceData, m interface{}) error {
45+
sync := &RegionsDataSourceCrud{}
46+
sync.D = d
47+
sync.Client = m.(*OracleClients).identityClient
48+
49+
return crud.ReadResource(sync)
50+
}
51+
52+
type RegionsDataSourceCrud struct {
53+
D *schema.ResourceData
54+
Client *oci_identity.IdentityClient
55+
Res *oci_identity.ListRegionsResponse
56+
}
57+
58+
func (s *RegionsDataSourceCrud) VoidState() {
59+
s.D.SetId("")
60+
}
61+
62+
func (s *RegionsDataSourceCrud) Get() error {
63+
request := oci_identity.ListRegionsRequest{}
64+
65+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity")
66+
67+
response, err := s.Client.ListRegions(context.Background())
68+
if err != nil {
69+
return err
70+
}
71+
72+
s.Res = &response
73+
return nil
74+
}
75+
76+
func (s *RegionsDataSourceCrud) SetData() {
77+
if s.Res == nil {
78+
return
79+
}
80+
81+
s.D.SetId(crud.GenerateDataSourceID())
82+
resources := []map[string]interface{}{}
83+
84+
for _, r := range s.Res.Items {
85+
region := map[string]interface{}{}
86+
87+
if r.Key != nil {
88+
region["key"] = *r.Key
89+
}
90+
91+
if r.Name != nil {
92+
region["name"] = *r.Name
93+
}
94+
95+
resources = append(resources, region)
96+
}
97+
98+
if f, fOk := s.D.GetOkExists("filter"); fOk {
99+
resources = ApplyFilters(f.(*schema.Set), resources)
100+
}
101+
102+
if err := s.D.Set("regions", resources); err != nil {
103+
panic(err)
104+
}
105+
106+
return
107+
}

0 commit comments

Comments
 (0)