Skip to content

Commit 939986b

Browse files
authored
Merge pull request #1526 from jasonrahm/enh.external_monitor
Issue #1521 - Add sys/file external monitor suport
2 parents ed488cb + df94f1f commit 939986b

3 files changed

Lines changed: 101 additions & 14 deletions

File tree

f5/bigip/tm/sys/file.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding=utf-8
22
#
3-
# Copyright 2016 F5 Networks Inc.
3+
# Copyright 2019 F5 Networks Inc.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@ def __init__(self, sys):
3939
super(File, self).__init__(sys)
4040
self._meta_data['allowed_lazy_attributes'] = [
4141
Data_Groups,
42+
External_Monitors,
4243
Ifiles,
4344
Ssl_Certs,
4445
Ssl_Csrs,
@@ -72,6 +73,25 @@ def update(self, **kwargs):
7273
return self._update(**kwargs)
7374

7475

76+
class External_Monitors(Collection):
77+
"""BIG-IP® System sys file data-groups collection."""
78+
def __init__(self, File):
79+
super(External_Monitors, self).__init__(File)
80+
self._meta_data['allowed_lazy_attributes'] = [External_Monitor]
81+
self._meta_data['attribute_registry'] =\
82+
{'tm:sys:file:external-monitor:external-monitorstate': External_Monitor}
83+
84+
85+
class External_Monitor(Resource):
86+
"""BIG-IP® System sys file data-groups resource."""
87+
def __init__(self, external_monitors):
88+
super(External_Monitor, self).__init__(external_monitors)
89+
self._meta_data['required_json_kind'] =\
90+
'tm:sys:file:external-monitor:external-monitorstate'
91+
self._meta_data['required_creation_parameters'].update(
92+
('name', 'sourcePath'))
93+
94+
7595
class Ifiles(Collection):
7696
"""BIG-IP® System sys file iFiles collection."""
7797
def __init__(self, File):

f5/bigip/tm/sys/test/functional/test_file.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_CURDL_datagroup(request, mgmt_root):
9090
# Create
9191
ntf = NamedTemporaryFile(delete=False)
9292
ntf_basename = os.path.basename(ntf.name)
93-
ntf.write('"name1" := "value1",')
93+
ntf.write(b'"name1" := "value1",')
9494
ntf.seek(0)
9595
# Upload the file
9696
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
@@ -103,7 +103,7 @@ def test_CURDL_datagroup(request, mgmt_root):
103103
assert dg1.name == dg2.name
104104

105105
# Rewrite the contents and update the object
106-
ntf.write('"name2" := "value2",')
106+
ntf.write(b'"name2" := "value2",')
107107
ntf.seek(0)
108108
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
109109

@@ -120,6 +120,55 @@ def test_CURDL_datagroup(request, mgmt_root):
120120
assert dg1.revision != dg4.revision
121121

122122

123+
def setup_externalmonitor_test(request, mgmt_root, name, sourcepath, **kwargs):
124+
em1 = mgmt_root.tm.sys.file.external_monitors.external_monitor.create(name=name,
125+
sourcePath=sourcepath,
126+
**kwargs)
127+
128+
def teardown():
129+
# Remove the ifile.
130+
try:
131+
em1.delete()
132+
except HTTPError as err:
133+
if err.response.status_code != 404:
134+
raise
135+
136+
request.addfinalizer(teardown)
137+
138+
return em1
139+
140+
141+
def test_CURDL_externalmonitor(request, mgmt_root):
142+
# Create
143+
ntf = NamedTemporaryFile(delete=False)
144+
ntf_basename = os.path.basename(ntf.name)
145+
ntf.write(b'this is a test file')
146+
ntf.seek(0)
147+
# Upload the file
148+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
149+
150+
tpath_name = 'file:/var/config/rest/downloads/{0}'.format(ntf_basename)
151+
em1 = setup_externalmonitor_test(request, mgmt_root, ntf_basename, tpath_name)
152+
assert em1.name == ntf_basename
153+
154+
# Load Object
155+
em2 = mgmt_root.tm.sys.file.external_monitors.external_monitor.load(name=ntf_basename)
156+
assert em1.name == em2.name
157+
158+
# Rewrite file contents and Update Object
159+
ntf.write(b'this is still a test file')
160+
ntf.seek(0)
161+
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
162+
163+
em3 = mgmt_root.tm.sys.file.external_monitors.external_monitor.load(name=ntf_basename)
164+
em3.update(sourcePath=tpath_name)
165+
assert em1.revision != em3.revision
166+
167+
# Refresh em2 and make sure revision matches em3
168+
em2.refresh()
169+
assert em2.revision == em3.revision
170+
171+
123172
def setup_ifile_test(request, mgmt_root, name, sourcepath, **kwargs):
124173
if1 = mgmt_root.tm.sys.file.ifiles.ifile.create(name=name,
125174
sourcePath=sourcepath,
@@ -141,22 +190,21 @@ def test_CURDL_ifile(request, mgmt_root):
141190
# Create
142191
ntf = NamedTemporaryFile(delete=False)
143192
ntf_basename = os.path.basename(ntf.name)
144-
ntf.write('this is a test file')
193+
ntf.write(b'this is a test file')
145194
ntf.seek(0)
146195
# Upload the file
147196
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
148197

149198
tpath_name = 'file:/var/config/rest/downloads/{0}'.format(ntf_basename)
150-
if1 = setup_ifile_test(request, mgmt_root, ntf_basename, tpath_name,
151-
)
199+
if1 = setup_ifile_test(request, mgmt_root, ntf_basename, tpath_name)
152200
assert if1.name == ntf_basename
153201

154202
# Load Object
155203
if2 = mgmt_root.tm.sys.file.ifiles.ifile.load(name=ntf_basename)
156204
assert if1.name == if2.name
157205

158206
# Rewrite file contents and Update Object
159-
ntf.write('this is still a test file')
207+
ntf.write(b'this is still a test file')
160208
ntf.seek(0)
161209
mgmt_root.shared.file_transfer.uploads.upload_file(ntf.name)
162210

f5/bigip/tm/sys/test/unit/test_file.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pytest
1818

1919
from f5.bigip.tm.sys.file import Data_Group
20+
from f5.bigip.tm.sys.file import External_Monitor
2021
from f5.bigip.tm.sys.file import Ifile
2122
from f5.bigip.tm.sys.file import Ssl_Cert
2223
from f5.bigip.tm.sys.file import Ssl_Crl
@@ -41,8 +42,26 @@ def test_dg_create_no_args(FakeSysDatagroup):
4142
def test_dg_create_missing_arg(FakeSysDatagroup):
4243
with pytest.raises(MissingRequiredCreationParameter) as ex:
4344
FakeSysDatagroup.create(name='test_dg')
44-
assert 'sourcePath' in ex.value.message
45-
assert 'type' in ex.value.message
45+
assert 'sourcePath' in str(ex.value)
46+
assert 'type' in str(ex.value)
47+
48+
49+
@pytest.fixture
50+
def FakeSysExternalMonitor():
51+
fake_em_s = mock.MagicMock()
52+
fake_em = External_Monitor(fake_em_s)
53+
return fake_em
54+
55+
56+
def test_em_create_no_args(FakeSysExternalMonitor):
57+
with pytest.raises(MissingRequiredCreationParameter):
58+
FakeSysExternalMonitor.create()
59+
60+
61+
def test_em_create_missing_arg(FakeSysExternalMonitor):
62+
with pytest.raises(MissingRequiredCreationParameter) as ex:
63+
FakeSysExternalMonitor.create(name='test_em')
64+
assert 'sourcePath' in str(ex.value)
4665

4766

4867
@pytest.fixture
@@ -60,7 +79,7 @@ def test_ifile_create_no_args(FakeSysIfile):
6079
def test_ifile_create_missing_arg(FakeSysIfile):
6180
with pytest.raises(MissingRequiredCreationParameter) as ex:
6281
FakeSysIfile.create(name='test_ifile')
63-
assert 'sourcePath' in ex.value.message
82+
assert 'sourcePath' in str(ex.value)
6483

6584

6685
def test_ifile_modify(FakeSysIfile):
@@ -83,7 +102,7 @@ def test_cert_create_no_args(FakeSysCert):
83102
def test_cert_create_missing_arg(FakeSysCert):
84103
with pytest.raises(MissingRequiredCreationParameter) as ex:
85104
FakeSysCert.create(name='test_cert')
86-
assert 'sourcePath' in ex.value.message
105+
assert 'sourcePath' in str(ex.value)
87106

88107

89108
def test_cert_modify(FakeSysCert):
@@ -106,7 +125,7 @@ def test_crl_create_no_args(FakeSysCrl):
106125
def test_crl_create_missing_arg(FakeSysCrl):
107126
with pytest.raises(MissingRequiredCreationParameter) as ex:
108127
FakeSysCrl.create(name='test_crl')
109-
assert 'sourcePath' in ex.value.message
128+
assert 'sourcePath' in str(ex.value)
110129

111130

112131
def test_crl_modify(FakeSysCrl):
@@ -134,7 +153,7 @@ def test_csr_modify(FakeSysCsr):
134153
def test_csr_create_missing_arg(FakeSysCsr):
135154
with pytest.raises(MissingRequiredCreationParameter) as ex:
136155
FakeSysCsr.create(name='test_csr')
137-
assert 'sourcePath' in ex.value.message
156+
assert 'sourcePath' in str(ex.value)
138157

139158

140159
@pytest.fixture
@@ -152,7 +171,7 @@ def test_key_create_no_args(FakeSysKey):
152171
def test_key_create_missing_arg(FakeSysKey):
153172
with pytest.raises(MissingRequiredCreationParameter) as ex:
154173
FakeSysKey.create(name='test_key')
155-
assert 'sourcePath' in ex.value.message
174+
assert 'sourcePath' in str(ex.value)
156175

157176

158177
def test_key_modify(FakeSysKey):

0 commit comments

Comments
 (0)