前回の記事 で、btp CLI のコマンドを確認しました。 その中にあった、accounts に関するコマンドを使用し、実際に SAP BTP を操作し、 Cloud Foundry 環境の構築まで実施してみたいと思います。
前提
- BTP 環境(今回は Trial)を作成しておくこと。
 - btp CLI をインストール、ログインまで済ませていること
 
今回は US East(AWS) を使用します。
    
CLI で BTP を操作する
色々見てみる
Global Account
btp get accounts/global-account
<結果>
Showing details for global account 4xxxxxxxxxxxxxxxxxxx...
global account id: 4xxxxxxxxxxxxxxxxxxx
display name: xxxxxxxxxxxtrial
description:
subdomain: xxxxxxxxxxtrial-ga
cost center:
commercial model: Subscription
license type: TRIAL
geographical access: STANDARD
crm tenant id:
crm customer id:
service id:
use for:
state: OK
state message: Global account created.
Subaccount
## 項目が多いので、json 形式で取得
btp --format json list accounts/subaccount
<結果>
{
  "value": [
    {
      "guid": "1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "technicalName": "1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "displayName": "trial",
      "globalAccountGUID": "4xxxxxxxxxxxxxxxxx",
      "parentGUID": "4xxxxxxxxxxxxxxxxx",
      "parentType": "ROOT",
      "region": "us10",
      "subdomain": "xxxxxxxxxxtrial",
      "betaEnabled": false,
      "usedForProduction": "UNSET",
      "description": null,
      "state": "OK",
      "stateMessage": "Subaccount created.",
      "contentAutomationState": null,
      "contentAutomationStateDetails": null,
      "createdDate": 1638652732395,
      "createdBy": null,
      "modifiedDate": 1638652747723
    }
  ]
}
User
btp list security/user
<結果>
username
kxxxxxxxxxxx@aaaaaaaaaa
Role
btp list security/role
<結果>
[
  {
    "roleTemplateName": "xsuaa_auditor",
    "roleTemplateAppId": "xsuaa!t1",
    "name": "User and Role Auditor",
    "attributeList": [],
    "description": "Read-only access for authorizations, trusted identity providers, and users.",
    "scopes": [
      {
        "description": "Read permission for XS roles and role collections",
        "name": "xs_authorization.read",
        "granted-apps": [
    ...省略
]
role や entitlement といった、情報量が多いものを取得するときは、取得対象を絞るとか、name などで grep したほうが良さそうです。
作成してみる
Directory
btp create accounts/directory --features DEFAULT --display-name "Test Directory" --description "Test"
<結果>
Creating directory in global account 4xxxxxxxxxxxxxx...
directory id: 8xxxxxxxxxxxxxxxx
display name: Test Directory
description: Test
directory features: DEFAULT
created by: kxxxxxxxxxxx@zzzzzzzzzzzzzzz
parent id: 4xxxxxxxxxxxxxxxxxxx
parent type: global account
state: OK
state message: Directory created.
Command runs in the background.
Use 'btp get accounts/directory' to verify status.
一応 Cockpit からも確認
    
おまけ
btp help create accounts/directory
で、内容を確認したところ、 –features で “ENTITLEMENTS” や “AUTHORIZATIONS” を指定しつつコマンドを実行することで、 Global Account のクウォーターを割り当てたり、管理者を設定できたりするようです。 が、実例に落とし込めるほどまだ理解できていません。。
Subaccount
続いて Subaccount を作ってみましょう。
btp create accounts/subaccount --display-name "Cli Subaccount" --region us10 --subdomain cli-subaccount-subdomain
<結果>
Creating a subaccount in 4xxxxxxxxxx...
subaccount id:         5xxxxxxxxxx
display name:          Cli Subaccount
description:
subdomain:             cli-subaccount-subdomain
region:                us10
created by:
beta-enabled:          false
used for production:   false
parent id:             4xxxxxxxxxx
parent type:           global account
state:                 Started
Command runs in the background.
Use 'btp get accounts/subaccount' to verify status.
OK
こちらも Cockpit から確認してみましょう。
    
ここまでやってみた感想
とにかく早いです。 Cockpit 上から操作すると、画面の描画待ちが入るため、「BTP ってかなりもっさりしてるなー」とこれまでは思ってましたが、コマンドで操作した場合は全くそのようには思いませんでした。 また、Cockpit での操作だとよく起こるセッションタイムアウトですが、コマンドの場合はそこそこ長時間操作しなくとも発生せず、快適です。
作った Subaccount を Directory に移動させてみる
Directory のリストを取得するコマンドは無いので、以下で代替
btp get accounts/global-account --show-hierarchy
<結果>
Showing details for global account 4xxxxxxxxxxxx...
├─ 7xxxxxxxxxxxxxxxtrial (4xxxxxxxxxxxx - global account)
│  ├─ Cli Subaccount (5xxxxxxxxxxxxxx - subaccount)
│  ├─ trial (1xxxxxxxxxxxxxx - subaccount)
│  ├─ Test Directory (xxxxxxxxxx - directory)
これで Directory(ついでに subaccount のも) の ID が取得できたので、移動させてみます。
btp move accounts/subaccount 5xxxxxxxxxxxxxxxx --to-directory 8xxxxxxxxxxxxxxx
<結果>
Moving subaccount 5xxxxxxxxxxxxxxxxxxx to directory 8xxxxxxxxxxxxxxxxxxx...
subaccount id:         5xxxxxxxxxxxxxxxxxxx
display name:          Cli Subaccount
description:
subdomain:             cli-subaccount-subdomain
region:                us10
beta-enabled:          false
current parent id:     4xxxxxxxxxxxxxxxxxxx
current parent type:   global account
state:                 Moving
もう一度 hierarchy を見てみましょう。
btp get accounts/global-account --show-hierarchy
<結果>
├─ 7f657770trial (4xxxxxxxxxxxxxxxx - global account)
│  ├─ trial (xxxxxxxxxxxxxxxx - subaccount)
│  ├─ Test Directory (8xxxxxxxxxxxxxxxx - directory)
│  │  ├─ Cli Subaccount (5xxxxxxxxxxxxxxxx - subaccount)
画面からも念の為。
    
Cloud Foundry を使えるようにしてみる
ようやく、Cloud Foundry 環境の構築工程まで進みました。 最初に現在の状態を確認してから、構築を始めます。
Subaccount の持つ資格情報を確認
btp list accounts/entitlement --subaccount 5xxxxxxxxxxxxxxxxxxxx
<結果>
Showing entitlements for subaccount 5xxxxxxxxxxxxxxxxxxxxxxxxxx:
service name          service plan              quota
application-logs      lite                      1
autoscaler            standard                  1
one-mds               sap-integration           1
html5-apps-repo       app-host                  1
auditlog-api          default                   1
service-manager       service-operator-access   1
service-manager       subaccount-audit          1
connectivity          lite                      1
service-manager       subaccount-admin          1
print                 receiver                  1
content-agent         application               1
saas-registry         application               1
auditlog-management   default                   1
destination           lite                      1
service-manager       container                 1
html5-apps-repo       app-runtime               1
content-agent         standard                  1
credstore             proxy                     1
feature-flags         lite                      1
Trial 環境なので大量に出る一方、Cloud Foundry がリストに上がっていないことを確認出来ました。
Subaccount が Cloud Foundry を含むインスタンスを持っているかを確認
btp list accounts/environment-instance --subaccount 5xxxxxxxxxx
<結果>
Subaccount 5xxxxxxxxxxxxxxxx has no environment instances.
Cloud Foundry 環境を構築
btp create accounts/environment-instance --subaccount 5xxxxxxxxxx --display-name my-cf-environment --environment cloudfoundry --service cloudfoundry --plan standard --parameters "{\"instance_name\":\"my-org\"}"
instance_name が必須かつ、Json での記述となるため、エスケープが必要であったり微妙かも。
<結果>
Creating an environment instance for subaccount 5xxxxxxxxxxxxx...
environment id:     8xxxxxxxxxxxxxxxxxxxxx
environment name:   my-cf-environment
environment:        cloudfoundry
landscape:          cf-us10
state:              CREATING
state message:      Creating environment instance.
Command runs in the background.
Use 'btp get accounts/environment-instance' to verify status.
これで作成が始まったようです。
Subaccount の持つ資格情報を再確認
Cloud Foundry 環境の Create コマンドを実行してからしばらく後に、再確認を行ってみます。
btp --format json list accounts/environment-instance --subaccount 5xxxxxxxxxx
<結果>
{
  "environmentInstances": [
    {
      "id": "8xxxxxxxxxxxxx",
      "name": "my-cf-environment",
      "brokerId": "Exxxxxxxxxxxxxxx",
      "globalAccountGUID": "4xxxxxxxxxxxxxxx",
      "subaccountGUID": "5xxxxxxxxxxxxxxx",
      "tenantId": "5xxxxxxxxxxxxxxx",
      "serviceId": "fxxxxxxxxxxxxxxx",
      "planId": "fxxxxxxxxxxxxxxx",
      "operation": "provision",
      "parameters": "{\"instance_name\":\"my-org\",\"status\":\"active\"}",
      "labels": "{\"Org Name:\":\"my-org\",\"API Endpoint:\":\"https://api.cf.us10.hana.ondemand.com\",\"Org ID:\":\"0xxxxxxxxxxxxxxx\"}",
      "customLabels": {},
      "type": "Provision",
      "status": "Processed",
      "environmentType": "cloudfoundry",
      "landscapeLabel": "cf-us10",
      "platformId": "0xxxxxxxxxxxxxxx",
      "createdDate": 1639490670716,
      "modifiedDate": 1639490685525,
      "state": "OK",
      "stateMessage": "Environment instance created.",
      "serviceName": "cloudfoundry",
      "planName": "standard"
    }
  ]
}
無事できているようです。 続いて、画面からも確認します。
    
こちらからも環境が準備できたことを確認することが出来ました。
まとめ
確認をはさみながらだったので、Cloud Foundry 環境の作成までだいぶ時間がかかってしまいました。 しかしながら、途中の書いた通り、cli での操作は待機時間がほぼ無く、コマンドさえ把握出来ていさえすれば、かなり快適に環境を操作できることがわかりました。
そのため例えば、パラメータシート、定義書のようなものを作り、そこからコマンドを作成して流すだけ等運用方法を工夫することで、 BTP 環境のコントロールをかなりラクにすることができるのではないかと思いました。