Skip to content

Commit

Permalink
Add sr_cleanup method
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Nov 22, 2018
1 parent c137c35 commit cbad00c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# Version 0.1.0
- Add sr_cleanup method
- Improve instance init checking the parameters type

# Version 0.1.0
- First version of the project.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ List of dependencies are specified in the ["Requirements file"](requirements.txt

## License

See the [LICENSE](LICENSE.md) file for license rights and limitations.
See the [LICENSE](LICENSE) file for license rights and limitations.

## Author

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum==0.4.7
2 changes: 1 addition & 1 deletion shaptools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
:since: 2018-11-15
"""

__version__ = "0.1.0"
__version__ = "0.2.0"
24 changes: 19 additions & 5 deletions shaptools/hana.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class HanaInstance:
SYNCMODES = ['sync', 'syncmem', 'async']

def __init__(self, sid, inst, password):
if not all(isinstance(i, str) for i in [sid, inst, password]):
raise TypeError(
'provided sid, inst and password parameters must be str type')

self._logger = logging.getLogger('{}{}'.format(sid, inst))
self.sid = sid
self.inst = inst
Expand Down Expand Up @@ -199,7 +203,7 @@ def check_user_key(self, key):
Check the use key existance
Args:
key (str): User key name
key (str): Key name
Returns: True if it exists, False otherwise
"""
Expand All @@ -213,13 +217,13 @@ def check_user_key(self, key):
def create_user_key(
self, key, environment, user, user_password, database=None):
"""
Create user key entry for the database
Create or update user key entry for the database
Args:
key (str): User key
environment (str): Key environment
key (str): Key name
environment (str): Database location (host:port)
user (srt): User name
user_password (str): User password
database (str, opt): Database name
database (str, opt): Database name in MDC environment
"""
database = '@{}'.format(database) if database else None
cmd = 'hdbuserstore set {key} {env}{db} {user} {passwd}'.format(
Expand All @@ -244,6 +248,16 @@ def create_backup(
user_key, database, user_password, backup_name)
self._run_hana_command(cmd)

def sr_cleanup(self, force=False):
"""
Clean system replication state
Args:
force (bool): Force cleanup
"""
cmd = 'hdbnsutil -sr_cleanup{}'.format(' --force' if force else '')
self._run_hana_command(cmd)

"""
# pylint:disable=C0103
if __name__ == '__main__':
Expand Down
37 changes: 37 additions & 0 deletions tests/hana_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ def tearDownClass(cls):
Global tearDown.
"""

def test_init(self):
with self.assertRaises(TypeError) as err:
self._hana = hana.HanaInstance(1, '00', 'pass')

self.assertTrue(
'provided sid, inst and password parameters must be str type' in
str(err.exception))

with self.assertRaises(TypeError) as err:
self._hana = hana.HanaInstance('prd', 0, 'pass')

self.assertTrue(
'provided sid, inst and password parameters must be str type' in
str(err.exception))

with self.assertRaises(TypeError) as err:
self._hana = hana.HanaInstance('prd', '00', 1234)

self.assertTrue(
'provided sid, inst and password parameters must be str type' in
str(err.exception))


@mock.patch('shaptools.shell.execute_cmd')
def test_run_hana_command(self, mock_execute):
proc_mock = mock.Mock()
Expand Down Expand Up @@ -299,3 +322,17 @@ def test_create_backup(self):
'hdbsql -U {} -d {} -p {} '\
'\\"BACKUP DATA FOR FULL SYSTEM USING FILE (\'{}\')\\"'.format(
'key', 'db', 'pass', 'backup'))

def test_sr_cleanup(self):
mock_command = mock.Mock()
self._hana._run_hana_command = mock_command

self._hana.sr_cleanup()
mock_command.assert_called_once_with('hdbnsutil -sr_cleanup')

def test_sr_cleanup_force(self):
mock_command = mock.Mock()
self._hana._run_hana_command = mock_command

self._hana.sr_cleanup(force=True)
mock_command.assert_called_once_with('hdbnsutil -sr_cleanup --force')

0 comments on commit cbad00c

Please sign in to comment.