🐿️

momento cliの現存するサブコマンドを一通り触ってみたらPreview版のコマンドを触れて楽しかった

2023/11/13に公開

タイトルが「楽しかった」で締めてしまい粉蜜柑(小並感)ですが、前回のコンソールを触ってみた↓の続きでmomento cliを触ってみました。
https://zenn.dev/mjxo/articles/c907edc7af81ca


Document

日本語、英語、ポルトガル語に対応しているようです。
https://github.com/momentohq/momento-cli/blob/main/README.ja.md

※以降は2023年11月13日時点の仕様です。


インストール

インストール
$ brew tap momentohq/tap
$ brew install momento-cli

結果
$ brew tap momentohq/tap
Running `brew update --auto-update`...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/services, aws/tap).

You have xx outdated formulae and x outdated cask installed.

==> Tapping momentohq/tap
Cloning into '/usr/local/Homebrew/Library/Taps/momentohq/homebrew-tap'...
remote: Enumerating objects: 1033, done.
remote: Counting objects: 100% (514/514), done.
remote: Compressing objects: 100% (181/181), done.
remote: Total 1033 (delta 357), reused 438 (delta 290), pack-reused 519
Receiving objects: 100% (1033/1033), 158.52 KiB | 2.20 MiB/s, done.
Resolving deltas: 100% (495/495), done.
Tapped 1 formula (19 files, 217.9KB).
結果
$ brew install momento-cli
==> Fetching momentohq/tap/momento-cli
==> Downloading https://github.com/momentohq/homebrew-tap/releases/download/momento-cli-0.41.0/momento-cli-0.41.0.monterey
==> Downloading from https://objects.githubusercontent.com/github-production-release-asset-2e65be/424721048/d23d428d-125e-
################################################################################################################### 100.0%
==> Installing momento-cli from momentohq/tap
==> Pouring momento-cli-0.41.0.monterey.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/momento-cli/0.41.0: 5 files, 9.5MB
==> Running `brew cleanup momento-cli`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

ヘルプの実行でインストール成功を確認+一覧を見てみる

インストール成功を確認する意味も込めてhelpコマンドを実行してみます。

-h, --help
$ momento -h
Command line tool for Momento Serverless Cache

Usage: momento [OPTIONS] <COMMAND>

Commands:
  cache      Interact with caches
  topic      Interact with topics
  configure  Configure credentials
  help       Print this message or the help of the given subcommand(s)

Options:
      --verbose            Log more information
  -p, --profile <PROFILE>  User profile [default: default]
  -h, --help               Print help
  -V, --version            Print version

念の為ヴァージョンも確認

-hが実行出来ているのでインストールは成功していますが、念の為ヴァージョンを確認してみます。

-V, --version
$ momento -V
momento 0.41.0

-p, --profile

※こちらのオプションはいまいち用途がわかりませんでした。
後ほどドキュメントを再読して判明次第追記したいと思います。


トークンを作成して設定(configure)

先に触れておくと、以下のコマンドは現在disscomissioned(除籍)扱いとなっています。

サインアップという名前が紛らわしいですが、当該コマンドを叩く事で、
以前まではトークンが生成され入力したメールに送信されてくるという仕様だったようです。

現在はコンソールにログインしてトークンを発行してという手順が推奨(というか必要と)されています。

サインアップ
momento account signup aws --email <TYPE_YOUR_EMAIL_HERE> --region <TYPE_DESIRED_REGION>

※[available regions are us-west-2, us-east-1, ap-south-1, ap-northeast-1, eu-west-1]
との事。

「コンソールからMomentoにサインアップし、トークンを生成してください。」
$ momento account signup aws --email XXXXXXX@XXXXXXX.com --region ap-northeast-1
ERROR: This command has been decommissioned!
Please go to the console to sign up for Momento and generate a token:
https://console.gomomento.com

トークン発行

https://console.gomomento.com
左メニューから「APIキー」をクリックします。


画面が切り替わりました。

  • Super User Key
  • Fine-Grained Access Key(デフォルトはこちらが選択された状態)

が選択出来るようです。
画面翻訳しました。

後者の方Fine-Grained Access Keyの方がきめ細やかなアクセスを定義・制御でき、
前者のSuper User Keyは名前のイメージ通り全リソースへのフルアクセスを提供するようです。

Super User Keyを選択した場合は、有効期限より下の項目はなくなり「トークンを生成する」をクリックするだけとなります。

一方Fine-Grained Access Keyを選択した場合は、

Permission Typeから

  • Cache
  • Topic

Role Typeから

  • readwrite
  • readonly
  • writeonly

が選択出来るようです。
「Add Permission」をクリックすると
「Permissions Set Review Table」が表示されました。


トークンが生成されました。「JSONをダウンロード」をクリックしてみます。


ダウンロードされました。(↑の画面からコピーでも可能です。)

$ momento configure --quick
Please paste your Momento auth token.  (If you do not have an auth token, use `momento account` to generate one.)
Windows users: if CTRL-V does not work, try right-click or SHIFT-INSERT to paste.

Token: <コピーしたAPIキーを入力します。>

default-cache successfully created as the default with default TTL of 600s


コンソールを覗いてみると「default-cache」という名前のキャッシュが作成されています。

※ちなみに

ここで「If you do not have an auth token, use momento account to generate one.(認証トークンを持っていない場合は、momento accountを使って認証トークンを生成する。)」
と書いてある事について、以下のように Commandにサインアップはありますが、それをせずにTokenを発行するようなコマンドは存在しなさそうです。

accountサブコマンドのhelpを確認
$ momento account -h
Manage accounts

Usage: momento account [OPTIONS] <COMMAND>

Commands:
  signup  *DECOMMISSIONED* Please go to the Momento Console (https://console.gomomento.com) to sign up.
  help    Print this message or the help of the given subcommand(s)

Options:
      --verbose            Log more information
  -p, --profile <PROFILE>  User profile [default: default]
  -h, --help      

再掲ですがmomento account signupをしようとすると以下となります。

$ momento account signup aws --email XXXXXXX@XXXXXXX.com --region ap-northeast-1
ERROR: This command has been decommissioned!
Please go to the console to sign up for Momento and generate a token:
https://console.gomomento.com

以前まではこのコマンドを叩く事でメールでトークンが送られてくる仕様だったようですが、現在は書き換えているのでしょう。

とすると先ほどの部分もはmomento accountを叩く前にもわかるように、
「If you do not have an auth token, Please go to the console to sign up for Momento and generate a token:https://console.gomomento.com 」としていただいた上で先ほどのmomento accountコマンドがdisscomissionedになった旨はそのまま残す運用がよいように思いました。(※ドキュメントも修正するのが望ましい)

--quickをつけない場合は、以下のようにキャッシュ名とTTLの項目も省略されず対話の最中に質問されます。

Default Cache [default-cache]: test-cache
Default Ttl Seconds [600]: 300
test-cache successfully created as the default with default TTL of 300s

cacheサブコマンド

helpを確認

cacheサブコマンドのhelpを確認。
$ momento cache -h
Interact with caches

Usage: momento cache [OPTIONS] <COMMAND>

Commands:
  create       Create a cache
  delete       Delete a cache
  list         List all caches
  flush        Flush all contents from a cache
  set          Store an item in a cache
  get          Get an item from the cache
  delete-item  Delete an item from the cache
  help         Print this message or the help of the given subcommand(s)

Options:
  -e, --endpoint <ENDPOINT>  An explicit hostname to use; for example, cell-us-east-1-1.prod.a.momentohq.com
      --verbose              Log more information
  -p, --profile <PROFILE>    User profile [default: default]
  -h, --help                 Print help

create

先ほど既にconfigureでもひとつキャッシュが作成されましたが、以下コマンドでもう一つ作成してみます。

$ momento cache create example-cache

このまま結果は次のコマンドで確認してみます。


list

先ほどのcreateの結果はどうでしょうか。

list
$ momento cache list
example-cache
test-cache
default-cache

createコマンドでexample-cacheは確り作成されているようです。


delete

example-cacheを削除して確認してみます。

delete
$ momento cache delete example-cache
$ momento cache list
test-cache
default-cache

こちらもきちんと削除されているようです。


set

次はitemをセットしてみます。

set
$ momento cache set name test-cache --key testkey1 --value testvalue1 --ttl 200


コンソールでkeyの取得を試みてみると無事反映されていました。

--ttl でアイテムがキャッシュに保存される最大時間(秒単位)も設定可能です。
--endpoint でエンドポイントを指定してsetする事も可能です。


get

先ほどはコンソールで確認しましたが、getコマンドで確認してみます。

get
$ momento cache get --name test-cache --key testkey
testvalue

delete-item

ttlが切れないうちに急いで以下を実行しました。

delete-item
$ momento cache delete-item --name test-cache --key testkey
$ momento cache get --name test-cache --key testkey

※何も返ってこない=削除されていると見てよさそうです。


flush

事前に二つitemをsetしておきます。
$ momento cache set --name test-cache --key testkey1 --value testvalue1 --ttl 3000
$ momento cache set --name test-cache --key testkey2 --value testvalue2 --ttl 6000



他のオプションでは--nameでキャッシュ名を指定可能でしたが(※一部同役割で好きな方を利用可のもの有り)flushでは「--cache」で指定します。

flush
$ momento cache flush --cache test-cache

画面上部に現れる「does not exist」モーダルは秒速でいなくなるのでスクショしていませんが、全てのitemがflushされている事が確認できました。

--endpoint でエンドポイントを指定してflushする事も可能です。


topicサブコマンド

helpを確認

topicサブコマンドのhelpを確認。
$ momento topic -h
These commands requires a cache, which serves as a namespace
for your topics. If you haven't already, call `cache create`
to make one!

To create a topic, subscribe to it.
To delete a topic, stop subscribing to it.

Interact with topics

Usage: momento topic [OPTIONS] <COMMAND>

Commands:
  publish    Publish a value to all subscribers of a topic
  subscribe  Subscribe to messages coming in on a topic
  help       Print this message or the help of the given subcommand(s)

Options:
  -e, --endpoint <ENDPOINT>  An explicit hostname to use; for example, cell-us-east-1-1.prod.a.momentohq.com
      --verbose              Log more information
  -p, --profile <PROFILE>    User profile [default: default]
  -h, --help                 Print help

注意書きに「これらのコマンドにはキャッシュが必要で、これが名前空間として機能する。まだ作成していない場合は、cache create を呼び出してください。」とあります。


subscribe

subscribe側
$ momento cache create test-cache
$ momento topic subscribe --cache test-cache num3
#(※待ち状態となる)

publish側

publish
$ momento topic publish --cache test-chache num3 3


3が表示されました。

subscribe側
$ momento topic subscribe --cache test-cache num3 # さっき叩いたもの
#(※待ち状態となる)
3

ここについてはこちらでCLIのハンズオンをされており、紹介いただいている事で判明しました。


preview

エラーを起こした時、以下のメッセージが返ってきました。

error: 'momento' requires a subcommand but one was not provided
  [subcommands: cache, topic, configure, account, preview, help]

Usage: momento [OPTIONS] <COMMAND>

For more information, try '--help'.

accountは前述の通りですが、previewは初見なので実行してみたいと思います。

helpを確認

cpreviewサブコマンドのhelpを確認。
$ momento preview -h
**PREVIEW** features which are in beta. Feedback is welcome!

Usage: momento preview [OPTIONS] <COMMAND>

Commands:
  signing-key     **PREVIEW** Manage signing keys
  login           **PREVIEW** Log in to manage your Momento account
  generate-token  **PREVIEW** Generate an api token for Momento
  help            Print this message or the help of the given subcommand(s)

Options:
      --verbose            Log more information
  -p, --profile <PROFILE>  User profile [default: default]
  -h, --help               Print help

「PREVIEW機能はベータ版です。フィードバックを歓迎します。」との事。


signing-key

helpを確認

signing-keyサブコマンドのhelpを確認。
$ momento preview signing-key -h
!!                                                                !!
!!                        Preview feature                         !!
!!   For more information, contact us at support@gomomento.com.   !!
!!                                                                !!

Signing keys can be used to generate pre-signed URLs that allow access to a value in the cache
for a specified period of time.  The URLs can be distributed to browsers or devices to allow
them to access the cache value without having access to a Momento auth token.


**PREVIEW** Manage signing keys

Usage: momento preview signing-key [OPTIONS] <COMMAND>

Commands:
  create  Create a signing key
  revoke  Revoke the signing key
  list    List all signing keys
  help    Print this message or the help of the given subcommand(s)

Options:
  -e, --endpoint <ENDPOINT>  An explicit hostname to use; for example, cell-us-east-1-1.prod.a.momentohq.com
      --verbose              Log more information
  -p, --profile <PROFILE>    User profile [default: default]
  -h, --help                 Print help

「署名鍵は、指定された期間キャッシュ内の値へのアクセスを許可する、署名済みの URLを生成するために使用できます。この URL をブラウザやデバイスに配布することで、Momento の認証トークンにアクセスしなくてもキャッシュ値にアクセスできるようになります。」との事。


create


list

list
$ momento preview signing-key list
[
  {
    "key_id": "xxxxxxxxx",
    "expires_at": {
      "secs_since_epoch": 1705028542,
      "nanos_since_epoch": 0
    },
    "endpoint": "https://cache.cell-ap-northeast-1-1.prod.a.momentohq.com"
  }
]

revoke

revoke
$ momento preview signing-key revoke --key-id xxxxxxxxx
$ momento preview signing-key list
[]

login

helpを確認

loginサブコマンドのhelpを確認。
$ momento preview login -h
!!                                                                !!
!!                        Preview feature                         !!
!!   For more information, contact us at support@gomomento.com.   !!
!!                                                                !!

This command will be used to log in to your Momento account and generate secure, time-limited
tokens for accessing your Momento caches.


**PREVIEW** Log in to manage your Momento account

Usage: momento preview login [OPTIONS] [VIA]

Arguments:
  [VIA]  [default: browser] [possible values: browser, qr]

Options:
      --verbose            Log more information
  -p, --profile <PROFILE>  User profile [default: default]
  -h, --help               Print help

「このコマンドは、Momento アカウントにログインし、安全な時間制限付きのトークンを生成します。」との事。


browser

予めmomentoをログアウトしておきました。
[VIA]の部分はデフォルトはbrowserになっているようなのでそのまま実行(browserを明示しても可)

browser
$ momento preview login
awaiting login...


ブラウザでログインを求められました


ログイン手順を踏むと

fetching token...
Login valid for 1440m

となりました。


qr


※多分問題ないですが、一応QRもモザイク処理

スマートフォンでQRを読み込むと上と同じログイン画面に飛びました。


generate-token

helpを確認

generate-tokenサブコマンドのhelpを確認。
$ momento preview generate-token -h
!!                                                                !!
!!                        Preview feature                         !!
!!   For more information, contact us at support@gomomento.com.   !!
!!                                                                !!

This command will be used to generate api tokens to use with Momento. If `--never-expire` is specified,
then the generated token will never expire. Else, it will expire after the specified number of seconds.
Either `--valid-for` or `--never-expire` must be specified.


**PREVIEW** Generate an api token for Momento

Usage: momento preview generate-token [OPTIONS] <--valid-for <VALID_FOR>|--never-expire>

Options:
  -v, --valid-for <VALID_FOR>  Specify how long until the token expires. ex. 1d, 10m, 2M
      --verbose                Log more information
      --never-expire           Generate a token that never expires
  -p, --profile <PROFILE>      User profile [default: default]
  -e, --endpoint <ENDPOINT>    An explicit hostname to use; for example, cell-us-east-1-1.prod.a.momentohq.com
  -h, --help                   Print help

「このコマンドは、Momento で使用する api トークンを生成するために使用されます。もし --never-expire を指定すると、生成されたトークンは期限切れになりません、
を指定すると、生成されたトークンは期限切れになりません。それ以外の場合は、指定した秒数後に期限切れになります。valid-forまたは--never-expire`のどちらかを指定する必要があります。」との事。

私の理解が浅いのか、未実装と返ってきてうまくいきませんでした。(--never-expireでも、ap-northeast-1でも同じ)

失敗
$ momento preview generate-token --endpoint cell-us-east-1-1.prod.a.momentohq.com --valid-for 10m
ERROR: BadRequest { description: "unimplemented", source: Some(TonicStatus(Status { code: Unimplemented, message: "Method not found: control_client.ScsControl/GenerateApiToken", metadata: MetadataMap { headers: {"content-type": "application/grpc", "server": "envoy", "date": "Mon, 13 Nov 2023 03:34:25 GMT", "x-envoy-upstream-service-time": "0"} }, source: None })) }

以上でした

Previewのサブコマンドも触れて楽しかったです。
今後上記のコマンドが使えるようになると、ブラウザ作業が軽減されるかもしれません。

有難うございました。

Discussion