Class: Morpheus::Cli::Groups

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initializeGroups

Returns a new instance of Groups.



13
14
15
16
17
18
19
# File 'lib/morpheus/cli/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()
	@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



143
144
145
146
147
148
149
# File 'lib/morpheus/cli/groups.rb', line 143

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



162
163
164
165
166
167
168
169
# File 'lib/morpheus/cli/groups.rb', line 162

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



153
154
155
156
157
158
159
160
# File 'lib/morpheus/cli/groups.rb', line 153

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



171
172
173
# File 'lib/morpheus/cli/groups.rb', line 171

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



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

def add(args)
	if args.count < 1
		puts "\nUsage: morpheus groups add [name] [--location]\n\n"
		return
	end
	params = {}
	optparse = OptionParser.new do|opts|
		opts.on( '-l', '--location LOCATION', "Location" ) do |desc|
			params[:location] = desc
		end
	end
	optparse.parse(args)

	group = {name: args[0], location: params[:location]}
	begin
		@groups_interface.create(group)
	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/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 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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/morpheus/cli/groups.rb', line 98

def list(args)
	begin
		json_response = @groups_interface.get()
		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 => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		return nil
	end
end

#remove(args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/morpheus/cli/groups.rb', line 73

def remove(args)
	if args.count < 1
		puts "\nUsage: morpheus groups remove [name]\n\n"
		return
	end

	begin
		group_results = @groups_interface.get(args[0])
		if group_results['groups'].empty?
			puts "Group not found by name #{args[0]}"
			return
		end
		@groups_interface.destroy(group_results['groups'][0]['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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/morpheus/cli/groups.rb', line 122

def use(args) 
	if args.length < 1
		puts "Usage: morpheus groups use [name]"
	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
			puts "Group not found"
		end
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		return nil
	end
end