Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-lifshits committed Dec 16, 2024
1 parent 52832ac commit dfddb1e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
3 changes: 3 additions & 0 deletions docs/resources/dms_topic_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions docs/resources/dms_topic_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand All @@ -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 <instance_id>/<topic_name>
```
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 '<instance_id>/<name>', 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package dms

import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -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{
Expand Down Expand Up @@ -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 '<instance_id>/<topic_name>', but '%s'", d.Id())
}

d.SetId(parts[1])
return []*schema.ResourceData{d}, d.Set("instance_id", parts[0])
}

0 comments on commit dfddb1e

Please sign in to comment.