コード例 #1
0
    def _run_main_command(self, parsed_args, parsed_globals):
        ec2_result = None
        emr_result = None
        self.iam_endpoint_url = parsed_args.iam_endpoint

        self._check_for_iam_endpoint(self.region, self.iam_endpoint_url)
        self.emr_endpoint_url = \
            self._session.create_client(
                'emr',
                region_name=self.region,
                endpoint_url=parsed_globals.endpoint_url,
                verify=parsed_globals.verify_ssl).meta.endpoint_url

        LOG.debug('elasticmapreduce endpoint used for resolving'
                  ' service principal: ' + self.emr_endpoint_url)

        # Check if the default EC2 Role for EMR exists.
        role_name = EC2_ROLE_NAME
        if self._check_if_role_exists(role_name, parsed_globals):
            LOG.debug('Role ' + role_name + ' exists.')
        else:
            LOG.debug('Role ' + role_name + ' does not exist.'
                      ' Creating default role for EC2: ' + role_name)
            ec2_result = self._create_role_with_role_policy(
                role_name, role_name, EC2,
                emrutils.dict_to_string(EC2_ROLE_POLICY), parsed_globals)

        # Check if the default EC2 Instance Profile for EMR exists.
        instance_profile_name = EC2_ROLE_NAME
        if self._check_if_instance_profile_exists(instance_profile_name,
                                                  parsed_globals):
            LOG.debug('Instance Profile ' + instance_profile_name + ' exists.')
        else:
            LOG.debug('Instance Profile ' + instance_profile_name +
                      'does not exist. Creating default Instance Profile ' +
                      instance_profile_name)
            self._create_instance_profile_with_role(instance_profile_name,
                                                    instance_profile_name,
                                                    parsed_globals)

        # Check if the default EMR Role exists.
        role_name = EMR_ROLE_NAME
        if self._check_if_role_exists(role_name, parsed_globals):
            LOG.debug('Role ' + role_name + ' exists.')
        else:
            LOG.debug('Role ' + role_name + ' does not exist.'
                      ' Creating default role for EMR: ' + role_name)
            emr_result = self._create_role_with_role_policy(
                role_name, role_name, EMR,
                emrutils.dict_to_string(EMR_ROLE_POLICY), parsed_globals)

        configutils.update_roles(self._session)

        emrutils.display_response(
            self._session, 'create_role',
            self._construct_result(ec2_result, emr_result), parsed_globals)

        return 0
コード例 #2
0
    def test_default_roles_not_exist(self, get_rp_patch, role_exists_patch,
                                     instance_profile_exists_patch,
                                     construct_result_patch):
        get_rp_patch.return_value = False
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region cn-north-1'

        self.run_cmd(cmdline, expected_rc=0)

        # Only 8 operations will be called as we are mocking
        # _check_if_role_exists and _check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 6)

        self.assertEqual(self.operations_called[0][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[0][1]['RoleName'],
                         EC2_ROLE_NAME)
        self.assertEqual(
            self.operations_called[0][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.ec2_role_policy_document))

        self.assertEqual(self.operations_called[1][0].name, 'AttachRolePolicy')
        self.assertEqual(
            self.operations_called[1][1]['PolicyArn'],
            "arn:aws-cn:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role"
        )
        self.assertEqual(self.operations_called[1][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[2][0].name,
                         'CreateInstanceProfile')
        self.assertEqual(self.operations_called[2][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[3][0].name,
                         'AddRoleToInstanceProfile')
        self.assertEqual(self.operations_called[3][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[3][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[4][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[4][1]['RoleName'],
                         EMR_ROLE_NAME)
        self.assertEqual(
            self.operations_called[4][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_role_policy_document))

        self.assertEqual(self.operations_called[5][0].name, 'AttachRolePolicy')
        self.assertEqual(
            self.operations_called[5][1]['PolicyArn'],
            "arn:aws-cn:iam::aws:policy/service-role/AmazonElasticMapReduceRole"
        )
        self.assertEqual(self.operations_called[5][1]['RoleName'],
                         EMR_ROLE_NAME)
コード例 #3
0
    def test_default_roles_not_exist(self, get_rp_patch,
                                     role_exists_patch,
                                     instance_profile_exists_patch,
                                     construct_result_patch):
        get_rp_patch.return_value = False
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region cn-north-1'

        self.run_cmd(cmdline, expected_rc=0)

        # Only 8 operations will be called as we are mocking
        # _check_if_role_exists and _check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 6)

        self.assertEqual(self.operations_called[0][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[0][1]['RoleName'],
                         EC2_ROLE_NAME)
        self.assertEqual(
            self.operations_called[0][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.ec2_role_policy_document))

        self.assertEqual(self.operations_called[1][0].name,
                         'AttachRolePolicy')
        self.assertEqual(self.operations_called[1][1]['PolicyArn'],
                         "arn:aws-cn:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role")
        self.assertEqual(self.operations_called[1][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[2][0].name,
                         'CreateInstanceProfile')
        self.assertEqual(self.operations_called[2][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[3][0].name,
                         'AddRoleToInstanceProfile')
        self.assertEqual(self.operations_called[3][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[3][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[4][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[4][1]['RoleName'],
                         EMR_ROLE_NAME)
        self.assertEqual(
            self.operations_called[4][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_role_policy_document))

        self.assertEqual(self.operations_called[5][0].name, 'AttachRolePolicy')
        self.assertEqual(self.operations_called[5][1]['PolicyArn'],
                         "arn:aws-cn:iam::aws:policy/service-role/AmazonElasticMapReduceRole")
        self.assertEqual(self.operations_called[5][1]['RoleName'],
                         EMR_ROLE_NAME)
コード例 #4
0
    def test_default_roles_not_exist(self, role_exists_patch,
                                     instance_profile_exists_patch,
                                     construct_result_patch):
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region cn-north-1'
        self.run_cmd(cmdline, expected_rc=0)

        # Only 6 operations will be called as we are mocking
        # _check_if_role_exists and _check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 6)

        self.assertEqual(self.operations_called[0][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[0][1]['RoleName'],
                         EC2_ROLE_NAME)
        self.assertEqual(
            self.operations_called[0][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.ec2_role_policy_document))

        self.assertEqual(self.operations_called[1][0].name,
                         'PutRolePolicy')
        self.assertEqual(self.operations_called[1][1]['PolicyDocument'],
                         emrutils.dict_to_string(EC2_ROLE_POLICY))
        self.assertEqual(self.operations_called[1][1]['PolicyName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[1][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[2][0].name,
                         'CreateInstanceProfile')
        self.assertEqual(self.operations_called[2][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[3][0].name,
                         'AddRoleToInstanceProfile')
        self.assertEqual(self.operations_called[3][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[3][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[4][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[4][1]['RoleName'],
                         EMR_ROLE_NAME)
        self.assertEqual(
            self.operations_called[4][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_role_policy_document))

        self.assertEqual(self.operations_called[5][0].name, 'PutRolePolicy')
        self.assertEqual(self.operations_called[5][1]['PolicyName'],
                         EMR_ROLE_NAME)
        self.assertEqual(self.operations_called[5][1]['RoleName'],
                         EMR_ROLE_NAME)
コード例 #5
0
    def test_default_roles_not_exist(self, role_exists_patch,
                                     instance_profile_exists_patch,
                                     construct_result_patch):
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region cn-north-1'
        self.run_cmd(cmdline, expected_rc=0)

        # Only 6 operations will be called as we are mocking
        # _check_if_role_exists and _check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 6)

        self.assertEqual(self.operations_called[0][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[0][1]['RoleName'],
                         EC2_ROLE_NAME)
        self.assertEqual(
            self.operations_called[0][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.ec2_role_policy_document))

        self.assertEqual(self.operations_called[1][0].name,
                         'PutRolePolicy')
        self.assertEqual(self.operations_called[1][1]['PolicyDocument'],
                         emrutils.dict_to_string(EC2_ROLE_POLICY))
        self.assertEqual(self.operations_called[1][1]['PolicyName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[1][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[2][0].name,
                         'CreateInstanceProfile')
        self.assertEqual(self.operations_called[2][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[3][0].name,
                         'AddRoleToInstanceProfile')
        self.assertEqual(self.operations_called[3][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[3][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[4][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[4][1]['RoleName'],
                         EMR_ROLE_NAME)
        self.assertEqual(
            self.operations_called[4][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_role_policy_document))

        self.assertEqual(self.operations_called[5][0].name, 'PutRolePolicy')
        self.assertEqual(self.operations_called[5][1]['PolicyName'],
                         EMR_ROLE_NAME)
        self.assertEqual(self.operations_called[5][1]['RoleName'],
                         EMR_ROLE_NAME)
コード例 #6
0
    def _create_role_with_role_policy(self, role_name, service_names, role_arn,
                                      parsed_globals):

        if len(service_names) == 1:
            service_principal = get_service_principal(service_names[0],
                                                      self.emr_endpoint_url)
        else:
            service_principal = []
            for service in service_names:
                service_principal.append(
                    get_service_principal(service, self.emr_endpoint_url))

        LOG.debug(service_principal)

        parameters = {'RoleName': role_name}
        _assume_role_policy = \
            emrutils.dict_to_string(assume_role_policy(service_principal))
        parameters['AssumeRolePolicyDocument'] = _assume_role_policy
        create_role_response = self._call_iam_operation(
            'CreateRole', parameters, parsed_globals)

        parameters = {}
        parameters['PolicyArn'] = role_arn
        parameters['RoleName'] = role_name
        self._call_iam_operation('AttachRolePolicy', parameters,
                                 parsed_globals)

        return create_role_response
コード例 #7
0
ファイル: createdefaultroles.py プロジェクト: aws/aws-cli
    def _create_role_with_role_policy(
            self, role_name, service_names, role_arn, parsed_globals):

        if len(service_names) == 1:
            service_principal = get_service_principal(service_names[0], self.emr_endpoint_url)
        else:
            service_principal = []
            for service in service_names:
                service_principal.append(get_service_principal(service, self.emr_endpoint_url))

        LOG.debug(service_principal)

        parameters = {'RoleName': role_name}
        _assume_role_policy = \
            emrutils.dict_to_string(assume_role_policy(service_principal))
        parameters['AssumeRolePolicyDocument'] = _assume_role_policy
        create_role_response = self._call_iam_operation('CreateRole',
                                                        parameters,
                                                        parsed_globals)

        parameters = {}
        parameters['PolicyArn'] = role_arn
        parameters['RoleName'] = role_name
        self._call_iam_operation('AttachRolePolicy',
                                 parameters, parsed_globals)

        return create_role_response
コード例 #8
0
ファイル: createdefaultroles.py プロジェクト: monk-ee/aws-cli
    def _run_main(self, parsed_args, parsed_globals):
        ec2_result = None
        emr_result = None
        self.iam = self._session.get_service('iam')
        self.iam_endpoint_url = parsed_args.iam_endpoint
        region = self._get_region(parsed_globals)

        self._check_for_iam_endpoint(region, self.iam_endpoint_url)
        self.emr_endpoint_url = \
            self._session.get_service('emr').get_endpoint(
                region_name=parsed_globals.region,
                endpoint_url=parsed_globals.endpoint_url,
                verify=parsed_globals.verify_ssl).host

        LOG.debug('elasticmapreduce endpoint used for resolving'
                  ' service principal: ' + self.emr_endpoint_url)

        # Check if the default EC2 Role for EMR exists.
        role_name = EC2_ROLE_NAME
        if self._check_if_role_exists(role_name, parsed_globals):
            LOG.debug('Role ' + role_name + ' exists.')
        else:
            LOG.debug('Role ' + role_name + ' does not exist.'
                      ' Creating default role ' + role_name)
            ec2_result = self._create_role_with_role_policy(
                role_name, role_name, constants.EC2,
                emrutils.dict_to_string(EC2_ROLE_POLICY),
                parsed_globals)

        # Check if the default EC2 Instance Profile for EMR exists.
        instance_profile_name = EC2_ROLE_NAME
        if self._check_if_instance_profile_exists(instance_profile_name,
                                                  parsed_globals):
            LOG.debug('Instance Profile ' + instance_profile_name + ' exists.')
        else:
            LOG.debug('Instance Profile ' + instance_profile_name +
                      'does not exist. Creating default Instance Profile ' +
                      instance_profile_name)
            self._create_instance_profile_with_role(instance_profile_name,
                                                    instance_profile_name,
                                                    parsed_globals)

        emrutils.display_response(
            self._session,
            self._session.get_service('iam').get_operation('CreateRole'),
            self._construct_result(ec2_result),
            parsed_globals)

        return 0
コード例 #9
0
    def _create_role_with_role_policy(self, role_name, service_name, role_arn, parsed_globals):
        service_principal = get_service_principal(service_name, self.emr_endpoint_url)
        LOG.debug(service_principal)

        parameters = {"RoleName": role_name}
        _assume_role_policy = emrutils.dict_to_string(assume_role_policy(service_principal))
        parameters["AssumeRolePolicyDocument"] = _assume_role_policy
        create_role_response = self._call_iam_operation("CreateRole", parameters, parsed_globals)

        parameters = {}
        parameters["PolicyArn"] = role_arn
        parameters["RoleName"] = role_name
        self._call_iam_operation("AttachRolePolicy", parameters, parsed_globals)

        return create_role_response
コード例 #10
0
    def _run_main(self, parsed_args, parsed_globals):
        ec2_result = None
        emr_result = None
        self.iam = self._session.get_service('iam')
        self.iam_endpoint_url = parsed_args.iam_endpoint
        region = self._get_region(parsed_globals)

        self._check_for_iam_endpoint(region, self.iam_endpoint_url)
        self.emr_endpoint_url = \
            self._session.get_service('emr').get_endpoint(
                region_name=parsed_globals.region,
                endpoint_url=parsed_globals.endpoint_url,
                verify=parsed_globals.verify_ssl).host

        LOG.debug('elasticmapreduce endpoint used for resolving'
                  ' service principal: ' + self.emr_endpoint_url)

        # Check if the default EC2 Role for EMR exists.
        role_name = EC2_ROLE_NAME
        if self._check_if_role_exists(role_name, parsed_globals):
            LOG.debug('Role ' + role_name + ' exists.')
        else:
            LOG.debug('Role ' + role_name + ' does not exist.'
                      ' Creating default role ' + role_name)
            ec2_result = self._create_role_with_role_policy(
                role_name, role_name, constants.EC2,
                emrutils.dict_to_string(EC2_ROLE_POLICY), parsed_globals)

        # Check if the default EC2 Instance Profile for EMR exists.
        instance_profile_name = EC2_ROLE_NAME
        if self._check_if_instance_profile_exists(instance_profile_name,
                                                  parsed_globals):
            LOG.debug('Instance Profile ' + instance_profile_name + ' exists.')
        else:
            LOG.debug('Instance Profile ' + instance_profile_name +
                      'does not exist. Creating default Instance Profile ' +
                      instance_profile_name)
            self._create_instance_profile_with_role(instance_profile_name,
                                                    instance_profile_name,
                                                    parsed_globals)

        emrutils.display_response(
            self._session,
            self._session.get_service('iam').get_operation('CreateRole'),
            self._construct_result(ec2_result), parsed_globals)

        return 0
    def test_default_autoscaling_role_commercial(self, get_rp_patch,
                                                 role_exists_patch,
                                                 instance_profile_exists_patch,
                                                 construct_result_patch):
        get_rp_patch.return_value = False
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region us-east-1'

        self.run_cmd(cmdline, expected_rc=0)

        # Only 8 operations will be called as we are mocking
        # check_if_role_exists and check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 8)
        self.assertEqual(
            self.operations_called[6][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_autoscaling_role_policy_document))
コード例 #12
0
    def _create_role_with_role_policy(self, role_name, policy_name,
                                      service_name, policy_document,
                                      parsed_globals):
        service_principal = get_service_principal(service_name,
                                                  self.emr_endpoint_url)
        LOG.debug(service_principal)

        parameters = {'RoleName': role_name}
        _assume_role_policy = \
            emrutils.dict_to_string(assume_role_policy(service_principal))
        parameters['AssumeRolePolicyDocument'] = _assume_role_policy
        create_role_response = self._call_iam_operation(
            'CreateRole', parameters, parsed_globals)

        parameters = {}
        parameters['PolicyDocument'] = policy_document
        parameters['PolicyName'] = policy_name
        parameters['RoleName'] = role_name
        self._call_iam_operation('PutRolePolicy', parameters, parsed_globals)

        return create_role_response
コード例 #13
0
ファイル: createdefaultroles.py プロジェクト: monk-ee/aws-cli
    def _create_role_with_role_policy(
            self, role_name, policy_name, service_name, policy_document,
            parsed_globals):
        service_principal = get_service_principal(service_name,
                                                  self.emr_endpoint_url)
        LOG.debug(service_principal)

        parameters = {'RoleName': role_name}
        _assume_role_policy = \
            emrutils.dict_to_string(assume_role_policy(service_principal))
        parameters['AssumeRolePolicyDocument'] = _assume_role_policy
        create_role_response = self._call_iam_operation('CreateRole',
                                                        parameters,
                                                        parsed_globals)

        parameters = {}
        parameters['PolicyDocument'] = policy_document
        parameters['PolicyName'] = policy_name
        parameters['RoleName'] = role_name
        self._call_iam_operation('PutRolePolicy', parameters, parsed_globals)

        return create_role_response
    def test_default_roles_not_exist(self, get_rp_patch, role_exists_patch,
                                     instance_profile_exists_patch,
                                     construct_result_patch):
        get_rp_patch.return_value = False
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region cn-north-1'

        self.run_cmd(cmdline, expected_rc=0)

        # Only 8 operations will be called as we are mocking
        # check_if_role_exists and check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 8)

        self.assertEqual(self.operations_called[0][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[0][1]['RoleName'],
                         EC2_ROLE_NAME)
        self.assertEqual(
            self.operations_called[0][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.ec2_role_policy_document))

        self.assertEqual(self.operations_called[1][0].name, 'AttachRolePolicy')
        self.assertEqual(self.operations_called[1][1]['PolicyArn'],
                         CN_EC2_ROLE_ARN)
        self.assertEqual(self.operations_called[1][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[2][0].name,
                         'CreateInstanceProfile')
        self.assertEqual(self.operations_called[2][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[3][0].name,
                         'AddRoleToInstanceProfile')
        self.assertEqual(self.operations_called[3][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[3][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[4][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[4][1]['RoleName'],
                         EMR_ROLE_NAME)
        self.assertEqual(
            self.operations_called[4][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_role_policy_document))

        self.assertEqual(self.operations_called[5][0].name, 'AttachRolePolicy')
        self.assertEqual(self.operations_called[5][1]['PolicyArn'],
                         CN_EMR_ROLE_ARN)
        self.assertEqual(self.operations_called[5][1]['RoleName'],
                         EMR_ROLE_NAME)

        self.assertEqual(self.operations_called[6][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[6][1]['RoleName'],
                         EMR_AUTOSCALING_ROLE_NAME)
        self.assertEqual(
            self.operations_called[6][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(
                self.emr_autoscaling_role_policy_document_cn))

        self.assertEqual(self.operations_called[7][0].name, 'AttachRolePolicy')
        self.assertEqual(self.operations_called[7][1]['PolicyArn'],
                         CN_EMR_AUTOSCALING_ROLE_ARN)
        self.assertEqual(self.operations_called[7][1]['RoleName'],
                         EMR_AUTOSCALING_ROLE_NAME)
コード例 #15
0
    def _run_main_command(self, parsed_args, parsed_globals):
        ec2_result = None
        emr_result = None
        self.iam_endpoint_url = parsed_args.iam_endpoint

        self._check_for_iam_endpoint(self.region, self.iam_endpoint_url)
        self.emr_endpoint_url = \
            self._session.create_client(
                'emr',
                region_name=self.region,
                endpoint_url=parsed_globals.endpoint_url,
                verify=parsed_globals.verify_ssl).meta.endpoint_url

        LOG.debug('elasticmapreduce endpoint used for resolving'
                  ' service principal: ' + self.emr_endpoint_url)

        # Check if the default EC2 Role for EMR exists.
        role_name = EC2_ROLE_NAME
        if self._check_if_role_exists(role_name, parsed_globals):
            LOG.debug('Role ' + role_name + ' exists.')
        else:
            LOG.debug('Role ' + role_name + ' does not exist.'
                      ' Creating default role for EC2: ' + role_name)
            ec2_result = self._create_role_with_role_policy(
                role_name, role_name, EC2,
                emrutils.dict_to_string(EC2_ROLE_POLICY),
                parsed_globals)

        # Check if the default EC2 Instance Profile for EMR exists.
        instance_profile_name = EC2_ROLE_NAME
        if self._check_if_instance_profile_exists(instance_profile_name,
                                                  parsed_globals):
            LOG.debug('Instance Profile ' + instance_profile_name + ' exists.')
        else:
            LOG.debug('Instance Profile ' + instance_profile_name +
                      'does not exist. Creating default Instance Profile ' +
                      instance_profile_name)
            self._create_instance_profile_with_role(instance_profile_name,
                                                    instance_profile_name,
                                                    parsed_globals)

        # Check if the default EMR Role exists.
        role_name = EMR_ROLE_NAME
        if self._check_if_role_exists(role_name, parsed_globals):
            LOG.debug('Role ' + role_name + ' exists.')
        else:
            LOG.debug('Role ' + role_name + ' does not exist.'
                      ' Creating default role for EMR: ' + role_name)
            emr_result = self._create_role_with_role_policy(
                role_name, role_name, EMR,
                emrutils.dict_to_string(EMR_ROLE_POLICY),
                parsed_globals)

        configutils.update_roles(self._session)

        emrutils.display_response(
            self._session,
            'create_role',
            self._construct_result(ec2_result, emr_result),
            parsed_globals)

        return 0
コード例 #16
0
    def test_default_roles_not_exist(self, get_rp_patch,
                                     role_exists_patch,
                                     instance_profile_exists_patch,
                                     construct_result_patch):
        get_rp_patch.return_value = False
        role_exists_patch.return_value = False
        instance_profile_exists_patch.return_value = False
        construct_result_patch.return_value = []

        cmdline = self.prefix + ' --region cn-north-1'

        self.run_cmd(cmdline, expected_rc=0)

        # Only 8 operations will be called as we are mocking
        # check_if_role_exists and check_if_instance_profile_exists methods.
        self.assertEqual(len(self.operations_called), 8)

        self.assertEqual(self.operations_called[0][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[0][1]['RoleName'],
                         EC2_ROLE_NAME)
        self.assertEqual(
            self.operations_called[0][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.ec2_role_policy_document))

        self.assertEqual(self.operations_called[1][0].name,
                         'AttachRolePolicy')
        self.assertEqual(self.operations_called[1][1]['PolicyArn'],
                         CN_EC2_ROLE_ARN)
        self.assertEqual(self.operations_called[1][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[2][0].name,
                         'CreateInstanceProfile')
        self.assertEqual(self.operations_called[2][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[3][0].name,
                         'AddRoleToInstanceProfile')
        self.assertEqual(self.operations_called[3][1]['InstanceProfileName'],
                         EC2_ROLE_NAME)
        self.assertEqual(self.operations_called[3][1]['RoleName'],
                         EC2_ROLE_NAME)

        self.assertEqual(self.operations_called[4][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[4][1]['RoleName'],
                         EMR_ROLE_NAME)
        self.assertEqual(
            self.operations_called[4][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_role_policy_document))

        self.assertEqual(self.operations_called[5][0].name, 'AttachRolePolicy')
        self.assertEqual(self.operations_called[5][1]['PolicyArn'],
                         CN_EMR_ROLE_ARN)
        self.assertEqual(self.operations_called[5][1]['RoleName'],
                         EMR_ROLE_NAME)

        self.assertEqual(self.operations_called[6][0].name, 'CreateRole')
        self.assertEqual(self.operations_called[6][1]['RoleName'],
                         EMR_AUTOSCALING_ROLE_NAME)
        self.assertEqual(
            self.operations_called[6][1]['AssumeRolePolicyDocument'],
            emrutils.dict_to_string(self.emr_autoscaling_role_policy_document))

        self.assertEqual(self.operations_called[7][0].name, 'AttachRolePolicy')
        self.assertEqual(self.operations_called[7][1]['PolicyArn'],
                         CN_EMR_AUTOSCALING_ROLE_ARN)
        self.assertEqual(self.operations_called[7][1]['RoleName'],
                         EMR_AUTOSCALING_ROLE_NAME)