Project

General

Profile

Bug #11680 » CalAPITester.py

the python script used to send crush map - Kefu Chai, 05/25/2015 04:32 PM

 
#!/usr/bin/env python

import json
import logging
import requests

log = logging.getLogger(__name__)

class AuthenticatedHttpClient(requests.Session):
"""
Client for the calamari REST API, principally exists to do
authentication, but also helpfully prefixes
URLs in requests with the API base URL and JSONizes
POST data.
"""
def __init__(self, api_url, username, password):
super(AuthenticatedHttpClient, self).__init__()
self._username = username
self._password = password
self._api_url = api_url
self.headers = {
'Content-type': "application/json; charset=UTF-8"
}

@property
def prefix(self):
return self._api_url

def request(self, method, url, **kwargs):
url = self._api_url + url
print "REQUEST: **kwargs = ",kwargs
print "REQUEST: url = ", url
print "REQUEST: self = ", self
print "REQUEST: method = ", method
print "REQUEST: self.headers = ", self.headers
print "REQUEST: self.headers['Content-type'] = ", self.headers['Content-type']
if url == "http://10.16.184.71/api/v2/cluster/cbc3750d-763e-42f2-a929-b7043734f4f0/crush_map":
if method == "POST":
self.headers['Content-type'] = "text/plain"
print "REQUEST: After modification : self.headers['Content-type'] = ", self.headers['Content-type']

response = super(AuthenticatedHttpClient, self).request(method, url, **kwargs)
if response.status_code >= 400:
# For the benefit of test logs
print "%s: %s" % (response.status_code, response.content)
return response

def post(self, url, data=None, **kwargs):
print "dict = ", dict
if isinstance(data, dict):
data = json.dumps(data)

print "POST: **kwargs = ",kwargs
print "POST: url = ", url
print "POST: self = ", self
print "POST: data = ", data

return super(AuthenticatedHttpClient, self).post(url, data, **kwargs)

def patch(self, url, data=None, **kwargs):
if isinstance(data, dict) or isinstance(data, list):
data = json.dumps(data)
return super(AuthenticatedHttpClient, self).patch(url, data, **kwargs)

def delete(self, url, data=None, **kwargs):
if isinstance(data, dict):
data = json.dumps(data)
return super(AuthenticatedHttpClient, self).delete(url, data=data, **kwargs)

def login(self):
"""
Authenticate with the Django auth system as
it is exposed in the Calamari REST API.
"""
log.info("Logging in as %s" % self._username)
response = self.get("auth/login/")
response.raise_for_status()
self.headers['X-XSRF-TOKEN'] = response.cookies['XSRF-TOKEN']

response = self.post("auth/login/", {
'next': "/",
'username': self._username,
'password': self._password
})
response.raise_for_status()
# XSRF token rotates on login
self.headers['X-XSRF-TOKEN'] = response.cookies['XSRF-TOKEN']

# Check we're allowed in now.
response = self.get("cluster")
response.raise_for_status()

if __name__ == "__main__":

import argparse

p = argparse.ArgumentParser()
p.add_argument('-u', '--uri', default='http://mira035/api/v1/')
p.add_argument('--user', default='admin')
p.add_argument('--pass', dest='password', default='admin')
args, remainder = p.parse_known_args()

# Authicate the user.
# Run this script: python temp.py -u http://10.8.128.6/api/v2/ --user TEST12 --pass ghj

c = AuthenticatedHttpClient(args.uri, args.user, args.password)
c.login()
response = c.request('GET', ''.join(remainder)).json()
print json.dumps(response, indent=2)

# Get the crushmap
print "######################## GETting the original crushmap #########################################"
response_GET = c.request('GET', 'cluster/cbc3750d-763e-42f2-a929-b7043734f4f0/crush_map').json()
# print json.dumps(response_GET, indent=2)
print response_GET

# print "######################## POSTing the Modified crushmap #########################################"
# response_PUT = c.request('POST', 'cluster/cbc3750d-763e-42f2-a929-b7043734f4f0/crush_map').json()
# print json.dumps(response_PUT, indent=2)

response = c.post('cluster/cbc3750d-763e-42f2-a929-b7043734f4f0/crush_map', '# begin crush map. \ntunable choose_local_tries 0\ntunable choose_local_fallback_tries 0\ntunable choose_total_tries 68\ntunable chooseleaf_descend_once 1\ntunable straw_calc_version 1\n\n# devices\ndevice 0 osd.0\ndevice 1 osd.1\ndevice 2 osd.2\ndevice 3 osd.3\n\n# types\ntype 0 osd\ntype 1 host\ntype 2 chassis\ntype 3 rack\ntype 4 row\ntype 5 pdu\ntype 6 pod\ntype 7 room\ntype 8 datacenter\ntype 9 region\ntype 10 root\n\n# buckets\nhost osd0 {\n\tid -2\t\t# do not change unnecessarily\n\t# weight 1.820\n\talg straw\n\thash 0\t# rjenkins1\n\titem osd.0 weight 0.910\n\titem osd.2 weight 0.910\n}\nhost osd1 {\n\tid -3\t\t# do not change unnecessarily\n\t# weight 1.820\n\talg straw\n\thash 0\t# rjenkins1\n\titem osd.1 weight 0.910\n\titem osd.3 weight 0.910\n}\nroot default {\n\tid -1\t\t# do not change unnecessarily\n\t# weight 3.640\n\talg straw\n\thash 0\t# rjenkins1\n\titem osd0 weight 1.820\n\titem osd1 weight 1.820\n}\n\n# rules\nrule replicated_ruleset {\n\truleset 0\n\ttype replicated\n\tmin_size 1\n\tmax_size 10\n\tstep take default\n\tstep chooseleaf firstn 0 type host\n\tstep emit\n}\n\n# end crush map\n').json()

print json.dumps(response, indent=2)


(5-5/5)