Class: Knifecosmic::CosmicFirewallruleCreate
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Knifecosmic::CosmicFirewallruleCreate
- Includes:
- Chef::Knife::KnifecosmicBase
- Defined in:
- lib/chef/knife/cosmic_firewallrule_create.rb
Instance Method Summary collapse
- #create_port_forwarding_rule(ip_address, server_id, rule, connection, other_params) ⇒ Object
- #run ⇒ Object
Methods included from Chef::Knife::KnifecosmicBase
Instance Method Details
#create_port_forwarding_rule(ip_address, server_id, rule, connection, other_params) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/chef/knife/cosmic_firewallrule_create.rb', line 86 def create_port_forwarding_rule(ip_address, server_id, rule, connection, other_params) args = rule.split(':') startport = args[0] endport = args[1] || args[0] protocol = args[2] || "TCP" cidrlist = args[3] || "0.0.0.0/0" # Required parameters params = { 'ipaddressId' => ip_address['id'], 'protocol' => protocol } if config[:public_ip].nil? params['command'] = 'createFirewallRule' else params['command'] = 'createNetworkACL' # Random rule number; will be temp and hopefully wont hit collision params['number'] = (0...4).map { [rand(10)] }.join params['action'] = 'Allow' params['traffictype'] = 'Ingress' # To keep the def backwards compatible.. @hostname server = connection.get_server(@hostname) server_nic_default = connection.get_server_default_nic(server) networkid = server_nic_default['networkid'] params['aclid'] = connection.get_network(networkid)['aclid'] end # Optional parameters opt_params = { 'startport' => startport, 'endport' => endport, 'cidrlist' => cidrlist } params.merge!(opt_params) Chef::Log.debug("Creating Firewall Rule for #{ip_address['ipaddress']} with protocol: #{protocol}, start: #{startport} end: #{endport} cidr: #{cidrlist}") if locate_config_value(:syncrequest) result = connection.send_request(params) Chef::Log.debug("JobResult: #{result}") else result = connection.send_async_request(params) Chef::Log.debug("AsyncJobResult: #{result}") end end |
#run ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/knife/cosmic_firewallrule_create.rb', line 42 def run @hostname = @name_args.shift unless /^[a-zA-Z0-9][a-zA-Z0-9-]*$/.match @hostname then ui.error "Invalid hostname. Please specify a short hostname, not an fqdn (e.g. 'myhost' instead of 'myhost.domain.com')." exit 1 end params = {} locate_config_value(:openfirewall) ? params['openfirewall'] = 'true' : params['openfirewall'] = 'false' # Lookup all server objects. params_for_list_object = { 'command' => 'listVirtualMachines' } connection_result = connection.list_object(params_for_list_object, "virtualmachine") # Lookup the hostname in the connection result server = {} connection_result.map { |n| server = n if n['name'].upcase == @hostname.upcase } if server['name'].nil? ui.error "Cannot find hostname: #{@hostname}." exit 1 end # Lookup the public ip address of the server if config[:public_ip].nil? server_public_address = connection.get_server_public_ip(server) ip_address = connection.get_public_ip_address(server_public_address) else ip_address = connection.get_public_ip_address(config[:public_ip]) end if ip_address.nil? || ip_address['id'].nil? ui.error "Cannot find public ip address for hostname: #{@hostname}." exit 1 end @name_args.each do |rule| create_port_forwarding_rule(ip_address, server['id'], rule, connection, params) end end |