Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Filtering of templates, listing of security group rules

  • Loading branch information...
commit 64d2c3a70802d39cce621c49fb72c065c8162634 1 parent 905c0f8
@fifthecho authored
View
116 lib/chef/knife/cloudstack_securitygroup_list.rb
@@ -25,53 +25,91 @@ class CloudstackSecuritygroupList < Knife
include Knife::CloudstackBase
banner "knife cloudstack securitygroup list (options)"
- option :rules,
- :short => "-D GroupID",
- :long => "--rules GroupID",
- :description => "List the rules contained within a Security Group"
+ option :groupid,
+ :short => "-g GROUPID",
+ :long => "--groupid GROUPID",
+ :description => "List the rules contained within a Security Group, specified by ID",
+ :default => 'none'
+ option :groupname,
+ :short => "-G GROUPNAME",
+ :long => "--groupname GROUPNAME",
+ :description => "List the rules contained within a Security Group, specified by name.",
+ :default => 'none'
+
+ def sg_details_list(securitygroup_list, groups, options={})
+ temp = groups
+ if groupid = options[:groupid]
+ temp.reject!{|g| g['id'] != groupid.to_i}
+ end
+ if groupname = options[:groupname]
+ temp.reject!{|g| g['name'] != groupname}
+ end
+
+ temp.each do |securitygroup|
+ if securitygroup['ingressrule'].nil?
+ securitygroup_list << ' '
+ else
+ securitygroup['ingressrule'].each do |ingressrule|
+ rule_details = []
+ securitygroup_list << ingressrule['protocol'].to_s
+ securitygroup_list << ingressrule['startport'].to_s
+ securitygroup_list << ingressrule['endport'].to_s
+ if ingressrule['cidr'].nil?
+ rule_details << ingressrule['securitygroupname'].to_s
+ rule_details << ingressrule['account'].to_s
+ else
+ rule_details << ingressrule['cidr'].to_s
+ end
+ securitygroup_list << rule_details.join(", ")
+ end
+ end
+ end
+ end
def run
$stdout.sync = true
validate!
+ groupid = locate_config_value(:groupid)
+ groupname = locate_config_value(:groupname)
- securitygroup_list = [
- ui.color('ID', :bold),
- ui.color('Name', :bold),
- ui.color('Description', :bold)
-# ui.color('Rules', :bold)
- ]
- response = connection.list_security_groups['listsecuritygroupsresponse']
- if securitygroups = response['securitygroup']
- securitygroups.each do |securitygroup|
- securitygroup_list << securitygroup['id'].to_s
- securitygroup_list << securitygroup['name'].to_s
- securitygroup_list << securitygroup['description'].to_s
-# rule_list = []
-# if securitygroup['ingressrule'].nil?
-# rule_list << ' '
-# else
-# securitygroup['ingressrule'].each do |ingressrule|
-# rule_details = []
-# rule_details << ingressrule['protocol'].to_s
-# rule_details << ingressrule['startport'].to_s
-# rule_details << ingressrule['endport'].to_s
-# if ingressrule['cidr'].nil?
-# rule_details << ingressrule['securitygroupname'].to_s
-# rule_details << ingressrule['account'].to_s
-# else
-# rule_details << ingressrule['cidr'].to_s
-# end
-# rule_list << rule_details.join(", ")
-# end
-# end
-# securitygroup_list << rule_list.join("\n\t")
+ if (groupid == 'none' and groupname == 'none')
+ securitygroup_list = [
+ ui.color('ID', :bold),
+ ui.color('Name', :bold),
+ ui.color('Description', :bold)
+ ]
+ response = connection.list_security_groups['listsecuritygroupsresponse']
+ if securitygroups = response['securitygroup']
+ securitygroups.each do |securitygroup|
+ securitygroup_list << securitygroup['id'].to_s
+ securitygroup_list << securitygroup['name'].to_s
+ securitygroup_list << securitygroup['description'].to_s
+ end
end
- end
- puts ui.list(securitygroup_list, :columns_across, 3)
-
+ puts ui.list(securitygroup_list, :columns_across, 3)
+ else
+ securitygroup_list = [
+ ui.color('Protocol', :bold),
+ ui.color('Start Port', :bold),
+ ui.color('End Port', :bold),
+ ui.color('Restricted To', :bold)
+ ]
+
+ if response = connection.list_security_groups['listsecuritygroupsresponse']
+ if securitygroups = response['securitygroup']
+ filters = {}
+ filters[:groupid] = groupid unless groupid == 'none'
+ filters[:groupname] = groupname unless groupname == 'none'
+ sg_details_list(securitygroup_list, securitygroups, filters)
+
+ puts ui.list(securitygroup_list, :columns_across, 4)
+ end
+ end
+
+ end
end
-
+
end
end
end
View
8 lib/chef/knife/cloudstack_server_create.rb
@@ -74,7 +74,7 @@ class CloudstackServerCreate < Knife
:long => "--bootstrap-version VERSION",
:description => "The version of Chef to install",
:proc => Proc.new { |v| Chef::Config[:knife][:bootstrap_version] = v },
- :default => "0.10.4"
+ :default => "0.10.4"
option :distro,
:short => "-d DISTRO",
@@ -137,8 +137,8 @@ def run
:image_id => locate_config_value(:image),
:flavor_id => locate_config_value(:flavor),
:zoneid => Chef::Config[:knife][:availability_zone],
- :displayname => Chef::Config[:knife][:chef_node_name],
- :name => Chef::Config[:knife][:chef_node_name]
+ :displayname => Chef::Config[:knife][:chef_node_name],
+ :name => Chef::Config[:knife][:chef_node_name]
}
ipaddress_def = {
:zoneid => Chef::Config[:knife][:availability_zone]
@@ -188,7 +188,7 @@ def bootstrap_for_node(server)
bootstrap.name_args = [server.ipaddress.to_s]
bootstrap.config[:run_list] = config[:run_list]
bootstrap.config[:ssh_user] = 'root'
- bootstrap.config[:ssh_password] = server.password.to_s || 'Password01'
+ bootstrap.config[:ssh_password] = server.password.to_s || 'Password01'
bootstrap.config[:identity_file] = config[:identity_file]
bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id.to_s
bootstrap.config[:prerelease] = config[:prerelease]
View
85 lib/chef/knife/cloudstack_template_list.rb
@@ -31,11 +31,60 @@ class CloudstackTemplateList < Knife
option :filter,
:short => "-L FILTER",
:long => "--filter FILTER",
- :description => "The template search filter. Default is 'featured'",
+ :description => "The template search filter. Default is 'featured.' Other options are 'self,' 'self-executable,' 'executable,' and 'community.'",
:default => "featured"
+ option :zone,
+ :short => "-Z ZONE",
+ :long => "--zone ZONE",
+ :description => "Limit responses to templates only located in a specific zone. Default provides templates from all zones.",
+ :default => "all"
+ option :hypervisor,
+ :short => "-H HYPERVISOR",
+ :long => "--hypervisor HYPERVISOR",
+ :description => "Limit responses to templates only running on a specific hypervisor. Default provides templates from all hypervisors.",
+ :default => "all"
+ option :zoneid,
+ :short => "-z ZONEID",
+ :long => "--zoneid ZONEID",
+ :description => "Limit responses to templates only running in a specific zone (specified by ID #). Default provides templates from all zones.",
+ :default => "all"
+ option :templateid,
+ :short => "-T TEMPLATEID",
+ :long => "--templateid TEMPLATEID",
+ :description => "Limit responses to a single template ID. Default provides all templates.",
+ :default => "all"
+
+
+ def print_templates(template_list,templates,options={})
+ temp = templates
- def run
+ if templateid = options[:templateid]
+ temp.reject!{|t| t['id'] != templateid.to_i}
+ end
+ if zoneid = options[:zoneid]
+ temp.reject!{|t| t['zoneid'] != zoneid.to_i}
+ end
+ if zone = options[:zone]
+ temp.reject!{|t| t['zonename'] != zone}
+ end
+ if hypervisor = options[:hypervisor]
+ temp.reject!{|t| t['hypervisor'] != hypervisor}
+ end
+
+ temp.each do |template|
+ template_list << template['id'].to_s
+ template_list << template['hypervisor']
+
+ template_size = template['size']
+ template_size = (template_size/1024/1024/1024)
+ template_list << template_size.to_s
+ template_list << template['zonename']
+ template_list << template['name']
+ end
+ end
+
+ def run
validate!
template_list = [
@@ -46,26 +95,26 @@ def run
ui.color('Name', :bold)
]
- filter = config['filter']
- settings = connection.list_templates('templatefilter' => 'featured')
+ filter = locate_config_value(:filter)
+ zone = locate_config_value(:zone)
+ zoneid = locate_config_value(:zoneid)
+ hypervisor = locate_config_value(:hypervisor)
+ templateid = locate_config_value(:templateid)
+
+ settings = connection.list_templates('templatefilter' => filter)
if response = settings['listtemplatesresponse']
- response.each do |templates|
- if templates = response['template']
- templates.each do |template|
- template_list << template['id'].to_s
- template_list << template['hypervisor']
-
- template_size = template['size']
- template_size = (template_size/1024/1024/1024)
- template_list << template_size.to_s
-
- template_list << template['zonename']
- template_list << template['name']
- end
- end
+ if templates = response['template']
+ filters = {}
+ filters[:hypervisor] = hypervisor unless hypervisor == 'all'
+ filters[:zone] = zone unless zone == 'all'
+ filters[:zoneid] = zoneid unless zoneid == 'all'
+ filters[:templateid] = templateid unless templateid == 'all'
+
+ print_templates(template_list,templates,filters)
end
puts ui.list(template_list, :columns_across, 5)
end
+
end
end
end

0 comments on commit 64d2c3a

Please sign in to comment.
Something went wrong with that request. Please try again.