###########################################################################
# CLOUD SERVER API DOCUMENTATION
#
# Name: reconfigureMonitoringPolicy
# Language: Perl
#
# Description: this function reconfigures monitoring policy
###########################################################################
use strict;
use LWP::UserAgent;
use URI::Escape;
use JSON;
require Http::Request::Common;

#ID access to API
my $TOKEN = "{YOUR_API_TOKEN}"; # e.g.: "f03c3c76cc853ee690b879909c9c6f2a"
my $url = "https://cloudpanel-api.1and1.com/v1";

sub _reconfigureMonitoringPolicy
{
	my ($id, $content) = @_;
	my $_command = $url ."/monitoring_policies/" .$id;
	my $_method = 'PUT';

	#Create the request
	my $ua = LWP::UserAgent->new;
	my $request = HTTP::Request->new($_method => $_command);
	$request->header('X-TOKEN' => $TOKEN);
	$request->content_type('application/json');
	
	#Add additional parameters
	$request->content($content);

	#Get the response
	my $response = $ua->request($request);

	if($response)
	{
		#Get the response and close the handlers
		print $response->headers()->as_string ."\n";
		return $response->content;
		$response->close;
		$request->close;
	}
	else
	{
		#Get the error and close the handlers
		return "Http Error Code: ", $response->code, "\n";
		$request->close;
	}			
}

#Parameters
my $id = "{YOUR_MONITORING_ID}"; # e.g.: "5340033E7FBBC308BC329414A0DF3C20"
my $name = "My monitoring policy";
my $description = "My monitoring policy description";
my $thresholds = {"cpu" => {"warning" => {"value" => 90, "alert" => "false"}, "critical" => {"value" => 95, "alert" => "true"}},
                  "ram" => {"warning" => {"value" => 90, "alert" => "true"}, "critical" => {"value" => 95, "alert" => "true"}},
                  "disk" => {"warning" => {"value" => 80, "alert" => "true"}, "critical" => {"value" => 90, "alert" => "true"}},
                  "transfer" => {"warning" => {"value" => 100, "alert" => "true"}, "critical" => {"value" => 2000, "alert" => "true"}},
                  "internal_ping" => {"warning" => {"value" => 50, "alert" => "true"}, "critical" => {"value" => 100, "alert" => "true"}}};

my %_query = ("name" => $name, "description" => $description, "thresholds" => $thresholds);
my $query = encode_json \%_query;

#Reconfigure monitoring policy
print _reconfigureMonitoringPolicy($id, $query);