Skip to content

Commit e1ad874

Browse files
author
Esteban G
committed
adding region subscription
1 parent 5cd4291 commit e1ad874

File tree

6 files changed

+236
-3
lines changed

6 files changed

+236
-3
lines changed

docs/examples/identity/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
## Identity Resource and Datasource Examples
99

1010
This example demonstrates the following Identity concepts:
11-
* Show tenancy details and list OCI regions
11+
* Show tenancy details and list OCI regions and region subscriptions
1212
* List and filter Availability Domains
1313
* Create and list compartments
1414
* Create users, add an api key and access their password
1515
* Create groups and add users
1616
* Create policies affecting groups and compartments
17-
* Create dynamic groups and policies that govern it
17+
* Create dynamic groups and policies that govern it

docs/examples/identity/region.tf

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
/*
2-
* This example file shows how to list available OCI regions.
2+
* This example file shows how to list available OCI regions, as well as region subscriptions of your tenancy
33
*/
44

55
data "oci_identity_regions" "regions" {
66
}
77

88
output "regions" {
99
value = "${data.oci_identity_regions.regions.regions}"
10+
}
11+
12+
data "oci_identity_region_subscriptions" "test_region_subscriptions" {
13+
#Required
14+
tenancy_id = "${var.tenancy_ocid}"
15+
filter {
16+
name = "is_home_region"
17+
values = [true]
18+
}
19+
}
20+
21+
output "region_subscriptions" {
22+
value = "${data.oci_identity_region_subscriptions.test_region_subscriptions.region_subscriptions}"
1023
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
# oci_identity_region_subscriptions
3+
4+
## RegionSubscription DataSource
5+
6+
Gets a list of region_subscriptions.
7+
8+
### List Operation
9+
Lists the region subscriptions for the specified tenancy.
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+
* `region_subscriptions` - The list of region_subscriptions.
18+
19+
### Example Usage
20+
21+
```
22+
data "oci_identity_region_subscriptions" "test_region_subscriptions" {
23+
#Required
24+
tenancy_id = "${oci_identity_tenancy.test_tenancy.id}"
25+
}
26+
```
27+
### RegionSubscription Reference
28+
29+
The following attributes are exported:
30+
31+
* `is_home_region` - Indicates if the region is the home region or not.
32+
* `region_key` - The region's key. Allowed values are: - `PHX` - `IAD` - `FRA` - `LHR`
33+
* `region_name` - The region's name. Allowed values are: - `us-phoenix-1` - `us-ashburn-1` - `eu-frankurt-1` - `uk-london-1`
34+
* `state` - The region subscription state.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 TestIdentityRegionSubscriptionResource_basic(t *testing.T) {
13+
provider := testAccProvider
14+
config := testProviderConfig()
15+
16+
datasourceName := "data.oci_identity_region_subscriptions.test_region_subscriptions"
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+
variable "region_subscription_region_key" { default = "regionKey2" }
27+
28+
data "oci_identity_region_subscriptions" "test_region_subscriptions" {
29+
#Required
30+
tenancy_id = "${var.tenancy_ocid}"
31+
filter {
32+
name = "is_home_region"
33+
values = [true]
34+
}
35+
}
36+
`,
37+
Check: resource.ComposeTestCheckFunc(
38+
resource.TestCheckResourceAttrSet(datasourceName, "tenancy_id"),
39+
resource.TestCheckResourceAttr(datasourceName, "region_subscriptions.#", "1"),
40+
resource.TestCheckResourceAttr(datasourceName, "region_subscriptions.0.is_home_region", "true"),
41+
resource.TestCheckResourceAttrSet(datasourceName, "region_subscriptions.0.region_key"),
42+
resource.TestCheckResourceAttr(datasourceName, "region_subscriptions.0.region_name", getRequiredEnvSetting("region")),
43+
resource.TestCheckResourceAttrSet(datasourceName, "region_subscriptions.0.state"),
44+
),
45+
},
46+
},
47+
})
48+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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 RegionSubscriptionsDataSource() *schema.Resource {
15+
return &schema.Resource{
16+
Read: readRegionSubscriptions,
17+
Schema: map[string]*schema.Schema{
18+
"filter": dataSourceFiltersSchema(),
19+
"tenancy_id": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"region_subscriptions": {
24+
Type: schema.TypeList,
25+
Computed: true,
26+
Elem: &schema.Resource{
27+
Schema: map[string]*schema.Schema{
28+
// Required
29+
"region_key": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
ForceNew: true,
33+
},
34+
"tenancy_id": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
},
39+
40+
// Optional
41+
42+
// Computed
43+
"is_home_region": {
44+
Type: schema.TypeBool,
45+
Computed: true,
46+
},
47+
"region_name": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"state": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
},
56+
},
57+
},
58+
},
59+
}
60+
}
61+
62+
func readRegionSubscriptions(d *schema.ResourceData, m interface{}) error {
63+
sync := &RegionSubscriptionsDataSourceCrud{}
64+
sync.D = d
65+
sync.Client = m.(*OracleClients).identityClient
66+
67+
return crud.ReadResource(sync)
68+
}
69+
70+
type RegionSubscriptionsDataSourceCrud struct {
71+
D *schema.ResourceData
72+
Client *oci_identity.IdentityClient
73+
Res *oci_identity.ListRegionSubscriptionsResponse
74+
}
75+
76+
func (s *RegionSubscriptionsDataSourceCrud) VoidState() {
77+
s.D.SetId("")
78+
}
79+
80+
func (s *RegionSubscriptionsDataSourceCrud) Get() error {
81+
request := oci_identity.ListRegionSubscriptionsRequest{}
82+
83+
if tenancyId, ok := s.D.GetOkExists("tenancy_id"); ok {
84+
tmp := tenancyId.(string)
85+
request.TenancyId = &tmp
86+
}
87+
88+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity")
89+
90+
response, err := s.Client.ListRegionSubscriptions(context.Background(), request)
91+
if err != nil {
92+
return err
93+
}
94+
95+
s.Res = &response
96+
return nil
97+
}
98+
99+
func (s *RegionSubscriptionsDataSourceCrud) SetData() {
100+
if s.Res == nil {
101+
return
102+
}
103+
104+
s.D.SetId(crud.GenerateDataSourceID())
105+
resources := []map[string]interface{}{}
106+
107+
for _, r := range s.Res.Items {
108+
regionSubscription := map[string]interface{}{}
109+
110+
if r.IsHomeRegion != nil {
111+
regionSubscription["is_home_region"] = *r.IsHomeRegion
112+
}
113+
114+
if r.RegionKey != nil {
115+
regionSubscription["region_key"] = *r.RegionKey
116+
}
117+
118+
if r.RegionName != nil {
119+
regionSubscription["region_name"] = *r.RegionName
120+
}
121+
122+
regionSubscription["state"] = r.Status
123+
124+
resources = append(resources, regionSubscription)
125+
}
126+
127+
if f, fOk := s.D.GetOkExists("filter"); fOk {
128+
resources = ApplyFilters(f.(*schema.Set), resources)
129+
}
130+
131+
if err := s.D.Set("region_subscriptions", resources); err != nil {
132+
panic(err)
133+
}
134+
135+
136+
return
137+
}

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func dataSourcesMap() map[string]*schema.Resource {
204204
"oci_identity_tenancy": TenancyDataSource(),
205205
"oci_identity_user_group_memberships": UserGroupMembershipsDataSource(),
206206
"oci_identity_users": UsersDataSource(),
207+
"oci_identity_region_subscriptions": RegionSubscriptionsDataSource(),
207208
"oci_load_balancer_backends": BackendsDataSource(),
208209
"oci_load_balancer_backend_sets": BackendSetsDataSource(),
209210
"oci_load_balancer_backendsets": BackendSetsDataSource(),

0 commit comments

Comments
 (0)