Skip to content

Commit

Permalink
feat(network): add support for exposing routes to vswitch connection (#…
Browse files Browse the repository at this point in the history
…703)

* feat(network): add support for exposing routes to vswitch connection

Feature was released today. This adds:

- Setting and returning the field on all `network` resources
  • Loading branch information
apricote authored Jun 22, 2023
1 parent 17708d8 commit f213550
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 11 deletions.
4 changes: 4 additions & 0 deletions internal/e2etests/network/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccHcloudDataSourceNetworkTest(t *testing.T) {
Labels: map[string]string{
"key": strconv.Itoa(acctest.RandInt()),
},
ExposeRoutesToVSwitch: true,
}
res.SetRName("network-ds-test")
networkByName := &network.DData{
Expand Down Expand Up @@ -61,14 +62,17 @@ func TestAccHcloudDataSourceNetworkTest(t *testing.T) {
resource.TestCheckResourceAttr(networkByName.TFID(),
"name", fmt.Sprintf("%s--%d", res.Name, tmplMan.RandInt)),
resource.TestCheckResourceAttr(networkByName.TFID(), "ip_range", res.IPRange),
resource.TestCheckResourceAttr(networkByName.TFID(), "expose_routes_to_vswitch", "true"),

resource.TestCheckResourceAttr(networkByID.TFID(),
"name", fmt.Sprintf("%s--%d", res.Name, tmplMan.RandInt)),
resource.TestCheckResourceAttr(networkByID.TFID(), "ip_range", res.IPRange),
resource.TestCheckResourceAttr(networkByID.TFID(), "expose_routes_to_vswitch", "true"),

resource.TestCheckResourceAttr(networkBySel.TFID(),
"name", fmt.Sprintf("%s--%d", res.Name, tmplMan.RandInt)),
resource.TestCheckResourceAttr(networkBySel.TFID(), "ip_range", res.IPRange),
resource.TestCheckResourceAttr(networkBySel.TFID(), "expose_routes_to_vswitch", "true"),
),
},
},
Expand Down
60 changes: 60 additions & 0 deletions internal/e2etests/network/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,63 @@ func TestNetworkResource_Protection(t *testing.T) {
},
})
}

func TestNetworkResource_ExposeRouteToVSwitch(t *testing.T) {
var (
cert hcloud.Network

res = &network.RData{
Name: "network-routes-vswitch",
IPRange: "10.0.0.0/8",
ExposeRoutesToVSwitch: true,
}

updateExposure = func(d *network.RData, expose bool) *network.RData {
d.ExposeRoutesToVSwitch = expose
return d
}
)

tmplMan := testtemplate.Manager{}
resource.ParallelTest(t, resource.TestCase{
PreCheck: e2etests.PreCheck(t),
Providers: e2etests.Providers(),
CheckDestroy: testsupport.CheckResourcesDestroyed(network.ResourceType, network.ByID(t, &cert)),
Steps: []resource.TestStep{
{
// Create a new Network using the required values only.
Config: tmplMan.Render(t,
"testdata/r/hcloud_network", res,
),
Check: resource.ComposeTestCheckFunc(
testsupport.CheckResourceExists(res.TFID(), network.ByID(t, &cert)),
resource.TestCheckResourceAttr(res.TFID(), "name",
fmt.Sprintf("network-routes-vswitch--%d", tmplMan.RandInt)),
resource.TestCheckResourceAttr(res.TFID(), "ip_range", res.IPRange),
resource.TestCheckResourceAttr(res.TFID(), "expose_routes_to_vswitch", fmt.Sprintf("%t", res.ExposeRoutesToVSwitch)),
),
},
{
// Try to import the newly created Network
ResourceName: res.TFID(),
ImportState: true,
ImportStateVerify: true,
},
{
// Update expose_routes_to_vswitch
Config: tmplMan.Render(t,
"testdata/r/hcloud_network", updateExposure(res, false),
),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(res.TFID(), "expose_routes_to_vswitch", fmt.Sprintf("%t", res.ExposeRoutesToVSwitch)),
),
},
{
// Try to import the newly created Network
ResourceName: res.TFID(),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
5 changes: 5 additions & 0 deletions internal/network/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ func getCommonDataSchema() map[string]*schema.Schema {
Type: schema.TypeBool,
Computed: true,
},
"expose_routes_to_vswitch": {
Type: schema.TypeBool,
Description: "Indicates if the routes from this network should be exposed to the vSwitch connection. The exposing only takes effect if a vSwitch connection is active.",
Computed: true,
},
}
}

Expand Down
34 changes: 27 additions & 7 deletions internal/network/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func Resource() *schema.Resource {
Optional: true,
Default: false,
},
"expose_routes_to_vswitch": {
Type: schema.TypeBool,
Description: "Enable or disable exposing the routes to the vSwitch connection. The exposing only takes effect if a vSwitch connection is active.",
Optional: true,
Default: false,
},
},
}
}
Expand All @@ -68,8 +74,9 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, m interf
}

opts := hcloud.NetworkCreateOpts{
Name: d.Get("name").(string),
IPRange: ipRange,
Name: d.Get("name").(string),
IPRange: ipRange,
ExposeRoutesToVSwitch: d.Get("expose_routes_to_vswitch").(bool),
}
if labels, ok := d.GetOk("labels"); ok {
tmpLabels := make(map[string]string)
Expand Down Expand Up @@ -156,6 +163,18 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, m interf
}
}

if d.HasChange("expose_routes_to_vswitch") {
_, _, err := client.Network.Update(ctx, network, hcloud.NetworkUpdateOpts{
ExposeRoutesToVSwitch: hcloud.Ptr(d.Get("expose_routes_to_vswitch").(bool)),
})
if err != nil {
if resourceNetworkIsNotFound(err, d) {
return nil
}
return hcclient.ErrorToDiag(err)
}
}

if d.HasChange("delete_protection") {
deletionProtection := d.Get("delete_protection").(bool)
if err := setProtection(ctx, client, network, deletionProtection); err != nil {
Expand Down Expand Up @@ -210,11 +229,12 @@ func setNetworkSchema(d *schema.ResourceData, n *hcloud.Network) {

func getNetworkAttributes(n *hcloud.Network) map[string]interface{} {
return map[string]interface{}{
"id": n.ID,
"ip_range": n.IPRange.String(),
"name": n.Name,
"labels": n.Labels,
"delete_protection": n.Protection.Delete,
"id": n.ID,
"ip_range": n.IPRange.String(),
"name": n.Name,
"labels": n.Labels,
"delete_protection": n.Protection.Delete,
"expose_routes_to_vswitch": n.ExposeRoutesToVSwitch,
}
}

Expand Down
9 changes: 5 additions & 4 deletions internal/network/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ func (d *DData) TFID() string {
type RData struct {
testtemplate.DataCommon

Name string
IPRange string
Labels map[string]string
DeleteProtection bool
Name string
IPRange string
Labels map[string]string
DeleteProtection bool
ExposeRoutesToVSwitch bool
}

// TFID returns the resource identifier.
Expand Down
4 changes: 4 additions & 0 deletions internal/testdata/r/hcloud_network.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ resource "hcloud_network" "{{ .RName }}" {
{{- if .DeleteProtection }}
delete_protection = {{ .DeleteProtection }}
{{ end }}

{{- if .ExposeRoutesToVSwitch }}
expose_routes_to_vswitch = {{ .ExposeRoutesToVSwitch }}
{{ end }}
}
1 change: 1 addition & 0 deletions website/docs/d/network.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ data "hcloud_network" "network_3" {
- `name` - Name of the Network.
- `ip_range` - IPv4 prefix of the Network.
- `delete_protection` - (bool) Whether delete protection is enabled.
- `expose_routes_to_vswitch` - (bool) Indicates if the routes from this network should be exposed to the vSwitch connection. The exposing only takes effect if a vSwitch connection is active.
2 changes: 2 additions & 0 deletions website/docs/r/network.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ resource "hcloud_network" "privNet" {
- `ip_range` - (Required, string) IP Range of the whole Network which must span all included subnets and route destinations. Must be one of the private ipv4 ranges of RFC1918.
- `labels` - (Optional, map) User-defined labels (key-value pairs) should be created with.
- `delete_protection` - (Optional, bool) Enable or disable delete protection.
- `expose_routes_to_vswitch` - (Optional, bool) Enable or disable exposing the routes to the vSwitch connection. The exposing only takes effect if a vSwitch connection is active.

## Attributes Reference

Expand All @@ -33,6 +34,7 @@ resource "hcloud_network" "privNet" {
- `ip_range` - (string) IPv4 Prefix of the whole Network.
- `labels` - (map) User-defined labels (key-value pairs)
- `delete_protection` - (bool) Whether delete protection is enabled.
- `expose_routes_to_vswitch` - (bool) Indicates if the routes from this network should be exposed to the vSwitch connection. The exposing only takes effect if a vSwitch connection is active.

## Import

Expand Down

0 comments on commit f213550

Please sign in to comment.