Skip to content

Commit

Permalink
feat: add retry
Browse files Browse the repository at this point in the history
  • Loading branch information
int-tt committed May 4, 2024
1 parent 27de1eb commit 93846c0
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 44 deletions.
21 changes: 15 additions & 6 deletions slack/data_source_user_with_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package slack

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
slackapi "github.com/slack-go/slack"
)
Expand Down Expand Up @@ -32,12 +34,19 @@ func dataSourceUserWithEmail() *schema.Resource {

func dataSourceUserWithEmailRead(d *schema.ResourceData, meta interface{}) error {
userEmail := d.Get("email").(string)
user, err := meta.(*slackapi.Client).GetUserByEmail(userEmail)
if err != nil {
return fmt.Errorf("faild to get user(%s): %s", userEmail, err.Error())
}
if err = setUserInfo(d, user); err != nil {
return fmt.Errorf("faild to set user info:%s", err.Error())
for {
user, err := meta.(*slackapi.Client).GetUserByEmail(userEmail)
if err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}
return fmt.Errorf("faild to get user(%s): %s", userEmail, err.Error())
}
if err = setUserInfo(d, user); err != nil {
return fmt.Errorf("faild to set user info:%s", err.Error())
}
break
}
return nil
}
64 changes: 46 additions & 18 deletions slack/resource_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slack

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
slackapi "github.com/slack-go/slack"
Expand Down Expand Up @@ -34,39 +35,66 @@ func resourceChannel() *schema.Resource {
func resourceChannelCreate(d *schema.ResourceData, meta interface{}) error {
isPrivate := d.Get("private").(bool)
channelName := d.Get("name").(string)
channel, err := meta.(*slackapi.Client).CreateConversation(channelName, isPrivate)
if err != nil {
return fmt.Errorf("failed to create channel(%s): %s", channelName, err.Error())
for {
channel, err := meta.(*slackapi.Client).CreateConversation(channelName, isPrivate)
if err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}
return fmt.Errorf("failed to create channel(%s): %s", channelName, err.Error())
}
d.SetId(channel.ID)
break
}
d.SetId(channel.ID)

return resourceChannelRead(d, meta)
}

func resourceChannelRead(d *schema.ResourceData, meta interface{}) error {
channel, err := meta.(*slackapi.Client).GetConversationInfo(d.Id(), false)
if err != nil {
return fmt.Errorf("failed to read channel: %s", err.Error())
}
if channel.IsArchived {
return fmt.Errorf("failed to channel for archived")
for {
channel, err := meta.(*slackapi.Client).GetConversationInfo(d.Id(), false)
if err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}
return fmt.Errorf("failed to read channel: %s", err.Error())
}
if channel.IsArchived {
return fmt.Errorf("failed to channel for archived")
}
_ = d.Set("name", channel.Name)
break
}
_ = d.Set("name", channel.Name)

return nil
}

func resourceChannelUpdate(d *schema.ResourceData, meta interface{}) error {
if _, err := meta.(*slackapi.Client).RenameConversation(d.Id(), d.Get("name").(string)); err != nil {
return fmt.Errorf("failed to update channel: %s", err.Error())
for {
if _, err := meta.(*slackapi.Client).RenameConversation(d.Id(), d.Get("name").(string)); err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}
return fmt.Errorf("failed to update channel: %s", err.Error())
}
break
}

return resourceChannelRead(d, meta)
}

func resourceChannelDelete(d *schema.ResourceData, meta interface{}) error {
if err := meta.(*slackapi.Client).ArchiveConversation(d.Id()); err != nil {
return fmt.Errorf("failed to archive channel: %s", err.Error())
for {

if err := meta.(*slackapi.Client).ArchiveConversation(d.Id()); err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}

return fmt.Errorf("failed to archive channel: %s", err.Error())
}
break
}
return nil
}
Expand Down
58 changes: 41 additions & 17 deletions slack/resource_channel_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package slack

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
slack "github.com/slack-go/slack"
slackapi "github.com/slack-go/slack"
)

func resourceChannelInvite() *schema.Resource {
Expand Down Expand Up @@ -33,28 +35,42 @@ func resourceChannelInvite() *schema.Resource {

func resourceChannelInviteCreate(d *schema.ResourceData, meta interface{}) error {
channelID, userID := getUserAndChannelID(d)
_, err := meta.(*slack.Client).InviteUsersToConversation(channelID, userID)
if err != nil {
return fmt.Errorf("faild to invite user to channel:%s", err.Error())
for {
_, err := meta.(*slack.Client).InviteUsersToConversation(channelID, userID)
if err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}
return fmt.Errorf("faild to invite user to channel:%s", err.Error())
}
break
}
return resourceChannelInviteRead(d, meta)
}

func resourceChannelInviteRead(d *schema.ResourceData, meta interface{}) error {
channelID, userID := getUserAndChannelID(d)
channel, err := meta.(*slack.Client).GetConversationInfo(channelID, false)
if err != nil {
return fmt.Errorf("failed to read channel:%s", err.Error())
}
isJoin := "false"
for _, member := range channel.Members {
if member == userID {
isJoin = "true"
break
for {
channel, err := meta.(*slack.Client).GetConversationInfo(channelID, false)
if err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}
return fmt.Errorf("failed to read channel:%s", err.Error())
}
isJoin := "false"
for _, member := range channel.Members {
if member == userID {
isJoin = "true"
break
}
}
}

d.SetId(isJoin)
d.SetId(isJoin)
break
}
return nil
}

Expand All @@ -64,9 +80,17 @@ func resourceChannelInviteUpdate(d *schema.ResourceData, meta interface{}) error

func resourceChannelInviteDelete(d *schema.ResourceData, meta interface{}) error {
channelID, userID := getUserAndChannelID(d)
err := meta.(*slack.Client).KickUserFromConversation(channelID, userID)
if err != nil {
return fmt.Errorf("failed to kick user to channel:%s", err.Error())
for {
err := meta.(*slack.Client).KickUserFromConversation(channelID, userID)
if err != nil {
if e, ok := err.(*slackapi.RateLimitedError); ok {
time.Sleep(e.RetryAfter)
continue
}

return fmt.Errorf("failed to kick user to channel:%s", err.Error())
}
break
}
return resourceChannelInviteRead(d, meta)
}
Expand Down
14 changes: 11 additions & 3 deletions slack/resource_send_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
func resourceSendMessage() *schema.Resource {
return &schema.Resource{
Create: resourceSendMessageCreate,
Read: resourceChannelRead,
Update: resourceChannelUpdate,
Delete: resourceChannelDelete,
Read: resourceSendMessageRead,
Update: resourceSendMessageUpdate,
Delete: resourceSendMessageDelete,
Schema: map[string]*schema.Schema{
"channel_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -49,8 +49,16 @@ func resourceSendMessageRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceSendMessageUpdate(d *schema.ResourceData, meta interface{}) error {
_, _, _, err := meta.(*slackapi.Client).UpdateMessage(d.Get("channel_id").(string), d.Get("timestamp").(string), slackapi.MsgOptionText(d.Get("text").(string), false))
if err != nil {
return fmt.Errorf("failed to edit message: %s", err.Error())
}
return nil
}
func resourceSendMessageDelete(d *schema.ResourceData, meta interface{}) error {
_, _, err := meta.(*slackapi.Client).DeleteMessage(d.Get("channel_id").(string), d.Get("timestamp").(string))
if err != nil {
return fmt.Errorf("failed to delete message: %s", err.Error())
}
return nil
}

0 comments on commit 93846c0

Please sign in to comment.