From 0ef94e4176f4ff291c5be2f5c2884020163a24a8 Mon Sep 17 00:00:00 2001 From: Mohammad Derakhshani Date: Mon, 19 Jun 2017 22:04:16 -0700 Subject: [PATCH] cosmosdb: add support for default ttl (#3761) * documentdb cli minor improvement * updates help description for credentials * minor change to formatting of failure error message * --coll-name changed to --collection-name * documentdb: minor error message change * pyproj updated to include all cosmosdb files * cosmosdb: add support for collection default ttl * cosmosdb: history file updated * updated pyproj files * fixed code style * cosmosdb: review comments addressed * addressed code style issue * code style issue fixed * logic simplified * fixed history * remove unwanted stashed changes * remove unwanted files --- .gitignore | 3 ++- azure-cli.pyproj | 21 ++++++++++++++++++- azure-cli2017.pyproj | 3 +-- .../azure-cli-cosmosdb/HISTORY.rst | 7 +++++++ .../cli/command_modules/cosmosdb/_params.py | 2 ++ .../cli/command_modules/cosmosdb/custom.py | 20 ++++++++++++------ 6 files changed, 46 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index e55a696e6..05c0669b1 100644 --- a/.gitignore +++ b/.gitignore @@ -83,4 +83,5 @@ test_results/ artifacts/ # Nose test output -.noseids \ No newline at end of file +.noseids + diff --git a/azure-cli.pyproj b/azure-cli.pyproj index 842d462d2..8e8d73660 100644 --- a/azure-cli.pyproj +++ b/azure-cli.pyproj @@ -295,6 +295,18 @@ + + + + + + + + + + + + @@ -863,6 +875,12 @@ + + + + + + @@ -1073,6 +1091,7 @@ + @@ -1153,4 +1172,4 @@ - \ No newline at end of file + diff --git a/azure-cli2017.pyproj b/azure-cli2017.pyproj index a44411b7a..be5e84791 100644 --- a/azure-cli2017.pyproj +++ b/azure-cli2017.pyproj @@ -243,7 +243,6 @@ - @@ -936,4 +935,4 @@ - \ No newline at end of file + diff --git a/src/command_modules/azure-cli-cosmosdb/HISTORY.rst b/src/command_modules/azure-cli-cosmosdb/HISTORY.rst index 5987aaf3c..77976dc87 100644 --- a/src/command_modules/azure-cli-cosmosdb/HISTORY.rst +++ b/src/command_modules/azure-cli-cosmosdb/HISTORY.rst @@ -2,6 +2,12 @@ Release History =============== + +unreleased +++++++++++++++++++ + +* Added Support for Collection Default TTL. + 0.1.8 (2017-06-13) ++++++++++++++++++ * Remove useless line-too-long suppression @@ -14,6 +20,7 @@ Release History 0.1.6 (2017-05-09) ++++++++++++++++++ + * Rename documentdb module to cosmosdb. 0.1.5 (2017-05-05) diff --git a/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_params.py b/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_params.py index 044f96005..b27e57a1b 100644 --- a/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_params.py +++ b/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_params.py @@ -90,3 +90,5 @@ register_cli_argument('cosmosdb collection', 'indexing_policy', options_list=('--indexing-policy'), help='Indexing Policy, you can enter it as a string or as a file, e.g., --indexing-policy @policy-file.json)', type=shell_safe_json_parse, completer=FilesCompleter()) + +register_cli_argument('cosmosdb collection', 'default_ttl', help='Default TTL', type=int) diff --git a/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/custom.py b/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/custom.py index 6c7d154bb..82f48857d 100644 --- a/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/custom.py +++ b/src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/custom.py @@ -217,19 +217,23 @@ def cli_cosmosdb_collection_delete(client, database_id, collection_id): def _populate_collection_definition(collection, partition_key_path=None, + default_ttl=None, indexing_policy=None): - changed = False + if all(arg is None for arg in [partition_key_path, default_ttl, indexing_policy]): + return False - if partition_key_path: + if partition_key_path is not None: if 'partitionKey' not in collection: collection['partitionKey'] = {} collection['partitionKey'] = {'paths': [partition_key_path]} - changed = True - if indexing_policy: - changed = True + if default_ttl is not None: + collection['defaultTtl'] = default_ttl + + if indexing_policy is not None: collection['indexingPolicy'] = indexing_policy - return changed + + return True def cli_cosmosdb_collection_create(client, @@ -237,6 +241,7 @@ def cli_cosmosdb_collection_create(client, collection_id, throughput=None, partition_key_path=None, + default_ttl=None, indexing_policy=DEFAULT_INDEXING_POLICY): """Creates an Azure Cosmos DB collection """ collection = {'id': collection_id} @@ -247,6 +252,7 @@ def cli_cosmosdb_collection_create(client, _populate_collection_definition(collection, partition_key_path, + default_ttl, indexing_policy) created_collection = client.CreateCollection(_get_database_link(database_id), collection, @@ -268,6 +274,7 @@ def cli_cosmosdb_collection_update(client, database_id, collection_id, throughput=None, + default_ttl=None, indexing_policy=None): """Updates an Azure Cosmos DB collection """ logger.debug('reading collection') @@ -276,6 +283,7 @@ def cli_cosmosdb_collection_update(client, if (_populate_collection_definition(collection, None, + default_ttl, indexing_policy)): logger.debug('replacing collection') result['collection'] = client.ReplaceCollection(