2.3.3. Get
get メソッドは、単一のオブジェクトの表現を取得します。
次の例では、識別子が 123 の仮想マシンの表現を見つけて取得します。
結果は、対応するタイプのインスタンスになります。この場合、結果は Ruby クラス Vm のインスタンスになります。
一部のサービスの get メソッドは、オブジェクトの表現を取得する方法や、複数の表現がある場合にどの表現を取得するかを制御する追加のパラメーターをサポートします。
たとえば、起動後に仮想マシンの将来の状態を取得したい場合があります。仮想マシンを管理するサービスの get メソッドは、next_run ブール値パラメーターをサポートします。
仮想マシンの next_run 状態の取得
# Retrieve the representation of the virtual machine; not the current one, but the one that will be used after the next boot: vm = vm_service.get(next_run: true)
# Retrieve the representation of the virtual machine; not the
# current one, but the one that will be used after the next
# boot:
vm = vm_service.get(next_run: true)
詳細については、ソフトウェア開発キットの リファレンス ドキュメントを参照してください。
オブジェクトを取得できない場合、ソフトウェア開発キットはエラーの詳細を含む エラー 例外を発生させます。これは、存在しないオブジェクトを取得しようとした場合に発生します。
service locator メソッドはサーバーにリクエストを送信しないため、オブジェクトが存在しない場合でも、service locator メソッドの呼び出しが失敗することはありません。
次の例では、service locator メソッドは成功しますが、get メソッドは例外を発生させます。
存在しない仮想マシンのサービスの検索: エラーなし
# Find the service that manages a virtual machine that does
# not exist. This will succeed.
vm_service = vms_service.vm_service('non_existent_VM')
# Find the service that manages a virtual machine that does
# not exist. This will succeed.
vm_service = vms_service.vm_service('non_existent_VM')
存在しない仮想マシンサービスの取得: エラー
# Retrieve the virtual machine. This will raise an exception. vm = vm_service.get
# Retrieve the virtual machine. This will raise an exception.
vm = vm_service.get