diff --git a/f5/bigip/ltm/nat.py b/f5/bigip/ltm/nat.py index b8cce2ef3..02f7716d5 100644 --- a/f5/bigip/ltm/nat.py +++ b/f5/bigip/ltm/nat.py @@ -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 @@ -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. @@ -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 diff --git a/test/functional/ltm/test_nat.py b/test/functional/ltm/test_nat.py index d767a0750..49cc76cd6 100644 --- a/test/functional/ltm/test_nat.py +++ b/test/functional/ltm/test_nat.py @@ -13,6 +13,7 @@ # limitations under the License. # +from pprint import pprint as pp import pytest from f5.bigip.resource import MissingRequiredCreationParameter @@ -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') @@ -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 @@ -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 @@ -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' @@ -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') @@ -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) @@ -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'