summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/cli/scenarios.py
blob: 3f707729536edbeca36422cc62c6da883104f973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import json

from testapiclient.utils import command
from testapiclient.utils import urlparse


def scenarios_url():
    return urlparse.resource_join('scenarios')


def scenario_url(parsed_args):
    return urlparse.path_join(scenarios_url(), parsed_args.name)


def resources_url(name, resuorce):
    return urlparse.resource_join('scenarios', name, resuorce)


class ScenarioGet(command.Lister):

    def get_parser(self, prog_name):
        parser = super(ScenarioGet, self).get_parser(prog_name)
        parser.add_argument('-name',
                            help='Search scenarios using name')
        parser.add_argument('-installer',
                            help='Search scenarios using installer')
        parser.add_argument('---version',
                            help='Search scenarios using version')
        parser.add_argument('-project',
                            help='Search scenarios using project')
        return parser

    def take_action(self, parsed_args):
        columns = (
            'name',
            '_id',
            'creator',
            'creation_date'
        )
        data = self.app.client_manager.get(
            urlparse.query_by(scenarios_url(),
                              ['name', 'installer', 'version', 'project'],
                              parsed_args))
        return self.format_output(columns, data.get('scenarios', []))


class ScenarioGetOne(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(ScenarioGetOne, self).get_parser(prog_name)
        parser.add_argument('name',
                            help='Search scenario by name')
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.get(scenario_url(parsed_args)))


class ScenarioCreate(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(ScenarioCreate, self).get_parser(prog_name)
        parser.add_argument('scenario',
                            type=json.loads,
                            help='Scenario create request format :\n'
                                 '\'{ "installers": [], "name": ""}\',\n'
                                 'Intaller create request format :\n'
                                 '\'{"installer": "","versions": []}\',\n'
                                 'Version create request format :\n'
                                 '\'{"owner": "","version": "",'
                                 '"projects": []}\',\n'
                                 'Project create request format :\n'
                                 '\'{"project": "","customs": [],'
                                 '"scores": [],'
                                 '"trust_indicators": []}\',\n'
                                 'Custom create request format :\n'
                                 '\'["asf","saf"]\',\n'
                                 'Score create request format :\n'
                                 '\'{"date": "", "score": ""}\',\n'
                                 'Trust Indicators create request format :\n'
                                 '\'{"date": "", "status": ""}\'')
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.post(
                scenarios_url(), parsed_args.scenario))


class ScenarioDelete(command.Command):

    def get_parser(self, prog_name):
        parser = super(ScenarioDelete, self).get_parser(prog_name)
        parser.add_argument('name',
                            type=str,
                            help='Delete scenario by name')
        return parser

    def take_action(self, parsed_args):
        return self.app.client_manager.delete(scenario_url(parsed_args))


class ScenarioPut(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(ScenarioPut, self).get_parser(prog_name)
        parser.add_argument('name',
                            type=str,
                            help='Update scenario by name')
        parser.add_argument('scenario',
                            type=json.loads,
                            help='Scenario create request format :\n'
                                 '\'{ "installers": [], "name": ""}\',\n'
                                 'Intaller create request format :\n'
                                 '\'{"installer": "","versions": []}\',\n'
                                 'Version create request format :\n'
                                 '\'{"owner": "","version": "",'
                                 '"projects": []}\',\n'
                                 'Project create request format :\n'
                                 '\'{"project": "","customs": [],'
                                 '"scores": [],'
                                 '"trust_indicators": []}\',\n'
                                 'Custom create request format :\n'
                                 '\'["asf","saf"]\',\n'
                                 'Score create request format :\n'
                                 '\'{"date": "", "score": ""}\',\n'
                                 'Trust Indicators create request format :\n'
                                 '\'{"date": "", "status": ""}\'')
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.put(
                scenario_url(parsed_args), parsed_args.scenario))


class InstallerCreate(command.Command):

    def get_parser(self, prog_name):
        parser = super(InstallerCreate, self).get_parser(prog_name)
        parser.add_argument('--scenario-name',
                            required=True,
                            help='Create installer under scenario name')
        parser.add_argument('installer',
                            type=json.loads,
                            help='Intaller create request format :\n'
                                 '\'[{"installer": "","versions": []}]\',\n')
        return parser

    def take_action(self, parsed_args):
        return self.app.client_manager.post(
            resources_url(
                parsed_args.scenario_name,
                'installers'), parsed_args.installer)


class InstallerDelete(command.Command):

    def get_parser(self, prog_name):
        parser = super(InstallerDelete, self).get_parser(prog_name)
        parser.add_argument('--scenario-name',
                            required=True,
                            type=str,
                            help='Delete installer by scenario name')
        parser.add_argument('name',
                            nargs='+',
                            help='Delete installer by name')
        return parser

    def take_action(self, parsed_args):
        return self.app.client_manager.delete(
            resources_url(
                parsed_args.scenario_name,
                'installers'), parsed_args.name)


class InstallerPut(command.Command):

    def get_parser(self, prog_name):
        parser = super(InstallerPut, self).get_parser(prog_name)
        parser.add_argument('--scenario-name',
                            type=str,
                            required=True,
                            help='Update installer by scenario name')
        parser.add_argument('installer',
                            type=json.loads,
                            help='Intaller create request format :\n'
                                 '\'[{"installer": "","versions": []}]\',\n')
        return parser

    def take_action(self, parsed_args):
        return self.app.client_manager.put(
            resources_url(
                parsed_args.scenario_name,
                'installers'), parsed_args.installer)