forked from Ensighten/ansible-module-cassandra
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the possibility to alter a keyspace
Based on the work of Ensighten#1
- Loading branch information
dgoldenberg
committed
Aug 2, 2018
1 parent
d65156c
commit 4b938e7
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
# TODO: add documentation here | ||
try: | ||
import json | ||
except ImportError: | ||
import simplejson as json | ||
except: | ||
json_dep_found = False | ||
else: | ||
json_dep_found = True | ||
|
||
try: | ||
from cassandra.cluster import Cluster | ||
from cassandra.auth import PlainTextAuthProvider | ||
from cassandra.query import dict_factory | ||
except ImportError: | ||
cassandra_dep_found = False | ||
else: | ||
cassandra_dep_found = True | ||
|
||
|
||
ALTER_KEYSPACE_FORMAT = 'ALTER KEYSPACE {keyspace} WITH REPLICATION={config} AND DURABLE_WRITES={durable_writes}' | ||
|
||
|
||
def alter_keyspace(module, session, keyspace, replication_strategy, durable_writes): | ||
changed = True | ||
reasons = [] | ||
cql = ALTER_KEYSPACE_FORMAT.format(keyspace=keyspace, | ||
config=json.dumps(replication_strategy).replace('"', '\''), | ||
durable_writes=durable_writes) | ||
|
||
session.execute(cql) | ||
|
||
return changed, reasons | ||
|
||
|
||
def main(): | ||
|
||
arg_spec = { | ||
'keyspace': { | ||
'type': 'str', | ||
'required': True, | ||
}, | ||
'replication_strategy': { | ||
'type': 'dict', | ||
'required': True, | ||
}, | ||
'durable_writes': { | ||
'type': 'bool', | ||
'required': True, | ||
}, | ||
'login_user': { | ||
'type': 'str', | ||
'required': True, | ||
}, | ||
'login_password': { | ||
'type': 'str', | ||
'required': True, | ||
'no_log': True | ||
}, | ||
'login_hosts': { | ||
'type': 'list', | ||
'required': True, | ||
}, | ||
'login_port': { | ||
'type': 'int', | ||
'default': 9042, | ||
'required': False, | ||
}, | ||
'protocol': { | ||
'type': 'int', | ||
'default': 3, | ||
'required': False, | ||
}, | ||
} | ||
|
||
module = AnsibleModule(argument_spec=arg_spec) | ||
|
||
keyspace = module.params['keyspace'] | ||
replication_strategy = module.params['replication_strategy'] | ||
durable_writes = module.params['durable_writes'] | ||
login_hosts = module.params['login_hosts'] | ||
login_port = module.params['login_port'] | ||
login_user = module.params['login_user'] | ||
login_password = module.params['login_password'] | ||
protocol = module.params['protocol'] | ||
|
||
if not cassandra_dep_found: | ||
module.fail_json(msg="the python cassandra-driver module is required") | ||
|
||
if not json_dep_found: | ||
module.fail_json(msg="the python json or simplejson module is required") | ||
|
||
try: | ||
if not login_user: | ||
cluster = Cluster(login_hosts, port=login_port) | ||
|
||
else: | ||
auth_provider = PlainTextAuthProvider(username=login_user, | ||
password=login_password) | ||
cluster = Cluster(login_hosts, auth_provider=auth_provider, | ||
protocol_version=protocol, port=login_port) | ||
session = cluster.connect() | ||
session.row_factory = dict_factory | ||
except Exception, e: | ||
module.fail_json( | ||
msg="unable to connect to cassandra, check login_user and " + | ||
"login_password are correct. Exception message: %s" | ||
% e) | ||
|
||
changed, reasons = alter_keyspace(module, session, keyspace, replication_strategy, durable_writes) | ||
|
||
module.exit_json(changed=changed, msg='OK', name=keyspace, reasons=reasons) | ||
|
||
# Ansible "magic" (NOQA comments tells flake8 to ignore this line since it's | ||
# bad Python, but required for Ansible) | ||
from ansible.module_utils.basic import * # NOQA | ||
main() |