diff --git a/biocloudcentral/forms.py b/biocloudcentral/forms.py index a95d7515..af277504 100644 --- a/biocloudcentral/forms.py +++ b/biocloudcentral/forms.py @@ -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", diff --git a/biocloudcentral/tasks.py b/biocloudcentral/tasks.py index 2350f774..9f91d65a 100644 --- a/biocloudcentral/tasks.py +++ b/biocloudcentral/tasks.py @@ -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"] @@ -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 = {} @@ -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'] diff --git a/biocloudcentral/urls.py b/biocloudcentral/urls.py index c6809c10..8ac67fe3 100644 --- a/biocloudcentral/urls.py +++ b/biocloudcentral/urls.py @@ -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'), diff --git a/biocloudcentral/views.py b/biocloudcentral/views.py index 8394c356..7bfeaf4c 100644 --- a/biocloudcentral/views.py +++ b/biocloudcentral/views.py @@ -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 diff --git a/templates/launch.html b/templates/launch.html index 86e788e8..54da160e 100644 --- a/templates/launch.html +++ b/templates/launch.html @@ -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[i].id + " | " + data.subnets[i].name + + " (" + data.subnets[i].vpc_id + ")"; + } + $('#id_subnet_id').html(subnets); + $('#id_subnet_id').removeAttr('disabled'); + } else if (data.error) { + $('#id_subnet_id').attr('disabled', 'disabled'); + $('#id_subnet_id').html(""); + } else { + $('#id_subnet_id').attr('disabled', 'disabled'); + $('#id_subnet_id').html(""); + } + }, + error: function(data){ + $('#id_subnet_id').attr('disabled', 'disabled'); + $('#id_subnet_id').html(""); + } + }); + } + 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 @@ -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'}); @@ -609,6 +654,9 @@

{% if field.label == 'Key pair' %} {% endif %} + {% if field.label == 'Subnet IDs' %} + + {% endif %} {% include "bootstrap_toolkit/field_errors.html" with display="inline" %} {% autoescape off %}