Add CaaS Logging REST API and CLI plugins
[ta/caas-logging.git] / src / caas_logging / cli / caas.py
1 #!/usr/bin/env python
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # pylint: disable=line-too-long, too-few-public-methods
18
19 from copy import deepcopy
20
21 from hostcli.helper import ListerHelper, ShowOneHelper, CommandHelper
22
23 API_VERSION =       'v1'
24 RESOURCE_PREFIX =   'caas/%s/' % API_VERSION
25 ID =                'id'
26 NAMESPACE =         'namespace'
27 PLUGIN =            'plugin'
28 TARGETURL =         'target_url'
29 STREAM =            'stream'
30
31
32 FIELDMAP = {
33     ID:         {'display': 'ID',
34                  'help': 'The ID of the log entry'},
35     NAMESPACE:  {'display': 'namespace',
36                  'help': 'The kubernetes namespace where the log entry applies'},
37     PLUGIN:     {'display': 'plugin',
38                  'help': 'The fluentd plugin which is used for forwarding log entries. '
39                          'Should be one of remote_syslog, elasticsearch'},
40     TARGETURL:  {'display': 'target_url',
41                  'help': 'The URL of the log storage where fluentd will send log entries'},
42     STREAM:     {'display': 'stream',
43                  'help': 'The stream which will be logged by fluentd. '
44                          'Should be one of stdout, stderr, both'}
45 }
46
47
48 class CaasCliLister(ListerHelper):
49     """Helper class for Lister"""
50     def __init__(self, app, app_args, cmd_name=None):
51         super(CaasCliLister, self).__init__(app, app_args, cmd_name)
52         self.fieldmap = deepcopy(FIELDMAP)
53         self.resource_prefix = RESOURCE_PREFIX
54
55
56 class CaasCliShowOne(ShowOneHelper):
57     """Helper class for ShowOne"""
58     def __init__(self, app, app_args, cmd_name=None):
59         super(CaasCliShowOne, self).__init__(app, app_args, cmd_name)
60         self.fieldmap = deepcopy(FIELDMAP)
61         self.resource_prefix = RESOURCE_PREFIX
62
63
64 class CaasCliCommand(CommandHelper):
65     """Helper class for Command"""
66     def __init__(self, app, app_args, cmd_name=None):
67         super(CaasCliCommand, self).__init__(app, app_args, cmd_name)
68         self.fieldmap = deepcopy(FIELDMAP)
69         self.resource_prefix = RESOURCE_PREFIX
70
71
72 class CreateAppLogBackend(CaasCliCommand):
73     """A command for adding a new CaaS application log forwarding entry."""
74
75     def __init__(self, app, app_args, cmd_name=None):
76         super(CreateAppLogBackend, self).__init__(app, app_args, cmd_name)
77         self.operation = 'post'
78         self.endpoint = 'log/apps'
79         self.mandatory_positional = True
80         self.positional_count = 4
81         self.arguments = [NAMESPACE, PLUGIN, TARGETURL, STREAM]
82         self.message = 'Entry has been added.'
83
84
85 class ChangeAppLogBackend(CaasCliCommand):
86     """A command for modifying a CaaS application log forwarding entry."""
87
88     def __init__(self, app, app_args, cmd_name=None):
89         super(ChangeAppLogBackend, self).__init__(app, app_args, cmd_name)
90         self.operation = 'put'
91         self.endpoint = 'log/apps'
92         self.mandatory_positional = True
93         self.positional_count = 1
94         self.arguments = [ID, NAMESPACE, PLUGIN, TARGETURL, STREAM]
95         self.message = 'Entry has been updated.'
96
97
98 class DeleteAppLogBackend(CaasCliCommand):
99     """A command for removing a CaaS application log forwarding entry."""
100
101     def __init__(self, app, app_args, cmd_name=None):
102         super(DeleteAppLogBackend, self).__init__(app, app_args, cmd_name)
103         self.operation = 'delete'
104         self.endpoint = 'log/apps'
105         self.mandatory_positional = True
106         self.positional_count = 1
107         self.arguments = [ID]
108         self.message = 'Entry has been deleted.'
109
110
111 class ShowAppLogBackend(CaasCliShowOne):
112     """A command for showing detail of a CaaS application log forwarding entry."""
113
114     def __init__(self, app, app_args, cmd_name=None):
115         super(ShowAppLogBackend, self).__init__(app, app_args, cmd_name)
116         self.operation = 'get'
117         self.endpoint = 'log/apps'
118         self.mandatory_positional = True
119         self.positional_count = 1
120         self.arguments = [ID]
121         self.columns = [ID, NAMESPACE, PLUGIN, TARGETURL, STREAM]
122         self.default_sort = [ID, 'asc']
123
124
125 class ListAppLogBackend(CaasCliLister):
126     """A command for listing existing CaaS application log forwarding entries."""
127
128     def __init__(self, app, app_args, cmd_name=None):
129         super(ListAppLogBackend, self).__init__(app, app_args, cmd_name)
130         self.operation = 'get'
131         self.endpoint = 'log/apps'
132         self.no_positional = True
133         self.columns = [ID, NAMESPACE, PLUGIN, TARGETURL, STREAM]
134         self.default_sort = [ID, 'asc']
135
136
137 class ListAppLogBackendForNamespace(CaasCliLister):
138     """A command for listing existing CaaS application log forwarding entries for a namespace"""
139
140     def __init__(self, app, app_args, cmd_name=None):
141         super(ListAppLogBackendForNamespace, self).__init__(app, app_args, cmd_name)
142         self.operation = 'get'
143         self.endpoint = 'log/apps'
144         self.mandatory_positional = True
145         self.positional_count = 1
146         self.arguments = [NAMESPACE]
147         self.columns = [ID, NAMESPACE, PLUGIN, TARGETURL, STREAM]
148         self.default_sort = [ID, 'asc']