Skip to content

Commit

Permalink
feat(network): dhcp routes support
Browse files Browse the repository at this point in the history
  • Loading branch information
villevsv-upcloud committed Sep 28, 2023
1 parent 106d26f commit 3771d12
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
### Added
- gateway: add read-only `addresses` field
- kubernetes: `control_plane_ip_filter` field to `upcloud_kubernetes_cluster` resource
- network: `dhcp_routes` field to `ip_network` block in `upcloud_network` resource

### Changed
- kubernetes: remove node group maximum value validation. The maximum number of nodes (in the cluster) is determined by the cluster plan and the validation is done on the API side.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/UpCloudLtd/terraform-provider-upcloud
go 1.20

require (
github.com/UpCloudLtd/upcloud-go-api/v6 v6.6.0
github.com/UpCloudLtd/upcloud-go-api/v6 v6.6.1-0.20230927070743-7dd13ddf6ef5
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/hashicorp/go-uuid v1.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.6.0 h1:Fc9a083OBzl8i4pDV2KXCAfxo4gCjJFHgRuPvRnroBY=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.6.0/go.mod h1:I8rWmBBl+OhiY3AGzKbrobiE5TsLCLNYkCQxE4eJcTg=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.6.1-0.20230927070743-7dd13ddf6ef5 h1:gwwrCax+n27YwYXHmki5DAcDBNFuB5wFJEWkGhPZ8Gs=
github.com/UpCloudLtd/upcloud-go-api/v6 v6.6.1-0.20230927070743-7dd13ddf6ef5/go.mod h1:I8rWmBBl+OhiY3AGzKbrobiE5TsLCLNYkCQxE4eJcTg=
github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk=
github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
Expand Down
16 changes: 14 additions & 2 deletions internal/service/network/data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"regexp"
"time"

"github.com/UpCloudLtd/terraform-provider-upcloud/internal/utils"

"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/service"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/UpCloudLtd/terraform-provider-upcloud/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func DataSourceNetworks() *schema.Resource {
Expand Down Expand Up @@ -63,6 +64,16 @@ func DataSourceNetworks() *schema.Resource {
Type: schema.TypeString,
},
},
"dhcp_routes": {
Type: schema.TypeSet,
Description: "The additional DHCP classless static routes given by DHCP",
Computed: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsCIDR,
},
},
"family": {
Type: schema.TypeString,
Description: "IP address family",
Expand Down Expand Up @@ -181,6 +192,7 @@ func dataSourceNetworksRead(ctx context.Context, d *schema.ResourceData, meta in
"dhcp": fipn.DHCP.Bool(),
"dhcp_default_route": fipn.DHCPDefaultRoute.Bool(),
"dhcp_dns": fipn.DHCPDns,
"dhcp_routes": fipn.DHCPRoutes,
"family": fipn.Family,
"gateway": fipn.Gateway,
}
Expand Down
19 changes: 19 additions & 0 deletions internal/service/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func ResourceNetwork() *schema.Resource {
ValidateFunc: validation.Any(validation.IsIPv4Address, validation.IsIPv6Address),
},
},
"dhcp_routes": {
Type: schema.TypeSet,
Description: "The additional DHCP classless static routes given by DHCP",
Computed: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsCIDR,
},
},
"family": {
Type: schema.TypeString,
Description: "IP address family",
Expand Down Expand Up @@ -148,6 +158,10 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, meta int
uipn.DHCPDns = append(uipn.DHCPDns, dns.(string))
}

for _, route := range ipnConf["dhcp_routes"].(*schema.Set).List() {
uipn.DHCPRoutes = append(uipn.DHCPRoutes, route.(string))
}

req.IPNetworks = append(req.IPNetworks, uipn)
}

Expand Down Expand Up @@ -191,6 +205,7 @@ func resourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta inter
"dhcp": network.IPNetworks[0].DHCP.Bool(),
"dhcp_default_route": network.IPNetworks[0].DHCPDefaultRoute.Bool(),
"dhcp_dns": network.IPNetworks[0].DHCPDns,
"dhcp_routes": network.IPNetworks[0].DHCPRoutes,
"family": network.IPNetworks[0].Family,
"gateway": network.IPNetworks[0].Gateway,
}
Expand Down Expand Up @@ -235,6 +250,10 @@ func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, meta int
uipn.DHCPDns = append(uipn.DHCPDns, dns.(string))
}

for _, route := range ipnConf["dhcp_routes"].(*schema.Set).List() {
uipn.DHCPRoutes = append(uipn.DHCPRoutes, route.(string))
}

req.IPNetworks = []upcloud.IPNetwork{uipn}
}

Expand Down
83 changes: 50 additions & 33 deletions upcloud/resource_upcloud_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccUpCloudNetwork_basic(t *testing.T) {
Expand All @@ -27,7 +26,17 @@ func TestAccUpCloudNetwork_basic(t *testing.T) {
ProviderFactories: testAccProviderFactories(&providers),
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, false),
Config: testAccNetworkConfig(
netName,
"fi-hel1",
cidr,
gateway,
true,
false,
false,
[]string{"10.0.0.2", "10.0.0.3"},
[]string{"192.168.0.0/24", "192.168.100.0/32"},
),
Check: resource.ComposeAggregateTestCheckFunc(
testAccNetworkExists("upcloud_network.test_network"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "name", netName),
Expand All @@ -37,6 +46,8 @@ func TestAccUpCloudNetwork_basic(t *testing.T) {
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.address", cidr),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.gateway", gateway),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.family", "IPv4"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.dhcp_dns.#", "2"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.dhcp_routes.#", "2"),
),
},
},
Expand All @@ -56,7 +67,7 @@ func TestAccUpCloudNetwork_basicUpdate(t *testing.T) {
ProviderFactories: testAccProviderFactories(&providers),
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, false),
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, false, []string{"10.0.0.2"}, []string{"192.168.0.0/24"}),
Check: resource.ComposeAggregateTestCheckFunc(
testAccNetworkExists("upcloud_network.test_network"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "name", netName),
Expand All @@ -66,10 +77,12 @@ func TestAccUpCloudNetwork_basicUpdate(t *testing.T) {
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.address", cidr),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.gateway", gateway),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.family", "IPv4"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.dhcp_dns.0", "10.0.0.2"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.dhcp_routes.0", "192.168.0.0/24"),
),
},
{
Config: testAccNetworkConfig(netName+"_1", "fi-hel1", cidr, gateway, true, false, false),
Config: testAccNetworkConfig(netName+"_1", "fi-hel1", cidr, gateway, true, false, false, []string{"10.0.0.3"}, []string{"192.168.100.0/24"}),
Check: resource.ComposeAggregateTestCheckFunc(
testAccNetworkExists("upcloud_network.test_network"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "name", netName+"_1"),
Expand All @@ -79,6 +92,8 @@ func TestAccUpCloudNetwork_basicUpdate(t *testing.T) {
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.address", cidr),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.gateway", gateway),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.family", "IPv4"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.dhcp_dns.0", "10.0.0.3"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "ip_network.0.dhcp_routes.0", "192.168.100.0/24"),
),
},
},
Expand All @@ -98,7 +113,7 @@ func TestAccUpCloudNetwork_withRouter(t *testing.T) {
ProviderFactories: testAccProviderFactories(&providers),
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, true),
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, true, nil, nil),
Check: resource.ComposeAggregateTestCheckFunc(
testAccNetworkExists("upcloud_network.test_network"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "name", netName),
Expand Down Expand Up @@ -128,7 +143,7 @@ func TestAccUpCloudNetwork_amendWithRouter(t *testing.T) {
ProviderFactories: testAccProviderFactories(&providers),
Steps: []resource.TestStep{
{
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, false),
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, false, nil, nil),
Check: resource.ComposeAggregateTestCheckFunc(
testAccNetworkExists("upcloud_network.test_network"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "name", netName),
Expand All @@ -142,7 +157,7 @@ func TestAccUpCloudNetwork_amendWithRouter(t *testing.T) {
),
},
{
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, true),
Config: testAccNetworkConfig(netName, "fi-hel1", cidr, gateway, true, false, true, nil, nil),
Check: resource.ComposeAggregateTestCheckFunc(
testAccNetworkExists("upcloud_network.test_network"),
resource.TestCheckResourceAttr("upcloud_network.test_network", "name", netName),
Expand Down Expand Up @@ -172,35 +187,18 @@ func TestAccUpCloudNetwork_FamilyValidation(t *testing.T) {
ProviderFactories: testAccProviderFactories(&providers),
Steps: []resource.TestStep{
{
Config: testAccNetworkConfigWithFamily(netName, "fi-hel1", cidr, gateway, "rubbish", true, false, false),
Config: testAccNetworkConfigWithFamily(netName, "fi-hel1", cidr, gateway, "rubbish", true, false, false, nil, nil),
ExpectError: regexp.MustCompile(`'family' has incorrect value`),
},
},
})
}

func testAccNetworkConfig(
name string,
zone string,
address string,
gateway string,
dhcp bool,
dhcpDefaultRoute bool,
router bool,
) string {
return testAccNetworkConfigWithFamily(name, zone, address, gateway, "IPv4", dhcp, dhcpDefaultRoute, router)
func testAccNetworkConfig(name string, zone string, address string, gateway string, dhcp bool, dhcpDefaultRoute bool, router bool, dhcpDNS []string, dhcpRoutes []string) string {
return testAccNetworkConfigWithFamily(name, zone, address, gateway, "IPv4", dhcp, dhcpDefaultRoute, router, dhcpDNS, dhcpRoutes)
}

func testAccNetworkConfigWithFamily(
name string,
zone string,
address string,
gateway string,
family string,
dhcp bool,
dhcpDefaultRoute bool,
router bool,
) string {
func testAccNetworkConfigWithFamily(name string, zone string, address string, gateway string, family string, dhcp bool, dhcpDefaultRoute bool, router bool, dhcpDNS []string, dhcpRoutes []string) string {
config := strings.Builder{}

config.WriteString(fmt.Sprintf(`
Expand All @@ -220,11 +218,30 @@ func testAccNetworkConfigWithFamily(
address = "%s"
dhcp = "%t"
dhcp_default_route = "%t"
family = "%s"
gateway = "%s"
}
family = "%s"
gateway = "%s"
`,
address,
dhcp,
dhcpDefaultRoute,
family,
gateway))

if len(dhcpDNS) > 0 {
config.WriteString(fmt.Sprintf(`
dhcp_dns = ["%s"]`, strings.Join(dhcpDNS, "\", \"")))
}

if len(dhcpRoutes) > 0 {
config.WriteString(fmt.Sprintf(`
dhcp_routes = ["%s"]`, strings.Join(dhcpRoutes, "\", \"")))
}

config.WriteString(`
}
}
`, address, dhcp, dhcpDefaultRoute, family, gateway))
`)

if router {
config.WriteString(fmt.Sprintf(`
Expand Down

0 comments on commit 3771d12

Please sign in to comment.