-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: ` * **New Data Source:** `aws_credentials` ` Output from acceptance testing: ` make testacc TESTARGS='-run=TestAccAWSCredentials_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSCredentials_ -timeout 120m === RUN TestAccAWSCredentials_basic === PAUSE TestAccAWSCredentials_basic === CONT TestAccAWSCredentials_basic --- PASS: TestAccAWSCredentials_basic (62.60s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 64.457s `
- Loading branch information
1 parent
65cbd23
commit d8bef0b
Showing
6 changed files
with
173 additions
and
0 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,58 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceAwsCredentials() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsCredentialsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"access_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"secret_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
|
||
"token": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsCredentialsRead(d *schema.ResourceData, meta interface{}) error { | ||
providerCredentials := meta.(*AWSClient).credentials | ||
|
||
log.Printf("[DEBUG] Reading Provider Credentials") | ||
|
||
val, err := providerCredentials.Get() | ||
if err != nil { | ||
return fmt.Errorf("Error getting Provider Credentials: %v", err) | ||
} | ||
|
||
log.Printf("[DEBUG] Received Provider Credentials: %s", val.ProviderName) | ||
|
||
d.SetId(val.ProviderName) | ||
|
||
if val.HasKeys() { | ||
log.Printf("[DEBUG] Received provider has both AccessKeyID and SecretAccessKey value set") | ||
d.Set("access_key", val.AccessKeyID) | ||
d.Set("secret_key", val.SecretAccessKey) | ||
} | ||
|
||
d.Set("token", val.SessionToken) | ||
|
||
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,60 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccAWSCredentials_basic(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckAwsCredentialsConfig_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsCredentials("data.aws_credentials.current"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsCredentials(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Can't find credentials resource: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("credentials resource ID not set.") | ||
} | ||
|
||
expected, err := testAccProvider.Meta().(*AWSClient).credentials.Get() | ||
if err != nil { | ||
return fmt.Errorf("Error getting test Provider Credentials: %v", err) | ||
} | ||
|
||
if rs.Primary.Attributes["access_key"] != expected.AccessKeyID { | ||
return fmt.Errorf("Incorrect access_key: expected %q, got %q", expected.AccessKeyID, rs.Primary.Attributes["access_key"]) | ||
} | ||
|
||
if rs.Primary.Attributes["secret_key"] != expected.SecretAccessKey { | ||
return fmt.Errorf("Incorrect secret_key: expected %q, got %q", expected.SecretAccessKey, rs.Primary.Attributes["secret_key"]) | ||
} | ||
|
||
if rs.Primary.Attributes["token"] != expected.SessionToken { | ||
return fmt.Errorf("Incorrect token: expected %q, got %q", expected.SessionToken, rs.Primary.Attributes["token"]) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccCheckAwsCredentialsConfig_basic = ` | ||
data "aws_credentials" "current" {} | ||
` |
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,49 @@ | ||
--- | ||
subcategory: "" | ||
layout: "aws" | ||
page_title: "AWS: aws_credentials" | ||
description: |- | ||
Get the credentials of the configured provider. | ||
--- | ||
|
||
# Data Source: aws_arn | ||
|
||
use the `aws_credentials` data source to get access to the AWS credentials of a configured provider. | ||
|
||
~> **Note:** All attributes will be stored in | ||
the raw state as plain-text. [Read more about sensitive data in | ||
state](/docs/state/sensitive-data.html). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_credentials" "current" {} | ||
output "access_key" { | ||
value = data.aws_credentials.current.access_key | ||
} | ||
output "secret_key" { | ||
sensitive = true | ||
value = data.aws_credentials.current.secret_key | ||
} | ||
output "token" { | ||
sensitive = true | ||
value = data.aws_credentials.current.token | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
There are no arguments available for this data source. | ||
|
||
## Attributes Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `access_key` - The AWS access key part of the credentials. | ||
|
||
* `secret_key` - The AWS secret access key part of the credentials. | ||
|
||
* `token` - The AWS session token part of the credentials. |