Class: Morpheus::Cli::SecurityGroupRules

Inherits:
Object
  • Object
show all
Includes:
CliCommand, Term::ANSIColor
Defined in:
lib/morpheus/cli/security_group_rules.rb

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initialize ⇒ SecurityGroupRules

Returns a new instance of SecurityGroupRules.



13
14
15
16
17
18
# File 'lib/morpheus/cli/security_group_rules.rb', line 13

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
	@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
	@security_group_rules_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).security_group_rules
	@active_security_group = ::Morpheus::Cli::SecurityGroups.load_security_group_file
end

Instance Method Details

#add_custom_rule(args) ⇒ Object



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/morpheus/cli/security_group_rules.rb', line 45

def add_custom_rule(args)
	usage = <<-EOT
Usage: morpheus security-group-rules add_custom_rule SOURCE_CIDR PORT_RANGE PROTOCOL [options]
 SOURCE_CIDR: CIDR to white-list
 PORT_RANGE: Port value (i.e. 123) or port range (i.e. 1-65535)
 PROTOCOL: tcp, udp, icmp\n\n'
EOT

	if args.count < 3
		puts usage
		return
	end
	
	security_group_id = nil 
	optparse = OptionParser.new do|opts|
		opts.banner = "\nUsage: morpheus security-group-rules add_custom_rule SOURCE_CIDR PORT_RANGE PROTOCOL [options]"
		opts.on( '-s', '--secgroup SECGROUP', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id|
			security_group_id = id
		end
		opts.on( '-h', '--help', "Prints this help" ) do
			puts opts
			exit
		end
	end
	optparse.parse(args)

	if security_group_id.nil?
		security_group_id = @active_security_group[@appliance_name.to_sym]
	end

	if security_group_id.nil?
		puts "Security Group ID must be specified with options or set using 'security-groups use id'"
		exit
	end

	options = {
		:rule => {
			:source => args[0],
			:portRange => args[1],
			:protocol => args[2],
			:customRule => true
		}
	}

	begin
		@security_group_rules_interface.create(security_group_id, options)
	rescue => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
	list([])
end

#add_instance_rule(args) ⇒ Object



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/morpheus/cli/security_group_rules.rb', line 103

def add_instance_rule(args)
	usage = <<-EOT
Usage: morpheus security-group-rules add_instance_rule SOURCE_CIDR INSTANCE_TYPE_ID [options]
 SOURCE_CIDR: CIDR to white-list
 INSTANCE_TYPE_ID: ID of the Instance Type to access
EOT
	if args.count < 2
		puts usage
		return
	end
	
	security_group_id = nil 
	optparse = OptionParser.new do|opts|
		opts.banner = "\nmorpheus security-group-rules add_instance_rule SOURCE_CIDR INSTANCE_TYPE_ID [options]"
		opts.on( '-s', '--secgroup secgroup', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id|
			security_group_id = id
		end
		opts.on( '-h', '--help', "Prints this help" ) do
			puts opts
			exit
		end
	end
	optparse.parse(args)

	if security_group_id.nil?
		security_group_id = @active_security_group[@appliance_name.to_sym]
	end

	if security_group_id.nil?
		puts "Security Group ID must be specified with options or set using 'security-groups use id'"
		exit
	end

	options = {
		:rule => {
			:source => args[0],
			:instanceTypeId => args[1]
		}
	}

	begin
		@security_group_rules_interface.create(security_group_id, options)
	rescue => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
	list([])
end

#handle(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/morpheus/cli/security_group_rules.rb', line 21

def handle(args) 
	if @access_token.empty?
		print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
		return 1
	end
	if args.empty?
		puts "\nUsage: morpheus security-group-rules [list, add_custom_rule, remove]\n\n"
		return 
	end

	case args[0]
		when 'list'
			list(args[1..-1])
		when 'add_custom_rule'
			add_custom_rule(args[1..-1])
		when 'add_instance_rule'
			add_instance_rule(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		else
			puts "\nUsage: morpheus security-group-rules [list,add_custom_rule,remove]\n\n"
	end
end

#list(args) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/morpheus/cli/security_group_rules.rb', line 157

def list(args)
	options = {}
	security_group_id = nil 
	optparse = OptionParser.new do|opts|
		opts.banner = "\nUsage: morpheus security-group-rules list [ID]"
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	security_group_id = args[0].to_i
	optparse.parse(args)

	if security_group_id.nil?
		security_group_id = @active_security_group[@appliance_name.to_sym]
	end

	if security_group_id.nil?
		puts "Security Group ID must be specified with options or set using 'security-groups use id'"
		exit
	end

	begin
		params = {}
		json_response = @security_group_rules_interface.get(security_group_id, options)
		rules = json_response['rules']
		print "\n" ,cyan, bold, "Morpheus Security Group Rules for Security Group ID:#{security_group_id}\n","==================", reset, "\n\n"
		if rules.empty?
			puts yellow,"No Security Group Rules currently configured.",reset
		else
			rules.each do |rule|
				print cyan, "=  #{rule['id']} - (CIDR:#{rule['source']}, Port Range:#{rule['portRange']}, Protocol:#{rule['protocol']}, Custom Rule:#{rule['customRule']}, Instance Type:#{rule['instanceTypeId']})\n"
			end
		end
		print reset,"\n\n"
		
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		return nil
	end
end

#remove(args) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/morpheus/cli/security_group_rules.rb', line 196

def remove(args)
	if args.count < 1
		puts "\nUsage: morpheus security-group-rules remove ID [options]\n\n"
		return
	end

	security_group_id = nil 
	optparse = OptionParser.new do|opts|
		opts.banner = "\nUsage: morpheus security-group-rules remove ID [options]"
		opts.on( '-s', '--secgroup secgroup', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id|
			security_group_id = id
		end
		opts.on( '-h', '--help', "Prints this help" ) do
			puts opts
			exit
		end
	end
	optparse.parse(args)

	if security_group_id.nil?
		security_group_id = @active_security_group[@appliance_name.to_sym]
	end

	if security_group_id.nil?
		puts "Security Group ID must be specified with options or set using 'security-groups use id'"
		exit
	end

	begin
		@security_group_rules_interface.delete(security_group_id, args[0])
		list([])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end