summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/cli/projects.py
blob: 510acc80fb133e192d47d220b78d134a832d7cee (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
import json

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


def projects_url():
    return urlparse.resource_join('projects')


def project_url(parsed_args):
    return urlparse.path_join(projects_url(), parsed_args.name)


class ProjectGet(command.Lister):

    def get_parser(self, prog_name):
        parser = super(ProjectGet, self).get_parser(prog_name)
        parser.add_argument('-name',
                            help='Search projects by name')
        return parser

    def take_action(self, parsed_args):
        columns = (
            'name',
            '_id',
            'creator',
            'creation_date'
        )
        data = self.app.client_manager.get(
            urlparse.query_by(projects_url(), 'name', parsed_args))
        return self.format_output(columns, data.get('projects', []))


class ProjectGetOne(command.ShowOne):

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

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


class ProjectCreate(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(ProjectCreate, self).get_parser(prog_name)
        parser.add_argument('project',
                            type=json.loads,
                            help='Project create request format :{'
                                 ' "name": (required)"", '
                                 '"description": (optional)""}')
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.post(projects_url(), parsed_args.project))


class ProjectDelete(command.Command):

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

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


class ProjectPut(command.ShowOne):

    def get_parser(self, prog_name):
        parser = super(ProjectPut, self).get_parser(prog_name)
        parser.add_argument('name',
                            type=str,
                            help='Update project by name')
        parser.add_argument('project',
                            type=json.loads,
                            help='Project Update request format :{'
                                 '"name": (required)"", '
                                 '"description": (optional)""}')
        return parser

    def take_action(self, parsed_args):
        return self.format_output(
            self.app.client_manager.put(
                project_url(parsed_args), parsed_args.project))