Class: Morpheus::Cli::Roles

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

Instance Method Summary collapse

Methods included from AccountsHelper

#find_account_by_id, #find_account_by_name, #find_account_from_options, #find_role_by_id, #find_role_by_name, #find_user_by_id, #find_user_by_username, #print_accounts_table, #print_green_success, #print_red_alert, #print_roles_table, #print_users_table

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initializeRoles

Returns a new instance of Roles.



16
17
18
19
# File 'lib/morpheus/cli/roles.rb', line 16

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
	#@active_groups = ::Morpheus::Cli::Groups.load_group_file
end

Instance Method Details

#add(args) ⇒ Object



151
152
153
154
# File 'lib/morpheus/cli/roles.rb', line 151

def add(args)
	print red,bold, "\nNOT YET IMPLEMENTED!\n\n",reset
	exit 1
end

#connect(opts) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/morpheus/cli/roles.rb', line 21

def connect(opts)
	@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
	if @access_token.empty?
		print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
		exit 1
	end
	@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
	@users_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).users
	@accounts_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).accounts
	@roles_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).roles
end

#details(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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/morpheus/cli/roles.rb', line 97

def details(args)
	usage = "Usage: morpheus roles details [name] [options]"
	if args.count < 1
		puts "\n#{usage}\n\n"
		exit 1
	end
	 = nil
	name = args[0]
	options = {}
	params = {}
	optparse = OptionParser.new do|opts|
		opts.banner = usage
		Morpheus::Cli::CliCommand.accountScopeOptions(opts,options)
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	optparse.parse(args)
	connect(options)
	begin
		
		 = (options)
		 =  ? ['id'] : nil

		# todo: roles_response = @roles_interface.list(account_id, {name: name}) instead
		#       there may be response data outside of role that needs to be displayed
		role = find_role_by_name(, name)
		exit 1 if role.nil?

		if options[:json]
			print JSON.pretty_generate(role)
			print "\n"
		else
			print "\n" ,cyan, bold, "Role Details\n","==================", reset, "\n\n"
			print cyan
			puts "ID: #{role['id']}"
			puts "Name: #{role['authority']}"
			puts "Description: #{role['description']}"
			puts "Scope: #{role['scope']}"
			puts "Owner: #{role['owner'] ? role['owner']['name'] : nil}"
			puts "Date Created: #{format_local_dt(role['dateCreated'])}"
			puts "Last Updated: #{format_local_dt(role['lastUpdated'])}"
			print "\n" ,cyan, bold, "Role Instance Limits\n","==================", reset, "\n\n"
			print cyan
			puts "Max Storage (bytes): #{role['instanceLimits'] ? role['instanceLimits']['maxStorage'] : 0}"
			puts "Max Memory (bytes): #{role['instanceLimits'] ? role['instanceLimits']['maxMemory'] : 0}"
			puts "CPU Count: #{role['instanceLimits'] ? role['instanceLimits']['maxCpu'] : 0}"
			print cyan
			print reset,"\n\n"
		end
	rescue RestClient::Exception => e
		::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e)
		exit 1
	end
end

#handle(args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/morpheus/cli/roles.rb', line 33

def handle(args)
	if args.empty?
		puts "\nUsage: morpheus roles [list]\n\n"
		exit 1
	end

	case args[0]
		when 'list'
			list(args[1..-1])
		when 'details'
			details(args[1..-1])
		when 'add'
			add(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		else
			puts "\nUsage: morpheus hosts [list] \n\n"
			exit 127
	end
end

#list(args) ⇒ Object



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

def list(args)
	 = nil
	options = {}
	params = {}
	optparse = OptionParser.new do|opts|
		Morpheus::Cli::CliCommand.accountScopeOptions(opts,options)
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	optparse.parse(args)
	connect(options)
	begin
		 = nil 
		if !.nil?
			 = ()
			exit 1 if .nil?
			 = ['id']
		end
		
		[:phrase, :offset, :max, :sort, :direction].each do |k|
			params[k] = options[k] unless options[k].nil?
		end
		
		json_response = @roles_interface.list(, params)
		roles = json_response['roles']
		
		if options[:json]
			print JSON.pretty_generate(json_response)
			print "\n"
		else
			print "\n" ,cyan, bold, "Morpheus Roles\n","==================", reset, "\n\n"
			if roles.empty?
				puts yellow,"No roles found.",reset
			else
				print_roles_table(roles)
			end
			print reset,"\n\n"
		end
	rescue RestClient::Exception => e
		::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e)
		exit 1
	end
end

#remove(args) ⇒ Object



156
157
158
159
# File 'lib/morpheus/cli/roles.rb', line 156

def remove(args)
	print red,bold, "\nNOT YET IMPLEMENTED!\n\n",reset
	exit 1
end