Class: BuildCloud::IAMRole
- Inherits:
-
Object
- Object
- BuildCloud::IAMRole
- Includes:
- Component
- Defined in:
- lib/build-cloud/iamrole.rb
Constant Summary collapse
- @@objects =
[]
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
-
#initialize(fog_interfaces, log, options = {}) ⇒ IAMRole
constructor
A new instance of IAMRole.
- #read ⇒ Object (also: #fog_object)
Methods included from Component
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, = {} ) @iam = fog_interfaces[:iam] @log = log @options = @log.debug( .inspect ) (:rolename, :assume_role_policy_document) end |
Instance Method Details
#create ⇒ Object
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 |
# 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 ) 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 ) @iam.create_instance_profile( @options[:rolename] ) @iam.add_role_to_instance_profile( @options[:rolename], @options[:rolename] ) end end |
#delete ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/build-cloud/iamrole.rb', line 61 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 |
#read ⇒ Object Also known as: fog_object
55 56 57 |
# File 'lib/build-cloud/iamrole.rb', line 55 def read @iam.roles.select { |r| r.rolename == @options[:rolename] }.first end |