# Terraform Provider

The Cloudback Terraform Provider (`terraform-provider-cloudback`) is an Infrastructure as Code (IaC) tool that enables automated management of Cloudback backup configurations through [Terraform](https://www.terraform.io/).

## Overview

This provider allows users to manage backup definitions for all supported platforms using Cloudback's [Operations API](https://docs.cloudback.it/automation/operations-api) through Terraform configurations. It provides a smooth integration between Terraform and the Cloudback platform.

## Features

* Automated backup definition configuration for repositories
* Infrastructure as Code approach to backup management
* Multi-platform support
* Support for Terraform-based workflow automation

## Prerequisites

Before you begin, ensure that you have the following:

* **Terraform Installed**: Version 1.0 or later is recommended.
* **Cloudback Account**: An active account with appropriate permissions to create and manage backup resources.
* **API Key**: Your API key for Cloudback's API. You can create one at the [API Keys](https://app.cloudback.it/apikeys) page in the Cloudback dashboard.
* **Network Access**: Ensure that your environment can reach Cloudback's endpoints: `https://app.cloudback.it/ops/`

## Usage

### Provider Configuration

```hcl
terraform {
  required_providers {
    cloudback = {
      source = "cloudback/cloudback"
    }
  }
}

provider "cloudback" {
  api_key = "your-api-key" # Replace with your API key, see https://app.cloudback.it/apikeys
}
```

**Note**: You can provide your API key either directly in the configuration or through the `CLOUDBACK_API_KEY` environment variable:

```bash
export CLOUDBACK_API_KEY="your-api-key"
```

### Resource: Backup Definition

Backup definitions are the primary resources managed by Cloudback. They represent the configuration for a specific backup subject (repository, project, or workspace).

#### GitHub Example

```hcl
resource "cloudback_backup_definition" "github_demo" {
  platform     = "GitHub"
  account      = "cloudback"
  subject_name = "demo-repository"
  subject_type = "Repository"
  settings = {
    enabled   = true
    schedule  = "Daily at 9 pm"
    storage   = "Cloudback EU"
    retention = "Last 30 days"
  }
}
```

#### Azure DevOps Repository Example

```hcl
resource "cloudback_backup_definition" "azure_devops_repo" {
  platform     = "AzureDevOps"
  account      = "my-organization"
  subject_name = "my-project/demo-repository"
  subject_type = "Repository"
  settings = {
    enabled   = true
    schedule  = "Daily at 9 pm"
    storage   = "Cloudback EU"
    retention = "Last 30 days"
  }
}
```

#### Azure DevOps Project Example

```hcl
resource "cloudback_backup_definition" "azure_devops_project" {
  platform     = "AzureDevOps"
  account      = "my-organization"
  subject_name = "my-project"
  subject_type = "Project"
  settings = {
    enabled   = true
    schedule  = "Daily at 9 pm"
    storage   = "Cloudback EU"
    retention = "Last 30 days"
  }
}
```

#### GitLab Example

```hcl
resource "cloudback_backup_definition" "gitlab_demo" {
  platform     = "GitLab"
  account      = "my-group"
  subject_name = "my-project"
  subject_type = "Project"
  settings = {
    enabled   = true
    schedule  = "Daily at 9 pm"
    storage   = "Cloudback EU"
    retention = "Last 30 days"
  }
}
```

#### Linear Example

```hcl
resource "cloudback_backup_definition" "linear_demo" {
  platform     = "Linear"
  account      = "my-workspace"
  subject_name = "my-workspace"
  subject_type = "Workspace"
  settings = {
    enabled   = true
    schedule  = "Daily at 9 pm"
    storage   = "Cloudback EU"
    retention = "Last 30 days"
  }
}
```

### Attributes

| Attribute      | Type   | Required | Description                                                       |
| -------------- | ------ | -------- | ----------------------------------------------------------------- |
| `platform`     | string | Yes      | The platform. Values: `GitHub`, `AzureDevOps`, `GitLab`, `Linear` |
| `account`      | string | Yes      | The account or organization name                                  |
| `subject_name` | string | Yes      | The backup subject name (see format below)                        |
| `subject_type` | string | Yes      | The backup subject type: `Repository`, `Project`, or `Workspace`  |
| `settings`     | object | Yes      | The backup configuration settings                                 |

#### Subject Name Format

| Platform     | Subject Type | Format                         | Example                |
| ------------ | ------------ | ------------------------------ | ---------------------- |
| GitHub       | Repository   | `repository-name`              | `demo-repository`      |
| Azure DevOps | Repository   | `project-name/repository-name` | `my-project/demo-repo` |
| Azure DevOps | Project      | `project-name`                 | `my-project`           |
| GitLab       | Project      | `project-name`                 | `my-project`           |
| Linear       | Workspace    | `workspace-name`               | `my-workspace`         |

#### Settings Object

| Attribute   | Type   | Required | Description                                                                        |
| ----------- | ------ | -------- | ---------------------------------------------------------------------------------- |
| `enabled`   | bool   | No       | Whether automated backup is enabled                                                |
| `schedule`  | string | No       | Backup schedule name ([see list](https://app.cloudback.it/schedules))              |
| `storage`   | string | No       | Storage location name ([see list](https://app.cloudback.it/storages))              |
| `retention` | string | No       | Retention policy: `Last 30 days`, `Last 90 days`, `Last 180 days`, `Last 360 days` |

### Backwards Compatibility

The `repository` attribute is still supported as an alias for `subject_name` with `subject_type` defaulting to `Repository`. However, using `subject_name` and `subject_type` is recommended for clarity, especially when working with Azure DevOps projects.

```hcl
# Legacy format (still supported for repositories)
resource "cloudback_backup_definition" "legacy_example" {
  platform   = "GitHub"
  account    = "cloudback"
  repository = "demo-repository"  # Alias for subject_name with subject_type="Repository"
  settings = {
    enabled = true
  }
}
```

## Documentation

For detailed documentation and examples, visit the [official repository](https://github.com/cloudback/terraform-provider-cloudback).

## References

* [GitHub Repository](https://github.com/cloudback/terraform-provider-cloudback)
* [Terraform Registry](https://registry.terraform.io/providers/cloudback/cloudback)
* [Cloudback Operations API](https://docs.cloudback.it/automation/operations-api)
* [GitHub Installation Guide](https://docs.cloudback.it/github/installation-guide)
* [Azure DevOps Installation Guide](https://docs.cloudback.it/azure-devops/installation-guide)
* [Linear Installation Guide](https://docs.cloudback.it/linear/installation-guide)
* [GitLab Installation Guide](https://docs.cloudback.it/gitlab/installation-guide)
* [Terraform Documentation](https://developer.hashicorp.com/terraform/docs)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cloudback.it/automation/terraform-provider.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
