def main(): # setup logging for direct command-line use global logger FORMAT = "[%(asctime)s %(levelname)s] %(message)s" logging.basicConfig(level=logging.INFO, format=FORMAT) logger = logging.getLogger() # suppress boto3 internal logging below WARNING level boto3_log = logging.getLogger("boto3") boto3_log.setLevel(logging.WARNING) boto3_log.propagate = True # suppress botocore internal logging below WARNING level botocore_log = logging.getLogger("botocore") botocore_log.setLevel(logging.WARNING) botocore_log.propagate = True p = argparse.ArgumentParser( description='Tool to generate custodian config files ' 'from a c7n configuration directory/repo.', epilog='This tool is part of manheim_c7n_tools v%s.\n' 'For documentation, see: %s' % (VERSION, PROJECT_URL), formatter_class=argparse.RawDescriptionHelpFormatter ) p.add_argument('-V', '--version', action='version', version=VERSION) p.add_argument('-c', '--config', dest='config', action='store', default='manheim-c7n-tools.yml', help='Config file path (default: ./manheim-c7n-tools.yml)') p.add_argument('ACCT_NAME', action='store', type=str, help='account_name value from config file, for ' 'current account') args = p.parse_args(sys.argv[1:]) conf = ManheimConfig.from_file(args.config, args.ACCT_NAME) PolicyGen(conf).run()
def main(): global logger # setup logging for direct command-line use FORMAT = "[%(asctime)s %(levelname)s] %(message)s" logging.basicConfig(level=logging.WARNING, format=FORMAT) logger = logging.getLogger() # suppress boto3 internal logging below WARNING level boto3_log = logging.getLogger("boto3") boto3_log.setLevel(logging.WARNING) boto3_log.propagate = True # suppress botocore internal logging below WARNING level botocore_log = logging.getLogger("botocore") botocore_log.setLevel(logging.WARNING) botocore_log.propagate = True # end setup logging args = parse_args(sys.argv[1:]) # set logging level if args.verbose > 1: set_log_debug(logger) elif args.verbose == 1: set_log_info(logger) conf = ManheimConfig.from_file(args.config, args.ACCOUNT_NAME) DryRunDiffer(conf).run(git_dir=args.git_dir, diff_against=args.diff_against)
def test_from_file_name_missing(self): m_conf = Mock() with patch('%s.logger' % pbm, autospec=True) as mock_logger: with patch( '%s.open' % pbm, mock_open(read_data='foo'), create=True ) as m_open: with patch('%s.yaml.load' % pbm, autospec=True) as mock_load: with patch( '%s.ManheimConfig' % pbm, autospec=True ) as mock_conf: mock_conf.return_value = m_conf mock_load.return_value = [ { 'account_name': 'a1', 'foo': 'bar', 'baz': 2, 'regions': ['us-east-1'] }, { 'account_name': 'a2', 'foo': 'bar1', 'baz': 4, 'regions': ['us-east-2'] } ] with pytest.raises(RuntimeError) as exc: ManheimConfig.from_file('/tmp/conf.yml', 'BAD') assert str(exc.value) == 'ERROR: No account with name "BAD"' \ ' in /tmp/conf.yml' assert mock_logger.mock_calls == [ call.info('Loading config from: %s', '/tmp/conf.yml') ] assert m_open.mock_calls == [ call('/tmp/conf.yml', 'r'), call().__enter__(), call().read(), call().__exit__(None, None, None) ] assert mock_load.mock_calls == [ call('foo', Loader=yaml.SafeLoader) ] assert mock_conf.mock_calls == []
def __init__(self, account_name, config_path='manheim-c7n-tools.yml'): """ Initialize the Runner. :param account_name: name of the account to run against :type account_name: str :param config_path: path to ``manheim-c7n-tools.yml`` config file :type config_path: str """ self._config_path = config_path self.config = ManheimConfig.from_file(config_path, account_name)
def main(): args = parse_args(sys.argv[1:]) # set logging level if args.verbose > 1: set_log_debug(logger) elif args.verbose == 1: set_log_info(logger) conf = ManheimConfig.from_file(args.config, args.ACCOUNT_NAME) if args.assume_role: assume_role(conf) CustodianErrorReporter(conf, args.REGION_NAME).run()
def test_from_file(self): m_conf = Mock() with patch('%s.logger' % pbm, autospec=True) as mock_logger: with patch( '%s.open' % pbm, mock_open(read_data='foo'), create=True ) as m_open: with patch('%s.yaml.load' % pbm, autospec=True) as mock_load: with patch( '%s.ManheimConfig' % pbm, autospec=True ) as mock_conf: mock_conf.return_value = m_conf mock_load.return_value = [ { 'account_name': 'a1', 'foo': 'bar', 'baz': 2, 'regions': ['us-east-1'] }, { 'account_name': 'a2', 'foo': 'bar1', 'baz': 4, 'regions': ['us-east-2'] } ] res = ManheimConfig.from_file('/tmp/conf.yml', 'a2') assert res == m_conf assert mock_logger.mock_calls == [ call.info('Loading config from: %s', '/tmp/conf.yml') ] assert m_open.mock_calls == [ call('/tmp/conf.yml', 'r'), call().__enter__(), call().read(), call().__exit__(None, None, None) ] assert mock_load.mock_calls == [ call('foo', Loader=yaml.SafeLoader) ] assert mock_conf.mock_calls == [ call( account_name='a2', foo='bar1', baz=4, regions=['us-east-2'], config_path='/tmp/conf.yml' ) ]