ambari_api.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import logging
  18. from ambari_client.core.http_client import HttpClient
  19. from ambari_client.resources import clusters , hosts , stacks
  20. from ambari_client.core.rest_resource import RestResource
  21. __docformat__ = "epytext"
  22. LOG = logging.getLogger(__name__)
  23. API_VERSION = 1
  24. class AmbariClient(RestResource):
  25. """
  26. AmbariClient top-level root resources.
  27. """
  28. def __init__(self, host_name, port=None, user_name="admin", password="admin", use_https = False,
  29. version=API_VERSION , client=None ,http_header=None):
  30. """
  31. Creates a RestResource object.
  32. @param host_name: The hostname server.
  33. @param port: The port of the server.
  34. @param user_name: Login name.
  35. @param password: Login password.
  36. @param version: API version.
  37. @return RestResource object referring to the root.
  38. """
  39. self._version = version
  40. if use_https:
  41. protocol = "https"
  42. if port is None:
  43. port = 8443
  44. else:
  45. protocol = "http"
  46. if port is None:
  47. port = 8080
  48. host_url = "%s://%s:%s/api/v%s" % (protocol, host_name, port, version)
  49. if client is None:
  50. client = HttpClient(host_url, user_name , password )
  51. if http_header:
  52. client.set_headers(http_header)
  53. RestResource.__init__(self, client)
  54. @property
  55. def version(self):
  56. """
  57. Returns the API version .
  58. """
  59. return self._version
  60. def get_all_clusters(self):
  61. """
  62. Get all clusters.
  63. @return : A ModelList of ClusterModel.
  64. """
  65. return clusters._get_all_clusters(self)
  66. def get_cluster(self, cluster_name):
  67. """
  68. Get a cluster by cluster_name.
  69. @param cluster_name : Cluster's cluster_name.
  70. @return : An ClusterModel.
  71. """
  72. return clusters._get_cluster(self, cluster_name)
  73. def get_host(self, host_name):
  74. """
  75. Lookup a host by name
  76. @param root_resource: The root Resource.
  77. @param host_name: Host name
  78. @return: A HostModel object
  79. """
  80. return hosts._get_host(self, host_name)
  81. def get_all_hosts(self):
  82. """
  83. Get all hosts in the Data Center
  84. @return: A ModelList of HostModel objects.
  85. """
  86. return hosts._get_all_hosts(self)
  87. def get_request_status(self , request_id):
  88. """
  89. Get request status
  90. @param request_id : request id for the request
  91. @return: A StatusModel object.
  92. """
  93. return "TODO"
  94. def bootstrap_hosts(self , hosts_list , ssh_key=None):
  95. """
  96. Bootstrap hosts.
  97. @param hosts_list :list of host_names.
  98. @param ssh_key : ssh key for password-less access
  99. @return: A StatusModel object.
  100. """
  101. return hosts._bootstrap_hosts(self, hosts_list , ssh_key)
  102. def create_cluster(self, cluster_name, version):
  103. """
  104. Create a new cluster.
  105. @param cluster_name: Cluster name.
  106. @param version : HDP version.
  107. @return ClusterModel object.
  108. """
  109. return clusters._create_cluster(self, cluster_name, version)
  110. def delete_cluster(self , cluster_name):
  111. """
  112. Delete a cluster
  113. @param cluster_name: Cluster to be deleted
  114. """
  115. return clusters._delete_cluster(self, cluster_name)
  116. def delete_host(self , host_name):
  117. """
  118. Delete a cluster
  119. @param host_name: host to be deleted
  120. """
  121. return hosts._delete_host(self, host_name)
  122. def get_config(self, version, service_name):
  123. """
  124. get configurations from stack
  125. @param version: The HDP version.
  126. @param service_name: service name
  127. @return: A ConfigModel object
  128. """
  129. return stacks._get_config(self, version, service_name)
  130. def get_components(self, version, service_name):
  131. """
  132. get components from stack
  133. @param version: The HDP version.
  134. @param service_name: service name
  135. @return: A ConfigModel object
  136. """
  137. return stacks._get_components(self, version, service_name)
  138. def get_root_resource(server_host, server_port=None, username="admin", password="admin",
  139. version=1):
  140. """
  141. AmbariClient.
  142. """
  143. return AmbariClient(server_host, server_port, username, password, version)