def test_create_iam_user(self):
        """Test create_iam_user
        """
        treadmill_aws.iamclient.get_user.side_effect = exc.NotFoundError('x')
        treadmill_aws.iamclient.get_role.side_effect = exc.NotFoundError('x')

        iam_conn = mock.MagicMock()
        iam_conn.create_user.return_value = {
            'User': {
                'Arn': 'arn:aws:iam::236968667438:user/r',
                'CreateDate': datetime.datetime(2018, 5, 15, 19, 10, 52,
                                                915000),
                'Path': '/',
                'UserId': 'ABCDFEFGABCDEFG',
                'UserName': '******'
            }
        }

        result = usermanager.create_iam_user(iam_conn,
                                             user_name='foo',
                                             policy='xxx')

        iam_conn.create_user.assert_called_with(UserName='******', Path='/')
        iam_conn.create_role.assert_called_with(
            RoleName='foo', Path='/', AssumeRolePolicyDocument='"xxx"')
Esempio n. 2
0
    def configure_iam(policy_doc, create, username):
        """Create IAM user.
        """
        iam_conn = awscontext.GLOBAL.iam
        try:
            user = usermanager.get_iam_user(iam_conn=iam_conn,
                                            user_name=username)
        except exc.NotFoundError:
            if not create:
                raise

        if create or policy_doc:
            policy = _default_policy()
            if policy_doc:
                with io.open(policy_doc) as f:
                    policy = json.loads(f.read())

            user = usermanager.create_iam_user(
                iam_conn=iam_conn,
                user_name=username,
                policy=policy
            )

        cli.out(formatter(user))