diff --git a/README.md b/README.md index 37247ab..57ba094 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,16 @@ resource "confluentcloud_kafka_cluster" "test" { environment_id = confluentcloud_environment.environment.id } +resource "confluentcloud_schema_registry" "test" { + environment_id = confluentcloud_environment.environment.id + service_provider = "aws" + region = "EU" + + # Requires at least one kafka cluster to enable + # schema registry in the environment. + depends_on = [confluentcloud_kafka_cluster.test] +} + resource "confluentcloud_api_key" "provider_test" { cluster_id = confluentcloud_kafka_cluster.test.id environment_id = confluentcloud_environment.environment.id diff --git a/ccloud/provider.go b/ccloud/provider.go index 6f880b1..34dc545 100644 --- a/ccloud/provider.go +++ b/ccloud/provider.go @@ -29,11 +29,13 @@ func Provider() terraform.ResourceProvider { "confluentcloud_kafka_cluster": kafkaClusterResource(), "confluentcloud_api_key": apiKeyResource(), "confluentcloud_environment": environmentResource(), + "confluentcloud_schema_registry": schemaRegistryResource(), "confluentcloud_service_account": serviceAccountResource(), }, } } + func providerConfigure(d *schema.ResourceData) (interface{}, error) { log.Printf("[INFO] Initializing ConfluentCloud client") username := d.Get("username").(string) diff --git a/ccloud/resource_schema_registry.go b/ccloud/resource_schema_registry.go new file mode 100644 index 0000000..8dd5624 --- /dev/null +++ b/ccloud/resource_schema_registry.go @@ -0,0 +1,90 @@ +package ccloud + +import ( + "log" + + ccloud "github.com/cgroschupp/go-client-confluent-cloud/confluentcloud" + "github.com/hashicorp/terraform/helper/schema" +) + +func schemaRegistryResource() *schema.Resource { + return &schema.Resource{ + Create: schemaRegistryCreate, + Read: schemaRegistryRead, + Delete: schemaRegistryDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ + "environment_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Environment ID", + }, + "region": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "where", + }, + "service_provider": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + Description: "Cloud provider", + }, + "endpoint": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func schemaRegistryCreate(d *schema.ResourceData, meta interface{}) error { + c := meta.(*ccloud.Client) + + environment := d.Get("environment_id").(string) + region := d.Get("region").(string) + service_provider := d.Get("service_provider").(string) + + log.Printf("[INFO] Creating Schema Registry %s", environment) + + reg, err := c.CreateSchemaRegistry(environment, region, service_provider) + if err != nil { + return err + } + + d.SetId(reg.Name + " " + environment) + err = d.Set("endpoint", reg.Endpoint) + if err != nil { + return err + } + + return nil +} + +func schemaRegistryRead(d *schema.ResourceData, meta interface{}) error { + c := meta.(*ccloud.Client) + + environment := d.Get("environment_id").(string) + log.Printf("[INFO] Reading Schema Registry %s", environment) + + env, err := c.GetSchemaRegistry(environment) + if err != nil { + return err + } + + err = d.Set("environment_id", environment) + if err != nil { + err = d.Set("endpoint", env.Endpoint) + } + + return err +} + +func schemaRegistryDelete(d *schema.ResourceData, meta interface{}) error { + log.Printf("[INFO] Schema registry cannot be deleted: %s", d.Id()) + return nil +} \ No newline at end of file diff --git a/examples/main.tf b/examples/main.tf index 2bf0af4..b8061e2 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -22,6 +22,15 @@ resource "confluentcloud_kafka_cluster" "test" { storage = 5000 } +resource "confluentcloud_schema_registry" "test" { + environment_id = confluentcloud_environment.environment.id + service_provider = "aws" + region = "EU" + + depends_on = [confluentcloud_kafka_cluster.test] + +} + resource "confluentcloud_api_key" "provider_test" { cluster_id = confluentcloud_kafka_cluster.test.id environment_id = confluentcloud_environment.environment.id diff --git a/go.mod b/go.mod index aec9745..76d60eb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Mongey/terraform-provider-confluentcloud go 1.12 require ( - github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20201027120443-3f6a9405b3a7 + github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20201105075001-2e15b5846d7e github.com/go-resty/resty/v2 v2.2.0 // indirect github.com/hashicorp/terraform v0.12.1 golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 // indirect diff --git a/go.sum b/go.sum index fa40020..6e1eb4d 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,8 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g= github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k= -github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20201027120443-3f6a9405b3a7 h1:sakYkbNlsrF4tVwlMGiz9Wb5cOvy56SO32WudPN6Il8= -github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20201027120443-3f6a9405b3a7/go.mod h1:4qz2Pftxeus+mcGJkiysWrDwf/uQL1+jEI/Yu5BpS60= +github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20201105075001-2e15b5846d7e h1:BJ8iiXxVgxGllpGTih3FP7FtMOvXhKryqF3J3dcW1b8= +github.com/cgroschupp/go-client-confluent-cloud v0.0.0-20201105075001-2e15b5846d7e/go.mod h1:4qz2Pftxeus+mcGJkiysWrDwf/uQL1+jEI/Yu5BpS60= github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20161106042343-c914be64f07d/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=