Skip to content

Commit

Permalink
chore: add docs for import
Browse files Browse the repository at this point in the history
  • Loading branch information
Noroth committed Jan 20, 2025
1 parent 100a4d4 commit 23e40cb
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 90 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ output "router_token" {

Further in depth examples can be found in the [examples](examples) directory.

## Importing resources

Resources can be imported into Terraform using the `terraform import` command. The following resources are supported for import:

- `cosmo_namespace`
- `cosmo_federated_graph`
- `cosmo_subgraph`
- `cosmo_contract`
- `cosmo_monograph`

For detailed instructions on how to import each resource, refer to the corresponding resource documentation in the corresponding [docs](docs/resources).

## Cosmo Local Example

The module [cosmo-local](examples/cosmo-local) contains an example of how to use the provider to manage a local cosmo setup on minikube.
Expand Down
9 changes: 9 additions & 0 deletions docs/resources/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ resource "cosmo_contract" "test" {
### Read-Only

- `id` (String) The ID of this resource.

## Import

Import is supported using the following syntax:

```shell
# Contracts can be imported using the UUID of the contract, e.g.
terraform import cosmo_contract.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
```
9 changes: 9 additions & 0 deletions docs/resources/federated_graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ resource "cosmo_federated_graph" "test" {
### Read-Only

- `id` (String) The unique identifier of the federated graph resource, automatically generated by the system.

## Import

Import is supported using the following syntax:

```shell
# Federated Graphs can be imported using the UUID of the federated graph.
terraform import cosmo_federated_graph.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
```
9 changes: 9 additions & 0 deletions docs/resources/monograph.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ resource "cosmo_monograph" "example" {
### Read-Only

- `id` (String) The unique identifier of the monograph resource.

## Import

Import is supported using the following syntax:

```shell
# Monographs can be imported using the UUID of the monograph, e.g.
terraform import cosmo_monograph.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
```
11 changes: 11 additions & 0 deletions docs/resources/namespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ resource "cosmo_namespace" "test" {
### Read-Only

- `id` (String) The unique identifier of the namespace resource.

## Import

Import is supported using the following syntax:

```shell
# Namespaces can be imported using the name of the namespace
terraform import cosmo_namespace.example my-namespace
# or alternatively, the UUID of the namespace
terraform import cosmo_namespace.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
```
9 changes: 9 additions & 0 deletions docs/resources/subgraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ resource "cosmo_subgraph" "test" {
### Read-Only

- `id` (String) The unique identifier of the subgraph resource.

## Import

Import is supported using the following syntax:

```shell
# Subgraphs can be imported using the UUID of the subgraph, e.g.
terraform import cosmo_subgraph.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
```
18 changes: 0 additions & 18 deletions examples/import/import-resources/import.tf

This file was deleted.

8 changes: 0 additions & 8 deletions examples/import/import-resources/provider.tf

This file was deleted.

53 changes: 0 additions & 53 deletions examples/import/main.tf

This file was deleted.

8 changes: 0 additions & 8 deletions examples/import/provider.tf

This file was deleted.

2 changes: 2 additions & 0 deletions examples/resources/cosmo_contract/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Contracts can be imported using the UUID of the contract, e.g.
terraform import cosmo_contract.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
2 changes: 2 additions & 0 deletions examples/resources/cosmo_federated_graph/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Federated Graphs can be imported using the UUID of the federated graph.
terraform import cosmo_federated_graph.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
2 changes: 2 additions & 0 deletions examples/resources/cosmo_monograph/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Monographs can be imported using the UUID of the monograph, e.g.
terraform import cosmo_monograph.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
4 changes: 4 additions & 0 deletions examples/resources/cosmo_namespace/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Namespaces can be imported using the name of the namespace
terraform import cosmo_namespace.example my-namespace
# or alternatively, the UUID of the namespace
terraform import cosmo_namespace.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
2 changes: 2 additions & 0 deletions examples/resources/cosmo_subgraph/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Subgraphs can be imported using the UUID of the subgraph, e.g.
terraform import cosmo_subgraph.example 0b1b1b1b-1b1b-1b1b-1b1b-1b1b1b1b1b1b
8 changes: 8 additions & 0 deletions internal/service/namespace/import_cosmo_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ func TestAccCosmoNamespaceImportBasic(t *testing.T) {
Config: testAccNamespaceResourceConfig(name),
},
{
// import via ID
ResourceName: "cosmo_namespace.test",
ImportState: true,
ImportStateVerify: true,
},
{
// import via name
ResourceName: "cosmo_namespace.test",
ImportState: true,
ImportStateId: name,
ImportStateVerify: true,
},
},
})
}
20 changes: 17 additions & 3 deletions internal/service/namespace/resource_cosmo_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package namespace
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -13,6 +12,11 @@ import (
"github.com/wundergraph/cosmo/terraform-provider-cosmo/internal/utils"
)

var (
_ resource.Resource = (*NamespaceResource)(nil)
_ resource.ResourceWithImportState = (*NamespaceResource)(nil)
)

type NamespaceResource struct {
client *api.PlatformClient
}
Expand Down Expand Up @@ -195,5 +199,15 @@ func getNamespace(ctx context.Context, client api.PlatformClient, id, name strin
}

func (r *NamespaceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
var data NamespaceResourceModel

id := req.ID

if err := uuid.Validate(id); err != nil {
data.Name = types.StringValue(id) // We assume this is the namespace name
} else {
data.Id = types.StringValue(id)
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

0 comments on commit 23e40cb

Please sign in to comment.