mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[google|compute] Add Firewalls support
- Add "Firewalls" model and collection - Modify "insert" method to add "description" and "targetTags" properties - Modify "insert" method to not prepend network URI if is set by user
This commit is contained in:
parent
b7675cd8e8
commit
f523c66c07
4 changed files with 99 additions and 6 deletions
|
@ -110,9 +110,13 @@ module Fog
|
|||
|
||||
model :forwarding_rule
|
||||
collection :forwarding_rules
|
||||
|
||||
model :project
|
||||
collection :projects
|
||||
|
||||
model :firewall
|
||||
collection :firewalls
|
||||
|
||||
module Shared
|
||||
attr_reader :project, :api_version
|
||||
|
||||
|
|
48
lib/fog/google/models/compute/firewall.rb
Normal file
48
lib/fog/google/models/compute/firewall.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Google
|
||||
|
||||
##
|
||||
# Represents a Firewall resource
|
||||
#
|
||||
# @see https://developers.google.com/compute/docs/reference/latest/firewalls
|
||||
class Firewall < Fog::Model
|
||||
identity :name
|
||||
|
||||
attribute :kind
|
||||
attribute :id
|
||||
attribute :allowed
|
||||
attribute :creation_timestamp, :aliases => 'creationTimestamp'
|
||||
attribute :description
|
||||
attribute :network
|
||||
attribute :self_link, :aliases => 'selfLink'
|
||||
attribute :source_ranges, :aliases => 'sourceRanges'
|
||||
attribute :source_tags, :aliases => 'sourceTags'
|
||||
attribute :target_tags, :aliases => 'targetTags'
|
||||
|
||||
def save
|
||||
requires :identity, :allowed, :network
|
||||
|
||||
data = service.insert_firewall(identity, self.allowed, self.network, self.attributes)
|
||||
operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'])
|
||||
operation.wait_for { !pending? }
|
||||
reload
|
||||
end
|
||||
|
||||
def destroy(async=true)
|
||||
requires :identity
|
||||
|
||||
data = service.delete_firewall(identity)
|
||||
operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'])
|
||||
unless async
|
||||
operation.wait_for { ready? }
|
||||
end
|
||||
operation
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
27
lib/fog/google/models/compute/firewalls.rb
Normal file
27
lib/fog/google/models/compute/firewalls.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/google/models/compute/firewall'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class Google
|
||||
|
||||
class Firewalls < Fog::Collection
|
||||
model Fog::Compute::Google::Firewall
|
||||
|
||||
def all
|
||||
data = service.list_firewalls.body
|
||||
load(data['items'] || [])
|
||||
end
|
||||
|
||||
def get(identity)
|
||||
if firewall = service.get_firewall(identity).body
|
||||
new(firewall)
|
||||
end
|
||||
rescue Fog::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,7 +3,7 @@ module Fog
|
|||
class Google
|
||||
|
||||
class Mock
|
||||
def insert_firewall(firewall_name, source_range=[], allowed=[], network=@default_network, source_tags=[])
|
||||
def insert_firewall(firewall_name, allowed, network = @default_network, options = {})
|
||||
Fog::Mock.not_implemented
|
||||
end
|
||||
|
||||
|
@ -11,20 +11,34 @@ module Fog
|
|||
|
||||
class Real
|
||||
|
||||
def insert_firewall(firewall_name, source_range=[], allowed=[], network=@default_network, source_tags=[])
|
||||
def insert_firewall(firewall_name, allowed, network = @default_network, options = {})
|
||||
unless network.start_with? 'http'
|
||||
network = "#{@api_url}#{@project}/global/networks/#{network}"
|
||||
end
|
||||
|
||||
api_method = @compute.firewalls.insert
|
||||
parameters = {
|
||||
'project' => @project,
|
||||
}
|
||||
body_object = {
|
||||
"name" => firewall_name,
|
||||
"network" => "#{@api_url}#{@project}/global/networks/#{network}",
|
||||
"network" => network,
|
||||
"allowed" => allowed,
|
||||
}
|
||||
body_object["sourceRanges"] = source_range if !source_range.empty?
|
||||
body_object["sourceTags"] = source_tags if !source_tags.empty?
|
||||
unless options[:description].nil?
|
||||
body_object["description"] = options[:description]
|
||||
end
|
||||
unless options[:source_ranges].nil? || options[:source_ranges].empty?
|
||||
body_object["sourceRanges"] = options[:source_ranges]
|
||||
end
|
||||
unless options[:source_tags].nil? || options[:source_tags].empty?
|
||||
body_object["sourceTags"] = options[:source_tags]
|
||||
end
|
||||
unless options[:target_tags].nil? || options[:target_tags].empty?
|
||||
body_object["targetTags"] = options[:target_tags]
|
||||
end
|
||||
|
||||
result = self.build_result(api_method, parameters, body_object=body_object)
|
||||
result = self.build_result(api_method, parameters, body_object)
|
||||
response = self.build_response(result)
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue