RT #31482 making sure the tax class is not editable after the customer has been billed.
[freeside.git] / FS / FS / part_export / amazon_ec2.pm
1 package FS::part_export::amazon_ec2;
2
3 use base qw( FS::part_export );
4
5 use vars qw(@ISA %info $replace_ok_kludge);
6 use Tie::IxHash;
7 use FS::Record qw( qsearchs );
8 use FS::svc_external;
9
10 tie my %options, 'Tie::IxHash',
11   'access_key' => { label => 'AWS access key', },
12   'secret_key' => { label => 'AWS secret key', },
13   'ami'        => { label => 'AMI', 'default' => 'ami-ff46a796', },
14   'keyname'    => { label => 'Keypair name', },
15   #option to turn off (or on) ip address allocation
16 ;
17
18 %info = (
19   'svc'      => 'svc_external',
20   'desc'     =>
21     'Export to Amazon EC2',
22   'options'  => \%options,
23   'no_machine' => 1,
24   'notes'    => <<'END'
25 Create instances in the Amazon EC2 (Elastic compute cloud).  Install
26 Net::Amazon::EC2 perl module.  Advisable to set svc_external-skip_manual config
27 option.
28 END
29 );
30
31 $replace_ok_kludge = 0;
32
33 sub rebless { shift; }
34
35 sub _export_insert {
36   my($self, $svc_external) = (shift, shift);
37   $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'insert',
38     $svc_external->svcnum,
39     $self->option('ami'),
40     $self->option('keyname'),
41   );
42   ref($err_or_queue) ? '' : $err_or_queue;
43 }
44
45 sub _export_replace {
46   my( $self, $new, $old ) = (shift, shift, shift);
47   return '' if $replace_ok_kludge;
48   return "can't change instance id or IP address";
49   #$err_or_queue = $self->amazon_ec2_queue( $new->svcnum,
50   #  'replace', $new->username, $new->_password );
51   #ref($err_or_queue) ? '' : $err_or_queue;
52 }
53
54 sub _export_delete {
55   my( $self, $svc_external ) = (shift, shift);
56   my( $instance_id, $ip ) = split(/:/, $svc_external->title );
57   $err_or_queue = $self->amazon_ec2_queue( $svc_external->svcnum, 'delete',
58     $instance_id,
59     $ip,
60   );
61   ref($err_or_queue) ? '' : $err_or_queue;
62 }
63
64 #these three are optional
65 # fallback for svc_acct will change and restore password
66 #sub _export_suspend {
67 #  my( $self, $svc_something ) = (shift, shift);
68 #  $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
69 #    'suspend', $svc_something->username );
70 #  ref($err_or_queue) ? '' : $err_or_queue;
71 #}
72 #
73 #sub _export_unsuspend {
74 #  my( $self, $svc_something ) = (shift, shift);
75 #  $err_or_queue = $self->amazon_ec2_queue( $svc_something->svcnum,
76 #    'unsuspend', $svc_something->username );
77 #  ref($err_or_queue) ? '' : $err_or_queue;
78 #}
79
80 sub export_links {
81   my($self, $svc_external, $arrayref) = (shift, shift, shift);
82   my( $instance_id, $ip ) = split(/:/, $svc_external->title );
83    
84   push @$arrayref, qq!<A HREF="http://$ip/">http://$ip/</A>!;
85   '';
86 }
87
88 ###
89
90 #a good idea to queue anything that could fail or take any time
91 sub amazon_ec2_queue {
92   my( $self, $svcnum, $method ) = (shift, shift, shift);
93   my $queue = new FS::queue {
94     'svcnum' => $svcnum,
95     'job'    => "FS::part_export::amazon_ec2::amazon_ec2_$method",
96   };
97   $queue->insert( $self->option('access_key'),
98                   $self->option('secret_key'),
99                   @_
100                 )
101     or $queue;
102 }
103
104 sub amazon_ec2_new {
105   my( $access_key, $secret_key, @rest ) = @_;
106
107   eval 'use Net::Amazon::EC2;';
108   die $@ if $@;
109
110   my $ec2 = new Net::Amazon::EC2 'AWSAccessKeyId'  => $access_key,
111                                  'SecretAccessKey' => $secret_key;
112
113   ( $ec2, @rest );
114 }
115
116 sub amazon_ec2_insert { #subroutine, not method
117   my( $ec2, $svcnum, $ami, $keyname ) = amazon_ec2_new(@_);
118
119   my $reservation_info = $ec2->run_instances( 'ImageId'  => $ami,
120                                               'KeyName'  => $keyname,
121                                               'MinCount' => 1,
122                                               'MaxCount' => 1,
123                                             );
124
125   my $instance_id = $reservation_info->instances_set->[0]->instance_id;
126
127   my $ip = $ec2->allocate_address
128     or die "can't allocate address";
129   $ec2->associate_address('InstanceId' => $instance_id,
130                           'PublicIp'   => $ip,
131                          )
132     or die "can't assocate IP address $ip with instance $instance_id";
133
134   my $svc_external = qsearchs('svc_external', { 'svcnum' => $svcnum } )
135     or die "can't find svc_external.svcnum $svcnum\n";
136
137   $svc_external->title("$instance_id:$ip");
138
139   local($replace_ok_kludge) = 1;
140   my $error = $svc_external->replace;
141   die $error if $error;
142
143 }
144
145 #sub amazon_ec2_replace { #subroutine, not method
146 #}
147
148 sub amazon_ec2_delete { #subroutine, not method
149   my( $ec2, $id, $ip ) = amazon_ec2_new(@_);
150
151   my $instance_id = sprintf('i-%x', $id);
152   $ec2->disassociate_address('PublicIp'=>$ip)
153     or die "can't dissassocate $ip";
154
155   $ec2->release_address('PublicIp'=>$ip)
156     or die "can't release $ip";
157
158   my $result = $ec2->terminate_instances('InstanceId'=>$instance_id);
159   #check for instance_id match or something?
160
161 }
162
163 #sub amazon_ec2_suspend { #subroutine, not method
164 #}
165
166 #sub amazon_ec2_unsuspend { #subroutine, not method
167 #}
168
169 1;
170