/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.polaris.test.rustfs;

import java.net.URI;
import java.util.Map;
import java.util.Optional;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;

/**
 * Provides access to Rustfs via a preconfigured S3 client and providing the by default randomized
 * bucket and access/secret keys.
 *
 * <p>Annotate JUnit test instance or static fields or method parameters of this type with {@link
 * Rustfs}.
 */
// CODE_COPIED_TO_POLARIS from Project Nessie 0.104.2
public interface RustfsAccess {

  /** Host and port, separated by '{@code :}'. */
  String hostPort();

  String accessKey();

  String secretKey();

  String bucket();

  Optional<String> region();

  /** HTTP protocol endpoint. */
  String s3endpoint();

  S3Client s3Client();

  /** Properties needed by Apache Iceberg to access this instance. */
  Map<String, String> icebergProperties();

  /** Properties needed by Apache Hadoop to access this instance. */
  Map<String, String> hadoopConfig();

  /** S3 scheme URI including the bucket to access the given path. */
  URI s3BucketUri(String path);

  /** Convenience method to put an object into S3. */
  @SuppressWarnings("resource")
  default void s3put(String key, RequestBody body) {
    s3Client().putObject(b -> b.bucket(bucket()).key(key), body);
  }
}
