-
Notifications
You must be signed in to change notification settings - Fork 135
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
base: main
Are you sure you want to change the base?
Nat Network Support #103
Changes from all commits
85c67a3
734cb0c
8481266
e03d81b
d55c0d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.