File: mfa_device_collection.rb

package info (click to toggle)
ruby-aws-sdk 1.52.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,164 kB
  • ctags: 4,821
  • sloc: ruby: 28,085; makefile: 7
file content (127 lines) | stat: -rw-r--r-- 4,240 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class IAM

    # @attr_reader [User] user Returns the user that owns this collection.
    class MFADeviceCollection

      include Collection

      # @param [User] user The user that owns this device collection.
      def initialize user, options = {}
        @user = user
        super
      end

      # @return [User] Returns the user that this mfa device collection
      #   belongs to.
      attr_reader :user

      # Enables an MFA device for this user.
      # @param [String] serial_number The serial number that uniquely
      #   identifies the MFA device
      # @param [String] authentication_code_1 An authentication code emitted
      #   by the device.
      # @param [String] authentication_code_2 A subsequent authentication
      #   code emitted by the device.
      # @return [MFADevice] Returns the newly enabled MFA device.
      def enable serial_number, authentication_code_1, authentication_code_2
        client.enable_mfa_device({
          :user_name => user.name,
          :serial_number => serial_number,
          :authentication_code_1 => authentication_code_1.to_s,
          :authentication_code_2 => authentication_code_2.to_s,
        })
        self[serial_number]
      end

      alias_method :create, :enable

      # @param [String] serial_number The serial number of the MFA device you
      #   want to disable.
      # @return [nil]
      def disable serial_number
        self[serial_number].disable
        nil
      end

      # @param [String] serial_number The serial number of an MFA device.
      # @return [MFADevice] Returns a reference to an MFA device with the
      #   given serial number.
      def [] serial_number
        MFADevice.new(user, serial_number)
      end

      # Deactivates all of the MFA devices in this collection.
      # Virtual MFA devices in this collection will not be
      # deleted. Instead they will be available in the
      # {IAM#virtual_mfa_devices} collection so that they can either
      # be deleted or enabled for different users.
      #
      # @return [nil]
      def clear
        each do |device|
          device.deactivate
        end
        nil
      end

      # Yields once for each MFA device.
      #
      # You can limit the number of devices yielded using `:limit`.
      #
      # @param [Hash] options
      # @option options [Integer] :limit The maximum number of devices to yield.
      # @option options [Integer] :batch_size The maximum number of devices
      #   receive each service reqeust.
      # @yieldparam [User] user
      # @return [nil]
      def each options = {}, &block
        super(options.merge(:user_name => user.name), &block)
      end

      # Returns an enumerable object for this collection.  This can be
      # useful if you want to call an enumerable method that does
      # not accept options (e.g. `collect`, `first`, etc).
      #
      #   mfa_devices.enumerator(:limit => 10).collect(&:serial_number)
      #
      # @param (see #each)
      # @option (see #each)
      # @return [Enumerator]
      def enumerator options = {}
        super(options)
      end

      # @api private
      protected
      def each_item response, &block
        response.mfa_devices.each do |item|

          if item.serial_number =~ /^arn:/
            mfa_device = VirtualMfaDevice.new_from(:list_mfa_devices, item,
                                                   item.serial_number,
                                                   :config => config)
          else
            mfa_device = MFADevice.new(user, item.serial_number)
          end

          yield(mfa_device)

        end
      end

    end
  end
end