Skip to content

Instantly share code, notes, and snippets.

@gowatana
Created November 21, 2018 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gowatana/3a42a3f0a10ae6d393b8625f040c0b8f to your computer and use it in GitHub Desktop.
Save gowatana/3a42a3f0a10ae6d393b8625f040c0b8f to your computer and use it in GitHub Desktop.
Nutanix REST API v2 で Protection Domain の Snapshotを作成してみる。
import sys
import json
import requests
import time
args = sys.argv
conf_file = args[1]
pd_name = args[2]
seconds_to_wait_snapshot_report = 5
def main():
with open(conf_file, "r") as file:
conf = file.read()
conf = json.loads(conf)
prism_addr = conf["prism_address"]
prism_user = conf["user_name"]
prism_pass = conf["password"]
base_url = 'https://' + prism_addr + ':9440/PrismGateway/services/rest/v2.0/'
insecure_warn = requests.packages.urllib3.exceptions.InsecureRequestWarning
requests.packages.urllib3.disable_warnings(insecure_warn)
s = requests.Session()
s.auth = (prism_user, prism_pass)
s.headers.update({'Content-Type': 'application/json; charset=utf-8'})
# Create PD Snapshot.
url = base_url + 'protection_domains/' + pd_name + '/oob_schedules'
oob_schedule = s.post(url, data='{}', verify=False).json()
#print(json.dumps(oob_schedule, indent=2))
oob_schedule_id = oob_schedule['schedule_id']
# Show PD Snapshot.
time.sleep(seconds_to_wait_snapshot_report)
url = base_url + 'protection_domains/' + pd_name + '/dr_snapshots/?oob_schedule_ids=' + str(oob_schedule_id)
dr_snapshot = s.get(url, verify=False).json()
print(json.dumps(dr_snapshot, indent=2))
if __name__ == '__main__':
main()
@gowatana
Copy link
Author

準備

  • 事前にPD作成&VM追加しておく。例でのPD名は pd01
  • Prismの接続情報を記載したファイル(./prism-config.json)を作成しておく。

実行例

Snapshotの作成完了が情報取得にまに合わない場合は、
スクリプト内の seconds_to_wait_snapshot_report を増やす。

$ python create_pd-snapshot.py ./prism-config.json pd01
{
  "entities": [
    {
      "oob_schedule_ids": [
        409
      ],
      "nfs_files": [],
      "consistency_groups": [
        "vm02"
      ],
      "size_in_bytes": 0,
      "state": "AVAILABLE",
      "located_remote_site_name": null,
      "volume_groups": [],
      "snapshot_expiry_time_usecs": 3690307804871334,
      "protection_domain_name": "pd01",
      "snapshot_create_time_usecs": 1542824157871334,
      "snapshot_id": "413",
      "exclusive_usage_in_bytes": -1,
      "remote_site_names": [],
      "vms": [
        {
          "vm_name": "vm02",
          "vm_power_state_on_recovery": "Powered On",
          "consistency_group": "vm02",
          "vm_files": [
            "/st-container-01/.acropolis/vmdisk/53360633-9d77-49f1-bcde-6a6779875382",
            "/st-container-01/.acropolis/vmdisk/9f477036-8afa-4a7d-93e8-ead9432d49ab"
          ],
          "related_entity_uuids": [],
          "app_consistent_snapshots": false,
          "vm_recoverability": [],
          "vm_handle": 407,
          "vm_id": "8efc9f6b-f278-455a-8d5b-269ba3e14777"
        }
      ],
      "snapshot_uuid": "b1aa3c56-4288-4cfe-8806-bab5a25d6cc5"
    }
  ],
  "metadata": {
    "grand_total_entities": 1,
    "total_entities": 1,
    "sort_criteria": "",
    "filter_criteria": ""
  }
}

@gowatana
Copy link
Author

下記の投稿むけ。

Nutanix REST API v2 で Protection Domain の Snapshot を作成してみる。(Python)
http://blog.ntnx.jp/entry/2018/11/22/080555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment