Class: Morpheus::Cli::SecurityGroups

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initializeSecurityGroups

Returns a new instance of SecurityGroups.



13
14
15
16
17
18
# File 'lib/morpheus/cli/security_groups.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_groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).security_groups
	@active_security_group = ::Morpheus::Cli::SecurityGroups.load_security_group_file
end

Class Method Details

.load_security_group_fileObject



170
171
172
173
174
175
176
177
# File 'lib/morpheus/cli/security_groups.rb', line 170

def self.load_security_group_file
	remote_file = security_group_file_path
	if File.exist? remote_file
		return YAML.load_file(remote_file)
	else
		{}
	end
end

.save_security_group(security_group_map) ⇒ Object



188
189
190
# File 'lib/morpheus/cli/security_groups.rb', line 188

def self.save_security_group(security_group_map)
	File.open(security_group_file_path, 'w') {|f| f.write security_group_map.to_yaml } #Store
end

.security_group_file_pathObject



179
180
181
182
183
184
185
186
# File 'lib/morpheus/cli/security_groups.rb', line 179

def self.security_group_file_path
	home_dir = Dir.home
	morpheus_dir = File.join(home_dir,".morpheus")
	if !Dir.exist?(morpheus_dir)
		Dir.mkdir(morpheus_dir)
	end
	return File.join(morpheus_dir,"securitygroup")
end

Instance Method Details

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/security_groups.rb', line 90

def add(args)
	if args.count < 1
		puts "\nUsage: morpheus security-groups add NAME [options]\n\n"
		return
	end

	options = {:securityGroup => {:name => args[0]} }
	optparse = OptionParser.new do|opts|
		opts.banner = "\nUsage: morpheus security-groups add NAME [options]"
		opts.on( '-d', '--description Description', "Description of the security group" ) do |description|
			options[:securityGroup][:description] = description
		end

		opts.on( '-h', '--help', "Prints this help" ) do
			puts opts
			exit
		end
	end
	optparse.parse(args)

	begin
		@security_groups_interface.create(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

#get(args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/morpheus/cli/security_groups.rb', line 67

def get(args)
	if args.count < 1
		puts "\nUsage: morpheus security-groups get ID\n\n"
		return
	end

	begin
		json_response = @security_groups_interface.get({id: args[0]})
		security_group = json_response['securityGroup']
		print "\n" ,cyan, bold, "Morpheus Security Group\n","==================", reset, "\n\n"
		if security_group.nil?
			puts yellow,"Security Group not found by id #{args[0]}",reset
		else
			print cyan, "=  #{security_group['id']}: #{security_group['name']} (#{security_group['description']})\n"
		end
		print reset,"\n\n"
		
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		return nil
	end
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
44
45
# File 'lib/morpheus/cli/security_groups.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-groups [list,get,add,remove,use] [name]\n\n"
		return 
	end

	case args[0]
		when 'list'
			list(args[1..-1])
		when 'get'	
			get(args[1..-1])
		when 'add'
			add(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		when 'use'
			use(args[1..-1])
		else
			puts "\nUsage: morpheus security-groups [list,get,add,remove,use] [name]\n\n"
	end
end

#list(args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/morpheus/cli/security_groups.rb', line 47

def list(args)
	begin
		json_response = @security_groups_interface.list()
		security_groups = json_response['securityGroups']
		print "\n" ,cyan, bold, "Morpheus Security Groups\n","==================", reset, "\n\n"
		if security_groups.empty?
			puts yellow,"No Security Groups currently configured.",reset
		else
			security_groups.each do |security_group|
				print cyan, "=  #{security_group['id']}: #{security_group['name']} (#{security_group['description']})\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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/morpheus/cli/security_groups.rb', line 124

def remove(args)
	if args.count < 1
		puts "\nUsage: morpheus security-groups remove ID\n\n"
		return
	end
	begin
		json_response = @security_groups_interface.get({id: args[0]})
		security_group = json_response['securityGroup']
		if security_group.nil?
			puts "Security Group not found by id #{args[0]}"
			return
		end
		@security_groups_interface.delete(security_group['id'])
		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

#use(args) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/morpheus/cli/security_groups.rb', line 149

def use(args) 
	if args.length < 1
		puts "Usage: morpheus security-groups use ID"
		return
	end
	begin
		json_response = @security_groups_interface.get({id: args[0]})
		security_group = json_response['securityGroup']
		if !security_group.nil?
			@active_security_group[@appliance_name.to_sym] = security_group['id']
			::Morpheus::Cli::SecurityGroups.save_security_group(@active_security_group)
			puts "Using Security Group #{args[0]}"
		else
			puts "Security Group not found"
		end
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		return nil
	end
end