Reply
Explorer
Posts: 9
Registered: ‎06-19-2018

Error while trying to decommission host with Python CM API

Hello,

 

I'm using the package cm_client (new API library for Python) to automize host decommission and recommission.

 

First of all, method ApiHostnameList() returned me None result (but of course I've hosts in the cluster):

 

Screenshot_2018-10-15 All Hosts - Cloudera Manager.png

 

Secondly, instead of use ApiHostnameList(), I'm declaring an array with host ID list to passed out in the deommission command:

 

import cm_client
from cm_client.rest import ApiException

 

# Configure HTTP basic authorization: basic
cm_client.configuration.username = 'cmapi'
cm_client.configuration.password = 'B1gd4t@'
cm_client.configuration.verify_ssl = True
cm_client.configuration.ssl_ca_cert = '/home/rlopez/certs/CA.crt.pem'

 

api_url = 'https://clouderapre-mgr.fintonic.com:7183/api/v19'
api_client = cm_client.ApiClient(api_url)

 

cluster_api_instance = cm_client.ClustersResourceApi(api_client)
services_api_instance = cm_client.ServicesResourceApi(api_client)

cloudera_manager_instance = cm_client.ClouderaManagerResourceApi(api_client)

 

body=['c945347e-fa6e-4f89-a4ff-f2266918787a']

cloudera_manager_instance.hosts_decommission_command(body=body)

 

But it's giving me this error:

 

Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Transfer-Encoding': 'chunked', 'Set-Cookie': 'CLOUDERA_MANAGER_SESSIONID=uxvrdcukujfq1hjlv9vgvovl7;Path=/;Secure;HttpOnly', 'Expires': 'Thu, 01-Jan-1970 00:00:00 GMT', 'Server': 'Jetty(6.1.26.cloudera.4)', 'Date': 'Mon, 15 Oct 2018 07:05:49 GMT', 'Content-Type': 'application/json'})
HTTP response body: {
  "message" : "Can not deserialize instance of com.cloudera.api.model.ApiHostNameList out of START_ARRAY token\n at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@5c671d14; line: 1, column: 1]"
}

 

Cloudera Employee
Posts: 13
Registered: ‎11-17-2017

Re: Error while trying to decommission host with Python CM API

Hopefully this can help somebody in the future with this endpoint or understanding how to decipher the API if you have already moved past this issue.

 

 

However, if you look at the example for the hosts_decommission_command endpoint [0] you will see the following:

 

 

from __future__ import print_function
import time
import cm_client
from cm_client.rest import ApiException
from pprint import pprint

# Configure HTTP basic authorization: basic
configuration = cm_client.Configuration()
configuration.username = 'YOUR_USERNAME'
configuration.password = 'YOUR_PASSWORD'

# create an instance of the API class
api_instance = cm_client.ClouderaManagerResourceApi(cm_client.ApiClient(configuration))
body = cm_client.ApiHostNameList() # ApiHostNameList |  (optional)

try:
    # Decommission the given hosts.
    api_response = api_instance.hosts_decommission_command(body=body)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling ClouderaManagerResourceApi->hosts_decommission_command: %s\n" % e)

 

 

For CM to deserialize this request, the body being sent must be of type ApiHostNameList. ApiHostNameList is just a list of strings that are the host names you want decommissioned [1].

 

itemslist[str]

 

 

Here is an example that tells the defined CM (api_host) to decommission two hosts (host3.example.com and host4.example.com) defined in the ApiHostNameList assigned to the body:

 

import cm_client
from cm_client.rest import ApiException
from pprint import pprint

# Configure HTTP basic authorization: basic
cm_client.configuration.username = 'admin'
cm_client.configuration.password = 'admin'
cm_client.configuration.verify_ssl = False

# Create an instance of the API class
api_host = 'https://cmhost.example.com'
port = '7183'
api_version = 'v30'

# Construct base URL for API
# http://cmhost:7180/api/v30
api_url = api_host + ':' + port + '/api/' + api_version
api_client = cm_client.ApiClient(api_url)
api_instance = cm_client.ClouderaManagerResourceApi(api_client)

body = cm_client.ApiHostNameList(["host3.example.com", "host4.example.com"])

try:
# Decommission the given hosts.
api_response = api_instance.hosts_decommission_command(body=body)
pprint(api_response)
except ApiException as e:
print("Exception when calling ClouderaManagerResourceApi->hosts_decommission_command: %s\n" % e)

 

This endpoint will return an ApiCommand object which can be used in further code to monitor the progress of the command along with returning other details that I will not be covering in this answer [2].

 

 

 

[0] https://archive.cloudera.com/cm6/6.0.0/generic/jar/cm_api/swagger-html-sdk-docs/python/docs/Cloudera...

 

[1] https://archive.cloudera.com/cm6/6.0.0/generic/jar/cm_api/swagger-html-sdk-docs/python/docs/ApiHostN...

 

[2] https://archive.cloudera.com/cm6/6.0.0/generic/jar/cm_api/swagger-html-sdk-docs/python/docs/ApiComma...

Announcements