Skip to content

Commit

Permalink
wip 3
Browse files Browse the repository at this point in the history
  • Loading branch information
j-lanson committed Aug 12, 2024
1 parent e1c7be4 commit 6feaed3
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions hipcheck/src/plugin/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::hipcheck::plugin_client::PluginClient;
use crate::hipcheck::{Schema as PluginSchema, Empty};
use crate::hipcheck::{ConfigurationStatus, Configuration, ConfigurationResult as PluginConfigResult, Schema as PluginSchema, Empty};
use std::process::Child;
use tonic::transport::Channel;
use std::ops::Not;
use crate::{Result, hc_error};
use serde_json::Value;

Expand All @@ -13,6 +14,7 @@ pub struct Plugin {
pub entrypoint: String,
}

// Hipcheck-facing version of struct from crate::hipcheck
pub struct Schema {
pub query_name: String,
pub key_schema: Value,
Expand All @@ -31,12 +33,74 @@ impl TryFrom<PluginSchema> for Schema {
}
}

// Hipcheck-facing version of struct from crate::hipcheck
pub struct ConfigurationResult {
pub status: ConfigurationStatus,
pub message: Option<String>
}
impl TryFrom<PluginConfigResult> for ConfigurationResult {
type Error = crate::error::Error;
fn try_from(value: PluginConfigResult) -> Result<Self> {
let status: ConfigurationStatus = value.status.try_into()?;
let message = value.message.is_empty().not().then(|| value.message);
Ok(ConfigurationResult {
status,
message
})
}
}
// hipcheck::ConfigurationStatus has an enum that captures both error and success
// scenarios. The below code allows interpreting the struct as a Rust Result. If
// the success variant was the status, Ok(()) is returned, otherwise the code
// is stuffed into a custom error type enum that equals the protoc-generated one
// minus the success variant.
impl ConfigurationResult {
pub fn as_result(&self) -> std::result::Result<(), ConfigError> {
let Ok(error) = self.status.try_into() else {
return Ok(());
};
Err(ConfigError::new(error, self.message.clone()))
}
}
pub enum ConfigErrorType {
Unknown = 0,
MissingRequiredConfig = 2,
UnrecognizedConfig = 3,
InvalidConfigValue = 4,
}
impl TryFrom<ConfigurationStatus> for ConfigErrorType {
type Error = crate::error::Error;
fn try_from(value: ConfigurationStatus) -> Result<Self> {
use ConfigurationStatus::*;
use ConfigErrorType::*;
Ok(match value as i32 {
x if x == ErrorUnknown as i32 => Unknown,
x if x == ErrorMissingRequiredConfiguration as i32 => MissingRequiredConfig,
x if x == ErrorUnrecognizedConfiguration as i32 => UnrecognizedConfig,
x if x == ErrorInvalidConfigurationValue as i32 => InvalidConfigValue,
_ => { return Err(hc_error!("status value is not an error")); }
})
}
}
pub struct ConfigError {
error: ConfigErrorType,
message: Option<String>
}
impl ConfigError {
pub fn new(error: ConfigErrorType, message: Option<String>) -> Self {
ConfigError { error, message }
}
}

// State for managing an actively running plugin process
pub struct PluginContext {
pub plugin: Plugin,
pub port: u16,
pub grpc: HcPluginClient,
pub proc: Child,
}
// Redefinition of `grpc` field's functions with more useful types, additional
// error & sanity checking
impl PluginContext {
pub async fn get_query_schemas(&mut self) -> Result<Vec<Schema>> {
let mut res = self.grpc.get_query_schemas(Empty {}).await?;
Expand All @@ -57,7 +121,21 @@ impl PluginContext {
}
}
Ok(schemas)
}
}
pub async fn set_configuration(&mut self, conf: &Value) -> Result<ConfigurationResult> {
let conf_query = Configuration {
configuration: serde_json::to_string(&conf)?,
};
let res = self.grpc.set_configuration(conf_query).await?;
res.into_inner().try_into()
}
// TODO - the String in the result should be replaced with a structured
// type once the policy expression code is integrated
pub async fn get_default_policy_expression(&mut self) -> Result<String> {
let mut res = self.grpc.get_default_policy_expression(Empty {}).await?;
Ok(res.get_ref().policy_expression.to_owned())
}
pub async fn setup_
}
impl Drop for PluginContext {
fn drop(&mut self) {
Expand Down

0 comments on commit 6feaed3

Please sign in to comment.