Skip to content

Commit

Permalink
Merge pull request #2 from Mirantis/instance-name
Browse files Browse the repository at this point in the history
Instance name support
  • Loading branch information
devcamcar committed Jun 18, 2011
2 parents 28cd1b3 + 1f2563b commit ee85f2b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 2 additions & 0 deletions django-openstack/src/django_openstack/nova/forms.py
Expand Up @@ -137,6 +137,8 @@ class LaunchInstanceForm(forms.Form):
count = forms.ChoiceField(choices=[(x, x) for x in range(1, 6)])
size = forms.ChoiceField()
key_name = forms.ChoiceField()
display_name = forms.CharField(required=True, label="Name")
#security_group = forms.ChoiceField()
user_data = forms.CharField(required=False,
widget=forms.widgets.Textarea(
attrs={'rows': 4}))
Expand Down
20 changes: 18 additions & 2 deletions django-openstack/src/django_openstack/nova/manager.py
Expand Up @@ -97,12 +97,28 @@ def modify_image_attribute(self, image_id, attribute=None, operation=None,
groups=groups,)

@wrap_nova_error
def run_instances(self, image_id, **kwargs):
def run_instances(self, image_id, instance_type, count,
addressing_type='private', key_name=None,
display_name=None, user_data=None):
"""
Runs instances of the specified image id.
"""
conn = self.get_openstack_connection()
return conn.run_instances(image_id, **kwargs)
params = {
'ImageId': image_id,
'InstanceType': instance_type,
'MinCount': count,
'MaxCount': count,
'addressing_type': addressing_type,
}
if key_name is not None:
params['key_name'] = key_name
if display_name is not None:
params['display_name'] = display_name
if user_data is not None:
params['UserData'] = user_data
return conn.get_object('RunInstances', params,
boto.ec2.instance.Reservation, verb='POST')

def get_instance_count(self):
"""
Expand Down
23 changes: 15 additions & 8 deletions django-openstack/src/django_openstack/nova/tests/image_tests.py
Expand Up @@ -89,27 +89,34 @@ def test_launch(self):
reservation = boto.ec2.instance.Reservation()
reservation.instances = [instance]

conn = self.mox.CreateMockAnything()

self.mox.StubOutWithMock(forms, 'get_key_pair_choices')
self.mox.StubOutWithMock(forms, 'get_instance_type_choices')
self.mox.StubOutWithMock(self.project, 'run_instances')
self.mox.StubOutWithMock(self.project, 'get_openstack_connection')

self.project.get_openstack_connection().AndReturn(conn)

forms.get_key_pair_choices(self.project).AndReturn(
self.create_key_pair_choices([TEST_KEY]))
forms.get_instance_type_choices().AndReturn(
self.create_instance_type_choices())
self.project.run_instances(TEST_IMAGE_ID,
addressing_type=mox.IgnoreArg(),
key_name=TEST_KEY,
user_data='',
instance_type='m1.medium',
min_count='1',
max_count='1').AndReturn(reservation)

params = {'addressing_type': 'private',
'UserData': '', 'display_name': u'name',
'MinCount': '1', 'key_name': TEST_KEY,
'MaxCount': '1', 'InstanceType': 'm1.medium',
'ImageId': TEST_IMAGE_ID}
conn.get_object('RunInstances', params, boto.ec2.instance.Reservation,
verb='POST').AndReturn(reservation)

self.mox.ReplayAll()

url = reverse('nova_images_launch', args=[TEST_PROJECT, TEST_IMAGE_ID])
data = {'key_name': TEST_KEY,
'count': '1',
'size': 'm1.medium',
'display_name': 'name',
'user_data': ''}
res = self.client.post(url, data)
self.assertRedirectsNoFollow(res, reverse('nova_instances',
Expand Down
14 changes: 5 additions & 9 deletions django-openstack/src/django_openstack/nova/views/images.py
Expand Up @@ -78,17 +78,13 @@ def launch(request, project_id, image_id):
form = forms.LaunchInstanceForm(project, request.POST)
if form.is_valid():
try:
reservation = project.run_instances(
image_id,
addressing_type='private',
reservation = project.run_instances(image_id,
form.cleaned_data['size'],
form.cleaned_data['count'],
key_name=form.cleaned_data['key_name'],
#security_groups=[form.cleaned_data['security_group']],
display_name=form.cleaned_data['display_name'],
user_data=re.sub('\r\n', '\n',
form.cleaned_data['user_data']),
instance_type=form.cleaned_data['size'],
min_count=form.cleaned_data['count'],
max_count=form.cleaned_data['count']
)
form.cleaned_data['user_data']))
except exceptions.NovaApiError, e:
messages.error(request,
_('Unable to launch: %s') % e.message)
Expand Down
Expand Up @@ -2,7 +2,7 @@
{% if instances %}
<table style="width: 100%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Image</th>
<th>Size</th>
<th>IP</th>
Expand Down Expand Up @@ -74,7 +74,7 @@
</td>
{% else %}

<td><a href="{% url nova_instances_detail project.projectname instance.id %}">{{ instance.id }} {% if instance.displayName %}({{ instance.displayName }}){% endif %}
<td><a href="{% url nova_instances_detail project.projectname instance.id %}">{% if instance.displayName %}{{ instance.displayName }}{% endif %} ({{ instance.id }})
</a></td>
<td class="odd">{{ instance.image_id }}</td>
<td>{{ instance.instance_type }}</td>
Expand Down

0 comments on commit ee85f2b

Please sign in to comment.