cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
query.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.ec2.helpers.query
22 #
23 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
24 # @author Oleksandr Gituliar <gituliar@gmail.com>
25 #
26 
27 from datetime import datetime
28 import urllib
29 
30 from ec2.base.auth import _sign_parameters_ver2
31 
32 
33 def query(parameters, aws_key=None, aws_secret=None, endpoint=None,
34  method=None, secure=False):
35  parameters.setdefault('SignatureMethod', 'HmacSHA256')
36  parameters.setdefault('SignatureVersion', '2')
37 
38  parameters['AWSAccessKeyId'] = aws_key
39  #parameters['Expires'] = "2012-03-17T12:41:35Z"
40  parameters['Timestamp'] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
41  parameters['Version'] = "2012-03-01"
42 
43  # set Signature
44  signature = _sign_parameters_ver2(
45  parameters,
46  aws_secret,
47  endpoint=endpoint,
48  method=method,
49  )
50  parameters['Signature'] = signature
51 
52  # build request
53  protocol = 'http' if not secure else 'https'
54  query_parameters = urllib.urlencode(parameters)
55  if method == 'GET':
56  request = ("%s://%s/?%s" % (protocol, endpoint, query_parameters),)
57  elif method == 'POST':
58  request = ("%s://%s" % (protocol, endpoint), query_parameters)
59  else:
60  raise Exception('Unsupported %s method: %s' % (protocol.upper(), method))
61 
62  response = urllib.urlopen(*request).read()
63 
64  return request, response
65 
66 
67 def get_instance_tags(cluster_manager):
68  instances = cluster_manager.user.vm.get_list()
69 
70  tags = []
71  for instance in instances:
72  tags.append({'resource-id': 'i-' + str(instance['vm_id']),
73  'key': 'Name',
74  'resource-type': 'instance',
75  'value': instance['name']})
76  return tags
77 
78 
79 def get_instance_name_tag(cluster_manager, id):
80  instance = cluster_manager.user.vm.get_by_id({'vm_id':id})
81 
82  tags = {'resource-id': 'i-' + str(instance['vm_id']),
83  'key': 'Name',
84  'resource-type': 'instance',
85  'value': instance['name']}
86  return tags
87 
88 
89 def get_volume_name_tag(cluster_manager, id):
90  volume = cluster_manager.user.storage_image.get_by_id({'vm_id':id})
91 
92  tags = {'resource-id': 'i-' + str(volume['storage_image_id']),
93  'key': 'Name',
94  'resource-type': 'volume',
95  'value': volume['name']}
96  return tags
97 
98 
99 def get_volume_tags(cluster_manager):
100  volumes = cluster_manager.user.storage_image.get_list()
101 
102  tags = []
103  for volume in volumes:
104  tags.append({'resource-id': 'vol-' + str(volume['storage_image_id']),
105  'key': 'Name',
106  'resource-type': 'volume',
107  'value': volume['name']})
108  return tags
109