diff --git a/docs/resources/dms_topic_v1.md b/docs/resources/dms_topic_v1.md index c87a20d5e..1c176f7c2 100644 --- a/docs/resources/dms_topic_v1.md +++ b/docs/resources/dms_topic_v1.md @@ -14,6 +14,9 @@ Up-to-date reference of API arguments for DMS topic you can get at Manages a DMS topic in the OpenTelekomCloud DMS Service (Kafka Premium/Platinum). +~> +Deprecated, use `opentelekomcloud_dms_topic_v2` resource instead + ## Example Usage: creating dms instance with topic ```hcl diff --git a/docs/resources/dms_topic_v2.md b/docs/resources/dms_topic_v2.md index 17f41675b..ab45d611a 100644 --- a/docs/resources/dms_topic_v2.md +++ b/docs/resources/dms_topic_v2.md @@ -66,26 +66,26 @@ resource "opentelekomcloud_dms_topic_v2" "topic_1" { The following arguments are supported: -* `instance_id` - (Required) Indicates the ID of primary DMS instance. +* `instance_id` - (Required, String, ForceNew) Indicates the ID of primary DMS instance. -* `name` - (Required) Indicates the name of a topic. +* `name` - (Required, String, ForceNew) Indicates the name of a topic. -* `partition` - (Optional) Indicates the number of topic partitions, +* `partition` - (Optional, Int, ForceNew) Indicates the number of topic partitions, which is used to set the number of concurrently consumed messages. Value range: `1–200`. Default value: `3`. -* `replication` - (Optional) Indicates the number of replicas, +* `replication` - (Optional, Int, ForceNew) Indicates the number of replicas, which is configured to ensure data reliability. Value range: `1–3`. Default value: `3`. -* `sync_replication` - (Optional) Indicates whether to enable synchronous replication. +* `sync_replication` - (Optional, Bool, ForceNew) Indicates whether to enable synchronous replication. After this function is enabled, the `acks` parameter on the producer client must be set to `–1`. Otherwise, this parameter does not take effect. -* `retention_time` - (Required) Indicates the retention period of a message. Its default value is `72`. +* `retention_time` - (Optional, Int, ForceNew) Indicates the retention period of a message. Its default value is `72`. Value range: `1–720`. Default value: `72`. Unit: `hour`. -* `sync_message_flush` - (Optional) Indicates whether to enable synchronous flushing. +* `sync_message_flush` - (Optional, Bool, ForceNew) Indicates whether to enable synchronous flushing. Default value: `false`. Synchronous flushing compromises performance. @@ -98,3 +98,11 @@ All above argument parameters can be exported as attribute parameters along with * `remain_partitions` - Number of remaining partitions. * `max_partitions` - Total partitions number. + +## Import + +DMS topics can be imported using their `topic_name` and related `instance_id`, separated by a slash, e.g. + +```bash +$ terraform import opentelekomcloud_dms_topic_v2.test_topic / +``` diff --git a/opentelekomcloud/acceptance/dms/resource_opentelekomcloud_dms_topic_v2_test.go b/opentelekomcloud/acceptance/dms/resource_opentelekomcloud_dms_topic_v2_test.go index 306dd18c5..67f84fdd9 100644 --- a/opentelekomcloud/acceptance/dms/resource_opentelekomcloud_dms_topic_v2_test.go +++ b/opentelekomcloud/acceptance/dms/resource_opentelekomcloud_dms_topic_v2_test.go @@ -6,15 +6,53 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/opentelekomcloud/gophertelekomcloud/openstack/dms/v2/topics" "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" ) const resourceTopicV2Name = "opentelekomcloud_dms_topic_v2.topic_1" +func geDmsTopicFunc(config *cfg.Config, state *terraform.ResourceState) (interface{}, error) { + client, err := config.DmsV2Client(env.OS_REGION_NAME) + if err != nil { + return nil, fmt.Errorf("error creating DMS v2 client: %s", err) + } + var fTopic topics.Topic + + v, err := topics.List(client, state.Primary.Attributes["instance_id"]) + if err != nil { + return nil, fmt.Errorf("provided topic doesn't exist") + } + found := false + + for _, topic := range v.Topics { + if topic.Name == state.Primary.ID { + fTopic = topic + found = true + break + } + } + if !found { + return nil, fmt.Errorf("provided topic doesn't exist") + } + + return fTopic, nil +} + func TestAccDmsTopicsV2_basic(t *testing.T) { + var instance topics.Topic var instanceName = fmt.Sprintf("dms_instance_%s", acctest.RandString(5)) var topicName = fmt.Sprintf("topic_instance_%s", acctest.RandString(5)) + rc := common.InitResourceCheck( + resourceTopicV2Name, + &instance, + geDmsTopicFunc, + ) + resource.Test(t, resource.TestCase{ PreCheck: func() { common.TestAccPreCheck(t) }, ProviderFactories: common.TestAccProviderFactories, @@ -23,16 +61,37 @@ func TestAccDmsTopicsV2_basic(t *testing.T) { { Config: testAccDmsV2TopicBasic(instanceName, topicName), Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), resource.TestCheckResourceAttr(resourceTopicV2Name, "partition", "200"), resource.TestCheckResourceAttr(resourceTopicV2Name, "replication", "3"), resource.TestCheckResourceAttr(resourceTopicV2Name, "sync_replication", "true"), resource.TestCheckResourceAttr(resourceTopicV2Name, "retention_time", "600"), ), }, + { + ResourceName: resourceTopicV2Name, + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: testAccDmsTopicImportStateFunc(resourceTopicV2Name), + }, }, }) } +func testAccDmsTopicImportStateFunc(rName string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[rName] + if !ok { + return "", fmt.Errorf("resource (%s) not found: %s", rName, rs) + } + if rs.Primary.Attributes["instance_id"] == "" { + return "", fmt.Errorf("invalid format specified for import ID, want '/', but '%s/%s'", + rs.Primary.Attributes["instance_id"], rs.Primary.ID) + } + return fmt.Sprintf("%s/%s", rs.Primary.Attributes["instance_id"], rs.Primary.ID), nil + } +} + func testAccDmsV2TopicBasic(instanceName string, topicName string) string { return fmt.Sprintf(` %s diff --git a/opentelekomcloud/services/dms/resource_opentelekomcloud_dms_topic_v2.go b/opentelekomcloud/services/dms/resource_opentelekomcloud_dms_topic_v2.go index 55c81a36d..88a28a8b0 100644 --- a/opentelekomcloud/services/dms/resource_opentelekomcloud_dms_topic_v2.go +++ b/opentelekomcloud/services/dms/resource_opentelekomcloud_dms_topic_v2.go @@ -2,7 +2,9 @@ package dms import ( "context" + "fmt" "log" + "strings" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -21,7 +23,7 @@ func ResourceDmsTopicsV2() *schema.Resource { ReadContext: resourceDmsTopicsV2Read, DeleteContext: resourceDmsTopicsV2Delete, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: resourceDmsTopicV2ImportState, }, Schema: map[string]*schema.Schema{ @@ -189,3 +191,14 @@ func resourceDmsTopicsV2Delete(ctx context.Context, d *schema.ResourceData, meta d.SetId("") return nil } + +func resourceDmsTopicV2ImportState(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, + error) { + parts := strings.SplitN(d.Id(), "/", 2) + if len(parts) != 2 { + return nil, fmt.Errorf("invalid format for import ID, want '/', but '%s'", d.Id()) + } + + d.SetId(parts[1]) + return []*schema.ResourceData{d}, d.Set("instance_id", parts[0]) +}