This commit is contained in:
nora 2024-04-16 19:33:36 +02:00
parent 854f7bb2bc
commit 85d10ed893
15 changed files with 328 additions and 160 deletions

View file

@ -0,0 +1,68 @@
use std::collections::HashMap;
use eyre::Context;
use terustform::{
datasource::DataSource, Attribute, DResult, EyreExt, Mode, Schema, StringValue, Value,
ValueModel,
};
use crate::client::CorsClient;
pub struct HugoDataSource {
client: CorsClient,
}
#[derive(terustform::Model)]
struct HugoDataSourceModel {
hugo: StringValue,
}
#[terustform::async_trait]
impl DataSource for HugoDataSource {
type ProviderData = CorsClient;
async fn read(&self, _config: Value) -> DResult<Value> {
let hugo = self
.client
.get_hugo()
.await
.wrap_err("failed to get hugo")
.eyre_to_tf()?;
Ok(HugoDataSourceModel {
hugo: StringValue::Known(hugo),
}
.to_value())
}
fn name(provider_name: &str) -> String
where
Self: Sized,
{
format!("{provider_name}_hugo")
}
fn schema() -> Schema
where
Self: Sized,
{
Schema {
description: "Get Hugo Boss".to_owned(),
attributes: HashMap::from([(
"hugo".to_owned(),
Attribute::String {
description: "Hugo Boss".to_owned(),
mode: Mode::Computed,
sensitive: false,
},
)]),
}
}
fn new(data: Self::ProviderData) -> DResult<Self>
where
Self: Sized,
{
Ok(Self { client: data })
}
}

View file

@ -0,0 +1,75 @@
use std::collections::HashMap;
use terustform::{
datasource::DataSource, AttrPath, Attribute, DResult, Mode, Schema, StringValue, Value,
ValueModel,
};
use crate::client::CorsClient;
pub struct ExampleDataSource {}
#[derive(terustform::Model)]
struct ExampleDataSourceModel {
name: StringValue,
meow: StringValue,
id: StringValue,
}
#[terustform::async_trait]
impl DataSource for ExampleDataSource {
type ProviderData = CorsClient;
fn name(provider_name: &str) -> String {
format!("{provider_name}_kitty")
}
fn schema() -> Schema {
Schema {
description: "an example".to_owned(),
attributes: HashMap::from([
(
"name".to_owned(),
Attribute::String {
description: "a cool name".to_owned(),
mode: Mode::Required,
sensitive: false,
},
),
(
"meow".to_owned(),
Attribute::String {
description: "the meow of the cat".to_owned(),
mode: Mode::Computed,
sensitive: false,
},
),
(
"id".to_owned(),
Attribute::String {
description: "the ID of the meowy cat".to_owned(),
mode: Mode::Computed,
sensitive: false,
},
),
]),
}
}
fn new(_data: Self::ProviderData) -> DResult<Self> {
Ok(ExampleDataSource {})
}
async fn read(&self, config: Value) -> DResult<Value> {
let mut model = ExampleDataSourceModel::from_value(config, &AttrPath::root())?;
let name_str = model.name.expect_known(AttrPath::attr("name"))?;
let meow = format!("mrrrrr i am {name_str}");
model.meow = StringValue::Known(meow);
model.id = StringValue::Known("0".to_owned());
Ok(model.to_value())
}
}

View file

@ -0,0 +1,2 @@
pub mod hugo;
pub mod kitty;