Skip to content

Commit

Permalink
chatbot/slack_chan_conf: All needed to not have 'id'
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Jul 31, 2024
1 parent fee064c commit eda11ee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
19 changes: 7 additions & 12 deletions internal/service/chatbot/slack_channel_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
awstypes "github.com/aws/aws-sdk-go-v2/service/chatbot/types"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
Expand Down Expand Up @@ -48,7 +49,6 @@ func newSlackChannelConfigurationResource(_ context.Context) (resource.ResourceW

type slackChannelConfigurationResource struct {
framework.ResourceWithConfigure
framework.WithImportByID
framework.WithTimeouts
}

Expand All @@ -74,7 +74,6 @@ func (r *slackChannelConfigurationResource) Schema(ctx context.Context, request
names.AttrIAMRoleARN: schema.StringAttribute{
Required: true,
},
names.AttrID: framework.IDAttribute(),
"logging_level": schema.StringAttribute{
CustomType: fwtypes.StringEnumType[loggingLevel](),
Optional: true,
Expand Down Expand Up @@ -159,7 +158,6 @@ func (r *slackChannelConfigurationResource) Create(ctx context.Context, request

// Set values for unknowns
data.ChatConfigurationARN = fwflex.StringToFramework(ctx, output.ChatConfigurationArn)
data.setID()
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
if response.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -266,7 +264,7 @@ func (r *slackChannelConfigurationResource) Delete(ctx context.Context, request
conn := r.Meta().ChatbotClient(ctx)

tflog.Debug(ctx, "deleting Chatbot Slack Channel Configuration", map[string]interface{}{
names.AttrID: data.ChatConfigurationARN.ValueString(),
"chat_configuration_arn": data.ChatConfigurationARN.ValueString(),
})

input := &chatbot.DeleteSlackChannelConfigurationInput{
Expand Down Expand Up @@ -296,6 +294,10 @@ func (r *slackChannelConfigurationResource) ModifyPlan(ctx context.Context, requ
r.SetTagsAll(ctx, request, response)
}

func (r *slackChannelConfigurationResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("chat_configuration_arn"), request, response)
}

func findSlackChannelConfiguration(ctx context.Context, conn *chatbot.Client, input *chatbot.DescribeSlackChannelConfigurationsInput) (*awstypes.SlackChannelConfiguration, error) {
output, err := findSlackChannelConfigurations(ctx, conn, input)

Expand Down Expand Up @@ -400,7 +402,6 @@ type slackChannelConfigurationResourceModel struct {
ConfigurationName types.String `tfsdk:"configuration_name"`
GuardrailPolicyARNs types.List `tfsdk:"guardrail_policy_arns"`
IAMRoleARN types.String `tfsdk:"iam_role_arn"`
ID types.String `tfsdk:"id"`
LoggingLevel fwtypes.StringEnum[loggingLevel] `tfsdk:"logging_level"`
SlackChannelID types.String `tfsdk:"slack_channel_id"`
SlackChannelName types.String `tfsdk:"slack_channel_name"`
Expand All @@ -414,20 +415,14 @@ type slackChannelConfigurationResourceModel struct {
}

func (data *slackChannelConfigurationResourceModel) InitFromID() error {
_, err := arn.Parse(data.ID.ValueString())
_, err := arn.Parse(data.ChatConfigurationARN.ValueString())
if err != nil {
return err
}

data.ChatConfigurationARN = data.ID

return nil
}

func (data *slackChannelConfigurationResourceModel) setID() {
data.ID = data.ChatConfigurationARN
}

type loggingLevel string

const (
Expand Down
30 changes: 19 additions & 11 deletions internal/service/chatbot/slack_channel_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ func testAccSlackChannelConfiguration_basic(t *testing.T) {
),
},
{
ResourceName: testResourceSlackChannelConfiguration,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{},
ResourceName: testResourceSlackChannelConfiguration,
ImportState: true,
ImportStateIdFunc: testAccSlackChannelConfigurationImportStateIDFunc(testResourceSlackChannelConfiguration),
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: "chat_configuration_arn",
},
},
})
Expand Down Expand Up @@ -132,10 +133,10 @@ func testAccCheckSlackChannelConfigurationDestroy(ctx context.Context) resource.
}

if err != nil {
return create.Error(names.Chatbot, create.ErrActionCheckingDestroyed, tfchatbot.ResNameSlackChannelConfiguration, rs.Primary.ID, err)
return err
}

return create.Error(names.Chatbot, create.ErrActionCheckingDestroyed, tfchatbot.ResNameSlackChannelConfiguration, rs.Primary.ID, errors.New("not destroyed"))
return create.Error(names.Chatbot, create.ErrActionCheckingDestroyed, tfchatbot.ResNameSlackChannelConfiguration, rs.Primary.Attributes["chat_configuration_arn"], errors.New("not destroyed"))
}

return nil
Expand All @@ -149,16 +150,12 @@ func testAccCheckSlackChannelConfigurationExists(ctx context.Context, name strin
return create.Error(names.Chatbot, create.ErrActionCheckingExistence, tfchatbot.ResNameSlackChannelConfiguration, name, errors.New("not found"))
}

if rs.Primary.ID == "" {
return create.Error(names.Chatbot, create.ErrActionCheckingExistence, tfchatbot.ResNameSlackChannelConfiguration, name, errors.New("not set"))
}

conn := acctest.Provider.Meta().(*conns.AWSClient).ChatbotClient(ctx)

output, err := tfchatbot.FindSlackChannelConfigurationByARN(ctx, conn, rs.Primary.Attributes["chat_configuration_arn"])

if err != nil {
return create.Error(names.Chatbot, create.ErrActionCheckingExistence, tfchatbot.ResNameSlackChannelConfiguration, rs.Primary.ID, err)
return err
}

*slackchannelconfiguration = *output
Expand All @@ -180,6 +177,17 @@ func testAccPreCheck(ctx context.Context, t *testing.T) {
}
}

func testAccSlackChannelConfigurationImportStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return rs.Primary.Attributes["chat_configuration_arn"], nil
}
}

func testAccSlackChannelConfigurationConfig_basic(rName, channelID, teamID string) string {
return fmt.Sprintf(`
data "aws_iam_policy_document" "assume_role" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ The following arguments are optional:
This resource exports the following attributes in addition to the arguments above:

* `chat_configuration_arn` - ARN of the Slack channel configuration.
* `id` - ID (ARN) of the Slack channel configuration.
* `slack_channel_name` - Name of the Slack channel.
* `slack_team_name` - Name of the Slack team.
* `tags_all` - Map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).
Expand Down

0 comments on commit eda11ee

Please sign in to comment.