1import click 2from opnsense_cli.formatters.cli_output import CliOutputFormatter 3from opnsense_cli.callbacks.click import \ 4 formatter_from_formatter_name, bool_as_string, available_formats 5from opnsense_cli.commands.plugin.haproxy import haproxy 6from opnsense_cli.api.client import ApiClient 7from opnsense_cli.api.plugin.haproxy import Settings, Service 8from opnsense_cli.facades.commands.plugin.haproxy.lua import HaproxyLuaFacade 9 10pass_api_client = click.make_pass_decorator(ApiClient) 11pass_haproxy_lua_svc = click.make_pass_decorator(HaproxyLuaFacade) 12 13 14@haproxy.group() 15@pass_api_client 16@click.pass_context 17def lua(ctx, api_client: ApiClient, **kwargs): 18 """ 19 Lua code/scripts to extend HAProxy's functionality. 20 """ 21 settings_api = Settings(api_client) 22 service_api = Service(api_client) 23 ctx.obj = HaproxyLuaFacade(settings_api, service_api) 24 25 26@lua.command() 27@click.option( 28 '--output', '-o', 29 help='Specifies the Output format.', 30 default="table", 31 type=click.Choice(available_formats()), 32 callback=formatter_from_formatter_name, 33 show_default=True, 34) 35@click.option( 36 '--cols', '-c', 37 help='Which columns should be printed? Pass empty string (-c '') to show all columns', 38 default=( 39 "uuid,enabled,name,description,content" 40 ), 41 show_default=True, 42) 43@pass_haproxy_lua_svc 44def list(haproxy_lua_svc: HaproxyLuaFacade, **kwargs): 45 """ 46 Show all lua 47 """ 48 result = haproxy_lua_svc.list_luas() 49 50 CliOutputFormatter(result, kwargs['output'], kwargs['cols'].split(",")).echo() 51 52 53@lua.command() 54@click.argument('uuid') 55@click.option( 56 '--output', '-o', 57 help='Specifies the Output format.', 58 default="table", 59 type=click.Choice(available_formats()), 60 callback=formatter_from_formatter_name, 61 show_default=True, 62) 63@click.option( 64 '--cols', '-c', 65 help='Which columns should be printed? Pass empty string (-c '') to show all columns', 66 default=( 67 "enabled,name,description,content" 68 ), 69 show_default=True, 70) 71@pass_haproxy_lua_svc 72def show(haproxy_lua_svc: HaproxyLuaFacade, **kwargs): 73 """ 74 Show details for lua 75 """ 76 result = haproxy_lua_svc.show_lua(kwargs['uuid']) 77 78 CliOutputFormatter(result, kwargs['output'], kwargs['cols'].split(",")).echo() 79 80 81@lua.command() 82@click.argument('name') 83@click.option( 84 '--enabled/--no-enabled', 85 help=('Enable this Lua script.'), 86 show_default=True, 87 is_flag=True, 88 callback=bool_as_string, 89 default=True, 90 required=True, 91) 92@click.option( 93 '--description', 94 help=('Description for this Lua script.'), 95 show_default=True, 96 default=None, 97 required=False, 98) 99@click.option( 100 '--content', 101 help=('Paste the content of your Lua script here.'), 102 show_default=True, 103 default=None, 104 required=True, 105) 106@click.option( 107 '--output', '-o', 108 help='Specifies the Output format.', 109 default="plain", 110 type=click.Choice(available_formats()), 111 callback=formatter_from_formatter_name, 112 show_default=True, 113) 114@click.option( 115 '--cols', '-c', 116 help='Which columns should be printed? Pass empty string (-c '') to show all columns', 117 default="result,validations", 118 show_default=True, 119) 120@pass_haproxy_lua_svc 121def create(haproxy_lua_svc: HaproxyLuaFacade, **kwargs): 122 """ 123 Create a new lua 124 """ 125 json_payload = { 126 'lua': { 127 "enabled": kwargs['enabled'], 128 "name": kwargs['name'], 129 "description": kwargs['description'], 130 "content": kwargs['content'], 131 } 132 } 133 134 result = haproxy_lua_svc.create_lua(json_payload) 135 136 CliOutputFormatter(result, kwargs['output'], kwargs['cols'].split(",")).echo() 137 138 139@lua.command() 140@click.argument('uuid') 141@click.option( 142 '--enabled/--no-enabled', 143 help=('Enable this Lua script.'), 144 show_default=True, 145 is_flag=True, 146 callback=bool_as_string, 147 default=None 148) 149@click.option( 150 '--name', 151 help=('Name to identify this Lua script.'), 152 show_default=True, 153 default=None 154) 155@click.option( 156 '--description', 157 help=('Description for this Lua script.'), 158 show_default=True, 159 default=None 160) 161@click.option( 162 '--content', 163 help=('Paste the content of your Lua script here.'), 164 show_default=True, 165 default=None 166) 167@click.option( 168 '--output', '-o', 169 help='Specifies the Output format.', 170 default="plain", 171 type=click.Choice(available_formats()), 172 callback=formatter_from_formatter_name, 173 show_default=True, 174) 175@click.option( 176 '--cols', '-c', 177 help='Which columns should be printed? Pass empty string (-c '') to show all columns', 178 default="result,validations", 179 show_default=True, 180) 181@pass_haproxy_lua_svc 182def update(haproxy_lua_svc: HaproxyLuaFacade, **kwargs): 183 """ 184 Update a lua. 185 """ 186 json_payload = { 187 'lua': {} 188 } 189 options = ['enabled', 'name', 'description', 'content'] 190 for option in options: 191 if kwargs[option.lower()] is not None: 192 json_payload['lua'][option] = kwargs[option.lower()] 193 194 result = haproxy_lua_svc.update_lua(kwargs['uuid'], json_payload) 195 196 CliOutputFormatter(result, kwargs['output'], kwargs['cols'].split(",")).echo() 197 198 199@lua.command() 200@click.argument('uuid') 201@click.option( 202 '--output', '-o', 203 help='Specifies the Output format.', 204 default="plain", 205 type=click.Choice(available_formats()), 206 callback=formatter_from_formatter_name, 207 show_default=True, 208) 209@click.option( 210 '--cols', '-c', 211 help='Which columns should be printed? Pass empty string (-c '') to show all columns', 212 default="result,validations", 213 show_default=True, 214) 215@pass_haproxy_lua_svc 216def delete(haproxy_lua_svc: HaproxyLuaFacade, **kwargs): 217 """ 218 Delete lua 219 """ 220 result = haproxy_lua_svc.delete_lua(kwargs['uuid']) 221 222 CliOutputFormatter(result, kwargs['output'], kwargs['cols'].split(",")).echo() 223