Class: Morpheus::Cli::SecurityGroupRules

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#add_custom_rule(args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/morpheus/cli/commands/security_group_rules.rb', line 23

def add_custom_rule(args)
  usage = <<-EOT
Usage: morpheus #{command_name} add-custom-rule SOURCE_CIDR PORT_RANGE PROTOCOL [options]
\tSOURCE_CIDR: CIDR to white-list
\tPORT_RANGE: Port value (i.e. 123) or port range (i.e. 1-65535)
\tPROTOCOL: tcp, udp, icmp
EOT
  options = {}
  security_group_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = usage
    opts.on( '-s', '--secgroup SECGROUP', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id|
      security_group_id = id
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)

  if args.count < 3
    puts optparse.banner
    exit 1
  end

  if security_group_id.nil? && @active_security_group
    security_group_id = @active_security_group[@appliance_name.to_sym]
  end

  if security_group_id.nil?
    puts "Security Group ID must be specified with options or set using 'security-groups use id'"
    exit 1
  end

  params = {
    :rule => {
      :source => args[0],
      :portRange => args[1],
      :protocol => args[2],
      :customRule => true
    }
  }
  connect(options)
  begin
    @security_group_rules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @security_group_rules_interface.dry.create(security_group_id, params)
      return
    end
    json_response = @security_group_rules_interface.create(security_group_id, params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    list([security_group_id])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#add_instance_rule(args) ⇒ Object



83
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
130
131
132
133
134
135
136
137
# File 'lib/morpheus/cli/commands/security_group_rules.rb', line 83

def add_instance_rule(args)
  usage = <<-EOT
Usage: morpheus #{command_name} add_instance_rule SOURCE_CIDR INSTANCE_TYPE_ID [options]
\tSOURCE_CIDR: CIDR to white-list
\tINSTANCE_TYPE_ID: ID of the Instance Type to access
EOT

  options = {}
  security_group_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = usage
    opts.on( '-s', '--secgroup secgroup', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id|
      security_group_id = id
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 2
    puts optparse.banner
    exit 1
  end
  if security_group_id.nil? && @active_security_group
    security_group_id = @active_security_group[@appliance_name.to_sym]
  end

  if security_group_id.nil?
    puts "Security Group ID must be specified with options or set using 'security-groups use id'"
    exit
  end

  params = {
    :rule => {
      :source => args[0],
      :instanceTypeId => args[1]
    }
  }
  connect(options)
  begin
    @security_group_rules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @security_group_rules_interface.dry.create(security_group_id, params)
      return
    end
    json_response = @security_group_rules_interface.create(security_group_id, params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    list([security_group_id])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



12
13
14
15
16
# File 'lib/morpheus/cli/commands/security_group_rules.rb', line 12

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @security_group_rules_interface = @api_client.security_group_rules
  @active_security_group = ::Morpheus::Cli::SecurityGroups.load_security_group_file
end

#handle(args) ⇒ Object



18
19
20
21
# File 'lib/morpheus/cli/commands/security_group_rules.rb', line 18

def handle(args)
  print_error "#{yellow}DEPRECATION WARNING: `security-group-rules` is deprecated. Please use `security-groups` instead.#{reset}\n"
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/morpheus/cli/commands/security_group_rules.rb', line 139

def list(args)
  options = {}
  security_group_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  security_group_id = args[0]
  if security_group_id.nil? && @active_security_group
    security_group_id = @active_security_group[@appliance_name.to_sym]
  end

  if security_group_id.nil?
    puts "Security Group ID must be specified with options or set using 'security-groups use id'"
    exit 1
  end
  connect(options)
  begin
    params = {}
    @security_group_rules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @security_group_rules_interface.dry.get(security_group_id, params)
      return
    end
    json_response = @security_group_rules_interface.get(security_group_id, params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    rules = json_response['rules']
    print_h1 "Morpheus Security Group Rules for Security Group ID: #{security_group_id}"
    if rules.empty?
      print yellow,"No security group rules currently configured.",reset,"\n"
    else
      rules = rules.sort {|x,y| x["id"] <=> y["id"] }
      rules.each do |rule|
        print cyan, "=  #{rule['id']} - (CIDR:#{rule['source']}, Port Range:#{rule['portRange']}, Protocol:#{rule['protocol']}, Custom Rule:#{rule['customRule']}, Instance Type:#{rule['instanceTypeId']})\n"
      end
    end
    print reset,"\n"
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#remove(args) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/morpheus/cli/commands/security_group_rules.rb', line 187

def remove(args)
  options = {}
  security_group_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id] [options]")
    opts.on( '-s', '--secgroup secgroup', "Security Group ID (Use will use security as set with 'security-groups use id'" ) do |id|
      security_group_id = id
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    exit 1
  end
  if security_group_id.nil? && @active_security_group
    security_group_id = @active_security_group[@appliance_name.to_sym]
  end

  if security_group_id.nil?
    puts "Security Group ID must be specified with options or set using 'security-groups use id'"
    exit
  end
  connect(options)
  begin
    @security_group_rules_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @security_group_rules_interface.dry.delete(security_group_id, args[0])
      return
    end
    json_response = @security_group_rules_interface.delete(security_group_id, args[0])
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    list([security_group_id])
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end