Skip to content

Commit

Permalink
feat: define client interface; feat: implement rate limit decorator;
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei-Alexandru Dobre committed Jan 7, 2025
1 parent ce11382 commit ce3c127
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 7 deletions.
4 changes: 3 additions & 1 deletion internal/provider/data_source_all_usergroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"fmt"

"github.com/essent/terraform-provider-slack/internal/slackExt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -21,7 +23,7 @@ func NewAllUserGroupsDataSource() datasource.DataSource {
}

type AllUserGroupsDataSource struct {
client *slack.Client
client slackExt.Client
}

type AllUserGroupsDataSourceModel struct {
Expand Down
5 changes: 3 additions & 2 deletions internal/provider/data_source_all_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"context"
"fmt"

"github.com/essent/terraform-provider-slack/internal/slackExt"

"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 = &AllUsersDataSource{}
Expand All @@ -21,7 +22,7 @@ func NewAllUsersDataSource() datasource.DataSource {
}

type AllUsersDataSource struct {
client *slack.Client
client slackExt.Client
}

type AllUsersDataSourceModel struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/provider/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"fmt"

"github.com/essent/terraform-provider-slack/internal/slackExt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand All @@ -24,7 +26,7 @@ func NewUserDataSource() datasource.DataSource {
}

type UserDataSource struct {
client *slack.Client
client slackExt.Client
}

type UserDataSourceModel struct {
Expand Down
8 changes: 5 additions & 3 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"fmt"
"os"

"github.com/essent/terraform-provider-slack/internal/slackExt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/provider"
Expand All @@ -30,7 +32,7 @@ type SlackProviderModel struct {
}

type SlackProviderData struct {
Client *slack.Client
Client slackExt.Client
}

func (p *SlackProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand Down Expand Up @@ -83,8 +85,8 @@ func (p *SlackProvider) Configure(ctx context.Context, req provider.ConfigureReq
return
}

resp.DataSourceData = &SlackProviderData{Client: client}
resp.ResourceData = &SlackProviderData{Client: client}
resp.DataSourceData = &SlackProviderData{Client: slackExt.New(client)}
resp.ResourceData = &SlackProviderData{Client: slackExt.New(client)}
}

func (p *SlackProvider) Resources(ctx context.Context) []func() resource.Resource {
Expand Down
18 changes: 18 additions & 0 deletions internal/slackExt/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package slackExt

import (
"context"

"github.com/slack-go/slack"
)

type Client interface {
GetUserInfo(user string) (*slack.User, error)
GetUserByEmail(email string) (*slack.User, error)
GetUsersContext(ctx context.Context) ([]slack.User, error)
GetUserGroups(options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error)
}

func New(base *slack.Client) Client {
return &clientRateLimit{&clientImpl{base}}
}
27 changes: 27 additions & 0 deletions internal/slackExt/client_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package slackExt

import (
"context"

"github.com/slack-go/slack"
)

type clientImpl struct {
base *slack.Client
}

func (c *clientImpl) GetUserInfo(user string) (*slack.User, error) {
return c.base.GetUserInfo(user)
}

func (c *clientImpl) GetUserByEmail(email string) (*slack.User, error) {
return c.base.GetUserByEmail(email)
}

func (c *clientImpl) GetUsersContext(ctx context.Context) ([]slack.User, error) {
return c.base.GetUsersContext(ctx)
}

func (c *clientImpl) GetUserGroups(options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) {
return c.base.GetUserGroups(options...)
}
49 changes: 49 additions & 0 deletions internal/slackExt/client_rate_limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package slackExt

import (
"context"
"time"

"github.com/slack-go/slack"
)

type clientRateLimit struct {
base Client
}

func rateLimit[R any](f func() (R, error)) (R, error) {

Check failure on line 14 in internal/slackExt/client_rate_limit.go

View workflow job for this annotation

GitHub Actions / Build

rateLimit - result 1 (error) is always nil (unparam)
for {
result, err := f()

if err == nil {
return result, nil
}

if rateLimitedError, ok := err.(*slack.RateLimitedError); ok {
<-time.After(rateLimitedError.RetryAfter)
err = nil

Check failure on line 24 in internal/slackExt/client_rate_limit.go

View workflow job for this annotation

GitHub Actions / Build

ineffectual assignment to err (ineffassign)
}
}
}

func (c *clientRateLimit) GetUserInfo(user string) (result *slack.User, err error) {
return rateLimit(func() (*slack.User, error) {
return c.base.GetUserInfo(user)
})
}

func (c *clientRateLimit) GetUserByEmail(email string) (*slack.User, error) {
return c.base.GetUserByEmail(email)
}

func (c *clientRateLimit) GetUsersContext(ctx context.Context) ([]slack.User, error) {
return rateLimit(func() ([]slack.User, error) {
return c.base.GetUsersContext(ctx)
})
}

func (c *clientRateLimit) GetUserGroups(options ...slack.GetUserGroupsOption) ([]slack.UserGroup, error) {
return rateLimit(func() ([]slack.UserGroup, error) {
return c.base.GetUserGroups(options...)
})
}

0 comments on commit ce3c127

Please sign in to comment.