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

from testapiclient.client import pods
from testapiclient.utils import command
from testapiclient.utils import urlparse
from testapiclient.models import pods as pm


def pods_url():
    return urlparse.resource_join('pods')


def pod_url(parsed_args):
    return urlparse.path_join(pods_url(), parsed_args.name)


class PodGet(command.Lister):
    "Handle get request for pods"

    def get_parser(self, prog_name):
        parser = super(PodGet, self).get_parser(prog_name)
        parser.add_argument('-name',
                            default='',
                            help='Search pods using name')
        return parser

    def take_action(self, parsed_args):
        columns = (
            "name",
            "_id",
            "creator",
            "role",
            "mode",
            "creation_date",
        )

        data = self.app.client_manager.get(
            urlparse.query_by(pods_url(), 'name', parsed_args))
        return self.format_output(columns, data.get('pods', []))


class PodGetOne(command.ShowOne):
    "Handle get request for pod by name"

    def get_parser(self, prog_name):
        parser = super(PodGetOne, self).get_parser(prog_name)
        parser.add_argument('name',
                            default='',
                            help='Find pod using name')
        return parser

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


class PodCreate(command.ShowOne):
    "Handle post request for pods"

    def get_parser(self, prog_name):
        parser = super(PodCreate, self).get_parser(prog_name)
        parser.add_argument('pod',
                            type=json.loads,
                            help='Pod create request format :\n'
                                 '\'{}\''.format(json.dumps(
                                     pm.PodCreateRequest().__dict__
                                     )) +
                                 '\n role should be either '
                                 '"community-ci" or "production-ci", and '
                                 'mode should be either "metal" or "virtual.')
        return parser

    def take_action(self, parsed_args):
        client = pods.PodsClient(client_manager=self.app.client_manager)
        return self.format_output(client.create(parsed_args.pod))


class PodDelete(command.Command):
    "Handle delete request for pods"

    def get_parser(self, prog_name):
        parser = super(PodDelete, self).get_parser(prog_name)
        parser.add_argument('name',
                            type=str,
                            help='Delete pods using name')
        return parser

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