Skip to content

Commit 7598f30

Browse files
josephholstencodycushing
authored andcommitted
subnet: fix default values for dhcp_options_id, route_table_id, security_list_ids (#151)
Default values in schema differ from the defaults provided by API, so any configuration not specifying them is broken. Any configuration which does specify them should be unchanged. - resource_obmcs_core_subnet.go: make dhcp_options_id, route_table_id, security_list_ids required - resource_obmcs_core_subnet_test.go: rewrite - provider_test.go, data_source_obmcs_core_subnet_test.go: add required attributes
1 parent 628b1ae commit 7598f30

File tree

5 files changed

+134
-94
lines changed

5 files changed

+134
-94
lines changed

data_source_obmcs_core_subnet_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ resource "baremetal_core_subnet" "WebSubnetAD1" {
8787
vcn_id = "${baremetal_core_virtual_network.t.id}"
8888
route_table_id = "${baremetal_core_route_table.RouteForComplete.id}"
8989
security_list_ids = ["${baremetal_core_security_list.WebSubnet.id}"]
90+
dhcp_options_id = ["${baremetal_core_virtual_network.t.default_dhcp_options_id}"]
9091
}
9192
`
9293
s.Config += testProviderConfig()

docs/resources/core/subnet.md

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@ Gets a list of subnets.
55
## Example Usage
66

77
```
8+
data "baremetal_identity_availability_domains" "ADs" {
9+
compartment_id = "${var.compartment_id}"
10+
}
11+
12+
resource "baremetal_core_virtual_network" "t" {
13+
cidr_block = "10.0.0.0/16"
14+
compartment_id = "${var.compartment_id}"
15+
display_name = "network_name"
16+
}
17+
818
resource "baremetal_core_subnet" "t" {
9-
availability_domain = "availabilitydomainid"
10-
compartment_id = "compartmentid"
11-
display_name = "display_name"
12-
cidr_block = "10.10.10.0/24"
13-
route_table_id = "routetableid"
14-
vcn_id = "vcnid"
15-
security_list_ids = ["slid1", "slid2"]
16-
prohibit_public_ip_on_vnic = true
19+
compartment_id = "${var.compartment_id}"
20+
21+
availability_domain = "${data.baremetal_identity_availability_domains.ADs.availability_domains.0.name}"
22+
route_table_id = "${baremetal_core_virtual_network.t.default_route_table_id}"
23+
vcn_id = "${baremetal_core_virtual_network.t.id}"
24+
security_list_ids = ["${baremetal_core_virtual_network.t.default_security_list_id}"]
25+
dhcp_options_id = "${baremetal_core_virtual_network.t.default_dhcp_options_id}"
26+
27+
display_name = "display_name"
28+
cidr_block = "10.10.10.0/24"
29+
prohibit_public_ip_on_vnic = true
1730
}
1831
```
1932

@@ -22,30 +35,21 @@ resource "baremetal_core_subnet" "t" {
2235
The following arguments are supported:
2336

2437
* `availability_domain` - (Required) The Availability Domain to contain the subnet.
25-
* `compartment_id` - (Required) The OCID of the compartment to contain the subnet.
2638
* `cidr_block` - (Required) The CIDR IP address range of the subnet.
39+
* `compartment_id` - (Required) The OCID of the compartment to contain the subnet.
40+
* `dhcp_options_id` - (Required) The OCID of the set of DHCP options the subnet will use.
41+
* `route_table_id` - (Required) The OCID of the route table the subnet will use.
42+
* `security_list_ids` - (Required) OCIDs for the security lists to associate with the subnet. Remember that security lists are associated at the subnet level, but the rules are applied to the individual VNICs in the subnet.
2743
* `vcn_id` - (Required) The OCID of the VCN to contain the subnet.
28-
* `dhcp_options_id` - (Optional) The OCID of the set of DHCP options the subnet will use. If you don't provide a value, the subnet will use the VCN's default set of DHCP options.
29-
* `display_name` - (Optional) The maximum number of items to return in a paginated "List" call.
30-
* `prohibit_public_ip_on_vnic` - (Optional) Whether VNICs within this subnet can have public IP. If it is allowed, VNICs created in the subnet will automatically be assigned public IP unless otherwise specified in the VNIC. If it is prohibited, VNICs in the subnet cannot have public IP address assigned. The default value is false if unspecified.
31-
* `route_table_id` - (Optional) The OCID of the route table the subnet will use. If you don't provide a value, the subnet will use the VCN's default route table.
32-
* `security_list_ids` - (Optional) OCIDs for the security lists to associate with the subnet. If you don't provide a value, the VCN's default security list will be associated with the subnet. Remember that security lists are associated at the subnet level, but the rules are applied to the individual VNICs in the subnet.
33-
3444

45+
* `dns_label` - (Optional) DNS label for the subnet, used in conjunction with the VNIC's hostname and VCN's DNS label to form a fully qualified domain name (FQDN) for each VNIC within this subnet (e.g., bminstance-1.subnet123.vcn1.oraclevcn.com). Must be an alphanumeric string that begins with a letter and is unique within the VCN. The value cannot be changed. The absence of this parameter means the Internet and VCN Resolver will not resolve hostnames of instances in this subnet.
46+
* `display_name` - (Optional) User-friendly name. Does not have to be unique, and it's changeable.
47+
* `prohibit_public_ip_on_vnic` - (Optional) Whether VNICs within this subnet can have public IP. If it is allowed, VNICs created in the subnet will automatically be assigned public IP unless otherwise specified in the VNIC. If it is prohibited, VNICs in the subnet cannot have public IP address assigned. The default value is false if unspecified.
3548

3649
## Attributes Reference
3750

38-
* `availability_domain` - The subnet's Availability Domain.
39-
* `cidr_block` - The CIDR IP address block of the VCN.
40-
* `compartment_id` - The OCID of the compartment containing the VCN.
41-
* `dhcp_options_id` - The OCID for the VCN's default set of DHCP options.
42-
* `route_table_id` - The OCID for the VCN's default route table.
43-
* `security_list_ids` - OCIDs for the security lists to use for VNICs in this subnet.
44-
* `display_name` - A user-friendly name. Does not have to be unique, and it's changeable.
4551
* `id` - The subnet's Oracle ID (OCID).
46-
* `prohibit_public_ip_on_vnic` - Whether VNICs within this subnet can have public IPs. If it is allowed, VNICs created in the subnet will automatically be assigned public IP unless otherwise specified in the VNIC. If it is prohibited, VNICs in the subnet cannot have public IP address assigned. The default value is false if unspecified.
47-
* `vcn_id` - The OCID of the VCN the subnet is in.
4852
* `state` - The VCN's current state. [PROVISIONING, AVAILABLE, TERMINATING, TERMINATED]
4953
* `time_created` - The date and time the VCN was created.
5054
* `virtual_router_ip` - The IP address of the virtual router.
51-
* `virtual_router_mac` - The MAC address of the virtual router.
55+
* `virtual_router_mac` - The MAC address of the virtual router.

provider_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ resource "baremetal_core_security_list" "WebSubnet" {
9999
100100
resource "baremetal_core_subnet" "WebSubnetAD1" {
101101
availability_domain = "${lookup(data.baremetal_identity_availability_domains.ADs.availability_domains[0],"name")}"
102-
cidr_block = "10.0.1.0/24"
103-
display_name = "WebSubnetAD1"
104-
compartment_id = "${var.compartment_id}"
105-
vcn_id = "${baremetal_core_virtual_network.t.id}"
106-
route_table_id = "${baremetal_core_route_table.RouteForComplete.id}"
107-
security_list_ids = ["${baremetal_core_security_list.WebSubnet.id}"]
102+
cidr_block = "10.0.1.0/24"
103+
display_name = "WebSubnetAD1"
104+
compartment_id = "${var.compartment_id}"
105+
vcn_id = "${baremetal_core_virtual_network.t.id}"
106+
route_table_id = "${baremetal_core_route_table.RouteForComplete.id}"
107+
security_list_ids = ["${baremetal_core_security_list.WebSubnet.id}"]
108+
dhcp_options_id = "${baremetal_core_virtual_network.t.default_dhcp_options_id}"
108109
}
109110
110111
`

resource_obmcs_core_subnet.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,50 @@ func SubnetResource() *schema.Resource {
3838
Required: true,
3939
ForceNew: true,
4040
},
41-
"route_table_id": {
41+
"dhcp_options_id": {
4242
Type: schema.TypeString,
43-
Optional: true,
43+
Required: true,
4444
ForceNew: true,
4545
},
46-
"vcn_id": {
46+
"route_table_id": {
4747
Type: schema.TypeString,
4848
Required: true,
4949
ForceNew: true,
5050
},
5151
"security_list_ids": {
5252
Type: schema.TypeSet,
53-
Optional: true,
53+
Required: true,
5454
ForceNew: true,
5555
Set: schema.HashString,
5656
Elem: &schema.Schema{
5757
Type: schema.TypeString,
5858
},
5959
},
60-
"display_name": {
61-
Type: schema.TypeString,
62-
Optional: true,
63-
Computed: true,
64-
},
65-
66-
"dhcp_options_id": {
60+
"vcn_id": {
6761
Type: schema.TypeString,
68-
Optional: true,
69-
Computed: true,
62+
Required: true,
63+
ForceNew: true,
7064
},
65+
// Optional
7166
"dns_label": {
7267
Type: schema.TypeString,
7368
Optional: true,
69+
ForceNew: true,
7470
},
75-
"id": {
71+
"display_name": {
7672
Type: schema.TypeString,
73+
Optional: true,
7774
Computed: true,
7875
},
7976
"prohibit_public_ip_on_vnic": {
8077
Type: schema.TypeBool,
8178
Optional: true,
79+
Default: false,
80+
},
81+
// Computed
82+
"id": {
83+
Type: schema.TypeString,
84+
Computed: true,
8285
},
8386
"state": {
8487
Type: schema.TypeString,

resource_obmcs_core_subnet_test.go

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,92 +3,123 @@
33
package main
44

55
import (
6+
"regexp"
67
"testing"
7-
"time"
88

99
"github.com/MustWin/baremetal-sdk-go"
1010
"github.com/hashicorp/terraform/helper/resource"
1111
"github.com/hashicorp/terraform/helper/schema"
1212
"github.com/hashicorp/terraform/terraform"
13-
14-
"github.com/stretchr/testify/suite"
1513
)
1614

17-
type ResourceCoreSubnetTestSuite struct {
18-
suite.Suite
19-
Client mockableClient
20-
Provider terraform.ResourceProvider
21-
Providers map[string]terraform.ResourceProvider
22-
TimeCreated baremetal.Time
23-
Config string
24-
ResourceName string
25-
}
26-
27-
func (s *ResourceCoreSubnetTestSuite) SetupTest() {
28-
s.Client = GetTestProvider()
29-
30-
s.Provider = Provider(
15+
func TestResourceCoreSubnetCreate(t *testing.T) {
16+
client := GetTestProvider()
17+
provider := Provider(
3118
func(d *schema.ResourceData) (interface{}, error) {
32-
return s.Client, nil
19+
return client, nil
3320
},
3421
)
3522

36-
s.Providers = map[string]terraform.ResourceProvider{
37-
"baremetal": s.Provider,
38-
}
39-
40-
s.TimeCreated = baremetal.Time{Time: time.Now()}
41-
42-
s.Config = subnetConfig
43-
s.Config += testProviderConfig()
23+
config := `
24+
data "baremetal_identity_availability_domains" "ADs" {
25+
compartment_id = "${var.compartment_id}"
26+
}
4427
45-
s.ResourceName = "baremetal_core_subnet.WebSubnetAD1"
28+
resource "baremetal_core_virtual_network" "t" {
29+
cidr_block = "10.0.0.0/16"
30+
compartment_id = "${var.compartment_id}"
31+
display_name = "network_name"
32+
}
4633
34+
resource "baremetal_core_subnet" "s" {
35+
availability_domain = "${data.baremetal_identity_availability_domains.ADs.availability_domains.0.name}"
36+
compartment_id = "${var.compartment_id}"
37+
vcn_id = "${baremetal_core_virtual_network.t.id}"
38+
security_list_ids = ["${baremetal_core_virtual_network.t.default_security_list_id}"]
39+
route_table_id = "${baremetal_core_virtual_network.t.default_route_table_id}"
40+
dhcp_options_id = "${baremetal_core_virtual_network.t.default_dhcp_options_id}"
41+
cidr_block = "10.0.2.0/24"
4742
}
43+
`
44+
config += testProviderConfig()
4845

49-
func (s *ResourceCoreSubnetTestSuite) TestCreateResourceCoreSubnet() {
46+
resourceName := "baremetal_core_subnet.s"
5047

51-
resource.UnitTest(s.T(), resource.TestCase{
52-
Providers: s.Providers,
48+
resource.UnitTest(t, resource.TestCase{
49+
Providers: map[string]terraform.ResourceProvider{
50+
"baremetal": provider,
51+
},
5352
Steps: []resource.TestStep{
5453
{
5554
ImportState: true,
5655
ImportStateVerify: true,
57-
Config: s.Config,
58-
Check: resource.ComposeTestCheckFunc(
59-
resource.TestCheckResourceAttrSet(s.ResourceName, "availability_domain"),
60-
resource.TestCheckResourceAttr(s.ResourceName, "display_name", "WebSubnetAD1"),
61-
resource.TestCheckResourceAttrSet(s.ResourceName, "id"),
62-
resource.TestCheckResourceAttr(s.ResourceName, "state", baremetal.ResourceAvailable),
63-
resource.TestCheckResourceAttrSet(s.ResourceName, "time_created"),
56+
Config: config,
57+
Check: resource.ComposeAggregateTestCheckFunc(
58+
resource.TestMatchResourceAttr(resourceName, "compartment_id", regexp.MustCompile("ocid1\\.compartment\\.oc1\\..*")),
59+
resource.TestCheckResourceAttrSet(resourceName, "availability_domain"),
60+
resource.TestMatchResourceAttr(resourceName, "vcn_id", regexp.MustCompile("ocid1\\.vcn\\.oc1\\..*")),
61+
resource.TestMatchResourceAttr(resourceName, "dhcp_options_id", regexp.MustCompile("ocid1\\.dhcpoptions\\.oc1\\..*")),
62+
resource.TestMatchResourceAttr(resourceName, "route_table_id", regexp.MustCompile("ocid1\\.routetable\\.oc1\\..*")),
63+
resource.TestCheckResourceAttr(resourceName, "security_list_ids.#", "1"),
64+
resource.TestCheckResourceAttr(resourceName, "cidr_block", "10.0.2.0/24"),
65+
resource.TestCheckResourceAttrSet(resourceName, "display_name"),
66+
resource.TestCheckResourceAttr(resourceName, "dns_label", ""),
67+
resource.TestCheckResourceAttr(resourceName, "prohibit_public_ip_on_vnic", "false"),
68+
resource.TestMatchResourceAttr(resourceName, "id", regexp.MustCompile("ocid1\\.subnet\\.oc1\\..*")),
69+
resource.TestCheckResourceAttr(resourceName, "state", baremetal.ResourceAvailable),
70+
resource.TestCheckResourceAttrSet(resourceName, "time_created"),
71+
resource.TestCheckResourceAttrSet(resourceName, "virtual_router_ip"),
72+
resource.TestCheckResourceAttrSet(resourceName, "virtual_router_mac"),
6473
),
6574
},
6675
},
6776
})
6877
}
6978

70-
func (s *ResourceCoreSubnetTestSuite) TestTerminateSubnet() {
71-
if IsAccTest() {
72-
s.T().Skip()
73-
}
79+
func TestResourceCoreSubnetTerminate(t *testing.T) {
80+
client := GetTestProvider()
81+
provider := Provider(
82+
func(d *schema.ResourceData) (interface{}, error) {
83+
return client, nil
84+
},
85+
)
86+
config := `
87+
data "baremetal_identity_availability_domains" "ADs" {
88+
compartment_id = "${var.compartment_id}"
89+
}
90+
91+
resource "baremetal_core_virtual_network" "t" {
92+
cidr_block = "10.0.0.0/16"
93+
compartment_id = "${var.compartment_id}"
94+
display_name = "network_name"
95+
}
7496
75-
resource.UnitTest(s.T(), resource.TestCase{
76-
Providers: s.Providers,
97+
resource "baremetal_core_subnet" "s" {
98+
availability_domain = "${data.baremetal_identity_availability_domains.ADs.availability_domains.0.name}"
99+
compartment_id = "${var.compartment_id}"
100+
vcn_id = "${baremetal_core_virtual_network.t.id}"
101+
security_list_ids = ["${baremetal_core_virtual_network.t.default_security_list_id}"]
102+
route_table_id = "${baremetal_core_virtual_network.t.default_route_table_id}"
103+
dhcp_options_id = "${baremetal_core_virtual_network.t.default_dhcp_options_id}"
104+
cidr_block = "10.0.2.0/24"
105+
}
106+
`
107+
config += testProviderConfig()
108+
resource.UnitTest(t, resource.TestCase{
109+
Providers: map[string]terraform.ResourceProvider{
110+
"baremetal": provider,
111+
},
77112
Steps: []resource.TestStep{
78113
{
79114
ImportState: true,
80115
ImportStateVerify: true,
81-
Config: s.Config,
116+
Config: config,
82117
},
83118
{
84-
Config: s.Config,
119+
Config: config,
85120
Destroy: true,
86121
},
87122
},
88123
})
89124

90125
}
91-
92-
func TestResourceCoreSubnetTestSuite(t *testing.T) {
93-
suite.Run(t, new(ResourceCoreSubnetTestSuite))
94-
}

0 commit comments

Comments
 (0)