Add instance filters and group by options for EC2 inventory sources. Implements https://trello.com/c/QOVhP0mH

This commit is contained in:
Chris Church
2014-11-26 03:20:37 -05:00
parent 9756487edf
commit 35c392ab17
10 changed files with 869 additions and 115 deletions

View File

@@ -71,7 +71,16 @@ angular.module('SourceFormDefinition', [])
editRequired: false,
dataTitle: 'Instance Filters',
dataPlacement: 'right',
awPopOver: "<p>Open the <a href=http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html target='_blank'>documentation</a> for a complete list of filter options.</p>",
awPopOver: "<p>Provide a comma-separated list of filter expressions. " +
"Hosts are imported to Tower when <em>ANY</em> of the filters match.</p>" +
"Limit to hosts having a tag:<br />\n" +
"<blockquote>tag-key=TowerManaged</blockquote>\n" +
"Limit to hosts using either key pair:<br />\n" +
"<blockquote>key-name=staging, key-name=production</blockquote>\n" +
"Limit to hosts where the Name tag begins with <em>test</em>:<br />\n" +
"<blockquote>tag:Name=test*</blockquote>\n" +
"<p>View the <a href=\"http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html\" target=\"_blank\">DescribeInstances documentation</a> " +
"for a complete list of supported filters.</p>",
dataContainer: 'body'
},
group_by: {
@@ -81,23 +90,23 @@ angular.module('SourceFormDefinition', [])
addRequired: false,
editRequired: false,
awMultiselect: 'group_by_choices',
dataTitle: 'Group By',
dataTitle: 'Only Group By',
dataPlacement: 'right',
awPopOver: "<p>FIXME: Create these automatic groups by default. give examples</p>",
awPopOver: "<p>Select which groups to create automatically. " +
"Tower will create group names similar to the following examples based on the options selected:</p><ul>" +
"<li>Availability Zone: <strong>zones &raquo; us-east-1b</strong></li>" +
"<li>Image ID: <strong>images &raquo; ami-b007ab1e</strong></li>" +
"<li>Instance ID: <strong>instances &raquo; i-ca11ab1e</strong></li>" +
"<li>Instance Type: <strong>types &raquo; type_m1_medium</strong></li>" +
"<li>Key Name: <strong>keys &raquo; key_testing</strong></li>" +
"<li>Region: <strong>regions &raquo; us-east-1</strong></li>" +
"<li>Security Group: <strong>security_groups &raquo; security_group_default</strong></li>" +
"<li>Tags: <strong>tags &raquo; tag_Name &raquo; tag_Name_host1</strong></li>" +
"<li>VPC ID: <strong>vpcs &raquo; vpc-5ca1ab1e</strong></li>" +
"</ul><p>If blank, all groups above are created except <em>Instance ID</em>.</p>",
dataContainer: 'body'
},
// group_tag_filters: {
// label: 'Tag Filters',
// type: 'text',
// ngShow: "source && source.value == 'ec2' && group_by.value.indexOf('tag_keys') >= 0", // FIXME: Not sure what's needed to make the last expression work.
// addRequired: false,
// editRequired: false,
// dataTitle: 'Tag Filters',
// dataPlacement: 'right',
// awPopOver: "<p>FIXME: When grouping by tags, specify which tag keys become groups.</p>",
// dataContainer: 'body'
// },
source_script: {
custom_script: {
label : "Custom Inventory Scripts",
type: 'lookup',
ngShow: "source && source.value !== '' && source.value === 'custom'",

View File

@@ -238,22 +238,8 @@ angular.module('GroupsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', '
id: 'all',
text: 'All'
}]);
// FIXME: Should come from API.
scope.group_by_choices = [
{label: 'All', name: 'All', value: 'all'},
{label: 'Instance ID', name: 'Instance ID', value: 'instance_id'},
{label: 'Region', name: 'Region', value: 'region'},
{label: 'Availability Zone', name: 'Availability Zone', value: 'availability_zone'},
{label: 'AMI ID', name: 'AMI ID', value: 'ami_id'},
{label: 'Instance Type', name: 'Instance Type', value: 'instance_type'},
{label: 'Key Pair', name: 'Key Pair', value: 'key_pair'},
{label: 'Security Group', name: 'Security Group', value: 'security_group'},
{label: 'Tag Keys', name: 'Tag Keys', value: 'tag_keys'},
];
$('#s2id_source_group_by').select2('data', [{
id: 'all',
text: 'All'
}]);
scope.group_by_choices = scope.ec2_group_by;
$('#s2id_group_by').select2('data', []);
$('#source_form').addClass('squeeze');
} else if (scope.source.value === 'gce') {
scope.source_region_choices = scope.gce_regions;
@@ -860,10 +846,13 @@ function($compile, SchedulerInit, Rest, Wait, SetSchedulesInnerDialogSize, Sched
setTimeout(function(){ textareaResize('group_variables'); }, 300);
}
else if ($(e.target).text() === 'Source') {
if (sources_scope.source && (sources_scope.source.value === 'ec2' || sources_scope.source.value === 'custom')) {
if (sources_scope.source && (sources_scope.source.value === 'ec2')) {
Wait('start');
ParseTypeChange({ scope: sources_scope, variable: 'source_vars', parse_variable: SourceForm.fields.source_vars.parseTypeName,
field_id: 'source_source_vars', onReady: waitStop });
}
else if (sources_scope.source && (sources_scope.source.value === 'custom')) {
Wait('start');
ParseTypeChange({ scope: sources_scope, variable: 'extra_vars', parse_variable: SourceForm.fields.extra_vars.parseTypeName,
field_id: 'source_extra_vars', onReady: waitStop });
}
@@ -1009,6 +998,23 @@ function($compile, SchedulerInit, Rest, Wait, SetSchedulesInnerDialogSize, Sched
}];
$('#s2id_source_source_regions').select2('data', master.source_regions);
}
if (data.group_by && data.source === 'ec2') {
set = sources_scope.ec2_group_by;
opts = [];
list = data.group_by.split(',');
for (i = 0; i < list.length; i++) {
for (j = 0; j < set.length; j++) {
if (list[i] === set[j].value) {
opts.push({
id: set[j].value,
text: set[j].label
});
}
}
}
master.group_by = opts;
$('#s2id_source_group_by').select2('data', opts);
}
sources_scope.group_update_url = data.related.update;
modal_scope.$emit('groupVariablesLoaded'); // JT-- "groupVariablesLoaded" is where the schedule info is loaded, so I make a call after the sources_scope.source has been loaded
//Wait('stop');
@@ -1119,6 +1125,16 @@ function($compile, SchedulerInit, Rest, Wait, SetSchedulesInnerDialogSize, Sched
callback: 'choicesReadyGroup'
});
// Load options for group_by
GetChoices({
scope: sources_scope,
url: GetBasePath('inventory_sources'),
field: 'group_by',
variable: 'ec2_group_by',
choice_name: 'ec2_group_by_choices',
callback: 'choicesReadyGroup'
});
Wait('start');
if (parent_scope.removeAddTreeRefreshed) {
@@ -1179,6 +1195,17 @@ function($compile, SchedulerInit, Rest, Wait, SetSchedulesInnerDialogSize, Sched
data.source_regions = r.join();
if (sources_scope.source && (sources_scope.source.value === 'ec2')) {
data.instance_filters = sources_scope.instance_filters;
// Create a string out of selected list of regions
var group_by = $('#s2id_source_group_by').select2("data");
r = [];
for (i = 0; i < group_by.length; i++) {
r.push(group_by[i].id);
}
data.group_by = r.join();
}
if (sources_scope.source && (sources_scope.source.value === 'ec2' || sources_scope.source.value === 'custom')) {
// for ec2, validate variable data
data.source_vars = ToJSON(sources_scope.envParseType, sources_scope.source_vars, true);
}