Skip to content

Commit 577b34b

Browse files
Merge pull request #233 from oracle/brian_multi_vnic
Multi-VNIC support
2 parents 8a55b80 + bd7d7b0 commit 577b34b

18 files changed

+917
-65
lines changed

data_source_obmcs_core_vnic.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ func VnicDatasource() *schema.Resource {
3737
Type: schema.TypeString,
3838
Computed: true,
3939
},
40+
"is_primary": {
41+
Type: schema.TypeBool,
42+
Computed: true,
43+
},
44+
"mac_address": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
4048
"state": {
4149
Type: schema.TypeString,
4250
Computed: true,
@@ -53,6 +61,10 @@ func VnicDatasource() *schema.Resource {
5361
Type: schema.TypeString,
5462
Computed: true,
5563
},
64+
"time_created": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
5668
},
5769
}
5870
}
@@ -85,10 +97,13 @@ func (v *VnicDatasourceCrud) SetData() {
8597
v.D.Set("compartment_id", v.Resource.CompartmentID)
8698
v.D.Set("display_name", v.Resource.DisplayName)
8799
v.D.Set("hostname_label", v.Resource.HostnameLabel)
100+
v.D.Set("is_primary", v.Resource.IsPrimary)
101+
v.D.Set("mac_address", v.Resource.MacAddress)
88102
v.D.Set("state", v.Resource.State)
89103
v.D.Set("private_ip_address", v.Resource.PrivateIPAddress)
90104
v.D.Set("public_ip_address", v.Resource.PublicIPAddress)
91105
v.D.Set("subnet_id", v.Resource.SubnetID)
106+
v.D.Set("time_created", v.Resource.TimeCreated.String())
92107
}
93108
return
94109
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package main
4+
5+
import (
6+
"testing"
7+
8+
baremetal "github.com/MustWin/baremetal-sdk-go"
9+
"github.com/hashicorp/terraform/helper/resource"
10+
"github.com/hashicorp/terraform/helper/schema"
11+
"github.com/hashicorp/terraform/terraform"
12+
"github.com/stretchr/testify/suite"
13+
)
14+
15+
type DataSourceCoreVnicTestSuite struct {
16+
suite.Suite
17+
Client *baremetal.Client
18+
Config string
19+
Provider terraform.ResourceProvider
20+
Providers map[string]terraform.ResourceProvider
21+
ResourceName string
22+
}
23+
24+
func (s *DataSourceCoreVnicTestSuite) SetupTest() {
25+
s.Client = GetTestProvider()
26+
s.Provider = Provider(func(d *schema.ResourceData) (interface{}, error) {
27+
return s.Client, nil
28+
})
29+
30+
s.Providers = map[string]terraform.ResourceProvider{
31+
"baremetal": s.Provider,
32+
}
33+
34+
s.Config = instanceDnsConfig
35+
s.Config += testProviderConfig()
36+
s.ResourceName = "data.baremetal_core_vnic.v"
37+
}
38+
39+
func (s *DataSourceCoreVnicTestSuite) TestAttachVnic() {
40+
41+
resource.UnitTest(s.T(), resource.TestCase{
42+
Providers: s.Providers,
43+
Steps: []resource.TestStep{
44+
{
45+
ImportState: true,
46+
ImportStateVerify: true,
47+
Config: s.Config + `
48+
data "baremetal_core_vnic_attachments" "va" {
49+
compartment_id = "${var.compartment_id}"
50+
instance_id = "${baremetal_core_instance.t.id}"
51+
}
52+
data "baremetal_core_vnic" "v" {
53+
vnic_id = "${lookup(data.baremetal_core_vnic_attachments.va.vnic_attachments[0],"vnic_id")}"
54+
}
55+
`,
56+
Check: resource.ComposeTestCheckFunc(
57+
resource.TestCheckResourceAttrSet(s.ResourceName, "id"),
58+
resource.TestCheckResourceAttrSet(s.ResourceName, "availability_domain"),
59+
resource.TestCheckResourceAttrSet(s.ResourceName, "compartment_id"),
60+
resource.TestCheckResourceAttr(s.ResourceName, "display_name", "-tf-instance-vnic"),
61+
resource.TestCheckResourceAttr(s.ResourceName, "hostname_label", "testinstance"),
62+
resource.TestCheckResourceAttr(s.ResourceName, "is_primary", "true"),
63+
resource.TestCheckResourceAttrSet(s.ResourceName, "mac_address"),
64+
resource.TestCheckResourceAttr(s.ResourceName, "state", baremetal.ResourceAvailable),
65+
resource.TestCheckResourceAttrSet(s.ResourceName, "private_ip_address"),
66+
resource.TestCheckResourceAttrSet(s.ResourceName, "public_ip_address"),
67+
resource.TestCheckResourceAttrSet(s.ResourceName, "subnet_id"),
68+
resource.TestCheckResourceAttrSet(s.ResourceName, "time_created"),
69+
),
70+
},
71+
},
72+
})
73+
}
74+
75+
func TestDataSourceCoreVnicTestSuite(t *testing.T) {
76+
suite.Run(t, new(DataSourceCoreVnicTestSuite))
77+
}

docs/datasources/core/vnic.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ The following attributes are exported:
2424
* `availability_domain` - The VNIC's Availability Domain.
2525
* `display_name` - A user-friendly name. Does not have to be unique.
2626
* `id` - The OCID of the VNIC.
27-
* `hostname_label` - The hostname for the VNIC that is created during instance launch. Used for DNS. .
27+
* `hostname_label` - The hostname for the VNIC that is created during instance launch. Used for DNS.
28+
* `is_primary` - Whether the VNIC is the primary VNIC (the VNIC that is automatically created and attached during instance launch).
2829
* `state` - The current state of the VNIC. [PROVISIONING, AVAILABLE, TERMINATING, TERMINATED]
30+
* `mac_address` - The MAC address of the VNIC.
2931
* `private_ip_address` - The private IP addresses of the VNIC, which is within the VNIC subnet and is accessible within the VCN.
3032
* `public_ip_address` - The public IP address of the VNIC, which Oracle performs NAT for at the gateway.
3133
* `subnet_id` - The OCID of the subnet the VNIC is in.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
variable "tenancy_ocid" {}
2+
variable "user_ocid" {}
3+
variable "fingerprint" {}
4+
variable "private_key_path" {}
5+
variable "compartment_ocid" {}
6+
variable "region" {}
7+
variable "ssh_public_key" {}
8+
9+
variable "SecondaryVnicCount" {
10+
default = 2
11+
}
12+
13+
# Choose an Availability Domain
14+
variable "AD" {
15+
default = "1"
16+
}
17+
18+
variable "InstanceShape" {
19+
default = "VM.Standard1.8"
20+
}
21+
22+
variable "InstanceOS" {
23+
default = "Oracle Linux"
24+
}
25+
26+
variable "InstanceOSVersion" {
27+
default = "7.3"
28+
}
29+
30+
provider "baremetal" {
31+
tenancy_ocid = "${var.tenancy_ocid}"
32+
user_ocid = "${var.user_ocid}"
33+
fingerprint = "${var.fingerprint}"
34+
private_key_path = "${var.private_key_path}"
35+
region = "${var.region}"
36+
}
37+
38+
data "baremetal_identity_availability_domains" "ADs" {
39+
compartment_id = "${var.tenancy_ocid}"
40+
}
41+
42+
resource "baremetal_core_virtual_network" "ExampleVCN" {
43+
cidr_block = "10.0.0.0/16"
44+
compartment_id = "${var.compartment_ocid}"
45+
display_name = "CompleteVCN"
46+
dns_label = "examplevcn"
47+
}
48+
49+
resource "baremetal_core_subnet" "ExampleSubnet" {
50+
availability_domain = "${lookup(data.baremetal_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}"
51+
cidr_block = "10.0.1.0/24"
52+
display_name = "ExampleSubnet"
53+
compartment_id = "${var.compartment_ocid}"
54+
vcn_id = "${baremetal_core_virtual_network.ExampleVCN.id}"
55+
route_table_id = "${baremetal_core_virtual_network.ExampleVCN.default_route_table_id}"
56+
security_list_ids = ["${baremetal_core_virtual_network.ExampleVCN.default_security_list_id}"]
57+
dhcp_options_id = "${baremetal_core_virtual_network.ExampleVCN.default_dhcp_options_id}"
58+
dns_label = "examplesubnet"
59+
}
60+
61+
# Gets the OCID of the OS image to use
62+
data "baremetal_core_images" "OLImageOCID" {
63+
compartment_id = "${var.compartment_ocid}"
64+
operating_system = "${var.InstanceOS}"
65+
operating_system_version = "${var.InstanceOSVersion}"
66+
}
67+
68+
resource "baremetal_core_instance" "ExampleInstance" {
69+
availability_domain = "${lookup(data.baremetal_identity_availability_domains.ADs.availability_domains[var.AD - 1],"name")}"
70+
compartment_id = "${var.compartment_ocid}"
71+
display_name = "ExampleInstance"
72+
image = "${lookup(data.baremetal_core_images.OLImageOCID.images[0], "id")}"
73+
shape = "${var.InstanceShape}"
74+
subnet_id = "${baremetal_core_subnet.ExampleSubnet.id}"
75+
create_vnic_details {
76+
subnet_id = "${baremetal_core_subnet.ExampleSubnet.id}"
77+
hostname_label = "exampleinstance"
78+
}
79+
metadata {
80+
ssh_authorized_keys = "${var.ssh_public_key}"
81+
}
82+
83+
timeouts {
84+
create = "60m"
85+
}
86+
}
87+
88+
resource "baremetal_core_vnic_attachment" "SecondaryVnicAttachment" {
89+
instance_id = "${baremetal_core_instance.ExampleInstance.id}"
90+
display_name = "SecondaryVnicAttachment_${count.index}"
91+
create_vnic_details {
92+
subnet_id = "${baremetal_core_subnet.ExampleSubnet.id}"
93+
display_name = "SecondaryVnic_${count.index}"
94+
assign_public_ip = true
95+
}
96+
count = "${var.SecondaryVnicCount}"
97+
}
98+
99+
data "baremetal_core_vnic" "SecondaryVnic" {
100+
count = "${var.SecondaryVnicCount}"
101+
vnic_id = "${element(baremetal_core_vnic_attachment.SecondaryVnicAttachment.*.vnic_id, count.index)}"
102+
}
103+
104+
output "PrimaryIPAddresses" {
105+
value = ["${baremetal_core_instance.ExampleInstance.public_ip}",
106+
"${baremetal_core_instance.ExampleInstance.private_ip}"]
107+
}
108+
109+
output "SecondaryPublicIPAddresses" {
110+
value = ["${data.baremetal_core_vnic.SecondaryVnic.*.public_ip_address}"]
111+
}
112+
113+
output "SecondaryPrivateIPAddresses" {
114+
value = ["${data.baremetal_core_vnic.SecondaryVnic.*.private_ip_address}"]
115+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# baremetal\_core\_vnic\_attachment
2+
3+
Provides a VNIC attachment resource
4+
5+
## Example Usage
6+
7+
```
8+
resource "baremetal_core_vnic_attachment" "t" {
9+
instance_id = "${var.instance_id}"
10+
display_name = "secondary_vnic_attachment"
11+
create_vnic_details {
12+
subnet_id = "${var.subnet_id}"
13+
display_name = "secondary_vnic"
14+
assign_public_ip = true
15+
}
16+
}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `display_name` - (Optional) A user-friendly name. Does not have to be unique.
24+
* `instance_id` - (Required) The OCID of the instance.
25+
* `create_vnic_details` - (Required) Details for creating a new VNIC. See [Create Vnic Details](https://docs.us-phoenix-1.oraclecloud.com/api/#/en/iaas/20160918/requests/CreateVnicDetails).
26+
27+
## Create VNIC Details Argument Reference
28+
29+
* `assign_public_ip` - (Optional) Whether the VNIC should be assigned a public IP address.
30+
* `display_name` - (Optional) A user-friendly name for the VNIC. Does not have to be unique.
31+
* `hostname_label` - (Optional) The hostname for the VNIC's primary private IP.
32+
* `private_ip` - (Optional) A private IP address of your choice to assign to the VNIC.
33+
* `subnet_id` - (Required) The OCID of the subnet to create the VNIC in.
34+
35+
## Attributes Reference
36+
37+
* `availability_domain` - The VNIC Attachment's Availability Domain.
38+
* `compartment_id` - The OCID of the compartment the VNIC attachment is in, which is the same compartment the instance is in.
39+
* `display_name` - A user-friendly name. Does not have to be unique.
40+
* `id` - The OCID of the VNIC Attachment.
41+
* `instance_id` - The OCID of the instance.
42+
* `state` - The current state of the VNIC attachment. [ATTACHING, ATTACHED, DETACHING, DETACHED].
43+
* `subnet_id` - The OCID of the VNIC's subnet.
44+
* `time_created` - The date and time the VNIC attachment was created, in the format defined by RFC3339.
45+
* `vlan_tag` - The Oracle-assigned VLAN tag of the attached VNIC. Available after the attachment process is complete.
46+
* `vnic_id` - The OCID of the VNIC.

docs/resources/core/volume_attachment.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# baremetal\_core\_volume\_attachment
22

3-
Provides a volue attachment resource
3+
Provides a volume attachment resource
44

55
## Example Usage
66

@@ -17,7 +17,7 @@ resource "baremetal_core_volume_attachment" "t" {
1717

1818
The following arguments are supported:
1919

20-
* `display_name` - (Required) The OCID of the compartment.
20+
* `display_name` - (Required) A user-friendly name. Does not have to be unique, and it cannot be changed.
2121
* `instance_id` - (Required) The OCID of the instance.
2222
* `volume_id` - (Required) The OCID of the volume.
2323
* `type` - (Required) The type of volume. The only supported value is "iscsi".

helpers_core.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2+
3+
package main
4+
5+
import "github.com/MustWin/baremetal-sdk-go"
6+
7+
func SetCreateVnicOptions(rawCreateVnicDetails interface{}) (vnicOpts *baremetal.CreateVnicOptions) {
8+
vnic := rawCreateVnicDetails.(map[string]interface{})
9+
10+
vnicOpts = &baremetal.CreateVnicOptions{}
11+
vnicOpts.SubnetID = vnic["subnet_id"].(string)
12+
13+
displayName := vnic["display_name"]
14+
if displayName != nil {
15+
vnicOpts.DisplayName = displayName.(string)
16+
}
17+
18+
hostnameLabel := vnic["hostname_label"]
19+
if hostnameLabel != nil {
20+
vnicOpts.HostnameLabel = hostnameLabel.(string)
21+
}
22+
23+
privateIp := vnic["private_ip"]
24+
if privateIp != nil {
25+
vnicOpts.PrivateIp = privateIp.(string)
26+
}
27+
28+
//todo: work around for tf bug https://github.com/hashicorp/terraform/issues/13512
29+
assignPublicIp := vnic["assign_public_ip"]
30+
if assignPublicIp != nil {
31+
vnicOpts.AssignPublicIp = new(bool)
32+
*vnicOpts.AssignPublicIp = assignPublicIp.(string) == "1"
33+
}
34+
35+
return
36+
}

provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func resourcesMap() map[string]*schema.Resource {
173173
"baremetal_core_security_list": SecurityListResource(),
174174
"baremetal_core_subnet": SubnetResource(),
175175
"baremetal_core_virtual_network": VirtualNetworkResource(),
176+
"baremetal_core_vnic_attachment": VnicAttachmentResource(),
176177
"baremetal_core_volume": VolumeResource(),
177178
"baremetal_core_volume_attachment": VolumeAttachmentResource(),
178179
"baremetal_core_volume_backup": VolumeBackupResource(),

0 commit comments

Comments
 (0)