mirror of
https://github.com/Noratrieb/terustform.git
synced 2026-01-16 01:15:10 +01:00
stuff
This commit is contained in:
parent
854f7bb2bc
commit
85d10ed893
15 changed files with 328 additions and 160 deletions
|
|
@ -1,3 +1,51 @@
|
|||
pub struct _CorsClient {
|
||||
client: reqwest::Client
|
||||
use eyre::{Context, OptionExt, Result};
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CorsClient {
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
const URL: &str = "https://api.cors-school.nilstrieb.dev/api";
|
||||
|
||||
impl CorsClient {
|
||||
pub async fn new(email: String, password: String) -> Result<Self> {
|
||||
let client = reqwest::Client::new();
|
||||
let login = dto::UserLogin { email, password };
|
||||
let token = client
|
||||
.post(format!("{URL}/login"))
|
||||
.json(&login)
|
||||
.send()
|
||||
.await
|
||||
.wrap_err("failed to send login request")?;
|
||||
let token = token.error_for_status().wrap_err("failed to login")?;
|
||||
let token = token
|
||||
.headers()
|
||||
.get("Token")
|
||||
.ok_or_eyre("does not have Token header in login response")?
|
||||
.to_str()
|
||||
.wrap_err("Token is invalid utf8")?;
|
||||
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Authorization",
|
||||
HeaderValue::from_str(&format!("Bearer {}", token,)).unwrap(),
|
||||
);
|
||||
let client = reqwest::Client::builder()
|
||||
.default_headers(headers)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
Ok(Self { client })
|
||||
}
|
||||
|
||||
pub async fn get_hugo(&self) -> Result<String> {
|
||||
Ok(self
|
||||
.client
|
||||
.get(format!("{URL}/hugo"))
|
||||
.send()
|
||||
.await?
|
||||
.text()
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
mod client;
|
||||
mod resources;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use eyre::Context;
|
||||
use terustform::{
|
||||
datasource::{self, DataSource},
|
||||
datasource::DataSource,
|
||||
provider::{MkDataSource, Provider},
|
||||
AttrPath, DResult, StringValue, Value, ValueModel,
|
||||
DResult, EyreExt, Schema, Value,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -16,90 +18,36 @@ async fn main() -> eyre::Result<()> {
|
|||
pub struct ExampleProvider {}
|
||||
|
||||
impl Provider for ExampleProvider {
|
||||
type Data = ();
|
||||
type Data = client::CorsClient;
|
||||
fn name(&self) -> String {
|
||||
"corsschool".to_owned()
|
||||
}
|
||||
|
||||
fn schema(&self) -> datasource::Schema {
|
||||
datasource::Schema {
|
||||
fn schema(&self) -> Schema {
|
||||
Schema {
|
||||
description: "uwu".to_owned(),
|
||||
attributes: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn configure(&self, _config: Value) -> DResult<Self::Data> {
|
||||
Ok(())
|
||||
let username = std::env::var("CORSSCHOOL_USERNAME")
|
||||
.wrap_err("CORSSCHOOL_USERNAME environment variable not set")
|
||||
.eyre_to_tf()?;
|
||||
let password = std::env::var("CORSSCHOOL_PASSWORD")
|
||||
.wrap_err("CORSSCHOOL_PASSWORD environment variable not set")
|
||||
.eyre_to_tf()?;
|
||||
let client = client::CorsClient::new(username, password)
|
||||
.await
|
||||
.wrap_err("failed to create client")
|
||||
.eyre_to_tf()?;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
fn data_sources(&self) -> Vec<MkDataSource<Self::Data>> {
|
||||
vec![ExampleDataSource::erase()]
|
||||
}
|
||||
}
|
||||
|
||||
struct ExampleDataSource {}
|
||||
|
||||
#[derive(terustform::Model)]
|
||||
struct ExampleDataSourceModel {
|
||||
name: StringValue,
|
||||
meow: StringValue,
|
||||
id: StringValue,
|
||||
}
|
||||
|
||||
#[terustform::async_trait]
|
||||
impl DataSource for ExampleDataSource {
|
||||
type ProviderData = ();
|
||||
|
||||
fn name(provider_name: &str) -> String {
|
||||
format!("{provider_name}_kitty")
|
||||
}
|
||||
|
||||
fn schema() -> 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 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())
|
||||
vec![
|
||||
resources::kitty::ExampleDataSource::erase(),
|
||||
resources::hugo::HugoDataSource::erase(),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
68
terraform-provider-corsschool/src/resources/hugo.rs
Normal file
68
terraform-provider-corsschool/src/resources/hugo.rs
Normal 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 })
|
||||
}
|
||||
}
|
||||
75
terraform-provider-corsschool/src/resources/kitty.rs
Normal file
75
terraform-provider-corsschool/src/resources/kitty.rs
Normal 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())
|
||||
}
|
||||
}
|
||||
2
terraform-provider-corsschool/src/resources/mod.rs
Normal file
2
terraform-provider-corsschool/src/resources/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod hugo;
|
||||
pub mod kitty;
|
||||
Loading…
Add table
Add a link
Reference in a new issue