-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Added the data source for a Slack Workspace #37218
Merged
YakDriver
merged 5 commits into
hashicorp:main
from
madhavvishnubhatta:f-aws_chatbot_slack_workspace
May 23, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_chatbot_slack_workspace | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
internal/service/chatbot/slack_workspace_data_source.go
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,104 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package chatbot | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/chatbot" | ||
awstypes "github.com/aws/aws-sdk-go-v2/service/chatbot/types" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/create" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
// @FrameworkDataSource(name="Slack Workspace") | ||
func newDataSourceSlackWorkspace(context.Context) (datasource.DataSourceWithConfigure, error) { | ||
return &dataSourceSlackWorkspace{}, nil | ||
} | ||
|
||
const ( | ||
DSNameSlackWorkspace = "Slack Workspace Data Source" | ||
) | ||
|
||
type dataSourceSlackWorkspace struct { | ||
framework.DataSourceWithConfigure | ||
} | ||
|
||
func (d *dataSourceSlackWorkspace) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name | ||
resp.TypeName = "aws_chatbot_slack_workspace" | ||
} | ||
|
||
func (d *dataSourceSlackWorkspace) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"slack_team_id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"slack_team_name": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *dataSourceSlackWorkspace) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
conn := d.Meta().ChatbotClient(ctx) | ||
|
||
var data dataSourceSlackWorkspaceData | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
out, err := findSlackWorkspaceByName(ctx, conn, data.SlackTeamName.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
create.ProblemStandardMessage(names.Chatbot, create.ErrActionReading, DSNameSlackWorkspace, data.SlackTeamName.String(), err), | ||
err.Error(), | ||
) | ||
return | ||
} | ||
|
||
data.SlackTeamID = flex.StringToFramework(ctx, out.SlackTeamId) | ||
data.SlackTeamName = flex.StringToFramework(ctx, out.SlackTeamName) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func findSlackWorkspaceByName(ctx context.Context, conn *chatbot.Client, slack_team_name string) (*awstypes.SlackWorkspace, error) { | ||
input := &chatbot.DescribeSlackWorkspacesInput{ | ||
MaxResults: aws.Int32(10), | ||
} | ||
|
||
for { | ||
output, err := conn.DescribeSlackWorkspaces(ctx, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, workspace := range output.SlackWorkspaces { | ||
if aws.ToString(workspace.SlackTeamName) == slack_team_name { | ||
return &workspace, nil | ||
} | ||
} | ||
|
||
if output.NextToken == nil { | ||
break | ||
} | ||
input.NextToken = output.NextToken | ||
} | ||
// If we are here, then we need to return an error that the data source was not found. | ||
return nil, create.Error(names.Chatbot, "missing", DSNameSlackWorkspace, slack_team_name, nil) | ||
} | ||
|
||
type dataSourceSlackWorkspaceData struct { | ||
SlackTeamName types.String `tfsdk:"slack_team_name"` | ||
SlackTeamID types.String `tfsdk:"slack_team_id"` | ||
} |
58 changes: 58 additions & 0 deletions
58
internal/service/chatbot/slack_workspace_data_source_test.go
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 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package chatbot_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func TestAccChatbotSlackWorkspaceDataSource_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
// TIP: This is a long-running test guard for tests that run longer than | ||
// 300s (5 min) generally. | ||
if testing.Short() { | ||
t.Skip("skipping long-running test in short mode") | ||
} | ||
|
||
// The slack workspace must be created via the AWS Console. It cannot be created via APIs or Terraform. | ||
// Once it is created, export the name of the workspace in the env variable for this test | ||
key := "CHATBOT_SLACK_WORKSPACE_NAME" | ||
workspace_name := os.Getenv(key) | ||
if workspace_name == "" { | ||
t.Skipf("Environment variable %s is not set", key) | ||
} | ||
|
||
dataSourceName := "data.aws_chatbot_slack_workspace.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acctest.PreCheck(ctx, t) | ||
}, | ||
ErrorCheck: acctest.ErrorCheck(t, names.ChatbotServiceID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSlackWorkspaceDataSourceConfig_basic(workspace_name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(dataSourceName, "slack_team_name", workspace_name), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "slack_team_id"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSlackWorkspaceDataSourceConfig_basic(workspace_name string) string { | ||
return fmt.Sprintf(` | ||
data "aws_chatbot_slack_workspace" "test" { | ||
slack_team_name = "%[1]s" | ||
} | ||
`, workspace_name) | ||
} |
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,33 @@ | ||
--- | ||
subcategory: "Chatbot" | ||
layout: "aws" | ||
page_title: "AWS: aws_chatbot_slack_workspace" | ||
description: |- | ||
Terraform data source for managing an AWS Chatbot Slack Workspace. | ||
--- | ||
|
||
# Data Source: aws_chatbot_slack_workspace | ||
|
||
Terraform data source for managing an AWS Chatbot Slack Workspace. | ||
|
||
## Example Usage | ||
|
||
### Basic Usage | ||
|
||
```terraform | ||
data "aws_chatbot_slack_workspace" "example" { | ||
team_slack_name = "abc" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are required: | ||
|
||
* `slack_team_name` - (Required) Slack workspace name configured with AWS Chabot. | ||
|
||
## Attribute Reference | ||
|
||
This data source exports the following attributes in addition to the arguments above: | ||
|
||
* `slack_team_id` - ID of the Slack Workspace assigned by AWS Chatbot. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I can't readily test this, I won't make this change. However, I would encourage you to try it and, if it works, submit a PR to update this later. This simplifies code and should help make things more future proof. If it doesn't work, let us know so we can work on a fix.