import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Importing existing providers into OpenTofu

## Introduction

This tutorial will guide you through the process of importing your existing nullplatform provider configurations using
import blocks. This allows for better tracking, consistency, and automation of your infrastructure.

:::info This guide also applies to Terraform
The steps below work
for both OpenTofu and Terraform, as the syntax and commands are currently identical.
:::

## Step-by-step import process

### 1. Define a `providers.tf` file and install nullplatform's provider

```
terraform {
  required_providers {
    nullplatform = {
      source = "nullplatform/nullplatform"
    }
  }
}
```

then run on your terminal:

```bash
terraform init
```

### 2. Define the resource and import block

Create or reuse a `.tf` file where you'll define the resource you want to import along with an import block.

Example:

```hcl
import {
  to = nullplatform_provider_config.example
  # Replace with the actual ID of your nullplatform provider configuration.
  id = "provider_config_id_12345"
}

resource "nullplatform_provider_config" "example" {
  # Leave attributes empty for now
  # They will be filled in after the import
}
```

### 3. Run the import command

Execute the following command to process the import block:

<Tabs>
    <TabItem value="OpenTofu" label="OpenTofu">
        ```bash
        tofu plan -generate-config-out=generated_resource.tf
        ```
    </TabItem>
    <TabItem value="Terraform" label="Terraform">
        ```bash
        terraform plan -generate-config-out=generated_resource.tf
        ```
    </TabItem>
</Tabs>

This command will create a new file `generated_resource.tf` containing the imported resource configuration.

### 4. Review and adjust the imported configuration

Open the `generated_resource.tf` file and review the imported configuration. Copy the relevant attributes to your main
configuration file and adjust as necessary. Your final configuration might look something like this:

```hcl
resource "nullplatform_provider_config" "example" {
  account = "my-account"
  type    = "gcp-configuration"
  dimensions = {
    environment = "production"
  }
  attributes = jsonencode({
    "project": {
      "id": "my-gcp-project"
    },
    "networking": {
      "domain_name": "example.com",
      "public_dns_zone_name": "example-com",
      "private_dns_zone_name": "internal-example"
    },
    "authentication": {
      "credential_base_64": "eyAi... (base64 string) ...IiB9"
    }
  })
}
```

### 5. Validate your configuration

Run the plan command to ensure your configuration matches the imported resource:

<Tabs>
    <TabItem value="OpenTofu" label="OpenTofu">
        ```bash
        tofu plan
        ```
    </TabItem>
    <TabItem value="Terraform" label="Terraform">
        ```bash
        terraform plan
        ```
    </TabItem>
</Tabs>

If there are no changes proposed, your configuration matches the imported resource.

## Example: importing multiple provider configurations

You can import multiple provider configurations in the same configuration file. Here's an example importing both a GCP
and a GKE configuration:

```hcl
import {
  to = nullplatform_provider_config.gcp_config
  id = "gcp_config_id_12345"
}

resource "nullplatform_provider_config" "gcp_config" {
  # Attributes will be filled after import
}

import {
  to = nullplatform_provider_config.gke_config
  id = "gke_config_id_67890"
}

resource "nullplatform_provider_config" "gke_config" {
  # Attributes will be filled after import
}
```

After running the plan command with `-generate-config-out=generated_resource.tf`, review and adjust the generated
configurations as needed.

## Recommendations

When configuring nullplatform through providers, we recommend you to:
- test thoroughly in a non-production environment before applying changes to critical resources,
- consider your organization's needs when choosing between OpenTofu and Terraform,
- keep your configurations version controlled,
- follow your organization's security practices when handling sensitive credentials.
