Skip to content

Commit

Permalink
resource/server: support boot_type
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolai86 committed Apr 25, 2018
1 parent 6aec6e1 commit 1a35a04
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 3 deletions.
15 changes: 15 additions & 0 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ func String(val string) *string {
return &val
}

var bootTypes = []string{"local", "bootscript"}

func validateBootType(v interface{}, k string) (ws []string, errors []error) {
isKnown := false
requestedType := v.(string)
for _, knownType := range bootTypes {
isKnown = isKnown || strings.ToUpper(knownType) == strings.ToUpper(requestedType)
}

if !isKnown {
errors = append(errors, fmt.Errorf("%q must be one of %q", k, bootTypes))
}
return
}

func validateServerType(v interface{}, k string) (ws []string, errors []error) {
// only validate if we were able to fetch a list of commercial types
if len(commercialServerTypes) == 0 {
Expand Down
24 changes: 24 additions & 0 deletions scaleway/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scaleway

import "testing"

func TestValidateBootType(t *testing.T) {
testCases := []struct {
requestedType string
valid bool
}{{"local", true}, {"bootscript", true}, {"remote", false}}
for _, testCase := range testCases {
t.Run(testCase.requestedType, func(t *testing.T) {
_, errors := validateBootType(testCase.requestedType, "boot_type")
if testCase.valid {
if len(errors) != 0 {
t.Fatalf("Expected %q to be valid, but got %q", testCase.requestedType, errors)
}
} else {
if len(errors) == 0 {
t.Fatalf("Expected %q to be invalid, but got %q", testCase.requestedType, errors)
}
}
})
}
}
18 changes: 18 additions & 0 deletions scaleway/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ func resourceScalewayServer() *schema.Resource {
Description: "The instance type of the server",
ValidateFunc: validateServerType,
},
"boot_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The boot_type of the server",
ValidateFunc: validateBootType,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if new == "" {
return true
}
return false
},
},
"bootscript": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -159,6 +172,10 @@ func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
EnableIPV6: d.Get("enable_ipv6").(bool),
SecurityGroup: d.Get("security_group").(string),
}
bootType, ok := d.GetOk("boot_type")
if ok {
req.BootType = bootType.(string)
}

req.DynamicIPRequired = Bool(d.Get("dynamic_ip_required").(bool))
req.CommercialType = d.Get("type").(string)
Expand Down Expand Up @@ -249,6 +266,7 @@ func resourceScalewayServerRead(d *schema.ResourceData, m interface{}) error {
d.Set("enable_ipv6", server.EnableIPV6)
d.Set("private_ip", server.PrivateIP)
d.Set("public_ip", server.PublicAddress.IP)
d.Set("boot_type", server.BootType)

if server.EnableIPV6 && server.IPV6 != nil {
d.Set("public_ipv6", server.IPV6.Address)
Expand Down
30 changes: 30 additions & 0 deletions scaleway/resource_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func TestAccScalewayServer_Basic(t *testing.T) {
"scaleway_server.base", "name", "test"),
resource.TestCheckResourceAttr(
"scaleway_server.base", "tags.0", "terraform-test"),
resource.TestCheckResourceAttr(
"scaleway_server.base", "boot_type", "bootscript"),
),
},
resource.TestStep{
Expand All @@ -81,6 +83,23 @@ func TestAccScalewayServer_Basic(t *testing.T) {
})
}

func TestAccScalewayServer_BootType(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckScalewayServerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckScalewayServerConfig_LocalBoot,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"scaleway_server.base", "boot_type", "local"),
),
},
},
})
}

func TestAccScalewayServer_ExistingIP(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -301,6 +320,7 @@ func testAccCheckScalewayServerExists(n string) resource.TestCheckFunc {
}

var armImageIdentifier = "5faef9cd-ea9b-4a63-9171-9e26bec03dbc"
var x86_64ImageIdentifier = "e20532c4-1fa0-4c97-992f-436b8d372c07"

var testAccCheckScalewayServerConfig = fmt.Sprintf(`
resource "scaleway_server" "base" {
Expand All @@ -311,6 +331,16 @@ resource "scaleway_server" "base" {
tags = [ "terraform-test" ]
}`, armImageIdentifier)

var testAccCheckScalewayServerConfig_LocalBoot = fmt.Sprintf(`
resource "scaleway_server" "base" {
name = "test"
# ubuntu 14.04
image = "%s"
type = "VC1S"
tags = [ "terraform-test" ]
boot_type = "local"
}`, x86_64ImageIdentifier)

var testAccCheckScalewayServerConfig_IPAttachment = fmt.Sprintf(`
resource "scaleway_ip" "base" {}
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/nicolai86/scaleway-sdk/server.go

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

6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,10 @@
"revisionTime": "2016-10-03T17:45:16Z"
},
{
"checksumSHA1": "qPUxUkRxA0nnbZ9Pb7yUor1P7Fg=",
"checksumSHA1": "n8j/CVEarUKArSLFjbZ4vMcxKf4=",
"path": "github.com/nicolai86/scaleway-sdk",
"revision": "d749f7b83389f1afe19b81a610fad5ebb7a12744",
"revisionTime": "2018-04-01T05:36:48Z"
"revision": "514d84f85641e74c600b56fb9ec5083cd4803a73",
"revisionTime": "2018-04-25T16:23:49Z"
},
{
"checksumSHA1": "u5s2PZ7fzCOqQX7bVPf9IJ+qNLQ=",
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/server.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following arguments are supported:
* `image` - (Required) base image of server
* `type` - (Required) type of server
* `bootscript` - (Optional) server bootscript
* `boot_type` - (Optional) server boot_type (`local`, `bootscript`)
* `tags` - (Optional) list of tags for server
* `enable_ipv6` - (Optional) enable ipv6
* `dynamic_ip_required` - (Optional) make server publicly available
Expand Down

0 comments on commit 1a35a04

Please sign in to comment.