0

I'm following this to restart Ambari components that are in INSTALLED state, for this i have written a python code parse the Ambari services using Pycurl that works.

But once the JSON is parsed i will be generating a JSON file like:

    {
"RequestInfo": {
    "command": "START", 
    "context": "Restart all components on HOST"
}, 
"Requests/resource_filters": [
    {
    "component_name": "NAMENODE", 
    "hosts": "hadoopm", 
    "service_name": "HDFS"
    }, 
    {
    "component_name": "RESOURCEMANAGER", 
    "hosts": "hadoopm", 
    "service_name": "YARN"
    }]}

this works with:

curl -u username:password -H 'X-Requested-By-ambari' http://ambariserver:8080/api/v1/clusters/CLUSTERNAME/requests -d@service-restart.json

but the same is not working and failing with 400 Bad request error with the below code:

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, url_post)
c.setopt(pycurl.HTTPHEADER, ["X-Requested-By:ambari"])
data = json.dumps(json.loads(open(output_temp_file,'rb').read()), indent=1, sort_keys=True)
tabs = re.sub('\n +', lambda match: '\n' + '\t' * (len(match.group().strip('\n')) / 2), data)
tabJSON=json.dumps(json.loads(open(output_temp_file,'rb').read()), indent=1, sort_keys=True)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.USERPWD,'admin:'+admin_pass)
c.setopt(pycurl.POSTFIELDS, 'tabJSON')
c.setopt(pycurl.WRITEFUNCTION, service_buffer.write)
c.setopt(pycurl.VERBOSE, 1)
c.perform()
c.close()

and failing with HTTP/1.1 400 Bad Request

is there something wrong that i'm doing with this can someone please help me with this.

1 Answers1

1

Probably API call format is outdated in documentation. I'd suggest going by example:

  • open dev console in Chrome
  • use Ambari UI to perform an action (e.g. restart all services)
  • go to Network, find relevant POST/PUT request (sort by non-200 Status column)
  • copy request (right click on request -> Copy -> copy as cURL )

Now you have up-to-date CURL command example, and can go further playing around request body

Dmitriusan
  • 11,525
  • 3
  • 38
  • 38