2014-12-30 17:25:09 -05:00
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class IAM
|
|
|
|
class User < Fog::Model
|
|
|
|
identity :id, :aliases => 'UserName'
|
2015-05-21 12:30:27 -04:00
|
|
|
|
|
|
|
attribute :path, :aliases => 'Path'
|
|
|
|
attribute :arn, :aliases => 'Arn'
|
|
|
|
attribute :user_id, :aliases => 'UserId'
|
2014-12-30 17:25:09 -05:00
|
|
|
attribute :created_at, :aliases => 'CreateDate', :type => :time
|
|
|
|
|
2015-05-21 12:30:27 -04:00
|
|
|
def access_keys
|
2014-12-30 17:25:09 -05:00
|
|
|
requires :id
|
2015-05-21 12:30:27 -04:00
|
|
|
|
|
|
|
service.access_keys(:username => id)
|
2014-12-30 17:25:09 -05:00
|
|
|
end
|
|
|
|
|
2015-05-28 16:20:48 -04:00
|
|
|
def attach(policy_or_arn)
|
|
|
|
requires :identity
|
|
|
|
|
|
|
|
arn = if policy_or_arn.respond_to?(:arn)
|
|
|
|
policy_or_arn.arn
|
|
|
|
else
|
|
|
|
policy_or_arn
|
|
|
|
end
|
|
|
|
|
|
|
|
service.attach_user_policy(self.identity, arn)
|
|
|
|
end
|
|
|
|
|
|
|
|
def detach(policy_or_arn)
|
|
|
|
requires :identity
|
|
|
|
|
|
|
|
arn = if policy_or_arn.respond_to?(:arn)
|
|
|
|
policy_or_arn.arn
|
|
|
|
else
|
|
|
|
policy_or_arn
|
|
|
|
end
|
|
|
|
|
|
|
|
service.detach_user_policy(self.identity, arn)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attached_policies
|
|
|
|
requires :identity
|
|
|
|
|
|
|
|
service.managed_policies(:username => self.identity)
|
|
|
|
end
|
|
|
|
|
2014-12-30 17:25:09 -05:00
|
|
|
def destroy
|
|
|
|
requires :id
|
2015-05-21 12:30:27 -04:00
|
|
|
|
2014-12-30 17:25:09 -05:00
|
|
|
service.delete_user(id)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-05-21 12:30:27 -04:00
|
|
|
def groups
|
2015-05-28 16:20:48 -04:00
|
|
|
requires :identity
|
|
|
|
|
2015-05-21 12:30:27 -04:00
|
|
|
service.groups(:username => self.identity)
|
|
|
|
end
|
|
|
|
|
2014-12-30 17:25:09 -05:00
|
|
|
def policies
|
2015-05-28 16:20:48 -04:00
|
|
|
requires :identity
|
2015-05-21 12:30:27 -04:00
|
|
|
|
2015-05-28 16:20:48 -04:00
|
|
|
service.policies(:username => self.identity)
|
2014-12-30 17:25:09 -05:00
|
|
|
end
|
|
|
|
|
2015-05-21 15:02:21 -04:00
|
|
|
def password=(password)
|
|
|
|
requires :identity
|
|
|
|
|
|
|
|
has_password = !!self.password_created_at
|
|
|
|
|
|
|
|
if has_password && password.nil?
|
|
|
|
service.delete_login_profile(self.identity)
|
|
|
|
elsif has_password
|
|
|
|
service.update_login_profile(self.identity, password)
|
|
|
|
elsif !password.nil?
|
|
|
|
service.create_login_profile(self.identity, password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_created_at
|
|
|
|
requires :identity
|
|
|
|
|
|
|
|
service.get_login_profile(self.identity).body["LoginProfile"]["CreateDate"]
|
|
|
|
rescue Fog::AWS::IAM::NotFound
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-05-21 12:30:27 -04:00
|
|
|
def save
|
2014-12-30 17:25:09 -05:00
|
|
|
requires :id
|
2015-05-21 12:30:27 -04:00
|
|
|
data = service.create_user(id, path || '/').body['User']
|
|
|
|
merge_attributes(data)
|
|
|
|
true
|
2014-12-30 17:25:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|