Coverage for /Users/buh/.pyenv/versions/3.12.2/envs/es-testbed/lib/python3.12/site-packages/es_testbed/testbed_creator.py: 0%

94 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-09 18:23 -0600

1"""Create a sample index on a cluster with multiple docs for PII redaction testing""" 

2# pylint: disable=unused-argument, redefined-builtin 

3 

4import click 

5from es_client.defaults import OPTION_DEFAULTS 

6from es_client.helpers import config as esconfig 

7from es_client.helpers.logging import configure_logging 

8from es_testbed.helpers import es_api 

9from es_testbed.exceptions import TestbedException 

10from es_testbed.version import __version__ 

11 

12@click.command() 

13@click.option('--name', type=str, help='The index name', required=True) 

14@click.option('--start_num', type=int, help='Start numbers from this value', default=0) 

15@click.option('--count', type=int, help='Make this many documents', default=10) 

16@click.option('--match/--no-match', help='Make matchable documents or not', default=True) 

17@click.pass_context 

18def hot(ctx, name, start_num, count, match): 

19 """ 

20 Add an index to the hot tier 

21 """ 

22 client = ctx.obj['client'] 

23 es_api.delete_index(client, name) 

24 es_api.fill_index(client, name, count, start_num, match=match) 

25 

26@click.command() 

27@click.option('--name', type=str, help='The index name', required=True) 

28@click.option('--start_num', type=int, help='Start numbers from this value', default=0) 

29@click.option('--count', type=int, help='Make this many documents', default=10) 

30@click.option('--match/--no-match', help='Make matchable documents or not', default=True) 

31@click.option('--repo', type=str, help='The snapshot repository', required=True) 

32@click.option('--snap', type=str, help='The snapshot name', required=True) 

33@click.pass_context 

34def cold(ctx, name, start_num, count, match, repo, snap): 

35 """ 

36 Add an index to the cold tier 

37 """ 

38 client = ctx.obj['client'] 

39 es_api.delete_index(client, name) 

40 es_api.delete_index(client, f'restored-{name}') 

41 es_api.delete_snapshot(client, repo, snap) 

42 es_api.fill_index(client, name, count, start_num, match=match) 

43 es_api.do_snap(client, repo, snap, name) 

44 es_api.fix_aliases(client, name, f'restored-{name}') 

45 

46@click.command() 

47@click.option('--name', type=str, help='The index name', required=True) 

48@click.option('--start_num', type=int, help='Start numbers from this value', default=0) 

49@click.option('--count', type=int, help='Make this many documents', default=10) 

50@click.option('--match/--no-match', help='Make matchable documents or not', default=True) 

51@click.option('--repo', type=str, help='The snapshot repository', required=True) 

52@click.option('--snap', type=str, help='The snapshot name', required=True) 

53@click.pass_context 

54def frozen(ctx, name, start_num, count, match, repo, snap): 

55 """ 

56 Add an index to the frozen tier 

57 """ 

58 client = ctx.obj['client'] 

59 es_api.delete_index(client, name) 

60 es_api.delete_index(client, f'partial-{name}') 

61 es_api.delete_snapshot(client, repo, snap) 

62 es_api.fill_index(client, name, count, start_num, match=match) 

63 es_api.do_snap(client, repo, snap, name, tier='frozen') 

64 es_api.fix_aliases(client, name, f'partial-{name}') 

65 

66@click.command() 

67@click.option('--name', type=str, help='The index name', required=True) 

68@click.pass_context 

69def show_index(ctx, name): 

70 """ 

71 Show index settings 

72 """ 

73 client = ctx.obj['client'] 

74 click.echo(client.indices.get_settings(index=name)) 

75 

76@click.command() 

77@click.option('--repo', type=str, help='The snapshot repository', required=True) 

78@click.option('--snap', type=str, help='The snapshot name', required=True) 

79@click.pass_context 

80def show_snapshot(ctx, repo, snap): 

81 """ 

82 Show snapshot contents 

83 """ 

84 client = ctx.obj['client'] 

85 click.echo(client.snapshot.get(repository=repo, snapshot=snap)) 

86 

87@click.command() 

88@click.option('--name', type=str, help='The index name', required=True) 

89@click.pass_context 

90def delete_index(ctx, name): 

91 """ 

92 Purge index 

93 """ 

94 client = ctx.obj['client'] 

95 es_api.delete_index(client, name) 

96 

97@click.command() 

98@click.option('--repo', type=str, help='The snapshot repository', required=True) 

99@click.option('--snap', type=str, help='The snapshot name', required=True) 

100@click.pass_context 

101def delete_snapshot(ctx, repo, snap): 

102 """ 

103 Purge snapshot 

104 """ 

105 client = ctx.obj['client'] 

106 es_api.delete_snapshot(client, repo, snap) 

107 

108# pylint: disable=unused-argument, redefined-builtin, too-many-arguments, too-many-locals, line-too-long 

109@click.group(context_settings=esconfig.context_settings()) 

110@esconfig.options_from_dict(OPTION_DEFAULTS) 

111@click.version_option(__version__, '-v', '--version', prog_name="testbed_creator") 

112@click.pass_context 

113def cli( 

114 ctx, config, hosts, cloud_id, api_token, id, api_key, username, password, bearer_auth, 

115 opaque_id, request_timeout, http_compress, verify_certs, ca_certs, client_cert, client_key, 

116 ssl_assert_hostname, ssl_assert_fingerprint, ssl_version, master_only, skip_version_test, 

117 loglevel, logfile, logformat, blacklist 

118): 

119 """ 

120 Testbed Creator 

121 

122 Create indices and snapshots for testing elastic-pii-redacter 

123 """ 

124 esconfig.get_config(ctx) 

125 configure_logging(ctx) 

126 esconfig.generate_configdict(ctx) 

127 try: 

128 ctx.obj['client'] = esconfig.get_client(configdict=ctx.obj['configdict']) 

129 except Exception as exc: 

130 raise TestbedException('Unable to establish connection to Elasticsearch!') from exc 

131 

132# Add the subcommands 

133cli.add_command(hot) 

134cli.add_command(cold) 

135cli.add_command(frozen) 

136cli.add_command(show_index) 

137cli.add_command(show_snapshot) 

138cli.add_command(delete_index) 

139cli.add_command(delete_snapshot) 

140 

141if __name__ == '__main__': 

142 # pylint: disable=no-value-for-parameter 

143 cli()