]>
Commit | Line | Data |
---|---|---|
ee4f339e AD |
1 | package PVE::Network::SDN::Dns::Plugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Tools qw(run_command); | |
7 | use PVE::JSONSchema; | |
8 | use PVE::Cluster; | |
9 | use HTTP::Request; | |
10 | use LWP::UserAgent; | |
11 | use JSON; | |
12 | ||
13 | use Data::Dumper; | |
14 | use PVE::JSONSchema qw(get_standard_option); | |
15 | use base qw(PVE::SectionConfig); | |
16 | ||
17 | PVE::Cluster::cfs_register_file('sdn/dns.cfg', | |
18 | sub { __PACKAGE__->parse_config(@_); }, | |
19 | sub { __PACKAGE__->write_config(@_); }); | |
20 | ||
21 | PVE::JSONSchema::register_standard_option('pve-sdn-dns-id', { | |
22 | description => "The SDN dns object identifier.", | |
23 | type => 'string', format => 'pve-sdn-dns-id', | |
24 | }); | |
25 | ||
26 | PVE::JSONSchema::register_format('pve-sdn-dns-id', \&parse_sdn_dns_id); | |
27 | sub parse_sdn_dns_id { | |
28 | my ($id, $noerr) = @_; | |
29 | ||
30 | if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) { | |
31 | return undef if $noerr; | |
32 | die "dns ID '$id' contains illegal characters\n"; | |
33 | } | |
34 | return $id; | |
35 | } | |
36 | ||
37 | my $defaultData = { | |
38 | ||
39 | propertyList => { | |
40 | type => { | |
41 | description => "Plugin type.", | |
42 | type => 'string', format => 'pve-configid', | |
43 | }, | |
44 | ttl => { type => 'integer', optional => 1 }, | |
4ad78442 | 45 | reversev6mask => { type => 'integer', optional => 1 }, |
ee4f339e AD |
46 | dns => get_standard_option('pve-sdn-dns-id', |
47 | { completion => \&PVE::Network::SDN::Dns::complete_sdn_dns }), | |
48 | }, | |
49 | }; | |
50 | ||
51 | sub private { | |
52 | return $defaultData; | |
53 | } | |
54 | ||
55 | sub parse_section_header { | |
56 | my ($class, $line) = @_; | |
57 | ||
58 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
59 | my ($type, $id) = (lc($1), $2); | |
60 | my $errmsg = undef; # set if you want to skip whole section | |
61 | eval { PVE::JSONSchema::pve_verify_configid($type); }; | |
62 | $errmsg = $@ if $@; | |
63 | my $config = {}; # to return additional attributes | |
64 | return ($type, $id, $errmsg, $config); | |
65 | } | |
66 | return undef; | |
67 | } | |
68 | ||
69 | ||
70 | sub add_a_record { | |
71 | my ($class, $plugin_config, $type, $zone, $reversezone, $hostname, $ip) = @_; | |
72 | } | |
73 | ||
74 | sub del_a_record { | |
75 | my ($class, $plugin_config, $hostname, $ip) = @_; | |
76 | } | |
77 | ||
78 | sub on_update_hook { | |
79 | my ($class, $plugin_config) = @_; | |
80 | } | |
81 | ||
82 | #helpers | |
83 | sub api_request { | |
84 | my ($method, $url, $headers, $data) = @_; | |
85 | ||
86 | my $encoded_data = to_json($data) if $data; | |
87 | ||
88 | my $req = HTTP::Request->new($method,$url, $headers, $encoded_data); | |
89 | ||
90 | my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30); | |
91 | my $proxy = undef; | |
92 | ||
93 | if ($proxy) { | |
94 | $ua->proxy(['http', 'https'], $proxy); | |
95 | } else { | |
96 | $ua->env_proxy; | |
97 | } | |
98 | ||
99 | $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); | |
100 | ||
101 | my $response = $ua->request($req); | |
102 | my $code = $response->code; | |
103 | ||
104 | if ($code !~ /^2(\d+)$/) { | |
105 | my $msg = $response->message || 'unknown'; | |
106 | die "Invalid response from server: $code $msg\n"; | |
107 | } | |
108 | ||
109 | my $raw = ''; | |
110 | if (defined($response->decoded_content)) { | |
111 | $raw = $response->decoded_content; | |
112 | } else { | |
113 | $raw = $response->content; | |
114 | } | |
115 | return from_json($raw) if $raw ne ''; | |
116 | } | |
117 | ||
118 | 1; |