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

feat(instance): add ipv6 info on server resource #301

Merged
merged 3 commits into from
Oct 23, 2019
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/terraform v0.12.8
github.com/mitchellh/go-homedir v1.1.0
github.com/nicolai86/scaleway-sdk v0.0.0-20181024210327-b20018e944c4
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191021151953-1a8fd626ee2b
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191023160931-32816775aca2
github.com/stretchr/testify v1.3.0
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191017082803-e4a1a916fcb0 h1:msxZWAcJ9V7nzFkpNjsP3TAKYrcGjuY1FNCkxWjzjb0=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191017082803-e4a1a916fcb0/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191021151953-1a8fd626ee2b h1:8MeCmTWJNy5PgSIgbmjAosgv93DrfthtzBMCo+RrqI0=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191021151953-1a8fd626ee2b/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191023160931-32816775aca2 h1:7sTlsBtMwIM/Q6MnBjBzvbhJHXnQZjEQiRZeOiTMrC0=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191023160931-32816775aca2/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand Down
14 changes: 12 additions & 2 deletions scaleway/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package scaleway

import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"net"
"net/http"
"strings"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -227,3 +227,13 @@ func testCheckResourceAttrIPv4(name string, key string) resource.TestCheckFunc {
return nil
})
}

func testCheckResourceAttrIPv6(name string, key string) resource.TestCheckFunc {
jerome-quere marked this conversation as resolved.
Show resolved Hide resolved
return testCheckResourceAttrFunc(name, key, func(value string) error {
ip := net.ParseIP(value)
if ip.To16() == nil {
return fmt.Errorf("%s is not a valid IPv6", value)
}
return nil
})
}
29 changes: 27 additions & 2 deletions scaleway/resource_instance_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ func resourceScalewayInstanceServer() *schema.Resource {
Computed: true,
Description: "The public IPv4 address of the server",
},
"ipv6_address": {
Type: schema.TypeString,
Computed: true,
Description: "The default public IPv6 address routed to the server.",
},
"ipv6_gateway": {
Type: schema.TypeString,
Computed: true,
Description: "The IPv6 gateway address",
},
"ipv6_prefix_length": {
Type: schema.TypeInt,
Computed: true,
Description: "The IPv6 prefix length routed to the server.",
},
"disable_dynamic_ip": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -356,8 +371,18 @@ func resourceScalewayInstanceServerRead(d *schema.ResourceData, m interface{}) e
})
}

if response.Server.EnableIPv6 && response.Server.IPv6 != nil {
d.Set("public_ipv6", response.Server.IPv6.Address.String())
if response.Server.IPv6 != nil {
d.Set("ipv6_address", response.Server.IPv6.Address.String())
d.Set("ipv6_gateway", response.Server.IPv6.Gateway.String())
prefixLength, err := strconv.Atoi(response.Server.IPv6.Netmask)
if err != nil {
return err
}
d.Set("ipv6_prefix_length", prefixLength)
} else {
d.Set("ipv6_address", nil)
d.Set("ipv6_gateway", nil)
d.Set("ipv6_prefix_length", nil)
}

var additionalVolumesIDs []string
Expand Down
40 changes: 40 additions & 0 deletions scaleway/resource_instance_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,46 @@ func TestAccScalewayInstanceServerSwapVolume(t *testing.T) {
})
}

func TestAccScalewayInstanceServerIpv6(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckScalewayInstanceServerDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_instance_server" "server01" {
image = "ubuntu-bionic"
type = "DEV1-S"
enable_ipv6 = true
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceServerExists("scaleway_instance_server.server01"),
testCheckResourceAttrIPv6("scaleway_instance_server.server01", "ipv6_address"),
testCheckResourceAttrIPv6("scaleway_instance_server.server01", "ipv6_gateway"),
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_prefix_length", "64"),
),
},
{
Config: `
resource "scaleway_instance_server" "server01" {
image = "ubuntu-bionic"
type = "DEV1-S"
enable_ipv6 = false
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayInstanceServerExists("scaleway_instance_server.server01"),
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_address", ""),
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_gateway", ""),
resource.TestCheckResourceAttr("scaleway_instance_server.server01", "ipv6_prefix_length", "0"),
),
},
},
})
}

func testAccCheckScalewayInstanceServerExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ github.com/posener/complete
github.com/posener/complete/cmd
github.com/posener/complete/cmd/install
github.com/posener/complete/match
# github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191021151953-1a8fd626ee2b
# github.com/scaleway/scaleway-sdk-go v1.0.0-beta.3.0.20191023160931-32816775aca2
github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1
github.com/scaleway/scaleway-sdk-go/api/baremetal/v1alpha1
github.com/scaleway/scaleway-sdk-go/api/instance/v1
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/instance_server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ In addition to all above arguments, the following attributes are exported:
- `volume_id` - The volume ID of the root volume of the server.
- `private_ip` - The Scaleway internal IP address of the server.
- `public_ip` - The public IPv4 address of the server.
- `ipv6_address` - The default ipv6 address routed to the server. ( Only set when enable_ipv6 is set to true )
- `ipv6_gateway` - The ipv6 gateway address. ( Only set when enable_ipv6 is set to true )
- `ipv6_prefix_length` - The prefix length of the ipv6 subnet routed to the server. ( Only set when enable_ipv6 is set to true )

## Import

Expand Down