这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/data-sources/all_usergroups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "slack_all_usergroups Data Source - slack"
subcategory: ""
description: |-
Retrieve all Slack user groups.
---

# slack_all_usergroups (Data Source)

Retrieve all Slack user groups.



<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `total_usergroups` (Number) Total number of user groups retrieved.
- `usergroups` (Attributes List) List of Slack user groups. (see [below for nested schema](#nestedatt--usergroups))

<a id="nestedatt--usergroups"></a>
### Nested Schema for `usergroups`

Read-Only:

- `channels` (List of String) Channels shared by the user group.
- `description` (String) Description of the user group.
- `handle` (String) Handle of the user group (unique identifier).
- `id` (String) User group's Slack ID.
- `name` (String) Name of the user group.
- `users` (List of String) List of user IDs in the user group.
30 changes: 30 additions & 0 deletions docs/data-sources/all_users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "slack_all_users Data Source - slack"
subcategory: ""
description: |-

---

# slack_all_users (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `total_users` (Number) Number of users returned.
- `users` (Attributes List) List of activated and non-bot Slack users. (see [below for nested schema](#nestedatt--users))

<a id="nestedatt--users"></a>
### Nested Schema for `users`

Read-Only:

- `email` (String) User's email address.
- `id` (String) User's Slack ID.
- `name` (String) User's name.
25 changes: 25 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "slack_user Data Source - slack"
subcategory: ""
description: |-
Retrieve Slack user information. Either id or email must be specified, but not both.
---

# slack_user (Data Source)

Retrieve Slack user information. Either `id` or `email` must be specified, but not both.



<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `email` (String) Email of the user to look up.
- `id` (String) Slack user ID to look up.

### Read-Only

- `name` (String) User's name.
27 changes: 0 additions & 27 deletions docs/data-sources/user_data.md

This file was deleted.

4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ provider "scaffolding" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required
### Optional

- `slack_token` (String, Sensitive) Slack token to authenticate API calls.
- `slack_token` (String, Sensitive) Slack token to authenticate API calls. Can also be set with the `SLACK_TOKEN` environment variable.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.4

require (
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0
github.com/hashicorp/terraform-plugin-go v0.25.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/hashicorp/terraform-json v0.23.0 h1:sniCkExU4iKtTADReHzACkk8fnpQXrdD2
github.com/hashicorp/terraform-json v0.23.0/go.mod h1:MHdXbBAbSg0GvzuWazEGKAn/cyNfIB7mN6y7KJN6y2c=
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0 h1:O9QqGoYDzQT7lwTXUsZEtgabeWW96zUBh47Smn2lkFA=
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
github.com/hashicorp/terraform-plugin-go v0.25.0/go.mod h1:+SYagMYadJP86Kvn+TGeV+ofr/R3g4/If0O5sO96MVw=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
155 changes: 155 additions & 0 deletions internal/provider/data_source_all_usergroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"fmt"

"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-plugin-log/tflog"
"github.com/slack-go/slack"
)

var _ datasource.DataSource = &AllUserGroupsDataSource{}

func NewAllUserGroupsDataSource() datasource.DataSource {
return &AllUserGroupsDataSource{}
}

type AllUserGroupsDataSource struct {
client *slack.Client
}

type AllUserGroupsDataSourceModel struct {
TotalUserGroups types.Int64 `tfsdk:"total_usergroups"`
UserGroups []AllUserGroupsDataSourceGroupItem `tfsdk:"usergroups"`
}

type AllUserGroupsDataSourceGroupItem struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
Handle types.String `tfsdk:"handle"`
Channels []types.String `tfsdk:"channels"`
Users []types.String `tfsdk:"users"`
}

func (d *AllUserGroupsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_all_usergroups"
}

func (d *AllUserGroupsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Retrieve all Slack user groups.",
Attributes: map[string]schema.Attribute{
"total_usergroups": schema.Int64Attribute{
Description: "Total number of user groups retrieved.",
Computed: true,
},
"usergroups": schema.ListNestedAttribute{
Description: "List of Slack user groups.",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "User group's Slack ID.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "Name of the user group.",
Computed: true,
},
"description": schema.StringAttribute{
Description: "Description of the user group.",
Computed: true,
},
"handle": schema.StringAttribute{
Description: "Handle of the user group (unique identifier).",
Computed: true,
},
"channels": schema.ListAttribute{
Description: "Channels shared by the user group.",
ElementType: types.StringType,
Computed: true,
},
"users": schema.ListAttribute{
Description: "List of user IDs in the user group.",
ElementType: types.StringType,
Computed: true,
},
},
},
},
},
}
}

func (d *AllUserGroupsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

providerData, ok := req.ProviderData.(*SlackProviderData)
if !ok || providerData.Client == nil {
resp.Diagnostics.AddError(
"Invalid Provider Data",
fmt.Sprintf("Expected *SlackProviderData with initialized client, got: %T", req.ProviderData),
)
return
}

d.client = providerData.Client
}

func (d *AllUserGroupsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data AllUserGroupsDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

userGroups, err := d.client.GetUserGroups(slack.GetUserGroupsOptionIncludeUsers(true))
if err != nil {
resp.Diagnostics.AddError(
"Client Error",
fmt.Sprintf("Unable to fetch Slack user groups: %s", err),
)
return
}

tflog.Trace(ctx, "Fetched Slack user groups", map[string]any{"total_usergroups": len(userGroups)})

var resultingList []AllUserGroupsDataSourceGroupItem
for _, group := range userGroups {
groupItem := AllUserGroupsDataSourceGroupItem{
ID: types.StringValue(group.ID),
Name: types.StringValue(group.Name),
Description: types.StringValue(group.Description),
Handle: types.StringValue(group.Handle),
}

channels := make([]types.String, len(group.Prefs.Channels))
for i, ch := range group.Prefs.Channels {
channels[i] = types.StringValue(ch)
}
groupItem.Channels = channels

users := make([]types.String, len(group.Users))
for i, u := range group.Users {
users[i] = types.StringValue(u)
}
groupItem.Users = users

resultingList = append(resultingList, groupItem)
}

data.UserGroups = resultingList
data.TotalUserGroups = types.Int64Value(int64(len(resultingList)))

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading
Loading