Skip to content

Commit e683a1c

Browse files
committed
Add alter_retention_policy method to the client.
1 parent ab2e66c commit e683a1c

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

influxdb/client.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,43 @@ def create_retention_policy(self, name, duration, replication,
470470

471471
self.query(query_string)
472472

473+
def alter_retention_policy(self, name, database=None,
474+
duration=None, replication=None, default=None):
475+
"""Mofidy an existing retention policy for a database.
476+
477+
:param name: the name of the retention policy to modify
478+
:type name: str
479+
:param database: the database for which the retention policy is
480+
modified. Defaults to current client's database
481+
:type database: str
482+
:param duration: the new duration of the existing retention policy.
483+
Durations such as 1h, 90m, 12h, 7d, and 4w, are all supported
484+
and mean 1 hour, 90 minutes, 12 hours, 7 day, and 4 weeks,
485+
respectively. For infinite retention – meaning the data will
486+
never be deleted – use 'INF' for duration.
487+
The minimum retention period is 1 hour.
488+
:type duration: str
489+
:param replication: the new replication of the existing
490+
retention policy
491+
:type replication: str
492+
:param default: whether or not to set the modified policy as default
493+
:type default: bool
494+
495+
.. note:: at least one of duration, replication, or default flag
496+
should be set. Otherwise the operation will fail.
497+
"""
498+
query_string = (
499+
"ALTER RETENTION POLICY {} ON {}"
500+
).format(name, database or self._database)
501+
if duration:
502+
query_string += " DURATION {}".format(duration)
503+
if replication:
504+
query_string += " REPLICATION {}".format(replication)
505+
if default is True:
506+
query_string += " DEFAULT"
507+
508+
self.query(query_string)
509+
473510
def get_list_retention_policies(self, database=None):
474511
"""Get the list of retention policies for a database.
475512

tests/influxdb/client_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,44 @@ def test_create_retention_policy(self):
490490
'db duration 1d replication 4'
491491
)
492492

493+
def test_alter_retention_policy(self):
494+
example_response = '{"results":[{}]}'
495+
496+
with requests_mock.Mocker() as m:
497+
m.register_uri(
498+
requests_mock.GET,
499+
"http://localhost:8086/query",
500+
text=example_response
501+
)
502+
# Test alter duration
503+
self.cli.alter_retention_policy('somename', 'db',
504+
duration='4d')
505+
self.assertEqual(
506+
m.last_request.qs['q'][0],
507+
'alter retention policy somename on db duration 4d'
508+
)
509+
# Test alter replication
510+
self.cli.alter_retention_policy('somename', 'db',
511+
replication=4)
512+
self.assertEqual(
513+
m.last_request.qs['q'][0],
514+
'alter retention policy somename on db replication 4'
515+
)
516+
517+
# Test alter default
518+
self.cli.alter_retention_policy('somename', 'db',
519+
default=True)
520+
self.assertEqual(
521+
m.last_request.qs['q'][0],
522+
'alter retention policy somename on db default'
523+
)
524+
525+
@raises(Exception)
526+
def test_alter_retention_policy_invalid(self):
527+
cli = InfluxDBClient('host', 8086, 'username', 'password')
528+
with _mocked_session(cli, 'get', 400):
529+
self.cli.alter_retention_policy('somename', 'db')
530+
493531
def test_get_list_retention_policies(self):
494532
example_response = \
495533
'{"results": [{"series": [{"values": [["fsfdsdf", "24h0m0s", 2]],'\

tests/influxdb/client_test_with_server.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,61 @@ def test_create_retention_policy(self):
657657
rsp
658658
)
659659

660+
def test_alter_retention_policy(self):
661+
self.cli.create_retention_policy('somename', '1d', 1)
662+
663+
# Test alter duration
664+
self.cli.alter_retention_policy('somename', 'db',
665+
duration='4d')
666+
rsp = self.cli.get_list_retention_policies()
667+
self.assertEqual(
668+
[{'duration': '0', 'default': True,
669+
'replicaN': 1, 'name': 'default'},
670+
{'duration': '96h0m0s', 'default': False,
671+
'replicaN': 1, 'name': 'somename'}],
672+
rsp
673+
)
674+
675+
# Test alter replication
676+
self.cli.alter_retention_policy('somename', 'db',
677+
replication=4)
678+
rsp = self.cli.get_list_retention_policies()
679+
self.assertEqual(
680+
[{'duration': '0', 'default': True,
681+
'replicaN': 1, 'name': 'default'},
682+
{'duration': '96h0m0s', 'default': False,
683+
'replicaN': 4, 'name': 'somename'}],
684+
rsp
685+
)
686+
687+
# Test alter default
688+
self.cli.alter_retention_policy('somename', 'db',
689+
default=True)
690+
rsp = self.cli.get_list_retention_policies()
691+
self.assertEqual(
692+
[{'duration': '0', 'default': False,
693+
'replicaN': 1, 'name': 'default'},
694+
{'duration': '96h0m0s', 'default': True,
695+
'replicaN': 4, 'name': 'somename'}],
696+
rsp
697+
)
698+
699+
def test_alter_retention_policy_invalid(self):
700+
self.cli.create_retention_policy('somename', '1d', 1)
701+
with self.assertRaises(InfluxDBClientError) as ctx:
702+
self.cli.alter_retention_policy('somename', 'db')
703+
self.assertEqual(400, ctx.exception.code)
704+
self.assertIn('{"error":"error parsing query: ',
705+
ctx.exception.content)
706+
rsp = self.cli.get_list_retention_policies()
707+
self.assertEqual(
708+
[{'duration': '0', 'default': True,
709+
'replicaN': 1, 'name': 'default'},
710+
{'duration': '24h0m0s', 'default': False,
711+
'replicaN': 1, 'name': 'somename'}],
712+
rsp
713+
)
714+
660715
def test_issue_143(self):
661716
pt = partial(point, 'a_serie_name', timestamp='2015-03-30T16:16:37Z')
662717
pts = [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy