| 1 | package RightScale::Query; | ||
| 2 | # Author: Matthew Mallard | ||
| 3 | # Website: www.q-technologies.com.au | ||
| 4 | # Date: 25th May 2016 | ||
| 5 | |||
| 6 | # ABSTRACT: Query RightScale for server instances | ||
| 7 | |||
| 8 | |||
| 9 | use URI; | ||
| 10 | use JSON; | ||
| 11 | use HTTP::Headers; | ||
| 12 | use LWP::UserAgent; | ||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | @ISA = qw(Exporter); | ||
| 16 | @EXPORT = qw(rs_get_access_token run_rs_query rs_find_instance); | ||
| 17 | |||
| 18 | |||
| 19 | our $api_url; | ||
| 20 | our $internet_proxy; | ||
| 21 | our $internet_proxy_timeout = 30; | ||
| 22 | |||
| 23 | sub rs_get_access_token { | ||
| 24 | my $refresh_token = shift; | ||
| 25 | my $form = { | ||
| 26 | grant_type => 'refresh_token', | ||
| 27 | refresh_token => $refresh_token, | ||
| 28 | }; | ||
| 29 | my $headers = HTTP::Headers->new( | ||
| 30 | 'X-API-Version' => '1.5', | ||
| 31 | ); | ||
| 32 | my $path = "oauth2"; | ||
| 33 | my $token_request = run_rs_query( $headers, $form, $path, "POST" ); | ||
| 34 | return $token_request->{access_token}; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub run_rs_query { | ||
| 39 | my $headers = shift; | ||
| 40 | my $form = shift; | ||
| 41 | my $path = shift; | ||
| 42 | my $type = shift; | ||
| 43 | my $uri = URI->new("$api_url/$path"); | ||
| 44 | $uri->query_form($form) if $type eq "GET"; | ||
| 45 | my $ua = LWP::UserAgent->new(); | ||
| 46 | $ua->default_headers( $headers ); | ||
| 47 | $ua->timeout( $internet_proxy_timeout ); | ||
| 48 | $ua->proxy( 'https', "connect://$internet_proxy/") if $internet_proxy; | ||
| 49 | my $response; | ||
| 50 | if( $type eq "GET" ) { | ||
| 51 | $response = $ua->get($uri ) ; | ||
| 52 | } elsif( $type eq "POST" ){ | ||
| 53 | $response = $ua->post($uri, $form ); | ||
| 54 | } else { | ||
| 55 | die "Unsupported request type"; | ||
| 56 | } | ||
| 57 | my $output; | ||
| 58 | if ($response->is_success) { | ||
| 59 | $output = $response->decoded_content; | ||
| 60 | } else { | ||
| 61 | die $response->status_line; | ||
| 62 | } | ||
| 63 | return decode_json( $output ); | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub rs_find_instance { | ||
| 68 | my $instance_id = shift; | ||
| 69 | my $rs_cloud_id = shift; | ||
| 70 | my $access_token = shift; | ||
| 71 | my $form = { | ||
| 72 | "filter[]" => "resource_uid==$instance_id", | ||
| 73 | }; | ||
| 74 | my $headers = HTTP::Headers->new( | ||
| 75 | 'X-API-Version' => '1.5', | ||
| 76 | 'Authorization' => "Bearer $access_token", | ||
| 77 | ); | ||
| 78 | my $path = "clouds/$rs_cloud_id/instances"; | ||
| 79 | my $instance_request = run_rs_query( $headers, $form, $path, "GET" ); | ||
| 80 | # Return first element of the array as we should only get one answer | ||
| 81 | return $instance_request->[0] if @$instance_request > 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Connect to RightScale and get the temporary access token using the refresh token | ||
| 85 | sub rs_get_access_token_curl { | ||
| 86 | my $refresh_token = shift; | ||
| 87 | my $api_url = shift; | ||
| 88 | my $curl1 = <<CURL; | ||
| 89 | curl -s \\ | ||
| 90 | --connect-timeout 10 \\ | ||
| 91 | -H "X-API-Version:1.5" \\ | ||
| 92 | --request POST "$api_url/oauth2" \\ | ||
| 93 | -d "grant_type=refresh_token" \\ | ||
| 94 | -d "refresh_token=$refresh_token" | ||
| 95 | CURL | ||
| 96 | |||
| 97 | my $token_request = decode_json `$curl1`; | ||
| 98 | return $token_request->{access_token}; | ||
| 99 | } | ||
| 100 | |||
| 101 | # Connect to RightScale and look for the specified instance | ||
| 102 | # if it's found we will have a hash array of data about the instance | ||
| 103 | # if not found - the hash will be undef | ||
| 104 | sub rs_find_instance_curl { | ||
| 105 | my $instance_id = shift; | ||
| 106 | my $rs_cloud_id = shift; | ||
| 107 | my $access_token = shift; | ||
| 108 | my $api_url = shift; | ||
| 109 | my $curl2 = <<CURL; | ||
| 110 | curl -s \\ | ||
| 111 | --connect-timeout 10 \\ | ||
| 112 | -H "X-API-Version:1.5" \\ | ||
| 113 | -H "Authorization: Bearer $access_token" \\ | ||
| 114 | -d "filter[]=resource_uid==$instance_id" \\ | ||
| 115 | --request GET "$api_url/clouds/$rs_cloud_id/instances" | ||
| 116 | CURL | ||
| 117 | |||
| 118 | my $instance_request = decode_json `$curl2`; | ||
| 119 | return $instance_request; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | 1; | ||
| 127 | |||
| 128 | __END__ |