File: insert_firewall.rb

package info (click to toggle)
ruby-fog-google 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,568 kB
  • sloc: ruby: 16,775; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
module Fog
  module Compute
    class Google
      class Mock
        def insert_firewall(_firewall_name, _options = {})
          # :no-coverage:
          Fog::Mock.not_implemented
          # :no-coverage:
        end
      end

      class Real
        INSERTABLE_FIREWALL_FIELDS = %i{
          allowed
          denied
          description
          destination_ranges
          direction
          name
          network
          priority
          source_ranges
          source_service_accounts
          source_tags
          target_service_accounts
          target_tags
        }.freeze

        ##
        # Create a Firewall resource
        #
        # @param [Hash] opts The firewall object to create
        # @option opts [Array<Hash>] allowed
        # @option opts [Array<Hash>] denied
        # @option opts [String] description
        # @option opts [Array<String>] destination_ranges
        # @option opts [String] direction
        # @option opts [String] name
        # @option opts [String] network
        # @option opts [Fixnum] priority
        # @option opts [Array<String>] source_ranges
        # @option opts [Array<String>] source_service_accounts
        # @option opts [Array<String>] source_tags
        # @option opts [Array<String>] target_service_accounts
        # @option opts [Array<String>] target_tags
        #
        # @see https://cloud.google.com/compute/docs/reference/latest/firewalls/insert
        def insert_firewall(firewall_name, opts = {})
          if opts.key?(:network) && !opts[:network].empty?
            unless opts[:network].start_with?("http://", "https://", "projects/", "global/")
              opts[:network] = "projects/#{@project}/global/networks/#{opts[:network]}"
            end
          end

          opts = opts.select { |k, _| INSERTABLE_FIREWALL_FIELDS.include? k }
                     .merge(:name => firewall_name)

          @compute.insert_firewall(
            @project, ::Google::Apis::ComputeV1::Firewall.new(**opts)
          )
        end
      end
    end
  end
end