Created
August 18, 2017 08:13
-
-
Save mbukatov/f09b8ee509d16d8f8e0a4a4035c36c2c to your computer and use it in GitHub Desktop.
Tendrl Gluster Import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| """ | |
| Import gluster cluster via Tendrl API. | |
| The code here comes from usmqe module, which can't be directly used because | |
| it's tighly integrated with usmqe test and logging modules. | |
| """ | |
| import json | |
| import sys | |
| import requests | |
| USM_API_URL = "http://mbukatov-usm1-server.example.com/api/1.0/" | |
| class TendrlAuth(requests.auth.AuthBase): | |
| """ | |
| Implementation of Tendrl Auth Method (Bearer Token) for requests | |
| library, based on upstream documentation: | |
| https://github.com/Tendrl/api/blob/master/docs/authentication.adoc | |
| See also: http://docs.python-requests.org/en/master/user/authentication/ | |
| """ | |
| def __init__(self, token, username=None): | |
| """ | |
| Args: | |
| token (str): tendrl ``access_token`` string | |
| username (str): username of account associated with the token | |
| """ | |
| self.__bearer_token = token | |
| # metadata attributes for easier debugging, we need to trust login | |
| # function to store correct values there | |
| self.username = username | |
| def __repr__(self): | |
| return "TendrlAuth(token={})".format(self.__bearer_token) | |
| def __call__(self, r): | |
| """ | |
| Add Tendl Bearer Token into header of the request. | |
| For full description, see requests documentation: | |
| http://docs.python-requests.org/en/master/user/authentication/ | |
| """ | |
| headers = { | |
| "Authorization": "Bearer {}".format(self.__bearer_token), | |
| } | |
| r.prepare_headers(headers) | |
| return r | |
| def login(username, password): | |
| """ | |
| Login Tendrl user. | |
| Args: | |
| username: name of user that is going logged in | |
| password: password for username | |
| Returns requests auth object (instance of TendrlAuth) | |
| """ | |
| post_data = {"username": username, "password": password} | |
| request = requests.post(USM_API_URL + "login", data=json.dumps(post_data)) | |
| token = request.json().get("access_token") | |
| auth = TendrlAuth(token, username) | |
| return auth | |
| def logout(auth): | |
| request = requests.delete(USM_API_URL + "logout", auth=auth) | |
| if __name__ == "__main__": | |
| auth = login("admin", "adminuser") | |
| request = requests.get(USM_API_URL + "GetNodeList", auth=auth) | |
| getnodelist_data = request.json() | |
| for cluster in getnodelist_data["clusters"]: | |
| print("cluster_id:", cluster['cluster_id']) | |
| node_id_list = cluster["node_ids"] | |
| print("node ids:", " ".join(node_id_list)) | |
| import_data = { | |
| "node_ids": cluster["node_ids"], | |
| "sds_type": cluster["sds_name"], | |
| } | |
| import_request = requests.post( | |
| USM_API_URL + "ImportCluster", | |
| json=import_data, | |
| auth=auth) | |
| print("import", import_request) | |
| logout(auth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment