Skip to content

Commit

Permalink
Add resource importer (#2)
Browse files Browse the repository at this point in the history
Add resources importer
  • Loading branch information
ccgagnon authored May 16, 2019
1 parent 537d29a commit 87b4ec7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ Download the appropriate binary from the GitHub release, and install it on your

Have a look at the `main.tf` file for a sample configuration using the provider.

#### Importing resources
Using the command `import` you need to follow this syntax.

For resources `environment` and `feature_flag` :
You need 2 values in the resource import ID separated by `:` .

The project key and the resource key.
e.g.: `import launchdarkly_environment.my-env critical-updates-dev:dev`

For the `project` resource you only need the project key. e.g.: `import launchdarkly_project.my-project critical-updates-dev`

## Building the provider
Clone the repository, and run `make` at the root of the working copy.
34 changes: 34 additions & 0 deletions launchdarkly/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package launchdarkly

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)

func parseCompositeID(id string) (p1 string, p2 string, err error) {
parts := strings.SplitN(id, ":", 2)
if len(parts) == 2 {
p1 = parts[0]
p2 = parts[1]
} else {
err = fmt.Errorf("error: Import composite ID requires two parts separated by colon, eg x:y")
}
return
}

type importFunc func(d *schema.ResourceData, meta interface{}) error

func resourceImport(readMethod importFunc, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
projectKey, resourceKey, err := parseCompositeID(d.Id())
if err != nil {
return nil, err
}
d.SetId(resourceKey)
d.Set("project_key", projectKey)
d.Set("key", resourceKey)

readMethod(d, meta)

return []*schema.ResourceData{d}, nil
}
10 changes: 9 additions & 1 deletion launchdarkly/resource_environment.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package launchdarkly

import (
"github.com/hashicorp/terraform/helper/schema"
"sync"

"github.com/hashicorp/terraform/helper/schema"
)

// Since we cannot delete the last environment in a project, we use a hack with a temporary dummy
Expand All @@ -17,6 +18,9 @@ func resourceEnvironment() *schema.Resource {
Read: resourceEnvironmentRead,
Update: resourceEnvironmentUpdate,
Delete: resourceEnvironmentDelete,
Importer: &schema.ResourceImporter{
State: resourceEnvironmentImport,
},

Schema: map[string]*schema.Schema{
"project_key": {
Expand Down Expand Up @@ -50,6 +54,10 @@ func resourceEnvironment() *schema.Resource {
}
}

func resourceEnvironmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return resourceImport(resourceEnvironmentRead, d, meta)
}

func resourceEnvironmentCreate(d *schema.ResourceData, m interface{}) error {
client := m.(Client)

Expand Down
7 changes: 7 additions & 0 deletions launchdarkly/resource_feature_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func resourceFeatureFlag() *schema.Resource {
Read: resourceFeatureFlagRead,
Update: resourceFeatureFlagUpdate,
Delete: resourceFeatureFlagDelete,
Importer: &schema.ResourceImporter{
State: resourceFeatureFlagImport,
},

Schema: map[string]*schema.Schema{
"project_key": {
Expand Down Expand Up @@ -70,6 +73,10 @@ func resourceFeatureFlag() *schema.Resource {
}
}

func resourceFeatureFlagImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return resourceImport(resourceFeatureFlagRead, d, meta)
}

func resourceFeatureFlagCreate(d *schema.ResourceData, m interface{}) error {
client := m.(Client)

Expand Down
13 changes: 13 additions & 0 deletions launchdarkly/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func resourceProject() *schema.Resource {
Read: resourceProjectRead,
Update: resourceProjectUpdate,
Delete: resourceProjectDelete,
Importer: &schema.ResourceImporter{
State: resourceProjectImport,
},

Schema: map[string]*schema.Schema{
"name": {
Expand All @@ -25,6 +28,16 @@ func resourceProject() *schema.Resource {
}
}

func resourceProjectImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {

d.SetId(d.Id())
d.Set("key", d.Id())

resourceProjectRead(d, meta)

return []*schema.ResourceData{d}, nil
}

func resourceProjectCreate(d *schema.ResourceData, m interface{}) error {
client := m.(Client)

Expand Down

0 comments on commit 87b4ec7

Please sign in to comment.