This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from secrethub/release/v1.3.0
Release v1.3.0
- Loading branch information
Showing
12 changed files
with
303 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package secrethub | ||
|
||
import "github.com/hashicorp/terraform/helper/schema" | ||
|
||
func dataSourceDir() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceDirRead, | ||
Schema: map[string]*schema.Schema{ | ||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The path of the directory", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDirRead(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
|
||
path := d.Get("path").(string) | ||
|
||
_, err := client.Dirs().GetTree(path, 0, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(path) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package secrethub | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceDir(t *testing.T) { | ||
cases := map[string]struct { | ||
config string | ||
}{ | ||
"repo root directory": { | ||
config: fmt.Sprintf(` | ||
data "secrethub_dir" "repo" { | ||
path = "%v" | ||
} | ||
`, testAcc.repoPath), | ||
}, | ||
"subdirectory": { | ||
config: fmt.Sprintf(` | ||
data "secrethub_dir" "repo" { | ||
path = "%v" | ||
} | ||
resource "secrethub_dir" "subdir" { | ||
path = "${data.secrethub_dir.repo.path}/subdir" | ||
} | ||
data "secrethub_dir" "subdir" { | ||
path = secrethub_dir.subdir.path | ||
} | ||
`, testAcc.repoPath), | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
PreCheck: testAccPreCheck(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: tc.config, | ||
}, | ||
}, | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package secrethub | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/secrethub/secrethub-go/internals/api" | ||
) | ||
|
||
func resourceDir() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceDirCreate, | ||
Read: resourceDirRead, | ||
Update: resourceDirUpdate, | ||
Delete: resourceDirDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: resourceDirImport, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "The path of the directory.", | ||
}, | ||
"force_destroy": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "Whether to allow deleting this directory if it's not empty. When set to `false`, you'll get an error when trying to delete the directory if it still contains directories or secrets.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceDirCreate(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
|
||
path := d.Get("path").(string) | ||
|
||
_, err := client.Dirs().Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(path) | ||
|
||
return resourceDirRead(d, m) | ||
} | ||
|
||
func resourceDirRead(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
|
||
path := d.Id() | ||
|
||
_, err := client.Dirs().GetTree(path, 0, true) | ||
if api.IsErrNotFound(err) { | ||
// The directory was deleted outside of the current Terraform workspace, so invalidate this resource | ||
d.SetId("") | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error fetching directory: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceDirUpdate(d *schema.ResourceData, m interface{}) error { | ||
return resourceDirRead(d, m) | ||
} | ||
|
||
func resourceDirDelete(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
|
||
path := d.Id() | ||
|
||
forceDestroy := d.Get("force_destroy").(bool) | ||
|
||
if !forceDestroy { | ||
tree, err := client.Dirs().GetTree(path, 1, false) | ||
if api.IsErrNotFound(err) { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if len(tree.Dirs) > 1 || len(tree.Secrets) > 0 { | ||
return fmt.Errorf("cannot remove directory %s: it is not empty", path) | ||
} | ||
} | ||
|
||
err := client.Dirs().Delete(path) | ||
if api.IsErrNotFound(err) { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
func resourceDirImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { | ||
path := d.Id() | ||
|
||
err := api.ValidateDirPath(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = d.Set("path", path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} |
Oops, something went wrong.