From fc4c848778a6b83d6d0182e30371d78486786c81 Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Sat, 15 Oct 2016 23:49:14 +0200 Subject: [PATCH] provider/scaleway: add importer support --- .../scaleway/import_scaleway_ip_test.go | 28 ++++++++++++++ .../import_scaleway_security_group_test.go | 28 ++++++++++++++ .../scaleway/import_scaleway_server_test.go | 28 ++++++++++++++ .../scaleway/import_scaleway_volume_test.go | 28 ++++++++++++++ .../scaleway/resource_scaleway_ip.go | 4 ++ .../resource_scaleway_security_group.go | 4 ++ ...ource_scaleway_security_group_rule_test.go | 37 +++++++++++-------- .../scaleway/resource_scaleway_server.go | 8 ++++ .../scaleway/resource_scaleway_volume.go | 4 ++ .../providers/scaleway/r/ip.html.markdown | 8 ++++ .../scaleway/r/security_group.html.markdown | 8 ++++ .../providers/scaleway/r/server.html.markdown | 8 ++++ .../providers/scaleway/r/volume.html.markdown | 8 ++++ 13 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 builtin/providers/scaleway/import_scaleway_ip_test.go create mode 100644 builtin/providers/scaleway/import_scaleway_security_group_test.go create mode 100644 builtin/providers/scaleway/import_scaleway_server_test.go create mode 100644 builtin/providers/scaleway/import_scaleway_volume_test.go diff --git a/builtin/providers/scaleway/import_scaleway_ip_test.go b/builtin/providers/scaleway/import_scaleway_ip_test.go new file mode 100644 index 000000000..3fac64ad0 --- /dev/null +++ b/builtin/providers/scaleway/import_scaleway_ip_test.go @@ -0,0 +1,28 @@ +package scaleway + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccScalewayIP_importBasic(t *testing.T) { + resourceName := "scaleway_ip.base" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckScalewayIPDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckScalewayIPConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/scaleway/import_scaleway_security_group_test.go b/builtin/providers/scaleway/import_scaleway_security_group_test.go new file mode 100644 index 000000000..4d886966e --- /dev/null +++ b/builtin/providers/scaleway/import_scaleway_security_group_test.go @@ -0,0 +1,28 @@ +package scaleway + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccScalewaySecurityGroup_importBasic(t *testing.T) { + resourceName := "scaleway_security_group.base" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckScalewaySecurityGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckScalewaySecurityGroupConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/scaleway/import_scaleway_server_test.go b/builtin/providers/scaleway/import_scaleway_server_test.go new file mode 100644 index 000000000..33f292f29 --- /dev/null +++ b/builtin/providers/scaleway/import_scaleway_server_test.go @@ -0,0 +1,28 @@ +package scaleway + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccScalewayServer_importBasic(t *testing.T) { + resourceName := "scaleway_server.base" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckScalewayServerDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckScalewayServerConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/scaleway/import_scaleway_volume_test.go b/builtin/providers/scaleway/import_scaleway_volume_test.go new file mode 100644 index 000000000..66b646e14 --- /dev/null +++ b/builtin/providers/scaleway/import_scaleway_volume_test.go @@ -0,0 +1,28 @@ +package scaleway + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccScalewayVolume_importBasic(t *testing.T) { + resourceName := "scaleway_volume.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckScalewayVolumeDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckScalewayVolumeConfig, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/scaleway/resource_scaleway_ip.go b/builtin/providers/scaleway/resource_scaleway_ip.go index bf20e76fc..96572e62b 100644 --- a/builtin/providers/scaleway/resource_scaleway_ip.go +++ b/builtin/providers/scaleway/resource_scaleway_ip.go @@ -13,6 +13,10 @@ func resourceScalewayIP() *schema.Resource { Read: resourceScalewayIPRead, Update: resourceScalewayIPUpdate, Delete: resourceScalewayIPDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ "server": &schema.Schema{ Type: schema.TypeString, diff --git a/builtin/providers/scaleway/resource_scaleway_security_group.go b/builtin/providers/scaleway/resource_scaleway_security_group.go index a3421dee8..e30c08620 100644 --- a/builtin/providers/scaleway/resource_scaleway_security_group.go +++ b/builtin/providers/scaleway/resource_scaleway_security_group.go @@ -14,6 +14,10 @@ func resourceScalewaySecurityGroup() *schema.Resource { Read: resourceScalewaySecurityGroupRead, Update: resourceScalewaySecurityGroupUpdate, Delete: resourceScalewaySecurityGroupDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, diff --git a/builtin/providers/scaleway/resource_scaleway_security_group_rule_test.go b/builtin/providers/scaleway/resource_scaleway_security_group_rule_test.go index bedebe971..8dd2647ec 100644 --- a/builtin/providers/scaleway/resource_scaleway_security_group_rule_test.go +++ b/builtin/providers/scaleway/resource_scaleway_security_group_rule_test.go @@ -15,7 +15,7 @@ func TestAccScalewaySecurityGroupRule_Basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccCheckScalewaySecurityGroupRuleDestroy(&group), + CheckDestroy: testAccCheckScalewaySecurityGroupRuleDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccCheckScalewaySecurityGroupRuleConfig, @@ -66,24 +66,31 @@ func testAccCheckScalewaySecurityGroupsExists(n string, group *api.ScalewaySecur } } -func testAccCheckScalewaySecurityGroupRuleDestroy(group *api.ScalewaySecurityGroups) func(*terraform.State) error { - return func(s *terraform.State) error { - client := testAccProvider.Meta().(*Client).scaleway +func testAccCheckScalewaySecurityGroupRuleDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*Client).scaleway - for _, rs := range s.RootModule().Resources { - if rs.Type != "scaleway" { - continue - } - - _, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID) - - if err == nil { - return fmt.Errorf("Security Group still exists") - } + for _, rs := range s.RootModule().Resources { + if rs.Type != "scaleway" { + continue } - return nil + groups, err := client.GetSecurityGroups() + if err != nil { + return err + } + + all_err := true + for _, group := range groups.SecurityGroups { + _, err := client.GetASecurityGroupRule(group.ID, rs.Primary.ID) + all_err = all_err && err != nil + } + + if !all_err { + return fmt.Errorf("Security Group still exists") + } } + + return nil } func testAccCheckScalewaySecurityGroupRuleAttributes(n string, group *api.ScalewaySecurityGroups) resource.TestCheckFunc { diff --git a/builtin/providers/scaleway/resource_scaleway_server.go b/builtin/providers/scaleway/resource_scaleway_server.go index 0266fcc9d..443bc7ec4 100644 --- a/builtin/providers/scaleway/resource_scaleway_server.go +++ b/builtin/providers/scaleway/resource_scaleway_server.go @@ -14,6 +14,10 @@ func resourceScalewayServer() *schema.Resource { Read: resourceScalewayServerRead, Update: resourceScalewayServerUpdate, Delete: resourceScalewayServerDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, @@ -138,6 +142,10 @@ func resourceScalewayServerRead(d *schema.ResourceData, m interface{}) error { return err } + d.Set("name", server.Name) + d.Set("image", server.Image.Identifier) + d.Set("type", server.CommercialType) + d.Set("enable_ipv6", server.EnableIPV6) d.Set("private_ip", server.PrivateIP) d.Set("public_ip", server.PublicAddress.IP) diff --git a/builtin/providers/scaleway/resource_scaleway_volume.go b/builtin/providers/scaleway/resource_scaleway_volume.go index 6090a52cc..57f3ef2e4 100644 --- a/builtin/providers/scaleway/resource_scaleway_volume.go +++ b/builtin/providers/scaleway/resource_scaleway_volume.go @@ -16,6 +16,10 @@ func resourceScalewayVolume() *schema.Resource { Read: resourceScalewayVolumeRead, Update: resourceScalewayVolumeUpdate, Delete: resourceScalewayVolumeDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, diff --git a/website/source/docs/providers/scaleway/r/ip.html.markdown b/website/source/docs/providers/scaleway/r/ip.html.markdown index c271d7abb..0148a8aa0 100644 --- a/website/source/docs/providers/scaleway/r/ip.html.markdown +++ b/website/source/docs/providers/scaleway/r/ip.html.markdown @@ -32,3 +32,11 @@ The following attributes are exported: * `id` - id of the new resource * `ip` - IP of the new resource + +## Import + +Instances can be imported using the `id`, e.g. + +``` +$ terraform import scaleway_ip.jump_host 5faef9cd-ea9b-4a63-9171-9e26bec03dbc +``` diff --git a/website/source/docs/providers/scaleway/r/security_group.html.markdown b/website/source/docs/providers/scaleway/r/security_group.html.markdown index f02567b28..298ebbe9d 100644 --- a/website/source/docs/providers/scaleway/r/security_group.html.markdown +++ b/website/source/docs/providers/scaleway/r/security_group.html.markdown @@ -34,3 +34,11 @@ Field `name`, `description` are editable. The following attributes are exported: * `id` - id of the new resource + +## Import + +Instances can be imported using the `id`, e.g. + +``` +$ terraform import scaleway_security_group.test 5faef9cd-ea9b-4a63-9171-9e26bec03dbc +``` diff --git a/website/source/docs/providers/scaleway/r/server.html.markdown b/website/source/docs/providers/scaleway/r/server.html.markdown index d03f70df5..c73a6c833 100644 --- a/website/source/docs/providers/scaleway/r/server.html.markdown +++ b/website/source/docs/providers/scaleway/r/server.html.markdown @@ -43,3 +43,11 @@ The following attributes are exported: * `id` - id of the new resource * `private_ip` - private ip of the new resource * `public_ip` - public ip of the new resource + +## Import + +Instances can be imported using the `id`, e.g. + +``` +$ terraform import scaleway_server.web 5faef9cd-ea9b-4a63-9171-9e26bec03dbc +``` diff --git a/website/source/docs/providers/scaleway/r/volume.html.markdown b/website/source/docs/providers/scaleway/r/volume.html.markdown index 3041bbced..b1c3201ef 100644 --- a/website/source/docs/providers/scaleway/r/volume.html.markdown +++ b/website/source/docs/providers/scaleway/r/volume.html.markdown @@ -42,3 +42,11 @@ The following arguments are supported: The following attributes are exported: * `id` - id of the new resource + +## Import + +Instances can be imported using the `id`, e.g. + +``` +$ terraform import scaleway_volume.test 5faef9cd-ea9b-4a63-9171-9e26bec03dbc +```