Skip to content

Commit e84e7b7

Browse files
committed
Merge pull request #75 from whisk3y/master
Add helper functions to replace all or one ruleset
2 parents a487185 + 54547ff commit e84e7b7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

dyn/tm/services/dsf.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'DSFResponsePool', 'DSFRuleset', 'DSFMonitorEndpoint', 'DSFMonitor',
2828
'TrafficDirector']
2929

30+
3031
def get_all_dsf_services():
3132
""":return: A ``list`` of :class:`TrafficDirector` Services"""
3233
uri = '/DSF/'
@@ -3370,6 +3371,48 @@ def remove_orphans(self):
33703371
api_args = {'remove_orphans': 'Y'}
33713372
self._update(api_args)
33723373

3374+
def replace_all_rulesets(self, rulesets):
3375+
"""
3376+
This request will replace all rulesets with a new list of rulesets.
3377+
:param rulesets: a list of rulesets :class:DSFRuleset to be published to the service
3378+
Warning! This call takes extra time as it is several api calls.
3379+
"""
3380+
if (type(rulesets) is list or type(rulesets) is tuple) and isinstance(rulesets[0], DSFRuleset):
3381+
old_rulesets = self.all_rulesets
3382+
for old_rule in old_rulesets:
3383+
old_rule.delete()
3384+
for new_rule in rulesets:
3385+
new_rule._dsf_ruleset_id = None
3386+
new_rule.create(self)
3387+
else:
3388+
raise Exception("rulesets parameter must be a list of DSFRuleset objects")
3389+
3390+
def replace_one_ruleset(self, ruleset):
3391+
"""
3392+
This request will replace a single ruleset and maintain the order of the list.
3393+
:param ruleset: A single object of :class:DSFRuleset`
3394+
The replacement is keyed by the DSFRuleset label value
3395+
Warning! This call takes extra time as it is several api calls.
3396+
"""
3397+
if isinstance(ruleset, DSFRuleset):
3398+
old_rulesets = self.all_rulesets
3399+
old_rule = None
3400+
rule_index = 0
3401+
for rule in old_rulesets:
3402+
if rule.label == ruleset.label:
3403+
old_rule = rule
3404+
rule_index += 1
3405+
break
3406+
rule_index += 1
3407+
if old_rule is not None:
3408+
old_rule.delete()
3409+
ruleset._dsf_ruleset_id = None
3410+
ruleset.create(self, index=rule_index)
3411+
else:
3412+
ruleset.create(self)
3413+
else:
3414+
raise Exception("rulesets parameter must be a single object of class DSFRuleset")
3415+
33733416
@property
33743417
def service_id(self):
33753418
"""The unique System id of this DSF Service"""

0 commit comments

Comments
 (0)