def get_or_create_infra(base_name, plan, environment, retry_from=None):
    if retry_from:
        infra = retry_from.infra
        base_name['infra'] = infra.name
        base_name['name_prefix'] = infra.name_prefix
        base_name['name_stamp'] = infra.name_stamp
    else:
        infra = DatabaseInfra()
        infra.name = base_name['infra']
        infra.name_prefix = base_name['name_prefix']
        infra.name_stamp = base_name['name_stamp']
        infra.last_vm_created = 0
        infra.engine = plan.engine
        infra.plan = plan
        infra.disk_offering = plan.disk_offering
        infra.environment = environment
        infra.capacity = 1
        infra.per_database_size_mbytes = plan.max_db_size
        infra.save()

        driver = infra.get_driver()
        user, password, key = driver.build_new_infra_auth()
        infra.user = user
        infra.password = password
        infra.database_key = key
        infra.save()

    return infra
def get_or_create_infra(base_name,
                        plan,
                        environment,
                        backup_hour=None,
                        maintenance_window=None,
                        maintenance_day=None,
                        retry_from=None,
                        pool=None):
    if retry_from:
        infra = retry_from.infra
        base_name['infra'] = infra.name
        base_name['name_prefix'] = infra.name_prefix
        base_name['name_stamp'] = infra.name_stamp
    else:
        random_backup_hour, random_maintenance_hour, random_maintenance_day = (
            DatabaseForm.randomize_backup_and_maintenance_hour())
        infra = DatabaseInfra()
        infra.name = base_name['infra']
        infra.name_prefix = base_name['name_prefix']
        infra.name_stamp = base_name['name_stamp']
        infra.last_vm_created = 0
        infra.engine = plan.engine
        infra.plan = plan
        infra.disk_offering = plan.disk_offering
        infra.disk_offering_type = plan.disk_offering_type
        infra.environment = environment
        infra.capacity = 1
        infra.per_database_size_mbytes = plan.max_db_size
        infra.backup_hour = backup_hour or random_backup_hour
        infra.maintenance_window = (maintenance_window
                                    or random_maintenance_hour)
        infra.maintenance_day = maintenance_day or random_maintenance_day
        infra.engine_patch = plan.engine.default_engine_patch
        infra.pool = pool
        infra.save()

        driver = infra.get_driver()
        user, password, key = driver.build_new_infra_auth()
        infra.user = user
        infra.password = password
        infra.database_key = key
        infra.save()

    return infra