Class: Morpheus::Cli::Groups

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/groups.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initializeGroups

Returns a new instance of Groups.



11
12
13
14
15
16
17
# File 'lib/morpheus/cli/groups.rb', line 11

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
	@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
	@groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
	@active_groups = ::Morpheus::Cli::Groups.load_group_file

end

Class Method Details

.active_groupObject

Provides the current active group information



159
160
161
162
163
164
165
# File 'lib/morpheus/cli/groups.rb', line 159

def self.active_group
	appliance_name, appliance_url = Morpheus::Cli::Remote.active_appliance
	if !defined?(@@groups)
		@@groups = load_group_file
	end
	return @@groups[appliance_name.to_sym]
end

.groups_file_pathObject



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

def self.groups_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,"groups")
end

.load_group_fileObject



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

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

.save_groups(group_map) ⇒ Object



187
188
189
# File 'lib/morpheus/cli/groups.rb', line 187

def self.save_groups(group_map)
	File.open(groups_file_path, 'w') {|f| f.write group_map.to_yaml } #Store
end

Instance Method Details

#add(args) ⇒ Object



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

def add(args)
	options = {}
	params = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus groups add [name] [--location]"
		opts.on( '-l', '--location LOCATION', "Location" ) do |desc|
			params[:location] = desc
		end
		build_common_options(opts, options, [])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	group = {name: args[0], location: params[:location]}
	begin
		@groups_interface.create(group)
		list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#handle(args) ⇒ Object



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

def handle(args) 
	if @access_token.empty?
		print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
		return 1
	end
	if args.empty?
		puts "\nUsage: morpheus groups [list,add,remove] [name]\n\n"
		return
	end

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

#list(args) ⇒ Object



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

def list(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus groups list"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	begin
		json_response = @groups_interface.get()
		if options[:json]
			print JSON.pretty_generate(json_response)
			return
		end
		groups = json_response['groups']
		print "\n" ,cyan, bold, "Morpheus Groups\n","==================", reset, "\n\n"
		if groups.empty?
			puts yellow,"No groups currently configured.",reset
		else
			groups.each do |group|
				if @active_groups[@appliance_name.to_sym] == group['id']
					print cyan, bold, "=> #{group['name']} - #{group['location']}",reset,"\n"
				else
					print cyan, "=  #{group['name']} - #{group['location']}\n",reset
				end
			end
		end
		print reset,"\n\n"
		
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#remove(args) ⇒ Object



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

def remove(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus groups remove [name]"
		build_common_options(opts, options, [:auto_confirm])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end

	begin
		group_results = @groups_interface.get(args[0])
		if group_results['groups'].empty?
			print_red_alert "Group not found by name #{args[0]}"
			exit 1
		end
		unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the group #{group_results['groups'][0]['name']}?")
			exit
		end
		@groups_interface.destroy(group_results['groups'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#use(args) ⇒ Object



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
156
# File 'lib/morpheus/cli/groups.rb', line 131

def use(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus groups use [name]"
		build_common_options(opts, options, [])
	end
	optparse.parse(args)
	if args.length < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	begin
		json_response = @groups_interface.get(args[0])
		groups = json_response['groups']
		if groups.length > 0
			@active_groups[@appliance_name.to_sym] = groups[0]['id']
			::Morpheus::Cli::Groups.save_groups(@active_groups)
			list([])
		else
			print_red_alert "Group not found by name #{args[0]}"
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end