Skip to content

Commit 8f7d6a5

Browse files
snafuzhyder
andauthored
added the possibility to handle an existing DRG (#4)
* added the possibility to handle an existing DRG * Updated description for region Co-authored-by: Ali Mukadam <[email protected]>
1 parent 2c00b1d commit 8f7d6a5

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ It creates the following resources:
3636
* A DRG attached to one or more customizable VCNs
3737
* An optional Remote Peering Connection (RPC)
3838
39+
> This module can either create a new DRG or wrap around an existing one.
3940

4041
This module is primarily meant to be reusable to create more advanced infrastructure on {uri-oci}[OCI] either manually in the OCI Console or by extending the Terraform code.
4142

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ It creates the following resources:
77
* A DRG attached to one or more customizable VCNs
88
* An optional Remote Peering Connection (RPC)
99

10+
> This module can either create a new DRG or wrap around an existing one.
1011
1112
This module is primarily meant to be reusable to create more advanced infrastructure on [OCI][OCI] either manually in the OCI Console or by extending the Terraform code.
1213

drg.tf

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
# Copyright (c) 2022 Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

4+
45
resource "oci_core_drg" "drg" {
56
compartment_id = var.compartment_id
67
display_name = var.label_prefix == "none" ? var.drg_display_name : "${var.label_prefix}-${var.drg_display_name}"
78

89
freeform_tags = var.freeform_tags
910
defined_tags = var.defined_tags
11+
12+
count = var.drg_id == null ? 1 : 0
13+
}
14+
15+
data "oci_core_drgs" "drg_data" {
16+
compartment_id = var.compartment_id
17+
18+
filter {
19+
name = "id"
20+
values = [var.drg_id]
21+
}
22+
1023
}
1124

1225
resource "oci_core_drg_attachment" "vcns" {
@@ -16,7 +29,7 @@ resource "oci_core_drg_attachment" "vcns" {
1629
freeform_tags = var.freeform_tags
1730
defined_tags = var.defined_tags
1831

19-
drg_id = oci_core_drg.drg.id
32+
drg_id = var.drg_id == null ? oci_core_drg.drg[0].id : var.drg_id
2033

2134
network_details {
2235
id = each.value.vcn_id # required
@@ -32,9 +45,9 @@ resource "oci_core_drg_attachment" "vcns" {
3245
}
3346

3447
resource "oci_core_remote_peering_connection" "rpc" {
35-
48+
3649
compartment_id = var.compartment_id
37-
drg_id = oci_core_drg.drg.id
50+
drg_id = var.drg_id == null ? oci_core_drg.drg[0].id : var.drg_id
3851
display_name = var.label_prefix == "none" ? "rpc_created_from_${var.drg_display_name}" : "${var.label_prefix}_rpc"
3952

4053
freeform_tags = var.freeform_tags

outputs.tf

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@
33

44
output "drg_id" {
55
description = "id of drg if it is created"
6-
value = join(",", oci_core_drg.drg[*].id)
6+
value = join(",", length(data.oci_core_drgs.drg_data.drgs) == 0 ? oci_core_drg.drg[*].id : data.oci_core_drgs.drg_data.drgs[*].id)
77
}
88

9-
10-
119
output "drg_display_name" {
1210
description = "display name of drg if it is created"
13-
value = join(",", oci_core_drg.drg[*].display_name)
11+
value = join(",", length(data.oci_core_drgs.drg_data.drgs) == 0 ? oci_core_drg.drg[*].display_name : data.oci_core_drgs.drg_data.drgs[*].display_name)
1412
}
1513

16-
1714
# Complete outputs for each resources with provider parity. Auto-updating.
1815
# Useful for module composition.
19-
2016
output "drg_all_attributes" {
2117
description = "all attributes of created drg"
22-
value = { for k, v in oci_core_drg.drg : k => v }
18+
value = { for k, v in(length(data.oci_core_drgs.drg_data.drgs) == 0 ? oci_core_drg.drg[0] : data.oci_core_drgs.drg_data.drgs[0]) : k => v }
2319
}
2420

2521
output "drg_attachment_all_attributes" {
@@ -30,8 +26,8 @@ output "drg_attachment_all_attributes" {
3026
output "drg_summary" {
3127
description = "drg information summary"
3228
value = {
33-
(oci_core_drg.drg.display_name) = {
34-
drg_id = oci_core_drg.drg.id
29+
(length(data.oci_core_drgs.drg_data.drgs) == 0 ? oci_core_drg.drg[0].display_name : data.oci_core_drgs.drg_data.drgs[0].display_name) = {
30+
drg_id = length(data.oci_core_drgs.drg_data.drgs) == 0 ? oci_core_drg.drg[0].id : data.oci_core_drgs.drg_data.drgs[*].id
3531
vcn_attachments = { for k, v in oci_core_drg_attachment.vcns : k => v.network_details[0].id }
3632

3733
}
@@ -43,7 +39,6 @@ output "drg_summary" {
4339
}
4440

4541

46-
4742
output "rpc_id" {
4843
description = "id of RPC if it is created"
4944
value = join(",", oci_core_remote_peering_connection.rpc[*].id)

terraform.tfvars.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defined_tags = {}
2424

2525
# drg parameters
2626
drg_display_name = "drg"
27+
drg_id = null
2728

2829
# vcns to be attached
2930
drg_vcn_attachments = null

variables.tf

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
# Copyright (c) 2022 Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

4-
# provider identity parameters
4+
# general oci parameters
55

66
variable "region" {
77
# List of regions: https://docs.cloud.oracle.com/iaas/Content/General/Concepts/regions.htm#ServiceAvailabilityAcrossRegions
8-
description = "the OCI region where resources will be created"
8+
description = "The OCI region where the DRG will be created."
99
type = string
10-
default = null
1110
}
12-
13-
# general oci parameters
14-
1511
variable "compartment_id" {
1612
description = "compartment id where to create all resources"
1713
type = string
@@ -58,6 +54,12 @@ variable "drg_vcn_attachments" {
5854
default = null
5955
}
6056

57+
variable "drg_id" {
58+
description = "ID of an external created DRG"
59+
type = string
60+
default = null
61+
}
62+
6163
# rpc parameters
6264
variable "create_rpc" {
6365
description = "Whether to create Remote Peering Connection. If set to true, creates an RPC"
@@ -67,8 +69,8 @@ variable "create_rpc" {
6769

6870
variable "rpc_acceptor_id" {
6971
description = "the ID of the remote RPC"
70-
type = string
71-
default = null
72+
type = string
73+
default = null
7274
}
7375
variable "rpc_acceptor_region" {
7476
# List of regions: https://docs.cloud.oracle.com/iaas/Content/General/Concepts/regions.htm#ServiceAvailabilityAcrossRegions

0 commit comments

Comments
 (0)