Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New resource: aws_grafana_workspace_api_key #25286

Merged
merged 51 commits into from
Aug 22, 2022

Conversation

meetreks
Copy link
Contributor

@meetreks meetreks commented Jun 12, 2022

Community Note

  • Please vote on this pull request by adding a 👍 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for pull request followers and do not help prioritize the request

Closes #25100.
Closes #26374.

$ make testacc PKG=grafana TESTS=TestAccGrafana_serial/ApiKey
Output from acceptance testing:
:~/terraform-provider-aws (f-aws_grafana_workspace_api_key) 
$ make testacc PKG=grafana TESTS=TestAccGrafana_serial/ApiKey ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./internal/service/grafana/... -v -count 1 -parallel 20 -run='TestAccGrafana_serial/ApiKey' -timeout 180m === RUN TestAccGrafana_serial === RUN TestAccGrafana_serial/ApiKey === RUN TestAccGrafana_serial/ApiKey/basic --- PASS: TestAccGrafana_serial (134.54s) --- PASS: TestAccGrafana_serial/ApiKey (134.54s) --- PASS: TestAccGrafana_serial/ApiKey/basic (134.54s) PASS ok github.com/hashicorp/terraform-provider-aws/internal/service/grafana 134.618s :~/terraform-provider-aws (f-aws_grafana_workspace_api_key) $

...

@github-actions github-actions bot added size/L Managed by automation to categorize the size of a PR. documentation Introduces or discusses updates to documentation. provider Pertains to the provider itself, rather than any interaction with AWS. service/grafana Issues and PRs that pertain to the grafana service. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. needs-triage Waiting for first response or review from a maintainer. labels Jun 12, 2022
@justinretzolk justinretzolk removed the needs-triage Waiting for first response or review from a maintainer. label Jun 17, 2022
Copy link
Collaborator

@AdamTylerLynch AdamTylerLynch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great pull request! I would recommend you review Resource Contribution Guide and verify that everything that can be implemented is implemented.

I have suggested some changes.


func ResourceWorkspaceApiKey() *schema.Resource {
return &schema.Resource{
Create: resourceWorkspaceApiKeyInsert,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method should be named *Create, not *Insert.

Suggested change
Create: resourceWorkspaceApiKeyInsert,
Create: resourceWorkspaceApiKeyCreate,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to Create as suggested

"key_name": {
Type: schema.TypeString,
Required: true,
//Computed: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove left over code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested

"key_role": {
Type: schema.TypeString,
Required: true,
//Computed: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove left over code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested

}
}

func resourceWorkspaceApiKeyInsert(d *schema.ResourceData, meta interface{}) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer to use the standard name of *Create.

Suggested change
func resourceWorkspaceApiKeyInsert(d *schema.ResourceData, meta interface{}) error {
func resourceWorkspaceApiKeyCreate(d *schema.ResourceData, meta interface{}) error {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to Create as suggested

"key_name": {
Type: schema.TypeString,
Required: true,
//Computed: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add validation that matches the API documentation.

Suggested change
//Computed: true,
ValidateFunc: validation.StringLenBetween(1, 100),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested

})
}

func testAccWorkspaceApiKeyProvider_basic(rName string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing in rName, but it is not used in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using rName in Test

}

func testAccWorkspaceApiKeyProvider_basic(rName string) string {
return fmt.Sprintf(`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary use of return fmt.Sprintf(, as no strings are being formatted/interpolated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed.

resource.TestCheckResourceAttrSet(resourceName, "key_name"),
resource.TestCheckResourceAttrSet(resourceName, "key_role"),
resource.TestCheckResourceAttrSet(resourceName, "seconds_to_live"),
resource.TestCheckResourceAttrPair(resourceName, "workspace_id", workspaceResourceName, "id"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an evaluation of the requested Key computed output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added as requested.


In addition to all arguments above, the following attributes are exported:

* `key` - The key token in JSON format. Use this value as a bearer token to authenticate HTTP requests to the workspace.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see how this value is exported as the _, err = conn.CreateWorkspaceApiKey(input) call is discarding the CreateWorkspaceApiKeyOutput.

Please verify?


## Import

Grafana Workspace API Key cannot be imported.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason that the API Key can not be imported.

Suggested change
Grafana Workspace API Key cannot be imported.
Grafana Workspace API Key can be imported using the `id`, e.g.,
```sh
$ terraform import aws_grafana_workspace_api_key.example workspace-id/keyname

-> Note: The key attribute cannot be imported as it is a token and is only available at create time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as Requested.

@AdamTylerLynch
Copy link
Collaborator

Please also include a copy/paste of AccTest ouutput, not a screenshot. Thank you!

@ewbankkit ewbankkit changed the title wip-For-Internal-AWS-Review New resource: aws_grafana_workspace_api_key Jul 1, 2022
@ewbankkit ewbankkit added the new-resource Introduces a new resource. label Jul 1, 2022
@meetreks
Copy link
Contributor Author

meetreks commented Jul 4, 2022

Acceptance test results
image

@meetreks meetreks force-pushed the f-aws_grafana_workspace_api_key branch from d0d15df to f495674 Compare July 4, 2022 20:31
@meetreks meetreks force-pushed the f-aws_grafana_workspace_api_key branch from a2b392e to aa5c4b2 Compare July 13, 2022 09:58
@meetreks meetreks force-pushed the f-aws_grafana_workspace_api_key branch from 68dfbd7 to 73cff8d Compare July 13, 2022 15:38
Copy link
Collaborator

@GlennChia GlennChia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR! Some changes requested in addition to Tyler's first review

})
}

func testAccWorkspaceAPIKeyConfig_basic(rName string, apiKey string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this test function, 2 parameters are not required, we should be using 1 parameter since only 1 parameter is used in the hcl definition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the extra parameter.

Comment on lines 24 to 26
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this resource does not support imports, this block can possibly be removed. A similar resource can be found here: https://github.com/hashicorp/terraform-provider-aws/blob/main/internal/service/kms/ciphertext.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted as Requested.

Comment on lines 28 to 31
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timeouts are not used in this resource and this block can be removed.

Suggested change
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted as Requested.

{
Config: testAccWorkspaceAPIKeyConfig_basic(rName, "test-api-1"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "key_name", "test-api-1"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hard-coding test-api-1, we can use rName.

Suggested change
resource.TestCheckResourceAttr(resourceName, "key_name", "test-api-1"),
resource.TestCheckResourceAttr(resourceName, "key_name", rName),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as suggested.

ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccWorkspaceAPIKeyConfig_basic(rName, "test-api-1"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove "test-api-1" since only rName is used in the HCL

Suggested change
Config: testAccWorkspaceAPIKeyConfig_basic(rName, "test-api-1"),
Config: testAccWorkspaceAPIKeyConfig_basic(rName),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as requested, and used just rName as the random name.

Config: testAccWorkspaceAPIKeyConfig_basic(rName, "test-api-1"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "key_name", "test-api-1"),
resource.TestCheckResourceAttr(resourceName, "key_role", "EDITIR"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be changed to use the value that the SDK provides

const (
    // RoleAdmin is a Role enum value
    RoleAdmin = "ADMIN"

    // RoleEditor is a Role enum value
    RoleEditor = "EDITOR"

    // RoleViewer is a Role enum value
    RoleViewer = "VIEWER"
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done as requested.

Comment on lines 32 to 33


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty lines can be removed


The following arguments are required:

* `editor_role_values` - (Required) The editor role values.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The valid values for Role can be defined here. From the documentation at https://docs.aws.amazon.com/grafana/latest/APIReference/API_CreateWorkspaceApiKey.html, the valid values are VIEWER|EDITOR|ADMIN


* `key` - The key token in JSON format. Use this value as a bearer token to authenticate HTTP requests to the workspace.

## Import
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since imports are not supported, this can be removed

@meetreks meetreks force-pushed the f-aws_grafana_workspace_api_key branch from a5a3662 to 6b6f3bd Compare July 25, 2022 05:23
@meetreks
Copy link
Contributor Author

:/terraform-provider-aws (f-aws_grafana_workspace_api_key) $ make testacc PKG=grafana TESTS=TestAccGrafana_serial/ApiKey
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/grafana/... -v -count 1 -parallel 20 -run='TestAccGrafana_serial/ApiKey' -timeout 180m
=== RUN TestAccGrafana_serial
=== RUN TestAccGrafana_serial/ApiKey
=== RUN TestAccGrafana_serial/ApiKey/basic
--- PASS: TestAccGrafana_serial (139.80s)
--- PASS: TestAccGrafana_serial/ApiKey (139.80s)
--- PASS: TestAccGrafana_serial/ApiKey/basic (139.80s)
PASS
ok github.com/hashicorp/terraform-provider-aws/internal/service/grafana 139.908s
:
/terraform-provider-aws (f-aws_grafana_workspace_api_key) $

@louismrose
Copy link

Is there more to be done on this change? Anything I could help with? We're keen to see this change land and stop managing Grafana API keys outside of Terraform!

This reverts commit 3e32805.
This reverts commit dc1c31f.
Copy link
Contributor

@ewbankkit ewbankkit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀.

% make testacc TESTARGS='-run=TestAccGrafana_serial/ApiKey' PKG=grafana
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./internal/service/grafana/... -v -count 1 -parallel 20  -run=TestAccGrafana_serial/ApiKey -timeout 180m
=== RUN   TestAccGrafana_serial
=== RUN   TestAccGrafana_serial/ApiKey
=== RUN   TestAccGrafana_serial/ApiKey/basic
--- PASS: TestAccGrafana_serial (135.92s)
    --- PASS: TestAccGrafana_serial/ApiKey (135.92s)
        --- PASS: TestAccGrafana_serial/ApiKey/basic (135.92s)
PASS
ok  	github.com/hashicorp/terraform-provider-aws/internal/service/grafana	139.938s

@ewbankkit
Copy link
Contributor

@meetreks Thanks for the contribution 🎉 👏.

@meetreks
Copy link
Contributor Author

@meetreks Thanks for the contribution 🎉 👏.

Thanks sir, this is really helpful.

@github-actions
Copy link

This functionality has been released in v4.28.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 26, 2022
@justinretzolk justinretzolk added the partner Contribution from a partner. label May 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Introduces or discusses updates to documentation. new-resource Introduces a new resource. partner Contribution from a partner. provider Pertains to the provider itself, rather than any interaction with AWS. service/grafana Issues and PRs that pertain to the grafana service. size/L Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add resource to create an API key for managed grafana r/grafana_workspace_api_key
8 participants