Skip to content

Commit

Permalink
Added explicit mimetype override for uploaded files, Fixes Issue-25. …
Browse files Browse the repository at this point in the history
…Also fleshing out EC2 implementation.
  • Loading branch information
Mitch.Garnaat authored and Mitch.Garnaat committed Jan 17, 2007
1 parent 3e99c22 commit 49e9612
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 85 deletions.
199 changes: 128 additions & 71 deletions boto/connection.py
Expand Up @@ -46,12 +46,13 @@
import urllib
import os
import xml.sax
from boto.exception import SQSError, S3ResponseError, S3CreateError
from boto.exception import SQSError, S3ResponseError
from boto.exception import S3CreateError, EC2ResponseError
from boto import handler
from boto.sqs.queue import Queue
from boto.s3.bucket import Bucket
from boto.resultset import ResultSet
from boto.ec2.image import Image
from boto.ec2.image import Image, ImageAttribute
from boto.ec2.instance import Reservation, Instance
from boto.ec2.keypair import KeyPair
from boto.ec2.securitygroup import SecurityGroup
Expand Down Expand Up @@ -291,6 +292,8 @@ def make_request(self, action, params=None):
def build_list_params(self, params, items, label):
for i in range(1, len(items)+1):
params['%s.%d' % (label, i)] = items[i-1]

# Image methods

def get_all_images(self, image_ids=None, owners=None, executable_by=None):
params = {}
Expand All @@ -308,74 +311,96 @@ def get_all_images(self, image_ids=None, owners=None, executable_by=None):
xml.sax.parseString(body, h)
return rs
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

def get_all_instances(self, instance_ids=None):
params = {}
if instance_ids:
self.build_list_params(params, instance_ids, 'InstanceId')
response = self.make_request('DescribeInstances', params)
def register_image(self, image_location):
params = {'ImageLocation':image_location}
response = self.make_request('RegisterImage', params)
body = response.read()
if response.status == 200:
rs = ResultSet('item', Reservation)
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
return rs.imageId
else:
raise S3ResponseError(response.status, response.reason)

def get_all_key_pairs(self, keynames=None):
params = {}
if keynames:
self.build_list_params(params, keynames, 'KeyName')
response = self.make_request('DescribeKeyPairs', params)
raise EC2ResponseError(response.status, response.reason, body)

def deregister_image(self, image_id):
params = {'ImageId':image_id}
response = self.make_request('DeregisterImage', params)
body = response.read()
if response.status == 200:
rs = ResultSet('item', KeyPair)
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
return rs.status
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

def get_all_security_groups(self, groupnames=None):
params = {}
if groupnames:
self.build_list_params(params, groupnames, 'GroupName')
response = self.make_request('DescribeSecurityGroups', params)
# ImageAttribute methods

def get_image_attribute(self, image_id, attribute='launchPermission'):
params = {'ImageId' : image_id,
'Attribute' : attribute}
response = self.make_request('DescribeImageAttribute', params)
body = response.read()
if response.status == 200:
rs = ResultSet('item', SecurityGroup)
h = handler.XmlHandler(rs, self)
image_attr = ImageAttribute()
h = handler.XmlHandler(image_attr, self)
xml.sax.parseString(body, h)
return rs
print body
return image_attr
else:
raise S3ResponseError(response.status, response.reason)

def register_image(self, image_location):
params = {'ImageLocation':image_location}
response = self.make_request('RegisterImage', params)
raise EC2ResponseError(response.status, response.reason, body)

def modify_image_attribute(self, image_id, attribute='launchPermission',
operation='add', user_ids=None, groups=None):
params = {'ImageId' : image_id,
'Attribute' : attribute,
'OperationType' : operation}
if user_ids:
self.build_list_params(params, user_ids, 'UserId')
if groups:
self.build_list_params(params, groups, 'UserGroup')
response = self.make_request('ModifyImageAttribute', params)
body = response.read()
if response.status == 200:
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs.imageId
return rs.status
else:
raise S3ResponseError(response.status, response.reason)

def deregister_image(self, image_id):
params = {'ImageId':image_id}
response = self.make_request('DeregisterImage', params)
raise EC2ResponseError(response.status, response.reason, body)

def reset_image_attribute(self, image_id, attribute='launchPermission'):
params = {'ImageId' : image_id,
'Attribute' : attribute}
response = self.make_request('ResetImageAttribute', params)
body = response.read()
if response.status == 200:
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs.status
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

# Instance methods

def get_all_instances(self, instance_ids=None):
params = {}
if instance_ids:
self.build_list_params(params, instance_ids, 'InstanceId')
response = self.make_request('DescribeInstances', params)
body = response.read()
if response.status == 200:
rs = ResultSet('item', Reservation)
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise EC2ResponseError(response.status, response.reason, body)

def run_instances(self, image_id, min_count=1, max_count=1, key_name=None,
security_groups=None, user_data=None):
params = {'ImageId':image_id,
Expand All @@ -396,7 +421,7 @@ def run_instances(self, image_id, min_count=1, max_count=1, key_name=None,
xml.sax.parseString(body, h)
return res
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

def terminate_instances(self, instance_ids=None):
params = {}
Expand All @@ -410,7 +435,63 @@ def terminate_instances(self, instance_ids=None):
xml.sax.parseString(body, h)
return rs
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

# Keypair methods

def get_all_key_pairs(self, keynames=None):
params = {}
if keynames:
self.build_list_params(params, keynames, 'KeyName')
response = self.make_request('DescribeKeyPairs', params)
body = response.read()
if response.status == 200:
rs = ResultSet('item', KeyPair)
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise EC2ResponseError(response.status, response.reason, body)

def create_key_pair(self, key_name):
params = {'KeyName':key_name}
response = self.make_request('CreateKeyPair', params)
body = response.read()
if response.status == 200:
key = KeyPair(self)
h = handler.XmlHandler(key, self)
xml.sax.parseString(body, h)
return key
else:
raise EC2ResponseError(response.status, response.reason, body)

def delete_key_pair(self, key_name):
params = {'KeyName':key_name}
response = self.make_request('DeleteKeyPair', params)
body = response.read()
if response.status == 200:
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs.status
else:
raise EC2ResponseError(response.status, response.reason, body)

# SecurityGroup methods

def get_all_security_groups(self, groupnames=None):
params = {}
if groupnames:
self.build_list_params(params, groupnames, 'GroupName')
response = self.make_request('DescribeSecurityGroups', params)
body = response.read()
if response.status == 200:
rs = ResultSet('item', SecurityGroup)
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs
else:
raise EC2ResponseError(response.status, response.reason, body)

def create_security_group(self, name, description):
params = {'GroupName':name, 'GroupDescription':description}
Expand All @@ -425,7 +506,7 @@ def create_security_group(self, name, description):
else:
return None
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

def delete_security_group(self, name):
params = {'GroupName':name}
Expand All @@ -437,7 +518,7 @@ def delete_security_group(self, name):
xml.sax.parseString(body, h)
return rs.status
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason)

def authorize_security_group(self, group_name, src_security_group_name=None,
src_security_group_owner_id=None,
Expand All @@ -455,7 +536,7 @@ def authorize_security_group(self, group_name, src_security_group_name=None,
if to_port:
params['ToPort'] = to_port
if cidr_ip:
params['CidrIp'] = cidr_ip
params['CidrIp'] = urllib.quote(cidr_ip)
response = self.make_request('AuthorizeSecurityGroupIngress', params)
body = response.read()
if response.status == 200:
Expand All @@ -464,7 +545,7 @@ def authorize_security_group(self, group_name, src_security_group_name=None,
xml.sax.parseString(body, h)
return rs.status
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)

def revoke_security_group(self, group_name, src_security_group_name=None,
src_security_group_owner_id=None,
Expand All @@ -491,31 +572,7 @@ def revoke_security_group(self, group_name, src_security_group_name=None,
xml.sax.parseString(body, h)
return rs.status
else:
raise S3ResponseError(response.status, response.reason)

def create_key_pair(self, key_name):
params = {'KeyName':key_name}
response = self.make_request('CreateKeyPair', params)
body = response.read()
if response.status == 200:
key = KeyPair(self)
h = handler.XmlHandler(key, self)
xml.sax.parseString(body, h)
return key
else:
raise S3ResponseError(response.status, response.reason)

def delete_key_pair(self, key_name):
params = {'KeyName':key_name}
response = self.make_request('DeleteKeyPair', params)
body = response.read()
if response.status == 200:
rs = ResultSet()
h = handler.XmlHandler(rs, self)
xml.sax.parseString(body, h)
return rs.status
else:
raise S3ResponseError(response.status, response.reason)
raise EC2ResponseError(response.status, response.reason, body)



34 changes: 34 additions & 0 deletions boto/ec2/image.py
Expand Up @@ -62,3 +62,37 @@ def run(self, min_count=1, max_count=1, keypair=None,
def deregister(self):
return self.connection.deregister_image(self.id)

def get_attribute(self, attribute_name):
return self.connection.get_image_attribute(self.id, attribute_name)

def reset_attribute(self, attribute_name):
return self.connection.reset_image_attribute(self.id, attribute_name)


class ImageAttribute:

def __init__(self, parent=None):
self.name = None
self.attrs = {}

def startElement(self, name, attrs, connection):
return None

def endElement(self, name, value, connection):
if name == 'launchPermission':
self.name = 'launch_permission'
elif name == 'group':
if self.attrs.has_key('groups'):
self.attrs['groups'].append(value)
else:
self.attrs['groups'] = [value]
elif name == 'userId':
if self.attrs.has_key('user_ids'):
self.attrs['user_ids'].append(value)
else:
self.attrs['user_ids'] = [value]
print self.attrs
elif name == 'imageId':
self.image_id = value
else:
setattr(self, name, value)

0 comments on commit 49e9612

Please sign in to comment.