File: create_role.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (79 lines) | stat: -rw-r--r-- 2,850 bytes parent folder | download | duplicates (5)
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
module Fog
  module AWS
    class IAM
        # At the moment this is the only policy you can use
        EC2_ASSUME_ROLE_POLICY = <<-JSON
{
  "Version":"2008-10-17",
  "Statement":[
      {
        "Effect":"Allow",
        "Principal":{
          "Service":["ec2.amazonaws.com"]
        },
        "Action":["sts:AssumeRole"]
      }
  ]
}
        JSON

      class Real
        require 'fog/aws/parsers/iam/single_role'

        # Creates a new role for your AWS account
        #
        # ==== Parameters
        # * RoleName<~String>: name of the role to create
        # * AssumeRolePolicyDocument<~String>: The policy that grants an entity permission to assume the role.
        # * Path<~String>: This parameter is optional. If it is not included, it defaults to a slash (/).
        # ==== Returns
        # * response<~Excon::Response>:
        #   * body<~Hash>:
        #     * 'Role'<~Hash>:
        #       * 'Arn'<~String> -
        #       * 'AssumeRolePolicyDocument'<~String<
        #       * 'Path'<~String> -
        #       * 'RoleId'<~String> -
        #       * 'RoleName'<~String> -
        #     * 'RequestId'<~String> - Id of the request
        #
        # ==== See Also
        # http://docs.amazonwebservices.com/IAM/latest/APIReference/API_CreateRole.html
        #
        def create_role(role_name, assume_role_policy_document, path = '/')
          request(
            'Action'                   => 'CreateRole',
            'RoleName'                 => role_name,
            'AssumeRolePolicyDocument' => assume_role_policy_document,
            'Path'                     => path,
            :parser                    => Fog::Parsers::AWS::IAM::SingleRole.new
          )
        end
      end

      class Mock
        def create_role(role_name, assume_role_policy_document, path = '/')
          if data[:roles].key?(role_name)
            raise Fog::AWS::IAM::EntityAlreadyExists.new("Role with name #{role_name} already exists")
          else
            data[:roles][role_name][:path] = path
            Excon::Response.new.tap do |response|
              response.body = {
                'Role' => {
                  'Arn'                      => data[:roles][role_name][:arn].strip,
                  'AssumeRolePolicyDocument' => Fog::JSON.encode(data[:roles][role_name][:assume_role_policy_document]),
                  'CreateDate'               => data[:roles][role_name][:create_date],
                  'Path'                     => path || "/",
                  'RoleId'                   => data[:roles][role_name][:role_id].strip,
                  'RoleName'                 => role_name,
                },
                'RequestId' => Fog::AWS::Mock.request_id
              }
              response.status = 200
            end
          end
        end
      end
    end
  end
end