Skip to main content

TerraformConfig

Struct TerraformConfig 

Source
pub struct TerraformConfig { /* private fields */ }
Expand description

Builder for constructing Terraform JSON configuration.

Produces a .tf.json file that Terraform can process identically to an HCL .tf file.

Implementations§

Source§

impl TerraformConfig

Source

pub fn new() -> Self

Create a new empty configuration.

Source

pub fn required_provider(self, name: &str, source: &str, version: &str) -> Self

Add a required provider.

let config = TerraformConfig::new()
    .required_provider("aws", "hashicorp/aws", "~> 5.0")
    .required_provider("null", "hashicorp/null", "~> 3.0");
Source

pub fn backend(self, backend_type: &str, config: Value) -> Self

Configure a backend for remote state storage.

let config = TerraformConfig::new()
    .backend("s3", json!({
        "bucket": "my-tf-state",
        "key": "terraform.tfstate",
        "region": "us-west-2"
    }));
Source

pub fn provider(self, name: &str, config: Value) -> Self

Configure a provider.

let config = TerraformConfig::new()
    .provider("aws", json!({ "region": "us-west-2" }));
Source

pub fn resource(self, resource_type: &str, name: &str, config: Value) -> Self

Add a managed resource.

let config = TerraformConfig::new()
    .resource("aws_instance", "web", json!({
        "ami": "ami-0c55b159",
        "instance_type": "t3.micro"
    }));
Source

pub fn data(self, data_type: &str, name: &str, config: Value) -> Self

Add a data source.

let config = TerraformConfig::new()
    .data("aws_ami", "latest", json!({
        "most_recent": true,
        "owners": ["amazon"]
    }));
Source

pub fn variable(self, name: &str, config: Value) -> Self

Add a variable.

let config = TerraformConfig::new()
    .variable("region", json!({
        "type": "string",
        "default": "us-west-2",
        "description": "AWS region"
    }));
Source

pub fn output(self, name: &str, config: Value) -> Self

Add an output.

let config = TerraformConfig::new()
    .output("instance_id", json!({
        "value": "${aws_instance.web.id}",
        "description": "The instance ID"
    }));
Source

pub fn local(self, name: &str, value: Value) -> Self

Add a local value.

let config = TerraformConfig::new()
    .local("common_tags", json!({
        "Environment": "production",
        "ManagedBy": "terraform-wrapper"
    }));
Source

pub fn module(self, name: &str, config: Value) -> Self

Add a module reference.

let config = TerraformConfig::new()
    .module("vpc", json!({
        "source": "terraform-aws-modules/vpc/aws",
        "version": "~> 5.0",
        "cidr": "10.0.0.0/16"
    }));
Source

pub fn to_json(&self) -> Result<String>

Serialize to a JSON string.

Source

pub fn to_json_pretty(&self) -> Result<String>

Serialize to a pretty-printed JSON string.

Source

pub fn write_to(&self, path: impl AsRef<Path>) -> Result<()>

Write the configuration to a file as main.tf.json.

Creates parent directories if they don’t exist.

Source

pub fn write_to_tempdir(&self) -> Result<TempDir>

Write the configuration to a temporary directory as main.tf.json.

Returns the TempDir which will be cleaned up when dropped. Use .path() to get the directory path for Terraform::builder().working_dir().

Trait Implementations§

Source§

impl Clone for TerraformConfig

Source§

fn clone(&self) -> TerraformConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TerraformConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TerraformConfig

Source§

fn default() -> TerraformConfig

Returns the “default value” for a type. Read more
Source§

impl Serialize for TerraformConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more