Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit 0347450

Browse files
committed
Add support for shard duration in retention policies
- SHARD DURATION was added in 45b3e61, this adds support to the python client so it can be modified/added
1 parent 045e76f commit 0347450

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

influxdb/client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ def drop_database(self, dbname):
482482
self.query("DROP DATABASE \"%s\"" % dbname)
483483

484484
def create_retention_policy(self, name, duration, replication,
485-
database=None, default=False):
485+
database=None, default=False,
486+
shard_duration=None):
486487
"""Create a retention policy for a database.
487488
488489
:param name: the name of the new retention policy
@@ -501,19 +502,26 @@ def create_retention_policy(self, name, duration, replication,
501502
:type database: str
502503
:param default: whether or not to set the policy as default
503504
:type default: bool
505+
:param shard_duration: the shard duration of the retention policy.
506+
Durations are similar to those allowed in the duration param.
507+
:type shard_duration: str
504508
"""
505509
query_string = \
506510
"CREATE RETENTION POLICY \"%s\" ON \"%s\" " \
507511
"DURATION %s REPLICATION %s" % \
508512
(name, database or self._database, duration, replication)
509513

514+
if shard_duration:
515+
query_string += " SHARD DURATION %s" % shard_duration
516+
510517
if default is True:
511518
query_string += " DEFAULT"
512519

513520
self.query(query_string)
514521

515522
def alter_retention_policy(self, name, database=None,
516-
duration=None, replication=None, default=None):
523+
duration=None, replication=None, default=None,
524+
shard_duration=None):
517525
"""Mofidy an existing retention policy for a database.
518526
519527
:param name: the name of the retention policy to modify
@@ -533,9 +541,13 @@ def alter_retention_policy(self, name, database=None,
533541
:type replication: str
534542
:param default: whether or not to set the modified policy as default
535543
:type default: bool
544+
:param shard_duration: the shard duration of the retention policy.
545+
Durations are similar to those allowed in the duration param.
546+
:type shard_duration: str
536547
537-
.. note:: at least one of duration, replication, or default flag
538-
should be set. Otherwise the operation will fail.
548+
.. note:: at least one of duration, replication, default or
549+
shard_duration flag should be set. Otherwise the operation
550+
will fail.
539551
"""
540552
query_string = (
541553
"ALTER RETENTION POLICY \"{0}\" ON \"{1}\""
@@ -544,6 +556,8 @@ def alter_retention_policy(self, name, database=None,
544556
query_string += " DURATION {0}".format(duration)
545557
if replication:
546558
query_string += " REPLICATION {0}".format(replication)
559+
if shard_duration:
560+
query_string += " SHARD DURATION {0}".format(shard_duration)
547561
if default is True:
548562
query_string += " DEFAULT"
549563

influxdb/tests/client_test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,47 @@ def test_create_retention_policy(self):
555555
'"db" duration 1d replication 4'
556556
)
557557

558+
def test_create_retention_policy_shard_duration(self):
559+
example_response = '{"results":[{}]}'
560+
561+
with requests_mock.Mocker() as m:
562+
m.register_uri(
563+
requests_mock.GET,
564+
"http://localhost:8086/query",
565+
text=example_response
566+
)
567+
self.cli.create_retention_policy(
568+
'somename2', '1d', 4, database='db',
569+
shard_duration='1h'
570+
)
571+
572+
self.assertEqual(
573+
m.last_request.qs['q'][0],
574+
'create retention policy somename on '
575+
'db duration 1d replication 4 shard duration 1h'
576+
)
577+
578+
def test_create_retention_policy_shard_duration_default(self):
579+
example_response = '{"results":[{}]}'
580+
581+
with requests_mock.Mocker() as m:
582+
m.register_uri(
583+
requests_mock.GET,
584+
"http://localhost:8086/query",
585+
text=example_response
586+
)
587+
self.cli.create_retention_policy(
588+
'somename3', '1d', 4, database='db',
589+
shard_duration='1h', default=True
590+
)
591+
592+
self.assertEqual(
593+
m.last_request.qs['q'][0],
594+
'create retention policy somename on '
595+
'db duration 1d replication 4 shard duration 1h '
596+
'default'
597+
)
598+
558599
def test_alter_retention_policy(self):
559600
example_response = '{"results":[{}]}'
560601

0 commit comments

Comments
 (0)