Skip to content

Commit 7d24b32

Browse files
committed
Support added for oci_core_public_ip_pool resource and byoip data sources
1 parent bbd9048 commit 7d24b32

File tree

45 files changed

+2562
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2562
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Support for query parameters added to `object_storage` `object` resource
33
- Support for custom certificates added to `apigateway` `certificate` resource
44
- Support added for update Instance Console Connections
5+
- Support added for `oci_core_public_ip_pool` resource and `byoip` data source
56

67
## 3.94.0 (September 23, 2020)
78

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
provider "oci" {
5+
tenancy_ocid = "${var.tenancy_ocid}"
6+
user_ocid = "${var.user_ocid}"
7+
fingerprint = "${var.fingerprint}"
8+
private_key_path = "${var.private_key_path}"
9+
region = "${var.region}"
10+
}
11+
12+
variable "tenancy_ocid" {}
13+
variable "user_ocid" {}
14+
variable "fingerprint" {}
15+
variable "private_key_path" {}
16+
variable "compartment_ocid" {}
17+
variable "region" {}
18+
variable "ssh_public_key" {}
19+
20+
variable "byoip_range_id" {}
21+
22+
variable "public_ip_pool_cidr_block" {}
23+
24+
resource "oci_core_public_ip" "test_public_ip" {
25+
compartment_id = "${var.compartment_ocid}"
26+
lifetime = "RESERVED"
27+
public_ip_pool_id = "${oci_core_public_ip_pool_capacity.test_public_ip_pool_capacity.public_ip_pool_id}"
28+
}
29+
30+
resource "oci_core_public_ip_pool_capacity" "test_public_ip_pool_capacity" {
31+
public_ip_pool_id = "${oci_core_public_ip_pool.test_public_ip_pool.id}"
32+
cidr_block = "${var.public_ip_pool_cidr_block}"
33+
byoip_id = "${var.byoip_range_id}"
34+
}
35+
36+
resource "oci_core_public_ip_pool" "test_public_ip_pool" {
37+
compartment_id = "${var.compartment_ocid}"
38+
}
39+
40+
resource "oci_core_vcn" "test_vcn" {
41+
cidr_block = "10.0.0.0/16"
42+
compartment_id = "${var.compartment_ocid}"
43+
}
44+
45+
resource "oci_core_nat_gateway" "test_nat_gateway" {
46+
block_traffic = "false"
47+
compartment_id = "${var.compartment_ocid}"
48+
display_name = "displayName"
49+
public_ip_id = "${oci_core_public_ip.test_public_ip.id}"
50+
vcn_id = "${oci_core_vcn.test_vcn.id}"
51+
}
52+
53+
data "oci_core_public_ip_pool" "public_ip_pool" {
54+
public_ip_pool_id = "${oci_core_public_ip_pool_capacity.test_public_ip_pool_capacity.public_ip_pool_id}"
55+
}
56+
57+
output "public_ip_pool" {
58+
value = [
59+
"${data.oci_core_public_ip_pool.public_ip_pool}",
60+
]
61+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package oci
5+
6+
import (
7+
"fmt"
8+
"log"
9+
"testing"
10+
"time"
11+
12+
"github.com/hashicorp/terraform/helper/resource"
13+
"github.com/hashicorp/terraform/terraform"
14+
15+
"github.com/terraform-providers/terraform-provider-oci/httpreplay"
16+
)
17+
18+
var (
19+
byoipAllocatedRangeDataSourceRepresentation = map[string]interface{}{
20+
"byoip_range_id": Representation{repType: Required, create: `${var.byoip_range_id}`},
21+
}
22+
23+
ByoipAllocatedRangeResourceConfig = PublicIpPoolAddCapacityResourceDependencies +
24+
generateResourceFromRepresentationMap("oci_core_public_ip_pool_capacity", "test_public_ip_pool_capacity", Required, Create, publicIpPoolCapacityRepresentation)
25+
)
26+
27+
func TestCoreByoipAllocatedRangeResource_basic(t *testing.T) {
28+
httpreplay.SetScenario("TestCoreByoipAllocatedRangeResource_basic")
29+
defer httpreplay.SaveScenario()
30+
31+
provider := testAccProvider
32+
config := testProviderConfig()
33+
34+
compartmentId := getEnvSettingWithBlankDefault("compartment_ocid")
35+
compartmentIdVariableStr := fmt.Sprintf("variable \"compartment_id\" { default = \"%s\" }\n", compartmentId)
36+
37+
datasourceName := "data.oci_core_byoip_allocated_ranges.test_byoip_allocated_ranges"
38+
39+
resource.Test(t, resource.TestCase{
40+
PreCheck: func() { testAccPreCheck(t) },
41+
Providers: map[string]terraform.ResourceProvider{
42+
"oci": provider,
43+
},
44+
Steps: []resource.TestStep{
45+
// create dependencies
46+
{
47+
Config: config + compartmentIdVariableStr + ByoipAllocatedRangeResourceConfig,
48+
Check: func(s *terraform.State) (err error) {
49+
log.Printf("[DEBUG] Wait for oci_core_public_ip_pool and oci_core_public_ip_pool_capacity resource to get created first")
50+
time.Sleep(2 * time.Minute)
51+
return nil
52+
},
53+
},
54+
// verify datasource
55+
{
56+
Config: config +
57+
generateDataSourceFromRepresentationMap("oci_core_byoip_allocated_ranges", "test_byoip_allocated_ranges", Required, Create, byoipAllocatedRangeDataSourceRepresentation) +
58+
compartmentIdVariableStr + ByoipAllocatedRangeResourceConfig,
59+
Check: resource.ComposeAggregateTestCheckFunc(
60+
resource.TestCheckResourceAttrSet(datasourceName, "byoip_range_id"),
61+
62+
resource.TestCheckResourceAttrSet(datasourceName, "byoip_allocated_range_collection.#"),
63+
resource.TestCheckResourceAttr(datasourceName, "byoip_allocated_range_collection.0.items.#", "1"),
64+
),
65+
},
66+
},
67+
})
68+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package oci
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform/helper/schema"
10+
oci_core "github.com/oracle/oci-go-sdk/v25/core"
11+
)
12+
13+
func init() {
14+
RegisterDatasource("oci_core_byoip_allocated_ranges", CoreByoipAllocatedRangesDataSource())
15+
}
16+
17+
func CoreByoipAllocatedRangesDataSource() *schema.Resource {
18+
return &schema.Resource{
19+
Read: readCoreByoipAllocatedRanges,
20+
Schema: map[string]*schema.Schema{
21+
"filter": dataSourceFiltersSchema(),
22+
"byoip_range_id": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"byoip_allocated_range_collection": {
27+
Type: schema.TypeList,
28+
Computed: true,
29+
Elem: &schema.Resource{
30+
Schema: map[string]*schema.Schema{
31+
// Required
32+
33+
// Optional
34+
35+
// Computed
36+
"items": {
37+
Type: schema.TypeList,
38+
Computed: true,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
// Required
42+
43+
// Optional
44+
45+
// Computed
46+
"cidr_block": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"public_ip_pool_id": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
},
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
}
62+
}
63+
64+
func readCoreByoipAllocatedRanges(d *schema.ResourceData, m interface{}) error {
65+
sync := &CoreByoipAllocatedRangesDataSourceCrud{}
66+
sync.D = d
67+
sync.Client = m.(*OracleClients).virtualNetworkClient()
68+
69+
return ReadResource(sync)
70+
}
71+
72+
type CoreByoipAllocatedRangesDataSourceCrud struct {
73+
D *schema.ResourceData
74+
Client *oci_core.VirtualNetworkClient
75+
Res *oci_core.ListByoipAllocatedRangesResponse
76+
}
77+
78+
func (s *CoreByoipAllocatedRangesDataSourceCrud) VoidState() {
79+
s.D.SetId("")
80+
}
81+
82+
func (s *CoreByoipAllocatedRangesDataSourceCrud) Get() error {
83+
request := oci_core.ListByoipAllocatedRangesRequest{}
84+
85+
if byoipRangeId, ok := s.D.GetOkExists("byoip_range_id"); ok {
86+
tmp := byoipRangeId.(string)
87+
request.ByoipRangeId = &tmp
88+
}
89+
90+
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "core")
91+
92+
response, err := s.Client.ListByoipAllocatedRanges(context.Background(), request)
93+
if err != nil {
94+
return err
95+
}
96+
97+
s.Res = &response
98+
request.Page = s.Res.OpcNextPage
99+
100+
for request.Page != nil {
101+
listResponse, err := s.Client.ListByoipAllocatedRanges(context.Background(), request)
102+
if err != nil {
103+
return err
104+
}
105+
106+
s.Res.Items = append(s.Res.Items, listResponse.Items...)
107+
request.Page = listResponse.OpcNextPage
108+
}
109+
110+
return nil
111+
}
112+
113+
func (s *CoreByoipAllocatedRangesDataSourceCrud) SetData() error {
114+
if s.Res == nil {
115+
return nil
116+
}
117+
118+
s.D.SetId(GenerateDataSourceID())
119+
resources := []map[string]interface{}{}
120+
byoipAllocatedRange := map[string]interface{}{}
121+
122+
items := []interface{}{}
123+
for _, item := range s.Res.Items {
124+
items = append(items, ByoipAllocatedRangeSummaryToMap(item))
125+
}
126+
byoipAllocatedRange["items"] = items
127+
128+
if f, fOk := s.D.GetOkExists("filter"); fOk {
129+
items = ApplyFiltersInCollection(f.(*schema.Set), items, CoreByoipAllocatedRangesDataSource().Schema["byoip_allocated_range_collection"].Elem.(*schema.Resource).Schema)
130+
byoipAllocatedRange["items"] = items
131+
}
132+
133+
resources = append(resources, byoipAllocatedRange)
134+
if err := s.D.Set("byoip_allocated_range_collection", resources); err != nil {
135+
return err
136+
}
137+
138+
return nil
139+
}
140+
141+
func ByoipAllocatedRangeSummaryToMap(obj oci_core.ByoipAllocatedRangeSummary) map[string]interface{} {
142+
result := map[string]interface{}{}
143+
144+
if obj.CidrBlock != nil {
145+
result["cidr_block"] = string(*obj.CidrBlock)
146+
}
147+
148+
if obj.PublicIpPoolId != nil {
149+
result["public_ip_pool_id"] = string(*obj.PublicIpPoolId)
150+
}
151+
152+
return result
153+
}

0 commit comments

Comments
 (0)