]>
Commit | Line | Data |
---|---|---|
70b03506 AD |
1 | package PVE::Network::SDN::Ipams::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/ipams.cfg', | |
18 | sub { __PACKAGE__->parse_config(@_); }, | |
19 | sub { __PACKAGE__->write_config(@_); }); | |
20 | ||
21 | PVE::JSONSchema::register_standard_option('pve-sdn-ipam-id', { | |
22 | description => "The SDN ipam object identifier.", | |
23 | type => 'string', format => 'pve-sdn-ipam-id', | |
24 | }); | |
25 | ||
26 | PVE::JSONSchema::register_format('pve-sdn-ipam-id', \&parse_sdn_ipam_id); | |
27 | sub parse_sdn_ipam_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 "ipam 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 | type => 'string', | |
44 | }, | |
45 | ipam => get_standard_option('pve-sdn-ipam-id', | |
46 | { completion => \&PVE::Network::SDN::Ipams::complete_sdn_ipam }), | |
47 | }, | |
48 | }; | |
49 | ||
50 | sub private { | |
51 | return $defaultData; | |
52 | } | |
53 | ||
54 | sub parse_section_header { | |
55 | my ($class, $line) = @_; | |
56 | ||
57 | if ($line =~ m/^(\S+):\s*(\S+)\s*$/) { | |
58 | my ($type, $id) = (lc($1), $2); | |
59 | my $errmsg = undef; # set if you want to skip whole section | |
60 | eval { PVE::JSONSchema::pve_verify_configid($type); }; | |
61 | $errmsg = $@ if $@; | |
62 | my $config = {}; # to return additional attributes | |
63 | return ($type, $id, $errmsg, $config); | |
64 | } | |
65 | return undef; | |
66 | } | |
67 | ||
68 | ||
69 | sub add_subnet { | |
70 | my ($class, $plugin_config, $subnetid, $subnet) = @_; | |
71 | } | |
72 | ||
73 | sub del_subnet { | |
74 | my ($class, $plugin_config, $subnetid, $subnet) = @_; | |
75 | } | |
76 | ||
77 | sub add_ip { | |
ceb972a9 | 78 | my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $description, $is_gateway) = @_; |
70b03506 AD |
79 | |
80 | } | |
81 | ||
82 | sub add_next_freeip { | |
e8736dac | 83 | my ($class, $plugin_config, $subnetid, $subnet) = @_; |
70b03506 AD |
84 | } |
85 | ||
86 | sub del_ip { | |
e8736dac | 87 | my ($class, $plugin_config, $subnetid, $subnet, $ip) = @_; |
70b03506 AD |
88 | } |
89 | ||
90 | sub on_update_hook { | |
91 | my ($class, $plugin_config) = @_; | |
92 | } | |
93 | ||
94 | ||
95 | #helpers | |
96 | sub api_request { | |
97 | my ($method, $url, $headers, $data) = @_; | |
98 | ||
99 | my $encoded_data = to_json($data) if $data; | |
100 | ||
101 | my $req = HTTP::Request->new($method,$url, $headers, $encoded_data); | |
102 | ||
103 | my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30); | |
104 | my $proxy = undef; | |
105 | ||
106 | if ($proxy) { | |
107 | $ua->proxy(['http', 'https'], $proxy); | |
108 | } else { | |
109 | $ua->env_proxy; | |
110 | } | |
111 | ||
112 | $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00); | |
113 | ||
114 | my $response = $ua->request($req); | |
115 | my $code = $response->code; | |
116 | ||
ee4f339e | 117 | if ($code !~ /^2(\d+)$/) { |
70b03506 AD |
118 | my $msg = $response->message || 'unknown'; |
119 | die "Invalid response from server: $code $msg\n"; | |
120 | } | |
121 | ||
122 | my $raw = ''; | |
123 | if (defined($response->decoded_content)) { | |
124 | $raw = $response->decoded_content; | |
125 | } else { | |
126 | $raw = $response->content; | |
127 | } | |
128 | return from_json($raw) if $raw ne ''; | |
129 | } | |
130 | ||
131 | 1; |