Class: McSecurityGroup

Inherits:
Object
  • Object
show all
Extended by:
RightScale::Api::GatewayExtend
Includes:
RightScale::Api::Gateway
Defined in:
lib/rest_connection/rightscale/mc_security_group.rb

Overview

API 1.5

Instance Attribute Summary

Attributes included from RightScale::Api::Base

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RightScale::Api::GatewayExtend

create, filters, find, find_all, find_by, find_with_filter, load, load_all, parse_args, resource_post_name

Methods included from RightScale::Api::GatewayConnection

#connection

Methods included from RightScale::Api::BaseExtend

#[], #create, #deny_methods, #filters, #find, #find_all, #find_by, #find_by_cloud_id, #find_by_id, #find_by_nickname, #find_by_nickname_speed, #find_with_filter

Methods included from RightScale::Api::BaseConnection

#connection

Methods included from RightScale::Api::Gateway

#[], #[]=, #actions, #hash_of_links, #href, #initialize, #load, #method_missing, #nickname, #parse_params, #rediscover, #save

Methods included from RightScale::Api::Base

#[], #[]=, #destroy, #initialize, #method_missing, #reload, #rs_id, #save

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RightScale::Api::Gateway

Class Method Details

.create(cloud_id, opts = {}) ⇒ Object

NOTE: Create & Destroy require “security_manager” permissions



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 58

def self.create(cloud_id, opts={})
  url = "#{parse_args(cloud_id)}#{self.resource_plural_name}"
  location = connection.post(url, self.resource_singular_name.to_sym => opts)
  newrecord = self.new('links' => [ {'rel' => 'self', 'href' => location } ])

  rules = opts[:rules] || opts["rules"]
  [rules].flatten.each { |rule_hash| newrecord.add_rule(rule_hash) } if rules

  newrecord.reload
  newrecord
end

.filtersObject



53
54
55
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 53

def self.filters
  [:name, :resource_uid]
end

.parse_args(cloud_id) ⇒ Object



49
50
51
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 49

def self.parse_args(cloud_id)
  "clouds/#{cloud_id}/"
end

.resource_plural_nameObject



41
42
43
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 41

def self.resource_plural_name
  "security_groups"
end

.resource_singular_nameObject



45
46
47
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 45

def self.resource_singular_name
  "security_group"
end

Instance Method Details

#add_rule(opts = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
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
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 74

def add_rule(opts={})
  opts.each { |k,v| opts["#{k}".to_sym] = v }
  fields = [
    {"1.0" => :owner,     "1.5" => :group_owner},         # optional
    {"1.0" => :group,     "1.5" => :group_name},          # optional
    {"1.0" => :cidr_ip,   "1.5" => :cidr_ips},            # optional
    {"1.0" => :protocol,  "1.5" => :protocol},            # "tcp" || "udp" || "icmp"
    {"1.0" => :from_port, "1.5" => :start_port},          # optional
    {"1.0" => :to_port,   "1.5" => :end_port},            # optional
    {                     "1.5" => :source_type},         # "cidr_ips" || "group"
    {                     "1.5" => :icmp_code},           # optional
    {                     "1.5" => :icmp_type},           # optional
    {                     "1.5" => :security_group_href}, # optional
  ]
  unless opts[:protocol]
    raise ArgumentError.new("add_rule requires the 'protocol' option")
  end
  params = {
    :source_type => ((opts[:cidr_ip] || opts[:cidr_ips]) ? "cidr_ips" : "group"),
    :security_group_href => self.href,
    :protocol_details => {}
  }

  fields.each { |ver|
    next unless val = opts[ver["1.0"]] || opts[ver["1.5"]]
    if ver["1.5"].to_s =~ /port|icmp/
      params[:protocol_details][ver["1.5"]] = val
    else
      params[ver["1.5"]] = val
    end
  }

  SecurityGroupRule.create(params)
end

#remove_rules_by_filters(filters = {}) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 109

def remove_rules_by_filters(filters={})
  rules_to_delete = rules
  filters.each do |filter,regex|
    @rules.reject! { |rule| rule[filter] =~ Regexp.new(regex) }
  end
  @rules.each { |rule| rule.destroy }
end

#resource_plural_nameObject



33
34
35
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 33

def resource_plural_name
  "security_groups"
end

#resource_singular_nameObject



37
38
39
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 37

def resource_singular_name
  "security_group"
end

#rulesObject



70
71
72
# File 'lib/rest_connection/rightscale/mc_security_group.rb', line 70

def rules
  self.load(SecurityGroupRule)
end