summaryrefslogtreecommitdiff
path: root/src/utils/vyos-hostsd-client
blob: a0515951a5e8d2ba3c620f5e980f657686e18f3e (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
#!/usr/bin/env python3
#
# Copyright (C) 2019 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

import sys
import argparse

import vyos.hostsd_client

parser = argparse.ArgumentParser(allow_abbrev=False)
group = parser.add_mutually_exclusive_group()

group.add_argument('--add-name-servers', type=str, nargs='*')
group.add_argument('--delete-name-servers', action='store_true')
group.add_argument('--get-name-servers', type=str, const='.*', nargs='?')

group.add_argument('--add-name-server-tags-recursor', type=str, nargs='*')
group.add_argument('--delete-name-server-tags-recursor', type=str, nargs='*')
group.add_argument('--get-name-server-tags-recursor', action='store_true')

group.add_argument('--add-name-server-tags-system', type=str, nargs='*')
group.add_argument('--delete-name-server-tags-system', type=str, nargs='*')
group.add_argument('--get-name-server-tags-system', action='store_true')

group.add_argument('--add-forward-zone', type=str, nargs='?')
group.add_argument('--delete-forward-zones', type=str, nargs='*')
group.add_argument('--get-forward-zones', action='store_true')

group.add_argument('--add-search-domains', type=str, nargs='*')
group.add_argument('--delete-search-domains', action='store_true')
group.add_argument('--get-search-domains', type=str, const='.*', nargs='?')

group.add_argument('--add-hosts', type=str, nargs='*')
group.add_argument('--delete-hosts', action='store_true')
group.add_argument('--get-hosts', type=str, const='.*', nargs='?')

group.add_argument('--set-host-name', type=str)

# for --set-host-name
parser.add_argument('--domain-name', type=str)

# for forward zones
parser.add_argument('--nameservers', type=str, nargs='*')
parser.add_argument('--addnta', action='store_true')
parser.add_argument('--recursion-desired', action='store_true')

parser.add_argument('--tag', type=str)

# users must call --apply either in the same command or after they're done
parser.add_argument('--apply', action="store_true")

args = parser.parse_args()

try:
    client = vyos.hostsd_client.Client()
    ops = 1

    if args.add_name_servers:
        if not args.tag:
            raise ValueError("--tag is required for this operation")
        client.add_name_servers({args.tag: args.add_name_servers})
    elif args.delete_name_servers:
        if not args.tag:
            raise ValueError("--tag is required for this operation")
        client.delete_name_servers([args.tag])
    elif args.get_name_servers:
        print(client.get_name_servers(args.get_name_servers))

    elif args.add_name_server_tags_recursor:
        client.add_name_server_tags_recursor(args.add_name_server_tags_recursor)
    elif args.delete_name_server_tags_recursor:
        client.delete_name_server_tags_recursor(args.delete_name_server_tags_recursor)
    elif args.get_name_server_tags_recursor:
        print(client.get_name_server_tags_recursor())

    elif args.add_name_server_tags_system:
        client.add_name_server_tags_system(args.add_name_server_tags_system)
    elif args.delete_name_server_tags_system:
        client.delete_name_server_tags_system(args.delete_name_server_tags_system)
    elif args.get_name_server_tags_system:
        print(client.get_name_server_tags_system())

    elif args.add_forward_zone:
        if not args.nameservers:
            raise ValueError("--nameservers is required for this operation")
        client.add_forward_zones(
                { args.add_forward_zone: {
                    'server': args.nameservers,
                    'addnta': args.addnta,
                    'recursion_desired': args.recursion_desired
                    }
                })
    elif args.delete_forward_zones:
        client.delete_forward_zones(args.delete_forward_zones)
    elif args.get_forward_zones:
        print(client.get_forward_zones())

    elif args.add_search_domains:
        if not args.tag:
            raise ValueError("--tag is required for this operation")
        client.add_search_domains({args.tag: args.add_search_domains})
    elif args.delete_search_domains:
        if not args.tag:
            raise ValueError("--tag is required for this operation")
        client.delete_search_domains([args.tag])
    elif args.get_search_domains:
        print(client.get_search_domains(args.get_search_domains))

    elif args.add_hosts:
        if not args.tag:
            raise ValueError("--tag is required for this operation")
        data = {}
        for h in args.add_hosts:
            entry = {}
            params = h.split(",")
            if len(params) < 2:
                raise ValueError("Malformed host entry")
            # Address needs to be a list because of changes made in T2683
            entry['address'] = [params[1]]
            entry['aliases'] = params[2:]
            data[params[0]] = entry
        client.add_hosts({args.tag: data})
    elif args.delete_hosts:
        if not args.tag:
            raise ValueError("--tag is required for this operation")
        client.delete_hosts([args.tag])
    elif args.get_hosts:
        print(client.get_hosts(args.get_hosts))

    elif args.set_host_name:
        if not args.domain_name:
            raise ValueError('--domain-name is required for this operation')
        client.set_host_name({'host_name': args.set_host_name, 'domain_name': args.domain_name})

    elif args.apply:
        pass
    else:
        ops = 0

    if args.apply:
        client.apply()

    if ops == 0:
        raise ValueError("Operation required")

except ValueError as e:
    print("Incorrect options: {0}".format(e))
    sys.exit(1)
except vyos.hostsd_client.VyOSHostsdError as e:
    print("Server returned an error: {0}".format(e))
    sys.exit(1)