Skip to content

Commit b795fa7

Browse files
author
Quan Yang
committed
[Cloudera Manager] OPSAPS-34769 Update CM python API Client
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.
1 parent c9525b2 commit b795fa7

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

python/src/cm_api/endpoints/services.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,36 @@ def query_activities(self, query_str=None):
164164
def get_activity(self, job_id):
165165
return self._get("activities/" + job_id, ApiActivity)
166166

167+
def list_watched_directories(self):
168+
"""
169+
Returns a list of directories being watched by the Reports Manager.
170+
171+
@return: A list of directories being watched
172+
@since: API v14
173+
"""
174+
return self._get("watcheddir", ApiWatchedDir, ret_is_list=True, api_version=14)
175+
176+
def add_watched_directory(self, dir_path):
177+
"""
178+
Adds a directory to the watching list.
179+
180+
@param dir_path: The path of the directory to be added to the watching list
181+
@return: The added directory, or null if failed
182+
@since: API v14
183+
"""
184+
req = ApiWatchedDir(self._get_resource_root(), path=dir_path)
185+
return self._post("watcheddir", ApiWatchedDir, data=req, api_version=14)
186+
187+
def remove_watched_directory(self, dir_path):
188+
"""
189+
Removes a directory from the watching list.
190+
191+
@param dir_path: The path of the directory to be removed from the watching list
192+
@return: The removed directory, or null if failed
193+
@since: API v14
194+
"""
195+
return self._delete("watcheddir/%s" % dir_path, ApiWatchedDir, api_version=14)
196+
167197
def get_impala_queries(self, start_time, end_time, filter_str="", limit=100,
168198
offset=0):
169199
"""

python/src/cm_api/endpoints/types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,25 @@ class ApiImpalaQuery(BaseApiObject):
10501050
def __str__(self):
10511051
return "<ApiImpalaQuery>: %s" % (self.queryId)
10521052

1053+
#
1054+
# WatchedDirectories data types
1055+
#
1056+
1057+
class ApiWatchedDir(BaseApiObject):
1058+
1059+
_ATTRIBUTES = {
1060+
'path' : None
1061+
}
1062+
1063+
def __str__(self):
1064+
return "<ApiWatchedDir>: %s" % (self.path)
1065+
1066+
class ApiWatchedDirList(ApiList):
1067+
1068+
_ATTRIBUTES = {
1069+
'watchedDirs' : ROAttr(ApiWatchedDir)
1070+
}
1071+
_MEMBER_CLASS = ApiWatchedDir
10531072

10541073
class ApiImpalaQueryResponse(BaseApiObject):
10551074

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Licensed to Cloudera, Inc. under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. Cloudera, Inc. licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import unittest
18+
from cm_api.endpoints.clusters import *
19+
from cm_api.endpoints.services import *
20+
from cm_api.endpoints.types import *
21+
from cm_api_tests import utils
22+
23+
class TestWatchedDir(unittest.TestCase):
24+
25+
def test_empty_list_watched_directories(self):
26+
resource = utils.MockResource(self)
27+
service = ApiService(resource, name="bar")
28+
29+
resource.expect("GET", "/cm/service/watcheddir",
30+
retdata={ "items" : [] })
31+
32+
responses = service.list_watched_directories()
33+
self.assertIsInstance(responses, ApiList)
34+
self.assertEquals(0, len(responses))
35+
36+
def test_non_empty_list_watched_directories(self):
37+
WATCHED_DIRS_LIST = '''{
38+
"items": [ {
39+
"path" : "/dir1"
40+
}, {
41+
"path" : "/dir2"
42+
} ]
43+
}'''
44+
45+
resource = utils.MockResource(self)
46+
service = ApiService(resource, name="bar")
47+
48+
resource.expect("GET", "/cm/service/watcheddir",
49+
retdata=json.loads(WATCHED_DIRS_LIST))
50+
responses = service.list_watched_directories()
51+
52+
self.assertIsInstance(responses, ApiList)
53+
self.assertEquals(2, len(responses))
54+
response = responses[0]
55+
self.assertIsInstance(response, ApiWatchedDir)
56+
self.assertEquals("/dir1", response.path)
57+
response = responses[1]
58+
self.assertIsInstance(response, ApiWatchedDir)
59+
self.assertEquals("/dir2", response.path)
60+
61+
def test_add_watched_directory(self):
62+
resource = utils.MockResource(self)
63+
service = ApiService(resource, name="bar")
64+
65+
resource.expect("POST", "/cm/service/watcheddir",
66+
retdata={ 'path' : '/dir' }, data={ 'path' : '/dir' })
67+
response = service.add_watched_directory("/dir")
68+
self.assertIsInstance(response, ApiWatchedDir)
69+
self.assertEquals("/dir", response.path)
70+
71+
def test_remove_watched_directory(self):
72+
resource = utils.MockResource(self)
73+
service = ApiService(resource, name="bar")
74+
75+
resource.expect("DELETE", "/cm/service/watcheddir/one_dir_path",
76+
retdata={ 'path' : 'one_dir_path' })
77+
response = service.remove_watched_directory("one_dir_path")
78+
self.assertIsInstance(response, ApiWatchedDir)
79+
self.assertEquals("one_dir_path", response.path)

0 commit comments

Comments
 (0)