Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RHTN2021.1.14 AnsibleTwitterModuleの話

Avatar for nanodayo nanodayo
January 14, 2021

RHTN2021.1.14 AnsibleTwitterModuleの話

RHTN2021.1.14 で話したもの

Avatar for nanodayo

nanodayo

January 14, 2021
Tweet

More Decks by nanodayo

Other Decks in Programming

Transcript

  1. Ansible Twitter module とは • Twitterにつぶやきを投稿する。 それだけの機能の Ansible moduleです •

    以下で公開中 Ansible Galaxy https://galaxy.ansible.com/nanodayo/twitter GitHub https://github.com/nanodayo/ansible_twitter_module
  2. Step1: Ansible module の作り方を調べる • 公式 ◦ Ansible module development:

    getting started ◦ https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html • 赤帽エンジニアブログ by ひよこ大佐 ◦ Ansibleのモジュールを開発してみよう ◦ https://rheb.hatenablog.com/entry/develop_ansible_module
  3. Step3: moduleを作る • Python で実装 • Pythonの twitter library https://pypi.org/project/twitter/

    • twitter library に必要な値を module のパラメータとして定義 ◦ Twitter のAPI Key ◦ つぶやき内容 module = AnsibleModule( argument_spec={ 'state': {'required': False, 'default': 'present'}, 'server': {'required': False, 'default': 'api.twitter.com'}, 'https': {'required': False, 'type': 'bool', 'default': True}, 'consumer_key': {'required': True}, 'consumer_secret_key': {'required': True}, 'access_token': {'required': True}, 'access_token_secret': {'required': True}, 'tweet': {'required': True} }, ) tweet_text = module.params.get('tweet') tweet_result = Twitter( auth=OAuth( module.params.get('access_token'), module.params.get('access_token_secret'), module.params.get('consumer_key'), module.params.get('consumer_secret_key') ), domain=module.params.get('server'), secure=module.params.get('https') ) try: tweet_result.statuses.update(status=tweet_text)
  4. Step6: CI対応 • GitHun Action + Toxで実行 • LINT ◦

    Pylint, flake8 • Unit test, sanity test ◦ 未着
  5. Step7: 機能拡張 • 公式以外のtwitter サーバを使う ◦ テスト用途 ◦ サーバを指定するための Module

    引数を追加 • 既に同じツイートがあった場合は changed にしないで正常終了 ◦ Twitter 側で重複ツイートはNGにしている ◦ Twitter のResponse Codeから判定可 Response Codes | Docs ▪ 同じコード(403, 187)を返す ダミーサーバを作成してテスト @app.route('/1.1/statuses/update.json', methods=['POST']) def post_tweet(): tweet = request.form['status'] if tweet not in tweets: tweets.append(request.form['status']) return "", 200 else: return jsonify({ "errors": [ { "code": 187, "message": "Status is a duplicate." } ] }), 403
  6. 実行例 (中略) "item": { "debug_info": { "errors": [ { "code":

    187, "message": "Status is a duplicate." } ] }, "tweet": "Ansible moduleからのつぶやきテスト https://galaxy.ansible.com/nanodayo/twitter" } } PLAY RECAP ******************************************************************* *************************************** localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 (中略) "https": true, "server": "api.twitter.com", "state": "present", "tweet": "Ansible moduleからのつぶやきテスト https://galaxy.ansible.com/nanodayo/twitter" } }, "item": { "tweet": "Ansible moduleからのつぶやきテスト https://galaxy.ansible.com/nanodayo/twitter" } } PLAY RECAP ******************************************************************* ******************************************************** localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 重複ツイートがない場合 重複ツイートがある場合
  7. 今後の開発計画 • CIのテスト拡充 ◦ Sanity Test ◦ Unit Test •

    twitter APIのダミーサーバでのテストをCIに組み込み ◦ CIの中で重複ツイートのテストを実施