Skip to content

Commit

Permalink
Merge pull request #260 from zancas/feature.implement_nat_disable_ena…
Browse files Browse the repository at this point in the history
…ble_109

en- dis- able both work for nat create/update
  • Loading branch information
pjbreaux committed Mar 9, 2016
2 parents ce35ac7 + f4df85f commit 62d29b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
17 changes: 16 additions & 1 deletion f5/bigip/ltm/nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
``tm:ltm:nat:*``
"""

from f5.bigip.mixins import ExclusiveAttributesMixin
from f5.bigip.resource import Collection
from f5.bigip.resource import MissingRequiredCreationParameter
from f5.bigip.resource import Resource
Expand All @@ -38,13 +39,14 @@ def __init__(self, ltm):
{'tm:ltm:nat:natstate': Nat}


class Nat(Resource):
class Nat(Resource, ExclusiveAttributesMixin):
"""BigIP LTM Nat collection resource"""
def __init__(self, nat_s):
super(Nat, self).__init__(nat_s)
self._meta_data['required_creation_parameters'].update(
('originatingAddress', 'translationAddress', 'partition'))
self._meta_data['required_json_kind'] = 'tm:ltm:nat:natstate'
self._meta_data['exclusive_attributes'].append(('enable', 'disable'))

def create(self, **kwargs):
"""Create the resource on the BigIP.
Expand Down Expand Up @@ -85,11 +87,24 @@ def create(self, **kwargs):
% kwargs['trafficGroup'])
except KeyError:
pass
kwargs = self._endis_able(kwargs)
self._create(**kwargs)
return self

def _endis_able(self, config_dict):
if 'enabled' in config_dict and not config_dict['enabled']:
config_dict['disabled'] = True
elif 'disabled' in config_dict and not config_dict['disabled']:
config_dict['enabled'] = True
return config_dict

def update(self, **kwargs):
# This is an example implementation of read-only params
stash_translation_address = self.__dict__.pop('translationAddress')
if 'enabled' in self.__dict__ and 'enabled' not in kwargs:
kwargs['enabled'] = self.__dict__.pop('enabled')
elif 'disabled' in self.__dict__ and 'disabled' not in kwargs:
kwargs['disabled'] = self.__dict__.pop('disabled')
kwargs = self._endis_able(kwargs)
self._update(**kwargs)
self.__dict__['translationAddress'] = stash_translation_address
28 changes: 20 additions & 8 deletions test/functional/ltm/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
#

from pprint import pprint as pp
import pytest

from f5.bigip.resource import MissingRequiredCreationParameter
Expand All @@ -30,7 +31,7 @@ def delete_nat(bigip, name, partition):
nat.delete()


def setup_standard_test(request, bigip, nat):
def setup_loadable_nat_test(request, bigip, nat):
def teardown():
delete_nat(bigip, 'nat1', 'Common')

Expand Down Expand Up @@ -177,7 +178,7 @@ def test_create_disabled_true(self, request, bigip, NAT):
disabled=True)
assert NAT.disabled is True

def itest_create_disabled_false(self, request, bigip, NAT):
def test_create_disabled_false(self, request, bigip, NAT):
'''Test that you can set enabled to false and create nat as disabled
This will fail until some fixups are made to the create function for
Expand All @@ -189,7 +190,18 @@ def itest_create_disabled_false(self, request, bigip, NAT):
translationAddress='192.168.1.1',
originatingAddress='192.168.2.1',
disabled=False)
assert NAT.disabled is False
pp(NAT.raw)
assert 'disabled' not in NAT.raw
assert NAT.enabled is True
NAT.enabled = False
NAT.update()
assert 'enabled' not in NAT.raw
assert NAT.disabled is True

NAT.disabled = False
NAT.update()
assert 'disabled' not in NAT.raw
assert NAT.enabled is True

def test_create_inheritedtrafficgroup_true(self, request, bigip, NAT):
'''Test that you can set inheritedTrafficGroup to True on create
Expand Down Expand Up @@ -293,7 +305,7 @@ def test_load_no_object(self, NAT):
assert err.response.status == 404

def test_load(self, request, bigip, NAT):
setup_standard_test(request, bigip, NAT)
setup_loadable_nat_test(request, bigip, NAT)
n1 = bigip.ltm.nats.nat.load(name='nat1', partition='Common')
assert n1.name == 'nat1'
assert n1.partition == 'Common'
Expand All @@ -303,7 +315,7 @@ def test_load(self, request, bigip, NAT):
class TestRefresh(object):

def test_refresh(self, request, bigip, NAT):
setup_standard_test(request, bigip, NAT)
setup_loadable_nat_test(request, bigip, NAT)

n1 = bigip.ltm.nats.nat.load(name='nat1', partition='Common')
n2 = bigip.ltm.nats.nat.load(name='nat1', partition='Common')
Expand All @@ -321,7 +333,7 @@ def test_refresh(self, request, bigip, NAT):
class TestDelete(object):

def test_delete(self, request, bigip, NAT):
setup_standard_test(request, bigip, NAT)
setup_loadable_nat_test(request, bigip, NAT)
n1 = bigip.ltm.nats.nat.load(name='nat1', partition='Common')
n1.delete()
del(n1)
Expand All @@ -332,14 +344,14 @@ def test_delete(self, request, bigip, NAT):

class TestUpdate(object):
def test_update_with_args(self, request, bigip, NAT):
setup_standard_test(request, bigip, NAT)
setup_loadable_nat_test(request, bigip, NAT)
n1 = bigip.ltm.nats.nat.load(name='nat1', partition='Common')
assert n1.arp == 'enabled'
n1.update(arp='disabled')
assert n1.arp == 'disabled'

def test_update_parameters(self, request, bigip, NAT):
setup_standard_test(request, bigip, NAT)
setup_loadable_nat_test(request, bigip, NAT)
n1 = bigip.ltm.nats.nat.load(name='nat1', partition='Common')
assert n1.arp == 'enabled'
n1.arp = 'disabled'
Expand Down

0 comments on commit 62d29b9

Please sign in to comment.