use std::collections::HashSet;
use std::fmt::Write as _;
use openapi_forge::{Field, Spec};
use crate::spec::{ProviderDefaults, ResourceSpec};
use crate::type_map::{go_to_tf_attr, openapi_to_go, tf_value_type, to_go_public_name, to_tf_name};
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::struct_excessive_bools)]
pub struct TfAttribute {
pub tf_name: String,
pub go_name: String,
pub description: String,
pub required: bool,
pub optional: bool,
pub computed: bool,
pub sensitive: bool,
pub force_new: bool,
pub tf_type_expr: String,
pub tf_value_type: String,
pub go_type: String,
pub default_value: Option<String>,
}
impl TfAttribute {
#[must_use]
pub fn new(tf_name: &str, go_type: &crate::type_map::GoType) -> Self {
let tf_type = crate::type_map::go_to_tf_attr(go_type);
Self {
tf_name: tf_name.to_string(),
go_name: crate::type_map::to_go_public_name(tf_name),
description: String::new(),
required: false,
optional: false,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: tf_type.to_string(),
tf_value_type: crate::type_map::tf_value_type(go_type).to_string(),
go_type: go_type.to_string(),
default_value: None,
}
}
}
#[allow(clippy::module_name_repetitions)]
pub fn generate_schema_attributes(
resource: &ResourceSpec,
api: &Spec,
defaults: &ProviderDefaults,
) -> Result<Vec<TfAttribute>, crate::error::ForgeError> {
let create_fields = api.fields(&resource.crud.create_schema)?;
let update_fields: Vec<Field> = if let Some(ref update_schema) = resource.crud.update_schema {
api.fields(update_schema).unwrap_or_default()
} else {
Vec::new()
};
let update_required: HashSet<String> = update_fields
.iter()
.filter(|f| f.required)
.map(|f| f.name.clone())
.collect();
let skip_fields: HashSet<&str> = defaults.skip_fields.iter().map(String::as_str).collect();
let mut attributes = Vec::new();
for field in &create_fields {
let tf_name = to_tf_name(&field.name);
if skip_fields.contains(field.name.as_str()) {
continue;
}
let override_cfg = resource.fields.get(&field.name);
if override_cfg.is_some_and(|o| o.skip) {
continue;
}
let computed = override_cfg.is_some_and(|o| o.computed);
let sensitive = override_cfg.is_some_and(|o| o.sensitive);
let force_new = override_cfg.is_some_and(|o| o.force_new)
|| resource.identity.force_new_fields.contains(&field.name);
let type_override = override_cfg.and_then(|o| o.type_override.as_deref());
let go_type = openapi_to_go(&field.type_info, type_override);
let tf_type = go_to_tf_attr(&go_type);
let is_create_required = field.required;
let _is_update_required = update_required.contains(&field.name);
let required = if computed { false } else { is_create_required };
let optional = !required || computed;
let description = override_cfg
.and_then(|o| o.description.clone())
.or_else(|| field.description.clone())
.unwrap_or_default();
let default_value = field.default.as_ref().map(|v| format!("{v}"));
attributes.push(TfAttribute {
tf_name,
go_name: to_go_public_name(&field.name),
description,
required,
optional,
computed,
sensitive,
force_new,
tf_type_expr: tf_type.to_string(),
tf_value_type: tf_value_type(&go_type).to_string(),
go_type: go_type.to_string(),
default_value,
});
}
Ok(attributes)
}
#[must_use]
pub fn render_schema_attributes(attrs: &[TfAttribute]) -> String {
let mut out = String::new();
out.push_str("func (r *Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {\n");
out.push_str("\tresp.Schema = schema.Schema{\n");
out.push_str("\t\tAttributes: map[string]schema.Attribute{\n");
let attr_code: String = attrs.iter().map(render_single_attribute).collect();
out.push_str(&attr_code);
out.push_str("\t\t},\n");
out.push_str("\t}\n");
out.push_str("}\n");
out
}
#[must_use]
pub fn render_model_struct(resource_name: &str, attrs: &[TfAttribute]) -> String {
let struct_name = format!("{resource_name}Model");
let mut out = String::new();
let _ = writeln!(out, "type {struct_name} struct {{");
for attr in attrs {
let _ = writeln!(
out,
"\t{} {} `tfsdk:\"{}\"`",
attr.go_name, attr.tf_value_type, attr.tf_name
);
}
out.push_str("}\n");
out
}
fn element_type_for_attr(attr: &TfAttribute) -> &'static str {
match attr.go_type.as_str() {
"[]int64" => "types.Int64Type",
"[]float64" => "types.Float64Type",
"[]bool" => "types.BoolType",
_ => "types.StringType",
}
}
fn plan_modifier_for_attr(attr: &TfAttribute) -> (&'static str, &'static str) {
match attr.tf_value_type.as_str() {
"types.Int64" => ("Int64", "int64planmodifier.RequiresReplace()"),
"types.Float64" => ("Float64", "float64planmodifier.RequiresReplace()"),
"types.Bool" => ("Bool", "boolplanmodifier.RequiresReplace()"),
"types.Set" => ("Set", "setplanmodifier.RequiresReplace()"),
"types.List" => ("List", "listplanmodifier.RequiresReplace()"),
"types.Map" => ("Map", "mapplanmodifier.RequiresReplace()"),
_ => ("String", "stringplanmodifier.RequiresReplace()"),
}
}
pub(crate) fn render_single_attribute(attr: &TfAttribute) -> String {
let mut out = String::new();
let indent = "\t\t\t";
let attr_kind = tf_schema_attr_kind(&attr.tf_value_type);
let _ = writeln!(out, "{indent}\"{}\": {attr_kind}{{", attr.tf_name);
let desc = attr.description.replace('"', "\\\"");
let _ = writeln!(out, "{indent}\tDescription: \"{desc}\",");
if attr.required {
let _ = writeln!(out, "{indent}\tRequired: true,");
} else if attr.computed && attr.optional {
let _ = writeln!(out, "{indent}\tOptional: true,");
let _ = writeln!(out, "{indent}\tComputed: true,");
} else if attr.computed {
let _ = writeln!(out, "{indent}\tComputed: true,");
} else {
let _ = writeln!(out, "{indent}\tOptional: true,");
}
if attr.sensitive {
let _ = writeln!(out, "{indent}\tSensitive: true,");
}
if attr.tf_value_type.contains("Set")
|| attr.tf_value_type.contains("List")
|| attr.tf_value_type.contains("Map")
{
let element_type = element_type_for_attr(attr);
let _ = writeln!(out, "{indent}\tElementType: {element_type}{{}},");
}
if attr.force_new {
let (modifier_type, modifier_fn) = plan_modifier_for_attr(attr);
let _ = writeln!(
out,
"{indent}\tPlanModifiers: []planmodifier.{modifier_type}{{"
);
let _ = writeln!(out, "{indent}\t\t{modifier_fn},");
let _ = writeln!(out, "{indent}\t}},");
}
let _ = writeln!(out, "{indent}}},");
out
}
pub(crate) fn tf_schema_attr_kind(tf_value_type: &str) -> &'static str {
if tf_value_type.contains("Set") {
"schema.SetAttribute"
} else if tf_value_type.contains("List") {
"schema.ListAttribute"
} else if tf_value_type.contains("Map") {
"schema.MapAttribute"
} else {
match tf_value_type {
"types.Int64" => "schema.Int64Attribute",
"types.Float64" => "schema.Float64Attribute",
"types.Bool" => "schema.BoolAttribute",
_ => "schema.StringAttribute",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn make_test_spec() -> (ResourceSpec, Spec) {
let toml_str = r#"
[resource]
name = "akeyless_static_secret"
description = "Static secret"
category = "secret"
[crud]
create_endpoint = "/create-secret"
create_schema = "CreateSecret"
read_endpoint = "/get-secret-value"
read_schema = "GetSecretValue"
delete_endpoint = "/delete-item"
delete_schema = "DeleteItem"
[identity]
id_field = "name"
force_new_fields = ["name"]
[fields]
token = { skip = true }
delete_protection = { type_override = "bool" }
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse resource");
let api_str = r#"
openapi: "3.0.0"
info:
title: Test
version: "1.0"
paths:
/create-secret:
post:
operationId: createSecret
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateSecret'
responses:
"200":
description: ok
/get-secret-value:
post:
operationId: getSecretValue
responses:
"200":
description: ok
/delete-item:
post:
operationId: deleteItem
responses:
"200":
description: ok
components:
schemas:
CreateSecret:
type: object
required:
- name
- value
properties:
name:
type: string
description: Secret name
value:
type: string
description: Secret value
tags:
type: array
items:
type: string
token:
type: string
delete_protection:
type: string
GetSecretValue:
type: object
properties:
names:
type: array
items:
type: string
DeleteItem:
type: object
properties:
name:
type: string
"#;
let api = Spec::parse(api_str).expect("parse api");
(resource, api)
}
#[test]
fn generate_attributes() {
let (resource, api) = make_test_spec();
let defaults = ProviderDefaults::default();
let attrs = generate_schema_attributes(&resource, &api, &defaults).expect("gen");
assert!(attrs.iter().all(|a| a.tf_name != "token"));
let name_attr = attrs.iter().find(|a| a.tf_name == "name").expect("name");
assert!(name_attr.required);
assert!(name_attr.force_new);
let dp = attrs
.iter()
.find(|a| a.tf_name == "delete_protection")
.expect("dp");
assert_eq!(dp.go_type, "bool");
}
#[test]
fn render_schema() {
let (resource, api) = make_test_spec();
let defaults = ProviderDefaults::default();
let attrs = generate_schema_attributes(&resource, &api, &defaults).expect("gen");
let code = render_schema_attributes(&attrs);
assert!(code.contains("schema.StringAttribute"));
assert!(code.contains("Required: true"));
}
#[test]
fn render_model() {
let (resource, api) = make_test_spec();
let defaults = ProviderDefaults::default();
let attrs = generate_schema_attributes(&resource, &api, &defaults).expect("gen");
let code = render_model_struct("StaticSecret", &attrs);
assert!(code.contains("type StaticSecretModel struct"));
assert!(code.contains("Name types.String"));
}
#[test]
fn global_skip_fields_removes_field() {
let (resource, api) = make_test_spec();
let defaults = ProviderDefaults {
skip_fields: vec!["value".to_string()],
};
let attrs = generate_schema_attributes(&resource, &api, &defaults).expect("gen");
assert!(
attrs.iter().all(|a| a.tf_name != "value"),
"value should be skipped via global defaults"
);
assert!(
attrs.iter().any(|a| a.tf_name == "name"),
"name should not be skipped"
);
}
#[test]
fn computed_field_is_optional_not_required() {
let toml_str = r#"
[resource]
name = "test"
[crud]
create_endpoint = "/create"
create_schema = "TestCreate"
read_endpoint = "/read"
read_schema = "TestRead"
delete_endpoint = "/delete"
delete_schema = "TestDelete"
[identity]
id_field = "name"
[fields]
auto_id = { computed = true }
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse");
let api_str = r#"
openapi: "3.0.0"
info: { title: Test, version: "1.0" }
paths:
/create:
post:
operationId: create
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestCreate'
responses:
"200": { description: ok }
/read:
post:
operationId: read
responses:
"200": { description: ok }
/delete:
post:
operationId: delete
responses:
"200": { description: ok }
components:
schemas:
TestCreate:
type: object
required: [name, auto_id]
properties:
name: { type: string }
auto_id: { type: string }
TestRead:
type: object
properties:
name: { type: string }
TestDelete:
type: object
properties:
name: { type: string }
"#;
let api = Spec::parse(api_str).expect("parse");
let attrs = generate_schema_attributes(&resource, &api, &ProviderDefaults::default())
.expect("gen");
let auto_id = attrs.iter().find(|a| a.tf_name == "auto_id").expect("auto_id");
assert!(!auto_id.required, "computed fields should not be required");
assert!(auto_id.optional, "computed fields should be optional");
assert!(auto_id.computed);
}
#[test]
fn sensitive_field_flag_set() {
let toml_str = r#"
[resource]
name = "test"
[crud]
create_endpoint = "/create"
create_schema = "TestCreate"
read_endpoint = "/read"
read_schema = "TestRead"
delete_endpoint = "/delete"
delete_schema = "TestDelete"
[identity]
id_field = "name"
[fields]
secret = { sensitive = true }
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse");
let api_str = r#"
openapi: "3.0.0"
info: { title: Test, version: "1.0" }
paths:
/create:
post:
operationId: create
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestCreate'
responses:
"200": { description: ok }
/read:
post: { operationId: read, responses: { "200": { description: ok } } }
/delete:
post: { operationId: delete, responses: { "200": { description: ok } } }
components:
schemas:
TestCreate:
type: object
required: [name]
properties:
name: { type: string }
secret: { type: string }
TestRead:
type: object
properties:
name: { type: string }
TestDelete:
type: object
properties:
name: { type: string }
"#;
let api = Spec::parse(api_str).expect("parse");
let attrs = generate_schema_attributes(&resource, &api, &ProviderDefaults::default())
.expect("gen");
let secret_attr = attrs.iter().find(|a| a.tf_name == "secret").expect("secret");
assert!(secret_attr.sensitive);
}
#[test]
fn render_schema_contains_sensitive_flag() {
let attr = TfAttribute {
tf_name: "api_key".to_string(),
go_name: "ApiKey".to_string(),
description: "API Key".to_string(),
required: true,
optional: false,
computed: false,
sensitive: true,
force_new: false,
tf_type_expr: "types.StringType".to_string(),
tf_value_type: "types.String".to_string(),
go_type: "string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("Sensitive: true"));
}
#[test]
fn render_schema_computed_only() {
let attr = TfAttribute {
tf_name: "generated_id".to_string(),
go_name: "GeneratedId".to_string(),
description: "Auto ID".to_string(),
required: false,
optional: false,
computed: true,
sensitive: false,
force_new: false,
tf_type_expr: "types.StringType".to_string(),
tf_value_type: "types.String".to_string(),
go_type: "string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("Computed: true"));
assert!(!code.contains("Required: true"));
assert!(!code.contains("Optional: true"));
}
#[test]
fn render_schema_optional_computed() {
let attr = TfAttribute {
tf_name: "maybe".to_string(),
go_name: "Maybe".to_string(),
description: "Opt+Comp".to_string(),
required: false,
optional: true,
computed: true,
sensitive: false,
force_new: false,
tf_type_expr: "types.StringType".to_string(),
tf_value_type: "types.String".to_string(),
go_type: "string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("Optional: true"));
assert!(code.contains("Computed: true"));
}
#[test]
fn render_schema_optional_only() {
let attr = TfAttribute {
tf_name: "opt".to_string(),
go_name: "Opt".to_string(),
description: "Opt only".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.StringType".to_string(),
tf_value_type: "types.String".to_string(),
go_type: "string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("Optional: true"));
assert!(!code.contains("Computed: true"));
assert!(!code.contains("Required: true"));
}
#[test]
fn render_schema_set_attribute() {
let attr = TfAttribute {
tf_name: "tags".to_string(),
go_name: "Tags".to_string(),
description: "Tag list".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.SetType{ElemType: types.StringType}".to_string(),
tf_value_type: "types.Set".to_string(),
go_type: "[]string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("schema.SetAttribute"));
assert!(code.contains("ElementType: types.StringType{}"));
}
#[test]
fn render_schema_list_attribute() {
let attr = TfAttribute {
tf_name: "ids".to_string(),
go_name: "Ids".to_string(),
description: "ID list".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.ListType{ElemType: types.Int64Type}".to_string(),
tf_value_type: "types.List".to_string(),
go_type: "[]int64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("schema.ListAttribute"));
assert!(code.contains("ElementType:"));
}
#[test]
fn render_schema_map_attribute() {
let attr = TfAttribute {
tf_name: "metadata".to_string(),
go_name: "Metadata".to_string(),
description: "Metadata".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.MapType{ElemType: types.StringType}".to_string(),
tf_value_type: "types.Map".to_string(),
go_type: "map[string]string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("schema.MapAttribute"));
assert!(code.contains("ElementType: types.StringType{}"));
}
#[test]
fn render_schema_int64_attribute() {
let attr = TfAttribute {
tf_name: "count".to_string(),
go_name: "Count".to_string(),
description: "count".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.Int64Type".to_string(),
tf_value_type: "types.Int64".to_string(),
go_type: "int64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("schema.Int64Attribute"));
}
#[test]
fn render_schema_float64_attribute() {
let attr = TfAttribute {
tf_name: "rate".to_string(),
go_name: "Rate".to_string(),
description: "rate".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.Float64Type".to_string(),
tf_value_type: "types.Float64".to_string(),
go_type: "float64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("schema.Float64Attribute"));
}
#[test]
fn render_schema_bool_attribute() {
let attr = TfAttribute {
tf_name: "enabled".to_string(),
go_name: "Enabled".to_string(),
description: "flag".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.BoolType".to_string(),
tf_value_type: "types.Bool".to_string(),
go_type: "bool".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("schema.BoolAttribute"));
}
#[test]
fn render_schema_force_new_string() {
let attr = TfAttribute {
tf_name: "name".to_string(),
go_name: "Name".to_string(),
description: "name".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.StringType".to_string(),
tf_value_type: "types.String".to_string(),
go_type: "string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("PlanModifiers"));
assert!(code.contains("stringplanmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.String"));
}
#[test]
fn render_schema_force_new_int64() {
let attr = TfAttribute {
tf_name: "ttl".to_string(),
go_name: "Ttl".to_string(),
description: "ttl".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.Int64Type".to_string(),
tf_value_type: "types.Int64".to_string(),
go_type: "int64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("int64planmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.Int64"));
}
#[test]
fn render_schema_force_new_bool() {
let attr = TfAttribute {
tf_name: "flag".to_string(),
go_name: "Flag".to_string(),
description: "flag".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.BoolType".to_string(),
tf_value_type: "types.Bool".to_string(),
go_type: "bool".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("boolplanmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.Bool"));
}
#[test]
fn render_schema_force_new_float64() {
let attr = TfAttribute {
tf_name: "ratio".to_string(),
go_name: "Ratio".to_string(),
description: "ratio".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.Float64Type".to_string(),
tf_value_type: "types.Float64".to_string(),
go_type: "float64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("float64planmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.Float64"));
}
#[test]
fn render_schema_force_new_set() {
let attr = TfAttribute {
tf_name: "regions".to_string(),
go_name: "Regions".to_string(),
description: "regions".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.SetType{ElemType: types.StringType}".to_string(),
tf_value_type: "types.Set".to_string(),
go_type: "[]string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("setplanmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.Set"));
}
#[test]
fn render_schema_force_new_list() {
let attr = TfAttribute {
tf_name: "items".to_string(),
go_name: "Items".to_string(),
description: "items".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.ListType{ElemType: types.Int64Type}".to_string(),
tf_value_type: "types.List".to_string(),
go_type: "[]int64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("listplanmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.List"));
}
#[test]
fn render_schema_force_new_map() {
let attr = TfAttribute {
tf_name: "labels".to_string(),
go_name: "Labels".to_string(),
description: "labels".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: true,
tf_type_expr: "types.MapType{ElemType: types.StringType}".to_string(),
tf_value_type: "types.Map".to_string(),
go_type: "map[string]string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("mapplanmodifier.RequiresReplace()"));
assert!(code.contains("planmodifier.Map"));
}
#[test]
fn render_schema_int64_list_element_type() {
let attr = TfAttribute {
tf_name: "nums".to_string(),
go_name: "Nums".to_string(),
description: "numbers".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.ListType{ElemType: types.Int64Type}".to_string(),
tf_value_type: "types.List".to_string(),
go_type: "[]int64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("ElementType: types.Int64Type{}"));
}
#[test]
fn render_schema_float64_list_element_type() {
let attr = TfAttribute {
tf_name: "scores".to_string(),
go_name: "Scores".to_string(),
description: "scores".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.ListType{ElemType: types.Float64Type}".to_string(),
tf_value_type: "types.List".to_string(),
go_type: "[]float64".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("ElementType: types.Float64Type{}"));
}
#[test]
fn render_schema_bool_list_element_type() {
let attr = TfAttribute {
tf_name: "flags".to_string(),
go_name: "Flags".to_string(),
description: "flags".to_string(),
required: false,
optional: true,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.ListType{ElemType: types.BoolType}".to_string(),
tf_value_type: "types.List".to_string(),
go_type: "[]bool".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains("ElementType: types.BoolType{}"));
}
#[test]
fn render_schema_description_escapes_quotes() {
let attr = TfAttribute {
tf_name: "desc_field".to_string(),
go_name: "DescField".to_string(),
description: "A field with \"quotes\" inside".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.StringType".to_string(),
tf_value_type: "types.String".to_string(),
go_type: "string".to_string(),
default_value: None,
};
let code = render_schema_attributes(&[attr]);
assert!(code.contains(r#"A field with \"quotes\" inside"#));
}
#[test]
fn render_schema_empty_attributes() {
let code = render_schema_attributes(&[]);
assert!(code.contains("Attributes: map[string]schema.Attribute{"));
assert!(code.contains("func (r *Resource) Schema"));
}
#[test]
fn render_model_empty_attributes() {
let code = render_model_struct("Empty", &[]);
assert!(code.contains("type EmptyModel struct {"));
assert!(code.contains('}'));
}
#[test]
fn render_model_tfsdk_tag() {
let attrs = vec![TfAttribute {
tf_name: "my_field".to_string(),
go_name: "MyField".to_string(),
description: "test".to_string(),
required: true,
optional: false,
computed: false,
sensitive: false,
force_new: false,
tf_type_expr: "types.Int64Type".to_string(),
tf_value_type: "types.Int64".to_string(),
go_type: "int64".to_string(),
default_value: None,
}];
let code = render_model_struct("Res", &attrs);
assert!(code.contains("MyField types.Int64 `tfsdk:\"my_field\"`"));
}
#[test]
fn tf_attribute_new_string() {
let attr = TfAttribute::new("my_field", &crate::type_map::GoType::String);
assert_eq!(attr.tf_name, "my_field");
assert_eq!(attr.go_name, "MyField");
assert_eq!(attr.go_type, "string");
assert_eq!(attr.tf_value_type, "types.String");
assert!(!attr.required);
assert!(!attr.computed);
assert!(!attr.sensitive);
assert!(!attr.force_new);
assert!(attr.default_value.is_none());
}
#[test]
fn tf_attribute_new_list_of_string() {
let attr = TfAttribute::new("tags", &crate::type_map::GoType::ListOfString);
assert_eq!(attr.go_type, "[]string");
assert_eq!(attr.tf_value_type, "types.Set");
assert!(attr.tf_type_expr.contains("SetType"));
}
#[test]
fn tf_attribute_partial_eq() {
let a = TfAttribute::new("name", &crate::type_map::GoType::String);
let b = TfAttribute::new("name", &crate::type_map::GoType::String);
assert_eq!(a, b);
}
#[test]
fn field_override_description_takes_priority() {
let toml_str = r#"
[resource]
name = "test"
[crud]
create_endpoint = "/c"
create_schema = "C"
read_endpoint = "/r"
read_schema = "R"
delete_endpoint = "/d"
delete_schema = "D"
[identity]
id_field = "name"
[fields.name]
description = "Custom description"
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse");
let api_str = r#"
openapi: "3.0.0"
info: { title: T, version: "1" }
paths:
/c:
post:
operationId: c
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/C'
responses:
"200": { description: ok }
/r:
post: { operationId: r, responses: { "200": { description: ok } } }
/d:
post: { operationId: d, responses: { "200": { description: ok } } }
components:
schemas:
C:
type: object
required: [name]
properties:
name: { type: string, description: "Original" }
R:
type: object
properties:
name: { type: string }
D:
type: object
properties:
name: { type: string }
"#;
let api = Spec::parse(api_str).expect("parse");
let attrs = generate_schema_attributes(&resource, &api, &ProviderDefaults::default())
.expect("gen");
let name_attr = attrs.iter().find(|a| a.tf_name == "name").expect("name");
assert_eq!(name_attr.description, "Custom description");
}
#[test]
fn generate_schema_attributes_missing_schema_errors() {
let toml_str = r#"
[resource]
name = "test"
[crud]
create_endpoint = "/c"
create_schema = "NonExistentSchema"
read_endpoint = "/r"
read_schema = "R"
delete_endpoint = "/d"
delete_schema = "D"
[identity]
id_field = "name"
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse");
let api_str = r#"
openapi: "3.0.0"
info: { title: T, version: "1" }
paths: {}
components:
schemas:
R:
type: object
properties:
name: { type: string }
D:
type: object
properties:
name: { type: string }
"#;
let api = Spec::parse(api_str).expect("parse");
let result = generate_schema_attributes(&resource, &api, &ProviderDefaults::default());
assert!(result.is_err(), "Missing create schema should error");
}
#[test]
fn tf_schema_attr_kind_all_types() {
assert_eq!(tf_schema_attr_kind("types.String"), "schema.StringAttribute");
assert_eq!(tf_schema_attr_kind("types.Int64"), "schema.Int64Attribute");
assert_eq!(
tf_schema_attr_kind("types.Float64"),
"schema.Float64Attribute"
);
assert_eq!(tf_schema_attr_kind("types.Bool"), "schema.BoolAttribute");
assert_eq!(tf_schema_attr_kind("types.Set"), "schema.SetAttribute");
assert_eq!(tf_schema_attr_kind("types.List"), "schema.ListAttribute");
assert_eq!(tf_schema_attr_kind("types.Map"), "schema.MapAttribute");
}
#[test]
fn tf_schema_attr_kind_unknown_falls_back_to_string() {
assert_eq!(
tf_schema_attr_kind("types.Unknown"),
"schema.StringAttribute"
);
}
#[test]
fn force_new_from_field_override() {
let toml_str = r#"
[resource]
name = "test"
[crud]
create_endpoint = "/create"
create_schema = "TestCreate"
read_endpoint = "/read"
read_schema = "TestRead"
delete_endpoint = "/delete"
delete_schema = "TestDelete"
[identity]
id_field = "name"
[fields.region]
force_new = true
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse");
let api_str = r#"
openapi: "3.0.0"
info: { title: T, version: "1" }
paths:
/create:
post:
operationId: c
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestCreate'
responses:
"200": { description: ok }
/read:
post: { operationId: r, responses: { "200": { description: ok } } }
/delete:
post: { operationId: d, responses: { "200": { description: ok } } }
components:
schemas:
TestCreate:
type: object
properties:
name: { type: string }
region: { type: string }
TestRead:
type: object
properties:
name: { type: string }
TestDelete:
type: object
properties:
name: { type: string }
"#;
let api = Spec::parse(api_str).expect("parse");
let attrs = generate_schema_attributes(&resource, &api, &ProviderDefaults::default())
.expect("gen");
let region = attrs.iter().find(|a| a.tf_name == "region").expect("region");
assert!(
region.force_new,
"force_new from field override should be set"
);
}
#[test]
fn openapi_default_value_propagated() {
let toml_str = r#"
[resource]
name = "test"
[crud]
create_endpoint = "/create"
create_schema = "TestCreate"
read_endpoint = "/read"
read_schema = "TestRead"
delete_endpoint = "/delete"
delete_schema = "TestDelete"
[identity]
id_field = "name"
"#;
let resource: ResourceSpec = toml::from_str(toml_str).expect("parse");
let api_str = r#"
openapi: "3.0.0"
info: { title: T, version: "1" }
paths:
/create:
post:
operationId: c
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/TestCreate'
responses:
"200": { description: ok }
/read:
post: { operationId: r, responses: { "200": { description: ok } } }
/delete:
post: { operationId: d, responses: { "200": { description: ok } } }
components:
schemas:
TestCreate:
type: object
properties:
name: { type: string }
timeout:
type: integer
default: 60
TestRead:
type: object
properties:
name: { type: string }
TestDelete:
type: object
properties:
name: { type: string }
"#;
let api = Spec::parse(api_str).expect("parse");
let attrs = generate_schema_attributes(&resource, &api, &ProviderDefaults::default())
.expect("gen");
let timeout = attrs
.iter()
.find(|a| a.tf_name == "timeout")
.expect("timeout");
assert!(
timeout.default_value.is_some(),
"OpenAPI default should propagate"
);
assert_eq!(timeout.default_value.as_deref(), Some("60"));
}
}