# Terraform Data Sources

Data sources allow you to query existing resources and use their attributes in your Terraform configuration. The DanubeData provider includes 4 data sources.

## danubedata_vps_images

Retrieves the list of available OS images for VPS instances.

### Example Usage

```hcl
# Get all available images
data "danubedata_vps_images" "available" {}

# Output all image names
output "available_images" {
  value = data.danubedata_vps_images.available.images[*].image
}

# Use in a VPS resource
resource "danubedata_vps" "web" {
  name       = "web-server"
  image      = data.danubedata_vps_images.available.images[0].image
  datacenter = "fsn1"
  # ...
}
```

### Filtering Images

```hcl
data "danubedata_vps_images" "available" {}

# Find Ubuntu images
locals {
  ubuntu_images = [
    for img in data.danubedata_vps_images.available.images :
    img if img.distro == "ubuntu"
  ]
}

output "ubuntu_images" {
  value = local.ubuntu_images[*].image
}
```

### Attribute Reference

The `images` attribute contains a list of objects with the following attributes:

| Attribute | Type | Description |
|-----------|------|-------------|
| `id` | string | Image ID. |
| `image` | string | Image identifier (e.g., `ubuntu-24.04`). |
| `label` | string | Human-readable label (e.g., "Ubuntu 24.04 LTS"). |
| `description` | string | Image description. |
| `distro` | string | Distribution name (e.g., `ubuntu`, `debian`, `alpine`). |
| `version` | string | OS version. |
| `family` | string | OS family. |
| `default_user` | string | Default SSH user (`root` for Linux, `Administrator` for Windows). |

### Available Images

The data source returns images including:

| Image | Label | Default User |
|-------|-------|--------------|
| `ubuntu-26.04` | Ubuntu 26.04 LTS | `root` |
| `ubuntu-24.04` | Ubuntu 24.04 LTS | `root` |
| `ubuntu-22.04` | Ubuntu 22.04 LTS | `root` |
| `debian-13` | Debian 13 (Trixie) | `root` |
| `debian-12` | Debian 12 (Bookworm) | `root` |
| `debian-11` | Debian 11 (Bullseye) | `root` |
| `alma-9` | AlmaLinux 9 | `root` |
| `alma-8` | AlmaLinux 8 | `root` |
| `rocky-9` | Rocky Linux 9 | `root` |
| `rocky-8` | Rocky Linux 8 | `root` |
| `fedora-42` | Fedora 42 | `root` |
| `fedora-41` | Fedora 41 | `root` |
| `alpine-3.22` | Alpine Linux 3.22 | `root` |
| `alpine-3.21` | Alpine Linux 3.21 | `root` |
| `alpine-3.20` | Alpine Linux 3.20 | `root` |
| `windows-server-2025` | Windows Server 2025 | `Administrator` |

---

## danubedata_cache_providers

Retrieves the list of available cache providers.

### Example Usage

```hcl
data "danubedata_cache_providers" "available" {}

output "cache_providers" {
  value = data.danubedata_cache_providers.available.providers[*].name
}

# Find a specific provider
locals {
  redis_provider = [
    for p in data.danubedata_cache_providers.available.providers :
    p if p.type == "redis"
  ][0]
}

output "redis_default_port" {
  value = local.redis_provider.default_port
}
```

### Attribute Reference

The `providers` attribute contains a list of objects with the following attributes:

| Attribute | Type | Description |
|-----------|------|-------------|
| `id` | string | Provider ID. |
| `name` | string | Provider name (e.g., "Redis", "Valkey", "Dragonfly"). |
| `type` | string | Provider type identifier (e.g., `redis`, `valkey`, `dragonfly`). |
| `description` | string | Provider description. |
| `version` | string | Default version. |
| `default_port` | number | Default connection port. |

### Available Providers

| Provider | Type | Default Port | Description |
|----------|------|--------------|-------------|
| Redis | `redis` | 6379 | Industry-standard in-memory data store |
| Valkey | `valkey` | 6379 | Redis-compatible, community-driven fork |
| Dragonfly | `dragonfly` | 6379 | Multi-threaded, high-performance cache |

---

## danubedata_database_providers

Retrieves the list of available database engines.

### Example Usage

```hcl
data "danubedata_database_providers" "available" {}

output "database_engines" {
  value = data.danubedata_database_providers.available.providers[*].name
}

# Find PostgreSQL info
locals {
  postgresql = [
    for p in data.danubedata_database_providers.available.providers :
    p if p.type == "postgresql"
  ][0]
}

output "postgresql_default_port" {
  value = local.postgresql.default_port
}
```

### Attribute Reference

The `providers` attribute contains a list of objects with the following attributes:

| Attribute | Type | Description |
|-----------|------|-------------|
| `id` | string | Provider ID. |
| `name` | string | Provider name (e.g., "PostgreSQL", "MySQL", "MariaDB"). |
| `type` | string | Provider type identifier (e.g., `postgresql`, `mysql`, `mariadb`). |
| `description` | string | Provider description. |
| `version` | string | Default version. |
| `default_port` | number | Default connection port. |

### Available Providers

| Provider | Type | Default Port | Versions |
|----------|------|--------------|----------|
| PostgreSQL | `postgresql` | 5432 | 15, 16, 17, 18 |
| MySQL | `mysql` | 3306 | 8.0, 8.4, 9.0, 9.1 |
| MariaDB | `mariadb` | 3306 | 10.11, 11.4, 11.6 |

---

## danubedata_ssh_keys

Retrieves the list of SSH keys in your account.

### Example Usage

```hcl
data "danubedata_ssh_keys" "all" {}

output "ssh_key_names" {
  value = data.danubedata_ssh_keys.all.ssh_keys[*].name
}

# Find a specific key by name
locals {
  deploy_key = [
    for key in data.danubedata_ssh_keys.all.ssh_keys :
    key if key.name == "deploy-key"
  ]
}

# Use existing key in VPS
resource "danubedata_vps" "web" {
  name        = "web-server"
  image       = "ubuntu-24.04"
  datacenter  = "fsn1"
  auth_method = "ssh_key"
  ssh_key_id  = local.deploy_key[0].id
  # ...
}
```

### Checking for Existing Keys

```hcl
data "danubedata_ssh_keys" "existing" {}

# Create key only if it doesn't exist
resource "danubedata_ssh_key" "deploy" {
  count = length([
    for k in data.danubedata_ssh_keys.existing.ssh_keys :
    k if k.name == "deploy-key"
  ]) == 0 ? 1 : 0

  name       = "deploy-key"
  public_key = file("~/.ssh/id_ed25519.pub")
}
```

### Attribute Reference

The `ssh_keys` attribute contains a list of objects with the following attributes:

| Attribute | Type | Description |
|-----------|------|-------------|
| `id` | string | SSH key ID. |
| `name` | string | Key name. |
| `fingerprint` | string | SHA256 fingerprint. |
| `public_key` | string | Public key content. |
| `created_at` | string | Creation timestamp. |

---

## Resource List Data Sources

In addition to the lookup data sources above, the provider exposes a data source for each resource type that returns all of your existing resources of that type. Each returns a single list attribute you can iterate over.

| Data Source | List Attribute | Notable Item Fields | Filter Arguments |
|-------------|----------------|---------------------|------------------|
| `danubedata_vpss` | `instances` | `id`, `name`, `status`, `image`, `datacenter` | none |
| `danubedata_databases` | `instances` | `id`, `name`, `status`, `engine`, `version` | none |
| `danubedata_caches` | `instances` | `id`, `name`, `status`, `cache_provider`, `version` | none |
| `danubedata_serverless_containers` | `containers` | `id`, `name`, `status`, `deployment_type`, `image_url` | none |
| `danubedata_static_sites` | `sites` | `id`, `name`, `status`, `url` | `team_id` (required) |
| `danubedata_storage_buckets` | `buckets` | `id`, `name`, `display_name`, `status`, `region` | none |
| `danubedata_storage_access_keys` | `keys` | `id`, `name`, `access_key_id`, `status`, `access_type` | none |
| `danubedata_firewalls` | `firewalls` | `id`, `name`, `description`, `status`, `default_action` | none |
| `danubedata_parameter_groups` | `groups` | `id`, `name`, `type`, `provider_type` | `type`, `provider_type` (optional) |
| `danubedata_vps_snapshots` | `snapshots` | `id`, `name`, `status`, `vps_instance_id` | none |
| `danubedata_database_snapshots` | `snapshots` | `id`, `name`, `status`, `database_instance_id` | none |
| `danubedata_cache_snapshots` | `snapshots` | `id`, `name`, `status`, `cache_instance_id` | none |

### Example Usage

```hcl
# List all buckets and output their names
data "danubedata_storage_buckets" "all" {}

output "bucket_names" {
  value = [for b in data.danubedata_storage_buckets.all.buckets : b.name]
}

# Static sites are scoped to a team
data "danubedata_static_sites" "team" {
  team_id = 42
}

# Filter parameter groups by provider
data "danubedata_parameter_groups" "redis" {
  type          = "cache"
  provider_type = "redis"
}
```

## Using Data Sources with Count and For Each

### Dynamic VPS Creation

```hcl
variable "servers" {
  type = map(object({
    image    = string
    profile  = string
  }))
  default = {
    "web" = {
      image   = "ubuntu-24.04"
      profile = "small_shared"
    }
    "api" = {
      image   = "debian-12"
      profile = "medium_shared"
    }
  }
}

data "danubedata_ssh_keys" "all" {}

resource "danubedata_vps" "servers" {
  for_each = var.servers

  name             = each.key
  image            = each.value.image
  resource_profile = each.value.profile
  datacenter       = "fsn1"
  auth_method      = "ssh_key"
  ssh_key_id       = data.danubedata_ssh_keys.all.ssh_keys[0].id
}
```

### Conditional Resource Creation

```hcl
data "danubedata_vps_images" "available" {}

locals {
  has_alpine = length([
    for img in data.danubedata_vps_images.available.images :
    img if img.distro == "alpine"
  ]) > 0
}

resource "danubedata_vps" "alpine" {
  count = local.has_alpine ? 1 : 0

  name       = "alpine-server"
  image      = "alpine-3.20"
  datacenter = "fsn1"
  # ...
}
```

---

## Best Practices

### Cache Data Source Results

Data sources are read on every plan/apply. For performance, cache results in locals:

```hcl
data "danubedata_vps_images" "available" {}

locals {
  images        = data.danubedata_vps_images.available.images
  ubuntu_latest = [for img in local.images : img if img.image == "ubuntu-24.04"][0]
}
```

### Use Meaningful Variable Names

```hcl
data "danubedata_ssh_keys" "team_keys" {}

locals {
  production_key = [
    for key in data.danubedata_ssh_keys.team_keys.ssh_keys :
    key if key.name == "production-deploy"
  ][0]
}
```

### Handle Empty Results

```hcl
data "danubedata_ssh_keys" "all" {}

locals {
  has_keys   = length(data.danubedata_ssh_keys.all.ssh_keys) > 0
  first_key  = local.has_keys ? data.danubedata_ssh_keys.all.ssh_keys[0] : null
}
```

---

## Next Steps

- [Terraform Resources](https://docs.danubedata.ro/terraform-resources) - Create and manage resources
- [Terraform Examples](https://docs.danubedata.ro/terraform-examples) - Real-world examples
- [Terraform Overview](https://docs.danubedata.ro/terraform-overview) - Getting started guide
