From 47fb70fa77e89680eb20768a5ebac5b7ace49a2f Mon Sep 17 00:00:00 2001 From: Evan Farrell Date: Thu, 20 Feb 2020 16:47:15 -0500 Subject: [PATCH] import project test and func. --- rundeck/import_resource_project_test.go | 32 +++++++++++++++++++++++++ rundeck/resource_project.go | 23 ++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 rundeck/import_resource_project_test.go diff --git a/rundeck/import_resource_project_test.go b/rundeck/import_resource_project_test.go new file mode 100644 index 0000000000..526f77b11f --- /dev/null +++ b/rundeck/import_resource_project_test.go @@ -0,0 +1,32 @@ +package rundeck + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/rundeck/go-rundeck/rundeck" +) + +func TestAccProject_Import(t *testing.T) { + name := "rundeck_project.main" + project_name := "terraform-acc-test-basic" + var project rundeck.Project + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccProjectCheckDestroy(&project), + Steps: []resource.TestStep{ + { + Config: testAccProjectConfig_basic, + Check: testAccProjectCheckExists(name, &project), + }, + { + ResourceName: name, + ImportStateId: project_name, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/rundeck/resource_project.go b/rundeck/resource_project.go index 273cbd4442..ad732ffe61 100644 --- a/rundeck/resource_project.go +++ b/rundeck/resource_project.go @@ -29,6 +29,9 @@ func resourceRundeckProject() *schema.Resource { Delete: DeleteProject, Exists: ProjectExists, Read: ReadProject, + Importer: &schema.ResourceImporter{ + State: resourceProjectImport, + }, Schema: map[string]*schema.Schema{ "name": { @@ -307,3 +310,23 @@ func DeleteProject(d *schema.ResourceData, meta interface{}) error { return err } + +func resourceProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + name := d.Id() + + ok, err := ProjectExists(d, meta) + if err != nil { + return nil, err + } + if !ok { + return nil, fmt.Errorf("Project doesn't exist. Please try again.") + } + d.SetId(name) + + err = ReadProject(d, meta) + if err != nil { + return nil, err + } + + return []*schema.ResourceData{d}, nil +}