Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: IConfiguration definition is base #351

Merged
merged 5 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/cpp/ArmoniK.Api.Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ find_package(Threads)

include(FetchContent)

FetchContent_Declare(
simdjson
GIT_REPOSITORY https://github.com/simdjson/simdjson.git
GIT_SHALLOW TRUE
GIT_TAG tags/v3.2.1
)
FETCHCONTENT_DECLARE(
simdjson
URL https://github.com/simdjson/simdjson/archive/refs/tags/v3.2.1.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
)

FetchContent_GetProperties(simdjson)
if(NOT simdjson_POPULATED)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <sstream>
#include <utils/IConfiguration.h>

/**
* @brief The armonik namespace contains classes and functions related to the Armonik API.
Expand Down
42 changes: 42 additions & 0 deletions packages/cpp/ArmoniK.Api.Common/header/options/ControlPlane.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef ARMONIK_API_CONTROLPLANE_H
#define ARMONIK_API_CONTROLPLANE_H

#include "utils/IConfiguration.h"

namespace armonik::api::common::options {
class ControlPlane {
public:
ControlPlane(const utils::IConfiguration &config) {
endpoint_ = config.get(EndpointKey);
user_cert_pem_path_ = config.get(UserCertKey);
user_key_pem_path_ = config.get(UserKeyKey);
user_p12_path_ = config.get(UserP12Key);
ca_cert_perm_path_ = config.get(CaCertKey);
sslValidation_ = config.get(SSLValidationKey) != "disable";
}

[[nodiscard]] std::string_view getEndpoint() const { return endpoint_; }
[[nodiscard]] std::string_view getUserCertPemPath() const { return user_cert_pem_path_; }
[[nodiscard]] std::string_view getUserKeyPemPath() const { return user_key_pem_path_; }
[[nodiscard]] std::string_view getUserP12Path() const { return user_p12_path_; }
[[nodiscard]] std::string_view getCaCertPermPath() const { return ca_cert_perm_path_; }
[[nodiscard]] bool isSslValidation() const { return sslValidation_; }

static constexpr char EndpointKey[] = "Grpc__EndPoint";
static constexpr char UserCertKey[] = "Grpc__ClientCert";
static constexpr char UserKeyKey[] = "Grpc__ClientKey";
static constexpr char UserP12Key[] = "Grpc__ClientP12";
static constexpr char CaCertKey[] = "Grpc__CaCert";
static constexpr char SSLValidationKey[] = "Grpc__SSLValidation";

private:
std::string endpoint_;
std::string user_cert_pem_path_;
std::string user_key_pem_path_;
std::string user_p12_path_;
std::string ca_cert_perm_path_;
bool sslValidation_;
};
} // namespace armonik::api::common::options

#endif // ARMONIK_API_CONTROLPLANE_H
30 changes: 3 additions & 27 deletions packages/cpp/ArmoniK.Api.Common/header/utils/EnvConfiguration.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

/**
* @file EnvConfiguration.h
* @brief Header file for the EnvConfiguration class
Expand All @@ -15,32 +17,6 @@ class EnvConfiguration : public IConfiguration {
/**
* @brief Default constructor
*/
EnvConfiguration() = default;

/**
* @brief Gets the value of an environment variable
* @param string The name of the environment variable
* @return The value of the environment variable, or an empty string if not found
*/
[[nodiscard]] std::string get(const std::string &string) const override {
std::string value = std::getenv(string.c_str());
if (!value.empty()) {
return value;
}
throw std::runtime_error("Can't get server address !");
}

/**
* @brief Sets the value of an environment variable
* @param string The name of the environment variable
* @param value The value to set
*/
void set(const std::string &string, const std::string &value) override {}

/**
* @brief Copies the values of another IConfiguration object into this one
* @param other The IConfiguration object to copy from
*/
void set(const IConfiguration &other) override {}
EnvConfiguration() { add_env_configuration(); }
};
} // namespace armonik::api::common::utils
167 changes: 95 additions & 72 deletions packages/cpp/ArmoniK.Api.Common/header/utils/IConfiguration.h
Original file line number Diff line number Diff line change
@@ -1,72 +1,95 @@
/**
* @file IConfiguration.h
* @brief Interface for a configuration class that stores and manages key-value pairs.
*/

#pragma once

#include <memory>
#include <string>
#include <unordered_map>

namespace armonik::api::common::options {
class ComputePlane;
}

namespace armonik::api::common::utils {
/**
* @class IConfiguration
* @brief Interface for a configuration class that stores and manages key-value pairs.
*/
class IConfiguration {
public:
/**
* @brief Default constructor.
*/
IConfiguration() = default;

/**
* @brief Default virtual destructor.
*/
virtual ~IConfiguration() = default;

/**
* @brief Get the value associated with the given key.
* @param string Key to look up.
* @return The value associated with the key, as a string.
*/
[[nodiscard]] virtual std::string get(const std::string &string) const = 0;

/**
* @brief Set the value associated with the given key.
* @param string Key to set the value for.
* @param value Value to set for the key.
*/
virtual void set(const std::string &string, const std::string &value) = 0;

/**
* @brief Set the values from another IConfiguration object.
* @param other IConfiguration object to copy values from.
*/
virtual void set(const IConfiguration &other) = 0;

/**
* @brief Add JSON configuration from a file.
* @param file_path Path to the JSON file.
* @return Reference to the current IConfiguration object.
*/
IConfiguration &add_json_configuration(const std::string &file_path);

/**
* @brief Add environment variable configuration.
* @return Reference to the current IConfiguration object.
*/
IConfiguration &add_env_configuration();

/**
* @brief Get the current ComputePlane configuration.
* @return A ComputePlane object representing the current configuration.
*/
options::ComputePlane get_compute_plane();
};
} // namespace armonik::api::common::utils
/**
* @file IConfiguration.h
* @brief Interface for a configuration class that stores and manages key-value pairs.
*/

#pragma once

#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

namespace armonik::api::common::options {
class ComputePlane;
class ControlPlane;
} // namespace armonik::api::common::options

namespace armonik::api::common::utils {
/**
* @class IConfiguration
* @brief Interface for a configuration class that stores and manages key-value pairs.
*/
class IConfiguration {
public:
/**
* @brief Default constructor.
*/
IConfiguration() = default;

/**
* @brief Default virtual destructor.
*/
virtual ~IConfiguration() = default;

/**
* @brief Get the value associated with the given key.
* @param string Key to look up.
* @return The value associated with the key, as a string.
*/
[[nodiscard]] std::string get(const std::string &string) const;

/**
* @brief Set the value associated with the given key.
* @param string Key to set the value for.
* @param value Value to set for the key.
*/
void set(const std::string &string, const std::string &value);

/**
* @brief Set the values from another IConfiguration object.
* @param other IConfiguration object to copy values from.
*/
void set(const IConfiguration &other);

/**
* @brief List defined values of this configuration.
* @note Does not include environment variables
*/
[[nodiscard]] const std::map<std::string, std::string> &list() const;

/**
* @brief Add JSON configuration from a file.
* @param file_path Path to the JSON file.
* @return Reference to the current IConfiguration object.
*/
IConfiguration &add_json_configuration(std::string_view file_path);

/**
* @brief Add environment variable configuration.
* @return Reference to the current IConfiguration object.
*/
IConfiguration &add_env_configuration();

/**
* @brief Get the current ComputePlane configuration.
* @return A ComputePlane object representing the current configuration.
*/
options::ComputePlane get_compute_plane();

/**
* @brief Get the current ControlPlane configuration
* @return A ControlPlane object
*/
options::ControlPlane get_control_plane();

private:
/**
* @brief Storage for the key-value pairs.
*/
std::map<std::string, std::string> options_;
std::set<std::string> above_env_keys_;
bool use_environment_ = false;
};
} // namespace armonik::api::common::utils
27 changes: 2 additions & 25 deletions packages/cpp/ArmoniK.Api.Common/header/utils/JsonConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace armonik::api::common::utils {
*/
class JsonConfiguration : public IConfiguration {
private:
/**
* Parsed values
*/
std::unordered_map<std::string, std::string> values;

JsonConfiguration() = default;

public:
Expand All @@ -26,26 +21,8 @@ class JsonConfiguration : public IConfiguration {
*/
explicit JsonConfiguration(const std::string &filepath);

static void fromPath(IConfiguration &config, std::string_view filepath);
static JsonConfiguration fromString(const std::string &json_string);

/**
* @brief Get the value associated with the given key.
* @param string Key to look up.
* @return The value associated with the key, as a string.
*/
[[nodiscard]] std::string get(const std::string &string) const override { return values.at(string); }

/**
* @brief Set the value associated with the given key.
* @param string Key to set the value for.
* @param value Value to set for the key.
*/
void set(const std::string &string, const std::string &value) override { values.insert_or_assign(string, value); }

/**
* @brief Set the values from another IConfiguration object.
* @param other IConfiguration object to copy values from.
*/
void set(const IConfiguration &other) override {}
static void fromString(IConfiguration &config, const std::string &json_string);
};
} // namespace armonik::api::common::utils
51 changes: 0 additions & 51 deletions packages/cpp/ArmoniK.Api.Common/header/utils/RootConfiguration.h

This file was deleted.

Loading