{"id":890,"date":"2015-08-27T15:52:08","date_gmt":"2015-08-27T22:52:08","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=890"},"modified":"2015-08-27T15:52:08","modified_gmt":"2015-08-27T22:52:08","slug":"zfs-storage-appliance-restful-api","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/zfs-storage-appliance-restful-api\/","title":{"rendered":"ZFS Storage Appliance RESTful API"},"content":{"rendered":"

Until now I have used ssh and javascript to do some of the more advanced automation tasks like snapshots, cloning and replication. I am starting to look at porting to REST and here is a quick example of two functions. <\/p>\n

** I needed fabric and python-requests linux packages installed for python.<\/p>\n

\r\n#!\/usr\/bin\/env fab\r\n \r\nfrom fabric.api import task,hosts,settings,env\r\nfrom fabric.utils import abort\r\nimport requests, json, os\r\nfrom datetime import date\r\n\r\n#requests.packages.urllib3.disable_warnings()  \r\ntoday = date.today()\r\n \r\n# ZFSSA API URL\r\nurl = "https:\/\/192.168.2.200:215"\r\n \r\n# ZFSSA authentication credentials, it reads username and password from environment variables ZFSUSER and ZFSPASSWORD\r\nzfsauth = (os.getenv('ZFSUSER'), os.getenv('ZFSPASSWORD'))\r\n \r\njsonheader={'Content-Type': 'application\/json'}\r\n\r\n# This gets the pool list\r\ndef list_pools():\r\n  r = requests.get("%s\/api\/storage\/v1\/pools" % (url), auth=zfsauth, verify=False, headers=jsonheader)\r\n  if r.status_code != 200:\r\n    abort("Error getting pools %s %s" % (r.status_code, r.text))\r\n  j = json.loads(r.text) \r\n  #print j\r\n\r\n  for pool in j["pools"]:\r\n    #print pool\r\n    #{u'status': u'online', u'profile': u'stripe', u'name': u'tank1', u'owner': u'zfsapp1', u'usage': {}, u'href': u'\/api\/storage\/v1\/pools\/tank1', u'peer': u'00000000-0000-0000-0000-000000000000', u'asn': u'91bdcaef-fea5-e796-8793-f2eefa46200a'}ation\r\n    print "pool: %s and status: %s" % (pool["name"], pool["status"])\r\n\r\n\r\n# Create project\r\ndef create_project(pool, projname):\r\n  # First check if the target project name already exists\r\n  r = requests.get("%s\/api\/storage\/v1\/pools\/%s\/projects\/%s" % (url, pool, projname), auth=zfsauth, verify=False, headers=jsonheader)\r\n  if r.status_code != 404:\r\n    abort("ZFS project %s already exists (or other error): %s" % (projname, r.status_code))\r\n\r\n  payload = { 'name': projname, 'sharenfs': 'ro' }\r\n  r = requests.post("%s\/api\/storage\/v1\/pools\/%s\/projects" % (url, pool), auth=zfsauth, verify=False, data=json.dumps(payload), headers=jsonheader)\r\n  if r.status_code == 201:\r\n    print "project created"\r\n  else:\r\n    abort("Error creating project %s %s" % (r.status_code, r.text))\r\n\r\nprint "\\n\\nTest list pools and create a project\\n"\r\nlist_pools()\r\ncreate_project('tank1','proj-01')\r\n<\/pre>\n

References:
\nhttp:\/\/www.oracle.com\/technetwork\/articles\/servers-storage-admin\/zfs-appliance-scripting-1508184.html
\nhttp:\/\/docs.oracle.com\/cd\/E51475_01\/html\/E52433\/
\nhttp:\/\/ilmarkerm.blogspot.com\/2014\/12\/sample-code-using-oracle-zfs-storage.html<\/p>\n","protected":false},"excerpt":{"rendered":"

Until now I have used ssh and javascript to do some of the more advanced automation tasks like snapshots, cloning<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-890","post","type-post","status-publish","format-standard","hentry","category-zfs"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/890","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=890"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/890\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}