| 1 | # | ||
| 2 | # This file is part of Net-Gandi | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2012 by Natal Ngétal. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | package Net::Gandi::Hosting::VM; | ||
| 10 | { | ||
| 11 | $Net::Gandi::Hosting::VM::VERSION = '1.122180'; | ||
| 12 | } | ||
| 13 | |||
| 14 | # ABSTRACT: Vm interface | ||
| 15 | |||
| 16 | use Moose; | ||
| 17 | use MooseX::Params::Validate; | ||
| 18 | |||
| 19 | use namespace::autoclean; | ||
| 20 | use Net::Gandi::Types Client => { -as => 'Client_T' }; | ||
| 21 | use Net::Gandi::Error qw(_validated_params); | ||
| 22 | |||
| 23 | use Carp; | ||
| 24 | |||
| 25 | |||
| 26 | has 'id' => ( is => 'rw', isa => 'Int' ); | ||
| 27 | |||
| 28 | has client => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => Client_T, | ||
| 31 | required => 1, | ||
| 32 | ); | ||
| 33 | |||
| 34 | |||
| 35 | sub list { | ||
| 36 | my ( $self, $params ) = validated_list( | ||
| 37 | \@_, | ||
| 38 | opts => { isa => 'HashRef', optional => 1 } | ||
| 39 | ); | ||
| 40 | |||
| 41 | $params ||= {}; | ||
| 42 | return $self->client->api_call( "vm.list", $params ); | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub count { | ||
| 47 | my ( $self, $params ) = validated_list( | ||
| 48 | \@_, | ||
| 49 | opts => { isa => 'HashRef', optional => 1 } | ||
| 50 | ); | ||
| 51 | |||
| 52 | $params ||= {}; | ||
| 53 | return $self->client->api_call('vm.count', $params); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub info { | ||
| 58 | my ( $self ) = @_; | ||
| 59 | |||
| 60 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 61 | return $self->client->api_call( 'vm.info', $self->id ); | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | sub create { | ||
| 66 | my ( $self, $params ) = validated_list( | ||
| 67 | \@_, | ||
| 68 | vm_spec => { isa => 'HashRef' } | ||
| 69 | ); | ||
| 70 | |||
| 71 | _validated_params('vm_create', $params); | ||
| 72 | |||
| 73 | foreach my $param ( 'hostname', 'password' ) { | ||
| 74 | $params->{$param} = XMLRPC::Data->type('string')->value($params->{$param}); | ||
| 75 | } | ||
| 76 | |||
| 77 | return $self->client->api_call( "vm.create", $params ); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub create_from { | ||
| 82 | my ( $self, $params, $disk_spec, $src_disk_id ) = validated_list( | ||
| 83 | \@_, | ||
| 84 | vm_spec => { isa => 'HashRef' }, | ||
| 85 | disk_spec => { isa => 'HashRef' }, | ||
| 86 | src_disk_id => { isa => 'Int' } | ||
| 87 | ); | ||
| 88 | |||
| 89 | _validated_params('vm_create_from', $params); | ||
| 90 | |||
| 91 | foreach my $param ( 'hostname', 'password' ) { | ||
| 92 | $params->{$param} = XMLRPC::Data | ||
| 93 | ->type('string') | ||
| 94 | ->value($params->{$param}); | ||
| 95 | } | ||
| 96 | |||
| 97 | return $self->client->api_call( "vm.create_from", $params, $disk_spec, $src_disk_id ); | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | sub update { | ||
| 102 | my ( $self, $params ) = validated_list( | ||
| 103 | \@_, | ||
| 104 | vm_spec => { isa => 'HashRef' } | ||
| 105 | ); | ||
| 106 | |||
| 107 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 108 | |||
| 109 | $params ||= {}; | ||
| 110 | return $self->client->api_call('vm.update', $self->id, $params); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub disk_attach { | ||
| 115 | my ( $self, $disk_id, $params ) = validated_list( | ||
| 116 | \@_, | ||
| 117 | disk_id => { isa => 'Int'}, | ||
| 118 | opts => { isa => 'HashRef' } | ||
| 119 | ); | ||
| 120 | |||
| 121 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 122 | |||
| 123 | if ( $params ) { | ||
| 124 | return $self->client->api_call('vm.disk_attach', $self->id, $disk_id, $params); | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | return $self->client->api_call('vm.disk_attach', $self->id, $disk_id); | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub disk_detach { | ||
| 133 | my ( $self, $disk_id ) = validated_list( | ||
| 134 | \@_, | ||
| 135 | disk_id => { isa => 'Int'} | ||
| 136 | ); | ||
| 137 | |||
| 138 | |||
| 139 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 140 | |||
| 141 | return $self->client->api_call('vm.disk_detach', $self->id, $disk_id); | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | sub start { | ||
| 147 | my ( $self ) = @_; | ||
| 148 | |||
| 149 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 150 | return $self->client->api_call('vm.start', $self->id); | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub stop { | ||
| 155 | my ( $self ) = @_; | ||
| 156 | |||
| 157 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 158 | return $self->client->api_call('vm.stop', $self->id); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | sub reboot { | ||
| 163 | my ( $self ) = @_; | ||
| 164 | |||
| 165 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 166 | return $self->client->api_call('vm.reboot', $self->id); | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | sub delete { | ||
| 171 | my ( $self ) = @_; | ||
| 172 | |||
| 173 | carp 'Required parameter id is not defined' if ( ! $self->id ); | ||
| 174 | return $self->client->api_call('vm.delete', $self->id); | ||
| 175 | } | ||
| 176 | |||
| 177 | no Moose; | ||
| 178 | __PACKAGE__->meta->make_immutable; | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | __END__ |