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 #6 from keylockerbv/secret-datasource
Data source secrethub_secret
- Loading branch information
Showing
9 changed files
with
301 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package secrethub | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceSecret() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceSecretRead, | ||
Schema: map[string]*schema.Schema{ | ||
"path": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The path where the secret is stored. To use a specific version, append the version number to the path, separated by a colon (path:version). Defaults to the latest version.", | ||
}, | ||
"path_prefix": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Overrides the `path_prefix` defined in the provider.", | ||
}, | ||
"version": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "The version of the secret.", | ||
}, | ||
"data": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
Description: "The secret contents.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSecretRead(d *schema.ResourceData, m interface{}) error { | ||
provider := m.(providerMeta) | ||
client := *provider.client | ||
|
||
path, err := getSecretPath(d, &provider) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
secret, err := client.Secrets().Versions().GetWithData(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("data", string(secret.Data)) | ||
d.Set("version", secret.Version) | ||
|
||
d.SetId(string(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,141 @@ | ||
package secrethub | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceSecret_absPath(t *testing.T) { | ||
config := fmt.Sprintf(` | ||
provider "secrethub" { | ||
credential = "${file("~/.secrethub/credential")}" | ||
} | ||
resource "secrethub_secret" "%v" { | ||
path = "%v" | ||
data = "secretpassword" | ||
} | ||
data "secrethub_secret" "%v" { | ||
path = "${secrethub_secret.%v.path}" | ||
} | ||
`, testAcc.secretName, testAcc.path, testAcc.secretName, testAcc.secretName) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
PreCheck: testAccPreCheck(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
fmt.Sprintf("data.secrethub_secret.%v", testAcc.secretName), | ||
"path", | ||
string(testAcc.path), | ||
), | ||
resource.TestCheckResourceAttr( | ||
fmt.Sprintf("data.secrethub_secret.%v", testAcc.secretName), | ||
"data", | ||
"secretpassword", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceSecret_absPathVersioned(t *testing.T) { | ||
configInit := fmt.Sprintf(` | ||
provider "secrethub" { | ||
credential = "${file("~/.secrethub/credential")}" | ||
} | ||
resource "secrethub_secret" "%v" { | ||
path = "%v" | ||
data = "secretpasswordv1" | ||
} | ||
data "secrethub_secret" "%v" { | ||
path = "${secrethub_secret.%v.path}:1" | ||
} | ||
`, testAcc.secretName, testAcc.path, testAcc.secretName, testAcc.secretName) | ||
|
||
configVersioned := fmt.Sprintf(` | ||
provider "secrethub" { | ||
credential = "${file("~/.secrethub/credential")}" | ||
} | ||
resource "secrethub_secret" "%v" { | ||
path = "%v" | ||
data = "secretpasswordv2" | ||
} | ||
data "secrethub_secret" "%v" { | ||
path = "${secrethub_secret.%v.path}:1" | ||
} | ||
`, testAcc.secretName, testAcc.path, testAcc.secretName, testAcc.secretName) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
PreCheck: testAccPreCheck(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configInit, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
fmt.Sprintf("data.secrethub_secret.%v", testAcc.secretName), | ||
"data", | ||
"secretpasswordv1", | ||
), | ||
), | ||
}, | ||
{ | ||
Config: configVersioned, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
fmt.Sprintf("data.secrethub_secret.%v", testAcc.secretName), | ||
"data", | ||
"secretpasswordv1", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceSecret_prefPath(t *testing.T) { | ||
config := fmt.Sprintf(` | ||
provider "secrethub" { | ||
credential = "${file("~/.secrethub/credential")}" | ||
path_prefix = "%v/%v" | ||
} | ||
resource "secrethub_secret" "%v" { | ||
path = "%v" | ||
data = "secretpassword" | ||
} | ||
data "secrethub_secret" "%v" { | ||
path = "${secrethub_secret.%v.path}" | ||
} | ||
`, testAcc.namespace, testAcc.repository, testAcc.secretName, testAcc.secretName, testAcc.secretName, testAcc.secretName) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: testAccProviders, | ||
PreCheck: testAccPreCheck(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
fmt.Sprintf("data.secrethub_secret.%v", testAcc.secretName), | ||
"data", | ||
"secretpassword", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.