File: list_security_groups.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 (67 lines) | stat: -rw-r--r-- 2,031 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
module Fog
  module OpenStack
    class Compute
      class Real
        def list_security_groups(options = {})
          path = "os-security-groups"

          if options.kind_of?(Hash)
            server_id = options.delete(:server_id)
            query = options
          else
            # Backwards compatibility layer, only server_id was passed as first parameter previously
            Fog::Logger.deprecation('Calling OpenStack[:compute].list_security_groups(server_id) is deprecated, use .list_security_groups(:server_id => value) instead')
            server_id = options
            query = {}
          end

          if server_id
            path = "servers/#{server_id}/#{path}"
          end

          request(
            :expects => [200],
            :method  => 'GET',
            :path    => path,
            :query   => query
          )
        end
      end

      class Mock
        def list_security_groups(options = {})
          server_id = if options.kind_of?(Hash)
                        options.delete(:server_id)
                      else
                        options
                      end

          security_groups = data[:security_groups].values

          groups = if server_id
                     server_group_names =
                       Array(data[:server_security_group_map][server_id])

                     server_group_names.map do |name|
                       security_groups.find do |sg|
                         sg['name'] == name
                       end
                     end.compact
                   else
                     security_groups
                   end

          Excon::Response.new(
            :body    => {'security_groups' => groups},
            :headers => {
              "X-Compute-Request-Id" => "req-#{Fog::Mock.random_base64(36)}",
              "Content-Type"         => "application/json",
              "Date"                 => Date.new
            },
            :status  => 200
          )
        end
      end
    end
  end
end