workspace

This commit is contained in:
nora 2024-04-14 22:03:39 +02:00
parent 43747f9cda
commit eea39e1f16
22 changed files with 166 additions and 107 deletions

View file

@ -0,0 +1,10 @@
[package]
name = "terraform-provider-example"
version = "0.1.0"
edition = "2021"
[dependencies]
eyre = "0.6.12"
terustform = { path = "../terustform" }
tokio = { version = "1.37.0", features = ["full"] }

View file

@ -0,0 +1,95 @@
use std::collections::{BTreeMap, HashMap};
use terustform::{
framework::{
datasource::{self, DataSource},
provider::Provider,
value::StringValue,
DResult,
},
values::{Value, ValueKind},
};
#[tokio::main]
async fn main() -> eyre::Result<()> {
terustform::serve(&ExampleProvider {}).await
}
pub struct ExampleProvider {}
impl Provider for ExampleProvider {
fn name(&self) -> String {
"example".to_owned()
}
fn data_sources(&self) -> Vec<Box<dyn DataSource>> {
vec![ExampleDataSource {}.erase()]
}
}
struct ExampleDataSource {}
struct _ExampleDataSourceModel {
name: StringValue,
}
impl DataSource for ExampleDataSource {
fn name(&self, provider_name: &str) -> String {
format!("{provider_name}_kitty")
}
fn schema(&self) -> datasource::Schema {
datasource::Schema {
description: "an example".to_owned(),
attributes: HashMap::from([
(
"name".to_owned(),
datasource::Attribute::String {
description: "a cool name".to_owned(),
mode: datasource::Mode::Required,
sensitive: false,
},
),
(
"meow".to_owned(),
datasource::Attribute::String {
description: "the meow of the cat".to_owned(),
mode: datasource::Mode::Computed,
sensitive: false,
},
),
(
"id".to_owned(),
datasource::Attribute::String {
description: "the ID of the meowy cat".to_owned(),
mode: datasource::Mode::Computed,
sensitive: false,
},
),
]),
}
}
fn read(&self, config: Value) -> DResult<Value> {
let name = match config {
Value::Known(ValueKind::Object(mut obj)) => obj.remove("name").unwrap(),
_ => unreachable!(),
};
let name_str = match &name {
Value::Known(ValueKind::String(s)) => s.clone(),
_ => unreachable!(),
};
Ok(Value::Known(ValueKind::Object(BTreeMap::from([
("name".to_owned(), name),
(
"meow".to_owned(),
Value::Known(ValueKind::String(format!("mrrrrr i am {name_str}"))),
),
(
"id".to_owned(),
Value::Known(ValueKind::String("0".to_owned())),
),
]))))
}
}

View file

@ -0,0 +1,24 @@
terraform {
required_providers {
example = {
source = "github.com/Nilstrieb/example"
}
}
}
provider "example" {}
//resource "terustform_hello" "test1" {}
data "example_kitty" "kitty" {
name = "aa mykitten"
}
data "example_kitty" "hellyes" {
name = "aa a cute kitty"
}
output "cat1" {
value = data.example_kitty.kitty.meow
}
output "cat2" {
value = data.example_kitty.hellyes.meow
}