Skip to content

Commit

Permalink
Add the ability to query a list of available subnets and choose which…
Browse files Browse the repository at this point in the history
… subnet to launch the cluster into, hence allowing use of non-default VPC. This has not been tested on clouds other than AWS.
  • Loading branch information
afgane committed Mar 17, 2016
1 parent b0ef3d4 commit 8d537a8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
6 changes: 6 additions & 0 deletions biocloudcentral/forms.py
Expand Up @@ -140,6 +140,12 @@ class CloudManForm(forms.Form):
help_text="A specific key pair to be used when launching your server. This "
"requires you have filled out the initial 6 fields!", required=False,
widget=forms.Select(attrs={"class": textbox_size, 'disabled': 'disabled'}))
subnet_id = DynamicChoiceField(
(("", "Fill above fields & click refresh to fetch"),),
label="Subnet IDs",
help_text="A specific subnet to be used when launching your server. This "
"requires you have filled out the initial 6 fields!", required=False,
widget=forms.Select(attrs={"class": textbox_size, 'disabled': 'disabled'}))
bucket_default = forms.CharField(
required=False,
label="Default bucket",
Expand Down
4 changes: 3 additions & 1 deletion biocloudcentral/tasks.py
Expand Up @@ -170,6 +170,7 @@ def run_instance(form):
instance_type = (form['custom_instance_type'] if form['custom_instance_type']
else form['instance_type'])
ebs_optimized = True if form.get('ebs_optimized', None) == 'on' else False
subnet_id = form.get('subnet_id') if form.get('subnet_id') else None
# Create cloudman connection with provided creds
cml = CloudManLauncher(form["access_key"], form["secret_key"], form['cloud'])
form["freenxpass"] = form["password"]
Expand All @@ -183,7 +184,7 @@ def run_instance(form):
for key in form.iterkeys():
if key in ['cluster_name', 'image_id', 'instance_type', 'password',
'placement', 'access_key', 'secret_key', 'cloud', 'key_pair',
'ebs_optimized']:
'ebs_optimized', 'subnet_id']:
del kwargs[key]

response = {}
Expand All @@ -200,6 +201,7 @@ def run_instance(form):
ramdisk_id=ramdisk_id,
placement=form['placement'],
ebs_optimized=ebs_optimized,
subnet_id=subnet_id,
**kwargs)
# Keep these parts of the form as part of the response
response['cluster_name'] = form['cluster_name']
Expand Down
1 change: 1 addition & 0 deletions biocloudcentral/urls.py
Expand Up @@ -19,6 +19,7 @@
url(r'^get-flavors$', 'biocloudcentral.views.get_flavors', name='get_flavors'),
url(r'^get-placements$', 'biocloudcentral.views.get_placements', name='get_placements'),
url(r'^get-key-pairs$', 'biocloudcentral.views.get_key_pairs', name='get_key_pairs'),
url(r'^get-subnets$', 'biocloudcentral.views.get_subnets', name='get_subnets'),
url(r'^fetch-clusters$', 'biocloudcentral.views.fetch_clusters', name='fetch_clusters'),
url(r'^update-clusters$', 'biocloudcentral.views.update_clusters',
name='update_clusters'),
Expand Down
31 changes: 31 additions & 0 deletions biocloudcentral/views.py
Expand Up @@ -386,6 +386,37 @@ def get_key_pairs(request):
return HttpResponse(simplejson.dumps(response), mimetype="application/json")


def get_subnets(request):
"""
Fetch all subnets available under the supplied account.
"""
response = {}
if request.is_ajax():
if request.method == 'POST':
cloud_id = request.POST.get('cloud_id', '')
a_key = request.POST.get('a_key', '')
s_key = request.POST.get('s_key', '')
subnets = []
if a_key != '' and s_key != '':
# Needed to get the cloud connection
cloud = models.Cloud.objects.get(pk=cloud_id)
cml = CloudManLauncher(a_key, s_key, cloud)
snl = cml.vpc_conn.get_all_subnets()
for sn in snl:
subnets.append({'id': sn.id, 'name': sn.tags.get('Name'),
'vpc_id': sn.vpc_id})
response = {'subnets': subnets}
else:
response = {"error": "Not a POST request", "subnets": []}
log.error("Not a POST request")
else:
response = {"error": "Not an AJAX request", "subnets": []}
log.error("No XHR")
if not response:
response = {"error": "Please specify access and secret keys", "subnets": []}
return HttpResponse(simplejson.dumps(response), mimetype="application/json")


def fetch_clusters(request):
"""
Intiate retrieval of a list of clusters associated with a given account on
Expand Down
48 changes: 48 additions & 0 deletions templates/launch.html
Expand Up @@ -180,6 +180,44 @@
});
}

function get_subnets(){
var jqxhr = $.ajax({
url: "{% url get_subnets %}",
type: "post",
data: {'cloud_id': $('#id_cloud').val(),
'a_key': $('#id_access_key').val(),
's_key': $('#id_secret_key').val(),
csrfmiddlewaretoken: csrf_token},
beforeSend: function(){
disable_fields(false);
$(this).attr('value', "Fetching data... please wait");
},
success: function(data) {
enable_fields();
if (data.subnets.length > 0){
var subnets = '';
for (var i=0; i<data.subnets.length; i++) {
subnets += '<option value="' + data.subnets[i].id + '">'
+ data.subnets[i].id + " | " + data.subnets[i].name
+ " (" + data.subnets[i].vpc_id + ")</option>";
}
$('#id_subnet_id').html(subnets);
$('#id_subnet_id').removeAttr('disabled');
} else if (data.error) {
$('#id_subnet_id').attr('disabled', 'disabled');
$('#id_subnet_id').html("<option>" + data.error + "</option>");
} else {
$('#id_subnet_id').attr('disabled', 'disabled');
$('#id_subnet_id').html("<option>Click refresh to update</option>");
}
},
error: function(data){
$('#id_subnet_id').attr('disabled', 'disabled');
$('#id_subnet_id').html("<option>" + data.error + "</option>");
}
});
}

function disable_fields(all){
// Disable inptu fields. Parameter ``all`` (default value is ``true``)
// indicates that all the input fields should be disabled. If the provided
Expand Down Expand Up @@ -462,6 +500,13 @@
$('#get_gey_pair_btn').tooltip({"placement": "right",
'title': 'Fetch available key pairs'});

$('#get_subnets_btn').click(function(event){
event.preventDefault();
get_subnets(true);
});
$('#get_subnets_btn').tooltip({"placement": "right",
'title': 'Fetch available subnets.'});

$('#abort_clusters_fetch').tooltip({"placement": "right",
'title': 'Abort cluster fetch request'});

Expand Down Expand Up @@ -609,6 +654,9 @@ <h2><small>
{% if field.label == 'Key pair' %}
<i id="get_gey_pair_btn" class="icon-location-arrow action-icons"></i>
{% endif %}
{% if field.label == 'Subnet IDs' %}
<i id="get_subnets_btn" class="icon-location-arrow action-icons"></i>
{% endif %}
</div>
{% include "bootstrap_toolkit/field_errors.html" with display="inline" %}
{% autoescape off %}
Expand Down

0 comments on commit 8d537a8

Please sign in to comment.