def test_aws_create_report_finalize_report_overwrite(self): """Test that an aws report file has an invoice id.""" start_date = datetime.datetime.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) end_date = datetime.datetime.now().replace(day=5, hour=0, minute=0, second=0, microsecond=0) aws_create_report({ 'start_date': start_date, 'end_date': end_date, 'aws_report_name': 'cur_report', 'aws_finalize_report': 'overwrite' }) month_output_file_name = '{}-{}-{}'.format( calendar.month_name[start_date.month], start_date.year, 'cur_report') expected_month_output_file = '{}/{}.csv'.format( os.getcwd(), month_output_file_name) self.assertTrue(os.path.isfile(expected_month_output_file)) with open(expected_month_output_file, 'r') as f: reader = csv.DictReader(f) row = next(reader) self.assertNotEqual(row['bill/InvoiceId'], '') os.remove(expected_month_output_file)
def run(provider_type, options): """Run nise.""" LOG.info("Creating reports...") if provider_type == "aws": aws_create_report(options) elif provider_type == "azure": azure_create_report(options) elif provider_type == "ocp": ocp_create_report(options) elif provider_type == "gcp": gcp_create_report(options)
def main(): """Run data generation program.""" parser = create_parser() args = parser.parse_args() options = vars(args) _load_static_report_data(options) _, provider_type = _validate_provider_inputs(parser, options) if not options.get('start_date'): parser.error('the following arguments are required: --start-date') if provider_type == 'aws': aws_create_report(options) elif provider_type == 'ocp': ocp_create_report(options)
def generate(self): """Create data and upload it to the necessary location.""" options = { "start_date": self.start_date, "end_date": self.end_date, "aws_bucket_name": self.s3_bucket, "aws_prefix_name": self.report_prefix, "aws_report_name": self.report_name, } if self.static_file: static_file_data = Source.obtain_static_file_data( self.static_file, self.start_date, self.end_date ) options["static_report_data"] = static_file_data aws_create_report(options)
def run(provider_type, options): """Run nise.""" static_data_bool = _load_static_report_data(options) if not options.get("start_date"): raise NiseError("'start_date' is required in static files.") if not static_data_bool: fix_dates(options, provider_type) LOG.info("Creating reports...") if provider_type == "aws": aws_create_report(options) elif provider_type == "azure": azure_create_report(options) elif provider_type == "ocp": ocp_create_report(options) elif provider_type == "gcp": gcp_create_report(options)
def test_aws_create_report_no_s3(self): """Test the aws report creation method no s3.""" now = datetime.datetime.now().replace(microsecond=0, second=0, minute=0, hour=0) one_day = datetime.timedelta(days=1) yesterday = now - one_day aws_create_report({ 'start_date': yesterday, 'end_date': now, 'aws_report_name': 'cur_report' }) month_output_file_name = '{}-{}-{}'.format( calendar.month_name[now.month], now.year, 'cur_report') expected_month_output_file = '{}/{}.csv'.format( os.getcwd(), month_output_file_name) self.assertTrue(os.path.isfile(expected_month_output_file)) os.remove(expected_month_output_file)
def test_aws_create_report_with_s3(self, mock_upload_to_s3): """Test the aws report creation method with s3.""" mock_upload_to_s3.return_value = None now = datetime.datetime.now().replace(microsecond=0, second=0, minute=0, hour=0) one_day = datetime.timedelta(days=1) yesterday = now - one_day options = { 'start_date': yesterday, 'end_date': now, 'aws_bucket_name': 'my_bucket', 'aws_report_name': 'cur_report' } aws_create_report(options) month_output_file_name = '{}-{}-{}'.format( calendar.month_name[now.month], now.year, 'cur_report') expected_month_output_file = '{}/{}.csv'.format( os.getcwd(), month_output_file_name) self.assertTrue(os.path.isfile(expected_month_output_file)) os.remove(expected_month_output_file)
def test_aws_create_report_with_local_dir(self): """Test the aws report creation method with local directory.""" now = datetime.datetime.now().replace(microsecond=0, second=0, minute=0, hour=0) one_day = datetime.timedelta(days=1) yesterday = now - one_day local_bucket_path = mkdtemp() options = { 'start_date': yesterday, 'end_date': now, 'aws_bucket_name': local_bucket_path, 'aws_report_name': 'cur_report' } aws_create_report(options) month_output_file_name = '{}-{}-{}'.format( calendar.month_name[now.month], now.year, 'cur_report') expected_month_output_file = '{}/{}.csv'.format( os.getcwd(), month_output_file_name) self.assertTrue(os.path.isfile(expected_month_output_file)) os.remove(expected_month_output_file) shutil.rmtree(local_bucket_path)
def test_aws_create_report_with_local_dir_static_generation_dates(self): """Test the aws report creation method with local directory and static generation with dates.""" now = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) one_day = datetime.timedelta(days=1) yesterday = now - one_day local_bucket_path = mkdtemp() static_aws_data = { 'generators': [{ 'EC2Generator': { 'start_date': str(now), 'end_date': str(now) } }], 'accounts': { 'payer': 9999999999999, 'user': [9999999999999] } } options = { 'start_date': yesterday, 'end_date': now, 'aws_bucket_name': local_bucket_path, 'aws_report_name': 'cur_report', 'static_report_data': static_aws_data } aws_create_report(options) month_output_file_name = '{}-{}-{}'.format( calendar.month_name[now.month], now.year, 'cur_report') expected_month_output_file = '{}/{}.csv'.format( os.getcwd(), month_output_file_name) self.assertTrue(os.path.isfile(expected_month_output_file)) os.remove(expected_month_output_file) shutil.rmtree(local_bucket_path)
def test_aws_create_report_with_local_dir_static_generation(self): """Test the aws report creation method with local directory and static generation.""" now = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) one_day = datetime.timedelta(days=1) yesterday = now - one_day local_bucket_path = mkdtemp() static_aws_data = { 'generators': [{ 'EC2Generator': { 'start_date': str(yesterday.date()), 'end_date': str(now.date()), 'processor_arch': '32-bit', 'resource_id': 55555555, 'product_sku': 'VEAJHRNKTJZQ', 'region': 'us-east-1a', 'tags': { 'resourceTags/user:environment': 'dev', 'resourceTags/user:version': 'alpha' }, 'instance_type': { 'inst_type': 'm5.large', 'vcpu': 2, 'memory': '8 GiB', 'storage': 'EBS Only', 'family': 'General Purpose', 'cost': 1.0, 'rate': 0.5 } } }, { 'S3Generator': { 'start_date': str(yesterday.date()), 'end_date': str(now.date()), 'product_sku': 'VEAJHRNAAAAA', 'amount': 10, 'rate': 3 } }, { 'EBSGenerator': { 'start_date': str(yesterday.date()), 'end_date': str(now.date()), 'product_sku': 'VEAJHRNBBBBB', 'amount': 10, 'rate': 3, 'resource_id': 12345678 } }, { 'DataTransferGenerator': { 'start_date': str(yesterday.date()), 'end_date': str(now.date()), 'product_sku': 'VEAJHRNCCCCC', 'amount': 10, 'rate': 3 } }], 'accounts': { 'payer': 9999999999999, 'user': [9999999999999] } } options = { 'start_date': yesterday, 'end_date': now, 'aws_bucket_name': local_bucket_path, 'aws_report_name': 'cur_report', 'static_report_data': static_aws_data } aws_create_report(options) month_output_file_name = '{}-{}-{}'.format( calendar.month_name[now.month], now.year, 'cur_report') expected_month_output_file = '{}/{}.csv'.format( os.getcwd(), month_output_file_name) self.assertTrue(os.path.isfile(expected_month_output_file)) os.remove(expected_month_output_file) shutil.rmtree(local_bucket_path)