provider/scaleway: allow public_ip to be set (#14515)

this allows the IP <-> server relationship to be inverted as requested by #14175.
This commit is contained in:
Raphael Randschau 2017-05-16 10:59:39 +02:00 committed by Paul Stack
parent 23b41ad764
commit 2c3d54b671
3 changed files with 151 additions and 9 deletions

View File

@ -21,6 +21,7 @@ func resourceScalewayIP() *schema.Resource {
"server": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ip": {
Type: schema.TypeString,

View File

@ -44,15 +44,6 @@ func resourceScalewayServer() *schema.Resource {
},
Optional: true,
},
"enable_ipv6": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"dynamic_ip_required": {
Type: schema.TypeBool,
Optional: true,
},
"security_group": {
Type: schema.TypeString,
Optional: true,
@ -80,12 +71,22 @@ func resourceScalewayServer() *schema.Resource {
},
},
},
"enable_ipv6": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"dynamic_ip_required": {
Type: schema.TypeBool,
Optional: true,
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
},
"public_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"public_ipv6": {
@ -168,6 +169,22 @@ func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {
}
err = waitForServerState(scaleway, id, "running")
if v, ok := d.GetOk("public_ip"); ok {
if ips, err := scaleway.GetIPS(); err != nil {
return err
} else {
for _, ip := range ips.IPS {
if ip.Address == v.(string) {
log.Printf("[DEBUG] Attaching IP %q to server %q\n", ip.ID, d.Id())
if err := scaleway.AttachIP(ip.ID, d.Id()); err != nil {
return err
}
break
}
}
}
}
}
if err != nil {
@ -257,6 +274,34 @@ func resourceScalewayServerUpdate(d *schema.ResourceData, m interface{}) error {
return fmt.Errorf("Failed patching scaleway server: %q", err)
}
if d.HasChange("public_ip") {
ips, err := scaleway.GetIPS()
if err != nil {
return err
}
if v, ok := d.GetOk("public_ip"); ok {
for _, ip := range ips.IPS {
if ip.Address == v.(string) {
log.Printf("[DEBUG] Attaching IP %q to server %q\n", ip.ID, d.Id())
if err := scaleway.AttachIP(ip.ID, d.Id()); err != nil {
return err
}
break
}
}
} else {
for _, ip := range ips.IPS {
if ip.Server != nil && ip.Server.Identifier == d.Id() {
log.Printf("[DEBUG] Detaching IP %q to server %q\n", ip.ID, d.Id())
if err := scaleway.DetachIP(ip.ID); err != nil {
return err
}
break
}
}
}
}
return resourceScalewayServerRead(d, m)
}

View File

@ -27,6 +27,35 @@ func TestAccScalewayServer_Basic(t *testing.T) {
"scaleway_server.base", "tags.0", "terraform-test"),
),
},
resource.TestStep{
Config: testAccCheckScalewayServerConfig_IPAttachment,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayServerIPAttachmentAttributes("scaleway_ip.base", "scaleway_server.base"),
),
},
resource.TestStep{
Config: testAccCheckScalewayServerConfig_IPDetachment,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayServerIPDetachmentAttributes("scaleway_server.base"),
),
},
},
})
}
func TestAccScalewayServer_ExistingIP(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckScalewayServerDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckScalewayServerConfig_IPAttachment,
Check: resource.ComposeTestCheckFunc(
testAccCheckScalewayServerExists("scaleway_server.base"),
testAccCheckScalewayServerIPAttachmentAttributes("scaleway_ip.base", "scaleway_server.base"),
),
},
},
})
}
@ -106,6 +135,52 @@ func testAccCheckScalewayServerDestroy(s *terraform.State) error {
return nil
}
func testAccCheckScalewayServerIPAttachmentAttributes(ipName, serverName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
ip, ok := s.RootModule().Resources[ipName]
if !ok {
return fmt.Errorf("Unknown scaleway_ip resource: %s", ipName)
}
server, ok := s.RootModule().Resources[serverName]
if !ok {
return fmt.Errorf("Unknown scaleway_server resource: %s", serverName)
}
client := testAccProvider.Meta().(*Client).scaleway
res, err := client.GetIP(ip.Primary.ID)
if err != nil {
return err
}
if res.IP.Server == nil || res.IP.Server.Identifier != server.Primary.ID {
return fmt.Errorf("IP %q is not attached to server %q", ip.Primary.ID, server.Primary.ID)
}
return nil
}
}
func testAccCheckScalewayServerIPDetachmentAttributes(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Unknown resource: %s", n)
}
client := testAccProvider.Meta().(*Client).scaleway
server, err := client.GetServer(rs.Primary.ID)
if err != nil {
return err
}
if server.PublicAddress.Identifier != "" {
return fmt.Errorf("Expected server to have no public IP but got %q", server.PublicAddress.Identifier)
}
return nil
}
}
func testAccCheckScalewayServerAttributes(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
@ -194,6 +269,27 @@ resource "scaleway_server" "base" {
tags = [ "terraform-test" ]
}`, armImageIdentifier)
var testAccCheckScalewayServerConfig_IPAttachment = fmt.Sprintf(`
resource "scaleway_ip" "base" {}
resource "scaleway_server" "base" {
name = "test"
# ubuntu 14.04
image = "%s"
type = "C1"
tags = [ "terraform-test" ]
public_ip = "${scaleway_ip.base.ip}"
}`, armImageIdentifier)
var testAccCheckScalewayServerConfig_IPDetachment = fmt.Sprintf(`
resource "scaleway_server" "base" {
name = "test"
# ubuntu 14.04
image = "%s"
type = "C1"
tags = [ "terraform-test" ]
}`, armImageIdentifier)
var testAccCheckScalewayServerVolumeConfig = fmt.Sprintf(`
resource "scaleway_server" "base" {
name = "test"