Class: BuildCloud::IAMRole

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/build-cloud/iamrole.rb

Constant Summary collapse

@@objects =
[]

Instance Method Summary collapse

Methods included from Component

included

Constructor Details

#initialize(fog_interfaces, log, options = {}) ⇒ IAMRole

Returns a new instance of IAMRole.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/build-cloud/iamrole.rb', line 9

def initialize ( fog_interfaces, log, options = {} )

    @iam     = fog_interfaces[:iam]
    @log     = log
    @options = options

    @log.debug( options.inspect )

    required_options(:rolename, :assume_role_policy_document)

end

Instance Method Details

#createObject



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
# File 'lib/build-cloud/iamrole.rb', line 21

def create
    
    return if exists?

    @log.info( "Creating new IAM role for #{@options[:rolename]}" )

    policies = @options.delete(:policies)

    # Genuinely don't think I've understood the data model with 
    # this stuff.  In particular how roles, instance profiles etc. relate
    #
    # It does what we need right now though, and can be revisited if necessary

    role = @iam.roles.new( @options )
    role.save

    @log.debug( role.inspect )

    @iam.create_instance_profile( @options[:rolename] )

    policies.each do |policy|

        @log.debug( "Adding policy #{policy}" )

        policy_document = JSON.parse( policy[:policy_document] )

        @iam.put_role_policy( @options[:rolename], policy[:policy_name],
            policy_document )

    end

    @iam.add_role_to_instance_profile( @options[:rolename], @options[:rolename] )

end

#deleteObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/build-cloud/iamrole.rb', line 62

def delete

    return unless exists?

    @log.info( "Deleting IAM role for #{@options[:rolename]}" )

    instance_profiles = @iam.list_instance_profiles_for_role( @options[:rolename] ).body['InstanceProfiles'].map { |k| k['InstanceProfileName'] }
    instance_profiles.each do |ip|
        @iam.delete_instance_profile( ip )
        @iam.remove_role_from_instance_profile( @options[:rolename], ip )
    end

    policies = @iam.list_role_policies( @options[:rolename] ).body['PolicyNames']
    policies.each do |policy|
        @iam.delete_role_policy( @options[:rolename], policy )
    end


    fog_object.destroy

end

#readObject Also known as: fog_object



56
57
58
# File 'lib/build-cloud/iamrole.rb', line 56

def read
    @iam.roles.select { |r| r.rolename == @options[:rolename] }.first
end