Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

#1 delete dynamodb data with slack slash command #34

Merged
merged 5 commits into from
Mar 10, 2019

Conversation

kdnakt
Copy link
Contributor

@kdnakt kdnakt commented Mar 3, 2019

🎫 Issue

#1

📝 概要

退会者が出た時に利用するDynamoDBからのデータ削除用のSlackのスラッシュコマンドを実装しました。
Slackで/blog_delete @kdnakt のように利用できる予定です。
テストコードがないのですが、自分のAWSアカウントで簡単に動作確認済みです。
(対象者がいない場合のメッセージ、いる場合の処理)

🙅‍♂️ やってないこと

(このPRではやってないことなど書きましょう)

✔️ 動作確認

(レビューアに確認してほしいことを書きましょう)

  • CIのテストが全て問題ない

@codecov-io
Copy link

codecov-io commented Mar 3, 2019

Codecov Report

Merging #34 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #34   +/-   ##
=======================================
  Coverage   87.32%   87.32%           
=======================================
  Files           4        4           
  Lines         142      142           
=======================================
  Hits          124      124           
  Misses         13       13           
  Partials        5        5

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7c5f7bc...0f17827. Read the comment docs.

Copy link
Contributor

@budougumi0617 budougumi0617 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしました。

func DeleteUser(configData config.ConfigData, member WriteBlogEveryWeek) {
table := getTableObject(configData)
if err := table.Delete("user_id", member.UserID).Run(); err != nil {
panic("削除エラー => " + err.Error())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

基本goのアプリではpanicはしないでちゃんとerrrorを返してその結果を戻したほうがいいですね。今回の場合だと以下の点で不便だと思います。

  • panicしたら結果が返らないのでcloud watch見れる人じゃないと結果がわからない
  • cloud watch見れる人でもSlackコマンド使ったらSlack上で結果を見たいと思う

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

勉強になります!エラーメッセージを返すよう修正しました。

main.go Outdated
@@ -96,3 +99,24 @@ func blogResult() {
}
// fmt.Println(sendText)
}

// blogDelete ブログの削除ロジックを実行
func blogDelete(_ context.Context, rawParams interface{}) (interface{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

監査的な機能なくいれちゃってダイジョブですかね?コマンドを実行したIDをログに残しておくとかとか(消したレコードもログに出力しておいたほうが復旧しやすそうです)
これってSlackコマンド知っていたら誰でも消せちゃう感じだと、しれっとDMとかでやってメッセージ消したら完全犯罪になってしまうような。

Copy link
Contributor Author

@kdnakt kdnakt Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slack.goでSlackから渡ってきたパラメータを出力しました。

と書いたところで、消したレコードがログに記載がないのに気づいたので追加で修正します 🙇

main.go Outdated
@@ -96,3 +99,24 @@ func blogResult() {
}
// fmt.Println(sendText)
}

// blogDelete ブログの削除ロジックを実行
func blogDelete(_ context.Context, rawParams interface{}) (interface{}, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

戻り値の interface{}stringですかね?自分で宣言する関数の引数戻り値でinterface{}は使わないほうがいいですね。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました!

Copy link
Contributor

@budougumi0617 budougumi0617 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なんどもごめんなさい、追記しました 🙇

main.go Outdated Show resolved Hide resolved
slack/slack.go Outdated
// また、ユーザー名が送られてきた場合は、先頭の@をtrimする
Text: strings.TrimRight(strings.TrimLeft(strings.TrimLeft(params["text"][0], "@"), "<"), ">"),
}
fmt.Printf("SlackParams: %v", slackParams)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Token: params["token"][0],っていうなんかお漏らししたらいけなそうなフィールドがあるのでUserID(UserName)、Textなど特定のフィールドのみ出力するだけに絞っておいたほうがいいと思います。
(業務でやるときはloggerとかに特定のフィールド名をマスキングする処理仕込んだりするのですが、そこまではするのはめんどくさいので。。。)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token以外のユーザーID、ユーザー名、Textを出力するように修正しました
(ユーザー名だけだとSlack側で変更できてしまうので、IDも念の為出力)

@@ -64,11 +65,14 @@ func ParseSlackParams(rawParams interface{}) (result *SlackParams, err error) {
return
}

return &SlackParams{
slackParams := SlackParams{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好みの問題(?)ですが、私はこっちはslackParams := &SlackParamsのままにしておきますね。
(これは変えなくてもいいです。)
理由はポインタのほうにメソッド生やしているときが多いので

// DeleteUser ユーザーデータを削除する
func DeleteUser(configData config.ConfigData, member WriteBlogEveryWeek) error {
table := getTableObject(configData)
return table.Delete("user_id", member.UserID).Run()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DynamoDBのuser_idってなんの文字列だったのかよくわかってなかったんですが、SlackのIDだったんですね!(今更)

Copy link
Contributor

@budougumi0617 budougumi0617 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最後にひとつだけコメントしました

@@ -96,3 +99,28 @@ func blogResult() {
}
// fmt.Println(sendText)
}

// blogDelete ブログの削除ロジックを実行
func blogDelete(_ context.Context, rawParams interface{}) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error返すPATHがなくなったのなら戻り値はstringだけでいいと思います。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

残念ながらLambdaのハンドラ関数の制約で戻り値1つの場合はerrorでないといけないので、戻り値を2個定義する必要があります……

https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/go-programming-model-handler-types.html

Copy link
Contributor

@budougumi0617 budougumi0617 Mar 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、今まで勘違いしていたのですが、この関数の戻り値がSlackに通知されるわけではないのですね。
そうするとこの関数で文字列を返す理由はなんでしょうか?
Slackに結果を通知して関数を正常に完了するだけならば、文字列を slack.SendMessageに渡してこの関数は戻り値なしにすればいいのでは?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、slack slash commandに紐づけるLambdaとしてはLambda関数の戻り値として設定した値がSlackに通知される感じですかね?
だからblogResultblogReminderslack.SendMessage使っているけど、blogRegisterblogDeleteは戻り値で結果返しているという理解でよいですかね(今更感)
なんか全然検討違いのコメントをしてしまって && ドメイン理解が甘いままレビューしてしまってごめんなさい 🙇 🙇

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

説明が雑ですみません。。 🙇

ご認識の通り、このQiita記事みたいに、Slack側にレスポンスを返せるようにAPI Gatewayが設定されているはずです。(IAMの権限の都合で見れませんが)
https://qiita.com/G-awa/items/fc803c285cb560dab796

blogRegister関数の動作が同じようになっていて、添付の画像のようにメッセージが表示されます
(main.goの72行目のメッセージが表示されたケースです)
2019-03-07 20 07 06

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

解説ありがとうございます!

Copy link
Contributor

@budougumi0617 budougumi0617 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントしました。

Copy link
Contributor

@budougumi0617 budougumi0617 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

昨夜盛大にトンチンカンなコメントをしてしまった気がします。私の不勉強で余計なやりとり発生してしまってごめんなさい 🙇
私の理解が間違っていなければLGTMです

@@ -96,3 +99,28 @@ func blogResult() {
}
// fmt.Println(sendText)
}

// blogDelete ブログの削除ロジックを実行
func blogDelete(_ context.Context, rawParams interface{}) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、slack slash commandに紐づけるLambdaとしてはLambda関数の戻り値として設定した値がSlackに通知される感じですかね?
だからblogResultblogReminderslack.SendMessage使っているけど、blogRegisterblogDeleteは戻り値で結果返しているという理解でよいですかね(今更感)
なんか全然検討違いのコメントをしてしまって && ドメイン理解が甘いままレビューしてしまってごめんなさい 🙇 🙇

Copy link
Collaborator

@kojirock5260 kojirock5260 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@kojirock5260 kojirock5260 merged commit bc29980 into write-blog-every-week:master Mar 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants