]> git.proxmox.com Git - pve-network.git/blame - PVE/Network/SDN/Ipams/Plugin.pm
ipam: add update_ip
[pve-network.git] / PVE / Network / SDN / Ipams / Plugin.pm
CommitLineData
70b03506
AD
1package PVE::Network::SDN::Ipams::Plugin;
2
3use strict;
4use warnings;
5
6use PVE::Tools qw(run_command);
7use PVE::JSONSchema;
8use PVE::Cluster;
9use HTTP::Request;
10use LWP::UserAgent;
11use JSON;
12
13use Data::Dumper;
14use PVE::JSONSchema qw(get_standard_option);
15use base qw(PVE::SectionConfig);
16
17PVE::Cluster::cfs_register_file('sdn/ipams.cfg',
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
21PVE::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
26PVE::JSONSchema::register_format('pve-sdn-ipam-id', \&parse_sdn_ipam_id);
27sub 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
37my $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
50sub private {
51 return $defaultData;
52}
53
54sub 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
69sub add_subnet {
70 my ($class, $plugin_config, $subnetid, $subnet) = @_;
dd54b5a3
AD
71
72 die "please implement inside plugin";
70b03506
AD
73}
74
75sub del_subnet {
76 my ($class, $plugin_config, $subnetid, $subnet) = @_;
dd54b5a3
AD
77
78 die "please implement inside plugin";
70b03506
AD
79}
80
81sub add_ip {
e9365ab0 82 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway) = @_;
70b03506 83
dd54b5a3
AD
84 die "please implement inside plugin";
85}
86
87sub update_ip {
88 my ($class, $plugin_config, $subnetid, $subnet, $ip, $hostname, $mac, $description, $is_gateway) = @_;
89 # only update ip attributes (mac,hostname,..). Don't change the ip addresses itself, as some ipam
90 # don't allow ip address change without del/add
91
92 die "please implement inside plugin";
70b03506
AD
93}
94
95sub add_next_freeip {
e9365ab0 96 my ($class, $plugin_config, $subnetid, $subnet, $hostname, $mac, $description) = @_;
dd54b5a3
AD
97
98 die "please implement inside plugin";
70b03506
AD
99}
100
101sub del_ip {
e8736dac 102 my ($class, $plugin_config, $subnetid, $subnet, $ip) = @_;
dd54b5a3
AD
103
104 die "please implement inside plugin";
70b03506
AD
105}
106
107sub on_update_hook {
108 my ($class, $plugin_config) = @_;
109}
110
111
112#helpers
113sub api_request {
114 my ($method, $url, $headers, $data) = @_;
115
116 my $encoded_data = to_json($data) if $data;
117
118 my $req = HTTP::Request->new($method,$url, $headers, $encoded_data);
119
120 my $ua = LWP::UserAgent->new(protocols_allowed => ['http', 'https'], timeout => 30);
121 my $proxy = undef;
122
123 if ($proxy) {
124 $ua->proxy(['http', 'https'], $proxy);
125 } else {
126 $ua->env_proxy;
127 }
128
129 $ua->ssl_opts(verify_hostname => 0, SSL_verify_mode => 0x00);
130
131 my $response = $ua->request($req);
132 my $code = $response->code;
133
ee4f339e 134 if ($code !~ /^2(\d+)$/) {
70b03506
AD
135 my $msg = $response->message || 'unknown';
136 die "Invalid response from server: $code $msg\n";
137 }
138
139 my $raw = '';
140 if (defined($response->decoded_content)) {
141 $raw = $response->decoded_content;
142 } else {
143 $raw = $response->content;
144 }
145 return from_json($raw) if $raw ne '';
146}
147
1481;