Skip to content

Commit

Permalink
[Cloudera Manager] OPSAPS-34769 Update CM python API Client
Browse files Browse the repository at this point in the history
for new add/list/delete watched dirs

Description:
Add 'add/list/remove watcheddir' methods in 'services.py' as the client functions;
Add 'ApiWatchedDirList' and 'ApiWatchedDir' classes in 'types.py' as new
data types; Create 'test_watcheddir.py' and add four test cases capturing
'add/list/remove watched directories' behaviors.

Test done:
End-to-end test, running the added methods (in 'services.py') on CM server and
verify results;
python setup.py install;
make clean test.
  • Loading branch information
Quan Yang committed Jul 14, 2016
1 parent c9525b2 commit b795fa7
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
30 changes: 30 additions & 0 deletions python/src/cm_api/endpoints/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ def query_activities(self, query_str=None):
def get_activity(self, job_id):
return self._get("activities/" + job_id, ApiActivity)

def list_watched_directories(self):
"""
Returns a list of directories being watched by the Reports Manager.
@return: A list of directories being watched
@since: API v14
"""
return self._get("watcheddir", ApiWatchedDir, ret_is_list=True, api_version=14)

def add_watched_directory(self, dir_path):
"""
Adds a directory to the watching list.
@param dir_path: The path of the directory to be added to the watching list
@return: The added directory, or null if failed
@since: API v14
"""
req = ApiWatchedDir(self._get_resource_root(), path=dir_path)
return self._post("watcheddir", ApiWatchedDir, data=req, api_version=14)

def remove_watched_directory(self, dir_path):
"""
Removes a directory from the watching list.
@param dir_path: The path of the directory to be removed from the watching list
@return: The removed directory, or null if failed
@since: API v14
"""
return self._delete("watcheddir/%s" % dir_path, ApiWatchedDir, api_version=14)

def get_impala_queries(self, start_time, end_time, filter_str="", limit=100,
offset=0):
"""
Expand Down
19 changes: 19 additions & 0 deletions python/src/cm_api/endpoints/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,25 @@ class ApiImpalaQuery(BaseApiObject):
def __str__(self):
return "<ApiImpalaQuery>: %s" % (self.queryId)

#
# WatchedDirectories data types
#

class ApiWatchedDir(BaseApiObject):

_ATTRIBUTES = {
'path' : None
}

def __str__(self):
return "<ApiWatchedDir>: %s" % (self.path)

class ApiWatchedDirList(ApiList):

_ATTRIBUTES = {
'watchedDirs' : ROAttr(ApiWatchedDir)
}
_MEMBER_CLASS = ApiWatchedDir

class ApiImpalaQueryResponse(BaseApiObject):

Expand Down
79 changes: 79 additions & 0 deletions python/src/cm_api_tests/test_watcheddir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Licensed to Cloudera, Inc. under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. Cloudera, Inc. licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from cm_api.endpoints.clusters import *
from cm_api.endpoints.services import *
from cm_api.endpoints.types import *
from cm_api_tests import utils

class TestWatchedDir(unittest.TestCase):

def test_empty_list_watched_directories(self):
resource = utils.MockResource(self)
service = ApiService(resource, name="bar")

resource.expect("GET", "/cm/service/watcheddir",
retdata={ "items" : [] })

responses = service.list_watched_directories()
self.assertIsInstance(responses, ApiList)
self.assertEquals(0, len(responses))

def test_non_empty_list_watched_directories(self):
WATCHED_DIRS_LIST = '''{
"items": [ {
"path" : "/dir1"
}, {
"path" : "/dir2"
} ]
}'''

resource = utils.MockResource(self)
service = ApiService(resource, name="bar")

resource.expect("GET", "/cm/service/watcheddir",
retdata=json.loads(WATCHED_DIRS_LIST))
responses = service.list_watched_directories()

self.assertIsInstance(responses, ApiList)
self.assertEquals(2, len(responses))
response = responses[0]
self.assertIsInstance(response, ApiWatchedDir)
self.assertEquals("/dir1", response.path)
response = responses[1]
self.assertIsInstance(response, ApiWatchedDir)
self.assertEquals("/dir2", response.path)

def test_add_watched_directory(self):
resource = utils.MockResource(self)
service = ApiService(resource, name="bar")

resource.expect("POST", "/cm/service/watcheddir",
retdata={ 'path' : '/dir' }, data={ 'path' : '/dir' })
response = service.add_watched_directory("/dir")
self.assertIsInstance(response, ApiWatchedDir)
self.assertEquals("/dir", response.path)

def test_remove_watched_directory(self):
resource = utils.MockResource(self)
service = ApiService(resource, name="bar")

resource.expect("DELETE", "/cm/service/watcheddir/one_dir_path",
retdata={ 'path' : 'one_dir_path' })
response = service.remove_watched_directory("one_dir_path")
self.assertIsInstance(response, ApiWatchedDir)
self.assertEquals("one_dir_path", response.path)

0 comments on commit b795fa7

Please sign in to comment.