File: list_policies.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (96 lines) | stat: -rw-r--r-- 3,009 bytes parent folder | download | duplicates (4)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module Fog
  module AWS
    class IAM
      class Real
        require 'fog/aws/parsers/iam/list_managed_policies'

        # Lists managed policies
        #
        # ==== Parameters
        # * options <~Hash>: options that filter the result set
        #   * Marker <~String>
        #   * MaxItems <~Integer>
        #   * OnlyAttached <~Boolean>
        #   * PathPrefix <~String>
        #   * Scope <~String>
        # ==== Returns
        # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'RequestId'<~String> - Id of the request
        #     * 'IsTruncated'<~Boolean>
        #     * 'Marker'<~String>
        #     * 'Policies'<~Array>:
        #       * Arn
        #       * AttachmentCount
        #       * CreateDate
        #       * DefaultVersionId
        #       * Description
        #       * IsAttachable
        #       * Path
        #       * PolicyId
        #       * PolicyName
        #       * UpdateDate
        # ==== See Also
        # http://docs.aws.amazon.com/IAM/latest/APIReference/API_ListPolicies.html
        #
        def list_policies(options={})
          request({
            'Action'          => 'ListPolicies',
            :parser           => Fog::Parsers::AWS::IAM::ListManagedPolicies.new
          }.merge(options))
        end
      end

      class Mock
        def list_policies(options={})
          limit  = options['MaxItems']
          marker = options['Marker']

          if limit
            if limit > 1_000
              raise Fog::AWS::IAM::Error.new(
                "ValidationError => 1 validation error detected: Value '#{limit}' at 'limit' failed to satisfy constraint: Member must have value less than or equal to 1000"
              )
            elsif limit <  1
              raise Fog::AWS::IAM::Error.new(
                "ValidationError => 1 validation error detected: Value '#{limit}' at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1"
              )
            end
          end

          data_set = if marker
                       self.data[:markers][marker] || []
                     else
                       self.data[:managed_policies].values
                     end

          if options["PathPrefix"]
            data_set = data_set.select { |p| p["Path"].match(/^#{options["PathPrefix"]}/) }
          end

          data = data_set.slice!(0, limit || 100)
          truncated = data_set.size > 0
          marker = truncated && Base64.encode64("metadata/l/#{account_id}/#{UUID.uuid}")

          response = Excon::Response.new

          body = {
            'Policies'    => data,
            'IsTruncated' => truncated,
            'RequestId'   => Fog::AWS::Mock.request_id
          }

          if marker
            self.data[:markers][marker] = data_set
            body.merge!('Marker' => marker)
          end

          response.body = body
          response.status = 200

          response
        end
      end
    end
  end
end