File: update_router.rb

package info (click to toggle)
ruby-fog-openstack 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,784 kB
  • sloc: ruby: 47,937; makefile: 5; sh: 4
file content (71 lines) | stat: -rw-r--r-- 2,651 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
module Fog
  module OpenStack
    class Network
      class Real
        # Update Router
        #
        # Beyond the name and the administrative state, the only
        # parameter which can be updated with this operation is
        # the external gateway.
        # @see http://docs.openstack.org/api/openstack-network/2.0/content/router_update.html
        def update_router(router_id, options = {})
          data = {'router' => {}}

          [:name, :admin_state_up, :routes].each do |key|
            data['router'][key] = options[key] if options[key]
          end

          # remove this in a future
          egi = options[:external_gateway_info]
          if egi
            if egi.kind_of?(Fog::OpenStack::Network::Network)
              Fog::Logger.deprecation "Passing a model objects into options[:external_gateway_info] is deprecated. \
              Please pass  external external gateway as follows options[:external_gateway_info] = { :network_id => NETWORK_ID }]"
              data['router'][:external_gateway_info] = {:network_id => egi.id}
            elsif egi.kind_of?(Hash)
              data['router'][:external_gateway_info] = egi
            else
              raise ArgumentError, 'Invalid external_gateway_info attribute'
            end
          end

          request(
            :body    => Fog::JSON.encode(data),
            :expects => 200,
            :method  => 'PUT',
            :path    => "routers/#{router_id}.json"
          )
        end
      end

      class Mock
        def update_router(router_id, options = {})
          response = Excon::Response.new
          router = list_routers.body['routers'].find { |r| r[:id] == router_id }

          raise Fog::OpenStack::Network::NotFound unless router

          options.keys.each { |k| router[k] = options[k] }

          # remove this in a future
          egi = options[:external_gateway_info]
          if egi
            if egi.kind_of?(Fog::OpenStack::Network::Network)
              Fog::Logger.deprecation "Passing a model objects into options[:external_gateway_info] is deprecated. \
              Please pass  external external gateway as follows options[:external_gateway_info] = { :network_id => NETWORK_ID }]"
              router[:external_gateway_info] = { :network_id => egi.id }
            elsif egi.is_a?(Hash)
              router[:external_gateway_info] = egi
            else
              raise ArgumentError.new('Invalid external_gateway_info attribute')
            end
          end

          response.body = {'router' => router}
          response.status = 200
          response
        end
      end
    end
  end
end