def create_cluster(resource_root, name, version): """ Create a cluster @param resource_root: The root Resource object. @param name: Cluster name @param version: Cluster CDH version @return: An ApiCluster object """ apicluster = ApiCluster(resource_root, name, version) apicluster_list = ApiList([apicluster]) body = json.dumps(apicluster_list.to_json_dict()) resp = resource_root.post(CLUSTERS_PATH, data=body) # The server returns a list of created clusters (with size 1) return ApiList.from_json_dict(ApiCluster, resp, resource_root)[0]
def create_service(resource_root, name, service_type, cluster_name="default"): """ Create a service @param resource_root: The root Resource object. @param name: Service name @param service_type: Service type @param cluster_name: Cluster name @return: An ApiService object """ apiservice = ApiService(resource_root, name, service_type) apiservice_list = ApiList([apiservice]) body = json.dumps(apiservice_list.to_json_dict()) resp = resource_root.post(SERVICES_PATH % (cluster_name, ), data=body) # The server returns a list of created services (with size 1) return ApiList.from_json_dict(ApiService, resp, resource_root)[0]
def create_role(resource_root, service_name, role_type, role_name, host_id, cluster_name="default"): """ Create a role @param resource_root: The root Resource object. @param service_name: Service name @param role_type: Role type @param role_name: Role name @param cluster_name: Cluster name @return: An ApiRole object """ apirole = ApiRole(resource_root, role_name, role_type, ApiHostRef(resource_root, host_id)) apirole_list = ApiList([apirole]) body = json.dumps(apirole_list.to_json_dict()) resp = resource_root.post(_get_roles_path(cluster_name, service_name), data=body) # The server returns a list of created roles (with size 1) return ApiList.from_json_dict(ApiRole, resp, resource_root)[0]
def create_user(resource_root, username, password, roles): """ Create a user. @param resource_root: The root Resource object @param username: Username @param password: Password @param roles: List of roles for the user. This should be [] for a regular user, or ['ROLE_ADMIN'] for an admin. @return: An ApiUser object """ apiuser = ApiUser(resource_root, username, password=password, roles=roles) apiuser_list = ApiList([apiuser]) body = json.dumps(apiuser_list.to_json_dict()) resp = resource_root.post(USERS_PATH, data=body) return ApiList.from_json_dict(ApiUser, resp, resource_root)[0]
def query_activities(self, query_str=None): path = self._path() + "/activities" params = { } if query_str: params['query'] = query_str resp = self._get_resource_root().get(path, params=params) return ApiList.from_json_dict(ApiActivity, resp, self._get_resource_root())
def create_service(resource_root, name, service_type, cluster_name="default"): """ Create a service @param resource_root: The root Resource object. @param name: Service name @param service_type: Service type @param cluster_name: Cluster name @return: An ApiService object """ apiservice = ApiService(resource_root, name, service_type) apiservice_list = ApiList([apiservice]) body = json.dumps(apiservice_list.to_json_dict()) resp = resource_root.post(SERVICES_PATH % (cluster_name,), data=body) # The server returns a list of created services (with size 1) return ApiList.from_json_dict(ApiService, resp, resource_root)[0]
def create_host(resource_root, host_id, name, ipaddr, rack_id=None): """ Create a host @param resource_root: The root Resource object. @param host_id: Host id @param name: Host name @param ipaddr: IP address @param rack_id: Rack id. Default None @return: An ApiHost object """ apihost = ApiHost(resource_root, host_id, name, ipaddr, rack_id) apihost_list = ApiList([apihost]) body = json.dumps(apihost_list.to_json_dict()) resp = resource_root.post(HOSTS_PATH, data=body) # The server returns a list of created hosts (with size 1) return ApiList.from_json_dict(ApiHost, resp, resource_root)[0]
def query_activities(self, query_str=None): path = self._path() + "/activities" params = {} if query_str: params['query'] = query_str resp = self._get_resource_root().get(path, params=params) return ApiList.from_json_dict(ApiActivity, resp, self._get_resource_root())
def get_all_clusters(resource_root, view=None): """ Get all clusters @param resource_root: The root Resource object. @return: A list of ApiCluster objects. """ dic = resource_root.get(CLUSTERS_PATH, params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiCluster, dic, resource_root)
def get_commands(self, view=None): """ Retrieve a list of running commands for this role. @param view: View to materialize ('full' or 'summary') @return: A list of running commands. """ resp = self._get_resource_root().get(self._path() + "/commands", params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiCommand, resp, self._get_resource_root())
def get_all_hosts(resource_root, view=None): """ Get all hosts @param resource_root: The root Resource object. @return: A list of ApiHost objects. """ dic = resource_root.get(HOSTS_PATH, params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiHost, dic, resource_root)
def get_all_roles(resource_root, service_name, cluster_name="default", view=None): """ Get all roles @param resource_root: The root Resource object. @param service_name: Service name @param cluster_name: Cluster name @return: A list of ApiRole objects. """ dic = resource_root.get(_get_roles_path(cluster_name, service_name), params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiRole, dic, resource_root)
def get_all_services(resource_root, cluster_name="default", view=None): """ Get all services @param resource_root: The root Resource object. @param cluster_name: Cluster name @return: A list of ApiService objects. """ dic = resource_root.get(SERVICES_PATH % (cluster_name, ), params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiService, dic, resource_root)
def get_all_services(resource_root, cluster_name="default", view=None): """ Get all services @param resource_root: The root Resource object. @param cluster_name: Cluster name @return: A list of ApiService objects. """ dic = resource_root.get(SERVICES_PATH % (cluster_name,), params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiService, dic, resource_root)
def get_all_users(resource_root, view=None): """ Get all users. @param resource_root: The root Resource object @param view: View to materialize ('full' or 'summary'). @return: A list of ApiUser objects. """ dic = resource_root.get(USERS_PATH, params=view and dict(view=view) or None) return ApiList.from_json_dict(ApiUser, dic, resource_root)
def query_events(resource_root, query_str=None): """ Search for events. @param query_str: Query string. @return: A list of ApiEvent. """ if query_str: params = dict(query=query_str) else: params = {} resp = resource_root.get(EVENTS_PATH, params=params) return ApiList.from_json_dict(ApiEvent, resp, resource_root)
def _role_cmd(self, cmd, roles): path = self._path() + '/roleCommands/' + cmd data = json.dumps({ApiList.LIST_KEY: roles}) resp = self._get_resource_root().post(path, data=data) return ApiList.from_json_dict(ApiCommand, resp, self._get_resource_root())
def get_running_activities(self): path = self._path() + "/activities" resp = self._get_resource_root().get(path) return ApiList.from_json_dict(ApiActivity, resp, self._get_resource_root())
def _role_cmd(self, cmd, roles): path = self._path() + '/roleCommands/' + cmd data = json.dumps({ ApiList.LIST_KEY : roles }) resp = self._get_resource_root().post(path, data = data) return ApiList.from_json_dict(ApiCommand, resp, self._get_resource_root())