"""Test utils for rule generator scanners."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import yaml

from deploy.rule_generator import project_config

_OVERALL_DICT = {
    'domain': 'domain.com',
    'organization_id': '246801357924',
    'folder_id': '357801357924',
    'billing_account': '<KEY>',
    'allowed_apis': ['compute.googleapis.com', 'storage.googleapis.com'],
}

_FORSETI_DICT = {
    'project': {
        'project_id': 'forseti-project',
        'owners_group': '<EMAIL>',
        'auditors_group': '<EMAIL>',
        'data_readwrite_groups': ['<EMAIL>'],
        'data_readonly_groups': ['<EMAIL>'],
        'generated_fields': {
            'project_number':
                9999,
            'log_sink_service_account': (
                '<EMAIL>'),
        },
    },
    'generated_fields': {
        'service_account': '<EMAIL>',
        'server_bucket': 'gs://forseti-project-server/',
    }
}

_PROJECT_YAML = """
projects:
- project_id: %(project_id)s
  owners_group: %(project_id)s-<EMAIL>
  auditors_group: %(<EMAIL>)<EMAIL>
  data_readwrite_groups:
  - %(project_id)<EMAIL>
  data_readonly_groups:
  - %(project_id)<EMAIL>
  - %(<EMAIL>)<EMAIL>
  generated_fields:
    project_number: %(project_num)s
    log_sink_service_account: <EMAIL>
    gce_instance_info:
    - name: 'instance'
      id: '123'
  data_buckets:
  - name_suffix: '-bucket'
    location: US-CENTRAL1
    storage_class: REGIONAL
  gce_instances:
  - name: 'instance'
    zone: 'us-central1-f'
    machine_type: 'n1-standard-1'
    existing_boot_image: 'projects/debian-cloud/global/images/family/debian-9'
    start_vm: true
  audit_logs:
    logs_gcs_bucket:
      location: US
      storage_class: MULTI_REGIONAL
      ttl_days: 365
    logs_bigquery_dataset:
      location: US
"""


def create_test_global_config():
  """Creates a global config dictionary for tests."""
  return _OVERALL_DICT.copy()


def create_test_project(project_id, project_num, extra_fields=None,
                        audit_logs_project=None):
  """Create a test project with optional extra project_config fields."""
  config_dict = yaml.load(_PROJECT_YAML % {
      'project_id': project_id, 'project_num': project_num})
  project = config_dict['projects'][0]
  if extra_fields:
    project.update(extra_fields)
  return project_config.ProjectConfig(
      project=project,
      audit_logs_project=audit_logs_project,
      forseti=_FORSETI_DICT)


def create_test_projects(num_projects):
  """Create a list of test projects."""
  return [
      create_test_project(project_id='project-%s' % i, project_num=1000000 + i)
      for i in range(num_projects)
  ]
