Implement class data source

This commit is contained in:
nora 2024-04-21 13:30:25 +02:00
parent 85d10ed893
commit 3c891034ee
7 changed files with 144 additions and 19 deletions

View file

@ -0,0 +1,92 @@
use std::collections::HashMap;
use eyre::Context;
use terustform::{
datasource::DataSource, AttrPath, Attribute, DResult, EyreExt, Mode, Schema, StringValue,
Value, ValueModel,
};
use crate::client::CorsClient;
pub struct ClassDataSource {
client: CorsClient,
}
#[derive(terustform::Model)]
struct ClassDataSourceModel {
id: StringValue,
name: StringValue,
description: StringValue,
discord_id: StringValue,
}
#[terustform::async_trait]
impl DataSource for ClassDataSource {
type ProviderData = CorsClient;
async fn read(&self, config: Value) -> DResult<Value> {
let mut model = ClassDataSourceModel::from_value(config, &AttrPath::root())?;
let class = self
.client
.get_class(&model.id.expect_known(AttrPath::attr("id"))?)
.await
.wrap_err("failed to get class")
.eyre_to_tf()?;
model.name = StringValue::Known(class.name);
model.description = StringValue::Known(class.description);
model.discord_id = StringValue::from(class.discord_id);
Ok(model.to_value())
}
fn name(provider_name: &str) -> String {
format!("{provider_name}_class")
}
fn schema() -> Schema {
Schema {
description: "Get a class by name".to_owned(),
attributes: HashMap::from([
(
"id".to_owned(),
// TODO: UUID validation :3
Attribute::String {
description: "The UUID".to_owned(),
mode: Mode::Required,
sensitive: false,
},
),
(
"name".to_owned(),
Attribute::String {
description: "The description".to_owned(),
mode: Mode::Computed,
sensitive: false,
},
),
(
"description".to_owned(),
Attribute::String {
description: "The description".to_owned(),
mode: Mode::Computed,
sensitive: false,
},
),
(
"discord_id".to_owned(),
Attribute::String {
description: "The discord ID of the class".to_owned(),
mode: Mode::Computed,
sensitive: false,
},
),
]),
}
}
fn new(data: Self::ProviderData) -> DResult<Self> {
Ok(Self { client: data })
}
}

View file

@ -35,17 +35,11 @@ impl DataSource for HugoDataSource {
.to_value())
}
fn name(provider_name: &str) -> String
where
Self: Sized,
{
fn name(provider_name: &str) -> String {
format!("{provider_name}_hugo")
}
fn schema() -> Schema
where
Self: Sized,
{
fn schema() -> Schema {
Schema {
description: "Get Hugo Boss".to_owned(),
attributes: HashMap::from([(
@ -59,10 +53,7 @@ impl DataSource for HugoDataSource {
}
}
fn new(data: Self::ProviderData) -> DResult<Self>
where
Self: Sized,
{
fn new(data: Self::ProviderData) -> DResult<Self> {
Ok(Self { client: data })
}
}

View file

@ -1,2 +1,3 @@
pub mod class_data_source;
pub mod hugo;
pub mod kitty;
pub mod kitty;