Coverage for curator/cli_singletons/show.py: 40%

50 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-16 15:27 -0600

1"""Show Index/Snapshot Singletons""" 

2from datetime import datetime 

3import click 

4from curator.cli_singletons.object_class import cli_action 

5from curator.cli_singletons.utils import get_width, validate_filter_json 

6from curator.utils import byte_size 

7 

8 

9#### Indices #### 

10@click.command(context_settings=get_width()) 

11@click.option('--verbose', help='Show verbose output.', is_flag=True, show_default=True) 

12@click.option('--header', help='Print header if --verbose', is_flag=True, show_default=True) 

13@click.option('--epoch', help='Print time as epoch if --verbose', is_flag=True, show_default=True) 

14@click.option( 

15 '--ignore_empty_list', 

16 is_flag=True, 

17 help='Do not raise exception if there are no actionable indices' 

18) 

19@click.option( 

20 '--allow_ilm_indices/--no-allow_ilm_indices', 

21 help='Allow Curator to operate on Index Lifecycle Management monitored indices.', 

22 default=False, 

23 show_default=True 

24) 

25@click.option( 

26 '--filter_list', 

27 callback=validate_filter_json, 

28 default='{"filtertype":"none"}', 

29 help='JSON string representing an array of filters.' 

30) 

31@click.pass_context 

32def show_indices(ctx, verbose, header, epoch, ignore_empty_list, allow_ilm_indices, filter_list): 

33 """ 

34 Show Indices 

35 """ 

36 # ctx.info_name is the name of the function or name specified in @click.command decorator 

37 action = cli_action( 

38 'show_indices', 

39 ctx.obj['config']['client'], 

40 {'allow_ilm_indices': allow_ilm_indices}, 

41 filter_list, 

42 ignore_empty_list 

43 ) 

44 action.get_list_object() 

45 action.do_filters() 

46 indices = sorted(action.list_object.indices) 

47 # Do some calculations to figure out the proper column sizes 

48 allbytes = [] 

49 alldocs = [] 

50 for idx in indices: 

51 allbytes.append(byte_size(action.list_object.index_info[idx]['size_in_bytes'])) 

52 alldocs.append(str(action.list_object.index_info[idx]['docs'])) 

53 if epoch: 

54 timeformat = '{6:>13}' 

55 column = 'creation_date' 

56 else: 

57 timeformat = '{6:>20}' 

58 column = 'Creation Timestamp' 

59 formatting = ( 

60 '{0:' + str(len(max(indices, key=len))) + '} ' 

61 '{1:>5} ' 

62 '{2:>' + str(len(max(allbytes, key=len)) + 1) + '} ' 

63 '{3:>' + str(len(max(alldocs, key=len)) + 1) + '} ' 

64 '{4:>3} {5:>3} ' + timeformat 

65 ) 

66 # Print the header, if both verbose and header are enabled 

67 if header and verbose: 

68 click.secho( 

69 formatting.format( 

70 'Index', 'State', 'Size', 'Docs', 'Pri', 'Rep', column 

71 ), bold=True, underline=True 

72 ) 

73 # Loop through indices and print info, if verbose 

74 for idx in indices: 

75 data = action.list_object.index_info[idx] 

76 if verbose: 

77 if epoch: 

78 datefield = data['age']['creation_date'] if 'creation_date' in data['age'] else 0 

79 else: 

80 datefield = '{0}Z'.format( 

81 datetime.utcfromtimestamp( 

82 data['age']['creation_date'] 

83 ).isoformat()) if 'creation_date' in data['age'] else 'unknown/closed' 

84 click.echo( 

85 formatting.format( 

86 idx, data['state'], byte_size(data['size_in_bytes']), 

87 data['docs'], data['number_of_shards'], data['number_of_replicas'], 

88 datefield 

89 ) 

90 ) 

91 else: 

92 click.echo('{0}'.format(idx)) 

93 

94#### Snapshots #### 

95@click.command(context_settings=get_width()) 

96@click.option('--repository', type=str, required=True, help='Snapshot repository name') 

97@click.option( 

98 '--ignore_empty_list', 

99 is_flag=True, 

100 help='Do not raise exception if there are no actionable snapshots' 

101) 

102@click.option( 

103 '--filter_list', 

104 callback=validate_filter_json, 

105 default='{"filtertype":"none"}', 

106 help='JSON string representing an array of filters.' 

107) 

108@click.pass_context 

109def show_snapshots(ctx, repository, ignore_empty_list, filter_list): 

110 """ 

111 Show Snapshots 

112 """ 

113 # ctx.info_name is the name of the function or name specified in @click.command decorator 

114 action = cli_action( 

115 'show_snapshots', 

116 ctx.obj['config']['client'], 

117 {}, 

118 filter_list, 

119 ignore_empty_list, 

120 repository=repository 

121 ) 

122 action.get_list_object() 

123 action.do_filters() 

124 for snapshot in sorted(action.list_object.snapshots): 

125 click.secho('{0}'.format(snapshot))