Skip to content

Commit

Permalink
Support importing existing Kafka clusters (fixes #64) (#74)
Browse files Browse the repository at this point in the history
This change should address the issue reported in #64 wherein it's not currently possible to import existing Kafka clusters.

The issue is that the read function for clusters requires both the cluster ID and the containing environment ID, which means that using the pass-through state importer function doesn't work.

I've addressed this by allowing clusters to be imported by passing an import ID of the form `<environment ID>/<cluster ID>`.
  • Loading branch information
benweint authored and Mongey committed Apr 26, 2021
1 parent 4bae913 commit d07a988
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ output "secret" {
}
```

## Importing existing resources

This provider supports importing existing Confluent Cloud resources via [`terraform import`][3].

Most resource types use the import IDs returned by the [`ccloud`][4] CLI, with the exception of clusters, which expect an import ID of the form `<environment ID>/<cluster ID>`.

[1]: https://www.terraform.io
[2]: https://confluent.cloud
[3]: https://www.terraform.io/docs/cli/import/index.html
[4]: https://docs.confluent.io/ccloud-cli/current/index.html
[third-party-plugins]: https://www.terraform.io/docs/configuration/providers.html#third-party-plugins
20 changes: 19 additions & 1 deletion ccloud/resource_kafka_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func kafkaClusterResource() *schema.Resource {
ReadContext: clusterRead,
DeleteContext: clusterDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: clusterImport,
},
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -243,6 +243,24 @@ func clusterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}
return diags
}

func clusterImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
envIDAndClusterID := d.Id()
parts := strings.Split(envIDAndClusterID, "/")

var err error
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format for cluster import: expected '<env ID>/<cluster ID>'")
}

d.SetId(parts[1])
err = d.Set("environment_id", parts[0])
if err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}

func clusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*ccloud.Client)
accountID := d.Get("environment_id").(string)
Expand Down
13 changes: 13 additions & 0 deletions ccloud/resource_kafka_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/hashicorp/go-uuid"
r "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -38,6 +40,17 @@ func TestAcc_BasicCluster(t *testing.T) {
{
Config: fmt.Sprintf(testResourceCluster_noConfig, u, u),
},
{
ResourceName: "confluentcloud_kafka_cluster.test",
ImportState: true,
ImportStateIdFunc: func(state *terraform.State) (string, error) {
resources := state.RootModule().Resources
clusterId := resources["confluentcloud_kafka_cluster.test"].Primary.ID
envId := resources["confluentcloud_environment.test"].Primary.ID
return envId + "/" + clusterId, nil
},
ImportStateVerify: true,
},
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/go-resty/resty/v2 v2.2.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/terraform-plugin-docs v0.3.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.4
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
Expand Down

0 comments on commit d07a988

Please sign in to comment.