Class: Morpheus::Cli::Zones

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

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Zones

Returns a new instance of Zones.



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

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
	@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
	@zones_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).zones
	@groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
	@zone_types = @zones_interface.zone_types['zoneTypes']
end

Instance Method Details

#add(args) ⇒ Object



40
41
42
43
44
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
# File 'lib/morpheus/cli/zones.rb', line 40

def add(args)
	if args.count < 1
		puts "\nUsage: morpheus zones add [name] --group GROUP --type TYPE\n\n"
		return
	end
	params = {zone_type: 'standard'}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			params[:group] = group
		end
		opts.on( '-t', '--type TYPE', "Zone Type" ) do |zone_type|
			params[:zone_type] = zone_type
		end
		opts.on( '-d', '--description DESCRIPTION', "Description (optional)" ) do |desc|
			params[:description] = desc
		end
	end
	optparse.parse(args)
	zone = {name: args[0], description: params[:description]}
	if !params[:group].nil?
		group = find_group_by_name(params[:group])
		if !group.nil?
			zone['groupId'] = group['id']
		end
	end

	if !params[:zone_type].nil?
		zone['zoneType'] = {code:zone_type_code_for_name(params[:zone_type])}
	end
	
	begin
		@zones_interface.create(zone)
	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



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

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 zones [list,add,remove] [name]\n\n"
		return
	end

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

#list(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
157
158
159
160
161
162
163
164
# File 'lib/morpheus/cli/zones.rb', line 131

def list(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			options[:group] = group
		end
	end
	optparse.parse(args)
	begin
		params = {}
		if !options[:group].nil?
			group = find_group_by_name(options[:group])
			if !group.nil?
				params['groupId'] = group['id']
			end
		end

		json_response = @zones_interface.get(params)
		zones = json_response['zones']
		print "\n" ,cyan, bold, "Morpheus Zones\n","==================", reset, "\n\n"
		if zones.empty?
			puts yellow,"No zones currently configured.",reset
		else
			zones.each do |zone|
				print cyan, "=  #{zone['name']} (#{zone_type_for_id(zone['zoneTypeId'])}) - #{zone['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



84
85
86
87
88
89
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
123
124
125
126
127
128
129
# File 'lib/morpheus/cli/zones.rb', line 84

def remove(args)
	if args.count < 2
		puts "\nUsage: morpheus zones remove [name] --group GROUP\n\n"
		return
	end

	params = {}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			params[:group] = group
		end
	end
	optparse.parse(args)

	if !params[:group].nil?
		group = find_group_by_name(params[:group])
		if !group.nil?
			params[:groupId] = group['id']
		else
			puts "\nGroup #{params[:group]} not found!"
			return
		end
	else params[:group].nil?
		puts "\nUsage: morpheus zones remove [name] --group GROUP"
		return
	end


	begin
		zone_results = @zones_interface.get({name: args[0], groupId: params[:groupId]})
		if zone_results['zones'].empty?
			puts "Zone not found by name #{args[0]}"
			return
		end
		@zones_interface.destroy(zone_results['zones'][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