diff --git a/rundeck/import_resource_project_test.go b/rundeck/import_resource_project_test.go new file mode 100644 index 000000000..5fba7faf8 --- /dev/null +++ b/rundeck/import_resource_project_test.go @@ -0,0 +1,32 @@ +package rundeck + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/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 273cbd444..ad732ffe6 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 +} diff --git a/website/docs/r/project.html.md b/website/docs/r/project.html.md index f80bd9d4e..7d5f3d7a7 100644 --- a/website/docs/r/project.html.md +++ b/website/docs/r/project.html.md @@ -90,3 +90,11 @@ The following attributes are exported: * `name` - The unique name that identifies the project, as set in the arguments. * `ui_url` - The URL of the index page for this project in the Rundeck UI. + +## Import + +Rundeck Project can be imported using the `name`, e.g. + +``` +$ terraform import rundeck_project.main project-name +```