Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nat Network Support #103

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion virtualbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func init() {
func Provider() terraform.ResourceProvider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"virtualbox_vm": resourceVM(),
"virtualbox_vm": resourceVM(),
"virtualbox_natnetwork": resourceNatNetwork(),
},
}
}
94 changes: 94 additions & 0 deletions virtualbox/resource_natnetwork.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// This Source Code Form is subject to the terms of the Mozilla Public
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am absolutely not a lawyer, so I have no idea how any of this works, but this repository is licences under MIT, Can we just pull in another licence for a single file?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was copied from provider.go
Feel free to re-license under MIT.

// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package virtualbox

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
vbox "github.com/terra-farm/go-virtualbox"
)

func resourceNatNetwork() *schema.Resource {
return &schema.Resource{
Exists: resourceNatNetworkExists,
Create: resourceNatNetworkCreate,
Read: resourceNatNetworkRead,
Update: resourceNatNetworkUpdate,
Delete: resourceNatNetworkDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"dhcp": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

func resourceNatNetworkExists(d *schema.ResourceData, meta interface{}) (bool, error) {
name := d.Get("name").(string)

_, err := vbox.GetNATNetwork(name)
if err != nil {
return false, err
}

return true, nil
}

func resourceNatNetworkCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
dhcp := d.Get("dhcp").(bool)
network := d.Get("network").(string)

_, err := vbox.CreateNATNet(name, network, dhcp)
if err != nil {
return err
}
d.SetId(name)
return resourceNatNetworkRead(d, meta)
}

func resourceNatNetworkRead(d *schema.ResourceData, meta interface{}) error {
natnet, err := vbox.GetNATNetwork(d.Id())
if err != nil {
return err
}
d.Set("name", natnet.Name)
d.Set("dhcp", natnet.DHCP)
d.Set("network", natnet.IPv4.String())
return nil
}

func resourceNatNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
natnet, err := vbox.GetNATNetwork(d.Id())
if err != nil {
return errLogf("unable to get nat network: %v", err)
}
if err := natnet.Config(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated values are not passed in anywhere, so I don't think the update will actually have the intended consequences.

return errLogf("unable to remove nat network: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message doesn't appear to be correct. We are updating the resource here, not deleting it.

}

return nil
}

func resourceNatNetworkDelete(d *schema.ResourceData, meta interface{}) error {
natnet, err := vbox.GetNATNetwork(d.Id())
if err != nil {
return errLogf("unable to get nat network: %v", err)
}
if err := natnet.Delete(); err != nil {
return errLogf("unable to remove nat network: %v", err)
}
return nil
}
19 changes: 19 additions & 0 deletions virtualbox/resource_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func resourceVM() *schema.Resource {
Optional: true,
},

"nat_network": {
Type: schema.TypeString,
Optional: true,
},

"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -543,6 +548,8 @@ func netTfToVbox(d *schema.ResourceData) ([]vbox.NIC, error) {
return vbox.NICNetBridged, nil
case "nat":
return vbox.NICNetNAT, nil
case "natnetwork":
return vbox.NICNetNATNetwork, nil
case "hostonly":
return vbox.NICNetHostonly, nil
case "internal":
Expand Down Expand Up @@ -595,6 +602,15 @@ func netTfToVbox(d *schema.ResourceData) ([]vbox.NIC, error) {
}
}

/* 'natnetwork' network need property 'nat_network' been set */
if adapter.Network == vbox.NICNetNATNetwork {
var ok bool
adapter.NatNetwork, ok = d.Get(prefix + "nat_network").(string)
if !ok || adapter.NatNetwork == "" {
err = fmt.Errorf("'nat_network' property not set for '#%d' network adapter", i)
}
}

if err != nil {
errs = append(errs, err)
continue
Expand Down Expand Up @@ -633,6 +649,8 @@ func netVboxToTf(vm *vbox.Machine, d *schema.ResourceData) error {
return "bridged"
case vbox.NICNetNAT:
return "nat"
case vbox.NICNetNATNetwork:
return "natnetwork"
case vbox.NICNetHostonly:
return "hostonly"
case vbox.NICNetInternal:
Expand Down Expand Up @@ -732,6 +750,7 @@ func netVboxToTf(vm *vbox.Machine, d *schema.ResourceData) error {
out["type"] = vboxToTfNetworkType(nic.Network)
out["device"] = vboxToTfVdevice(nic.Hardware)
out["host_interface"] = nic.HostInterface
out["nat_network"] = nic.NatNetwork
out["mac_address"] = nic.MacAddr

osNic, ok := osNicMap[nic.MacAddr]
Expand Down
10 changes: 10 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ resource "virtualbox_vm" "node" {
type = "hostonly"
host_interface = "vboxnet1"
}
network_adapter {
type = "natnetwork"
nat_network = virtualbox_natnetwork.vmnet10.name
}
}

resource "virtualbox_natnetwork" "vmnet10" {
name = "vmnet10"
dhcp = true
network = "192.168.6.0/24"
}

output "IPAddr" {
Expand Down
29 changes: 29 additions & 0 deletions website/docs/r/natnetwork.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
layout: "virtualbox"
page_title: "Virtualbox: NAT Net"
description: |
Manages a Virtualbox NAT network
---

# virtualbox_natnetwork

Creates and manages a Virtualbox NAT network

## Example Usage

```hcl
resource "virtualbox_natnetwork" "default_net" {
name = "NAT Network"
dhcp = true
network = "192.168.56.1/24"
}
```

## Argument Reference

The following arguments are supported:

- `name` - (Required) The name of the virtual NAT network.
box).
- `dhcp` - (Optional) If DHCP is used for the network.
- `network` - (Required) The CIDR range used for the network.