Skip to content

Commit

Permalink
resource_alicloud_ots_table: support new sse type ByOk
Browse files Browse the repository at this point in the history
resource/alicloud_ots_search_index: fix optional bug of index_setting and index_sort
resource/alicloud_ots_table: support allow_update param
  • Loading branch information
waterxjw authored and ChenHanZhang committed Jun 4, 2024
1 parent 4d5f26f commit ad3994c
Show file tree
Hide file tree
Showing 6 changed files with 330 additions and 11 deletions.
1 change: 1 addition & 0 deletions alicloud/extension_ots.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ type SseKeyTypeString string

const (
SseKMSService = SseKeyTypeString("SSE_KMS_SERVICE")
SseByOk = SseKeyTypeString("SSE_BYOK")
)

type SecondaryIndexTypeString string
Expand Down
13 changes: 10 additions & 3 deletions alicloud/resource_alicloud_ots_search_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,11 @@ func parseIndexSort(schemaArg map[string]interface{}) (*search.Sort, error) {
var indexSort *search.Sort
if v, ok := schemaArg["index_sort"]; ok {
var sorts []search.Sorter

indexSortArg := v.(*schema.Set).List()[0].(map[string]interface{})
valSet := v.(*schema.Set)
if valSet == nil || valSet.Len() == 0 {
return indexSort, nil
}
indexSortArg := valSet.List()[0].(map[string]interface{})
if v, ok := indexSortArg["sorter"]; ok {
sortersArg := v.([]interface{})
for _, s := range sortersArg {
Expand Down Expand Up @@ -308,7 +311,11 @@ func parseIndexSort(schemaArg map[string]interface{}) (*search.Sort, error) {

func parseIndexSetting(schemaArg map[string]interface{}) *tablestore.IndexSetting {
if v, ok := schemaArg["index_setting"]; ok {
indexSettingArg := v.(*schema.Set).List()[0].(map[string]interface{})
valSet := v.(*schema.Set)
if valSet == nil || valSet.Len() == 0 {
return nil
}
indexSettingArg := valSet.List()[0].(map[string]interface{})
if v, ok := indexSettingArg["routing_fields"]; ok {
routersArg := v.([]interface{})
var routerPKs []string
Expand Down
128 changes: 127 additions & 1 deletion alicloud/resource_alicloud_ots_search_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAlicloudOtsSearchIndex_basic(t *testing.T) {
func TestAccAliCloudOtsSearchIndex_basic(t *testing.T) {
var v *tablestore.DescribeTableResponse

resourceId := "alicloud_ots_search_index.default"
Expand Down Expand Up @@ -155,6 +155,132 @@ func resourceOtsSearchIndexConfig(name string) string {
`, name, string(OtsCapacity), name)
}

func TestAccAliCloudOtsSearchIndex_ttl(t *testing.T) {
var v *tablestore.DescribeTableResponse

resourceId := "alicloud_ots_search_index.default"
ra := resourceAttrInit(resourceId, otsSearchIndexBasicMap)
serviceFunc := func() interface{} {
return &OtsService{testAccProvider.Meta().(*connectivity.AliyunClient)}
}
rc := resourceCheckInit(resourceId, &v, serviceFunc)

rac := resourceAttrCheckInit(rc, ra)

testAccCheck := rac.resourceAttrMapUpdateSet()
rand := acctest.RandIntRange(10000, 99999)
name := fmt.Sprintf("testAcc%d", rand)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckWithRegions(t, false, connectivity.OtsCapacityNoSupportedRegions)
},
// module name
IDRefreshName: resourceId,
Providers: testAccProviders,
CheckDestroy: rac.checkResourceDestroy(),
Steps: []resource.TestStep{
{
Config: resourceOtsSearchIndexTtlConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheck(getSearchIndexCheckMap()),
),
},
{
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"schema", "time_to_live", "index_name", "table_name", "instance_name"},
},
},
})
}

func resourceOtsSearchIndexTtlConfig(name string) string {
return fmt.Sprintf(`
variable "name" {
default = "%s"
}
variable "pks" {
default = ["pk1", "pk2"]
}
variable "cols" {
default = ["col2", "col1"]
}
resource "alicloud_ots_instance" "default" {
name = "tf-${var.name}"
description = "${var.name}"
accessed_by = "Any"
instance_type = "%s"
tags = {
Created = "TF"
For = "acceptance test"
}
}
resource "alicloud_ots_table" "default" {
instance_name = "${alicloud_ots_instance.default.name}"
table_name = "${var.name}"
primary_key {
name = "pk1"
type = "Integer"
}
primary_key {
name = "pk2"
type = "String"
}
defined_column {
name = "col1"
type = "String"
}
defined_column {
name = "col2"
type = "Integer"
}
time_to_live = -1
max_version = 1
allow_update = false
}
resource "alicloud_ots_search_index" "default" {
instance_name = "${alicloud_ots_instance.default.name}"
table_name = "${alicloud_ots_table.default.table_name}"
index_name = "%s_search_index"
time_to_live = 86400
schema {
field_schema {
field_name = "col1"
field_type = "Text"
is_array = false
index = true
analyzer = "Split"
store = true
}
field_schema {
field_name = "col2"
field_type = "Long"
enable_sort_and_agg = true
}
field_schema {
field_name = "pk1"
field_type = "Long"
}
field_schema {
field_name = "pk2"
field_type = "Text"
}
}
}
`, name, string(OtsCapacity), name)
}

var otsSearchIndexBasicMap = map[string]string{}

func getSearchIndexCheckMap() map[string]string {
Expand Down
42 changes: 40 additions & 2 deletions alicloud/resource_alicloud_ots_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func resourceAlicloudOtsTable() *schema.Resource {
Required: true,
ValidateFunc: validation.IntBetween(1, INT_MAX),
},
"allow_update": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"deviation_cell_version_in_sec": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -104,7 +109,21 @@ func resourceAlicloudOtsTable() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(SseKMSService)}, false),
string(SseKMSService), string(SseByOk)}, false),
},
"sse_key_id": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return string(SseByOk) != d.Get("sse_key_type").(string)
},
},
"sse_role_arn": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return string(SseByOk) != d.Get("sse_key_type").(string)
},
},
},
}
Expand Down Expand Up @@ -150,6 +169,8 @@ func resourceAliyunOtsTableCreate(d *schema.ResourceData, meta interface{}) erro
tableOption := new(tablestore.TableOption)
tableOption.TimeToAlive = d.Get("time_to_live").(int)
tableOption.MaxVersion = d.Get("max_version").(int)
allowUpdate := d.Get("allow_update").(bool)
tableOption.AllowUpdate = &allowUpdate
if deviation, ok := d.GetOk("deviation_cell_version_in_sec"); ok {
tableOption.DeviationCellVersionInSec, _ = strconv.ParseInt(deviation.(string), 10, 64)
}
Expand All @@ -169,11 +190,22 @@ func resourceAliyunOtsTableCreate(d *schema.ResourceData, meta interface{}) erro
switch sseKeyType.(string) {
case string(SseKMSService):
typ = tablestore.SSE_KMS_SERVICE
case string(SseByOk):
typ = tablestore.SSE_BYOK
default:
return WrapError(Error("unknown sse key type: " + sseKeyType.(string)))
}
sseSpec.KeyType = &typ
}
if sseKeyId, ok3 := d.GetOk("sse_key_id"); ok3 {
keyId := sseKeyId.(string)
sseSpec.KeyId = &keyId
}

if sseRoleArn, ok4 := d.GetOk("sse_role_arn"); ok4 {
roleArn := sseRoleArn.(string)
sseSpec.RoleArn = &roleArn
}
request.SSESpecification = sseSpec
}

Expand Down Expand Up @@ -248,11 +280,15 @@ func resourceAliyunOtsTableRead(d *schema.ResourceData, meta interface{}) error

d.Set("time_to_live", tableResp.TableOption.TimeToAlive)
d.Set("max_version", tableResp.TableOption.MaxVersion)
d.Set("allow_update", *tableResp.TableOption.AllowUpdate)
d.Set("deviation_cell_version_in_sec", strconv.FormatInt(tableResp.TableOption.DeviationCellVersionInSec, 10))

if tableResp.SSEDetails != nil && tableResp.SSEDetails.Enable {
d.Set("enable_sse", tableResp.SSEDetails.Enable)
d.Set("sse_key_type", tableResp.SSEDetails.KeyType.String())
d.Set("sse_key_id", tableResp.SSEDetails.KeyId)
d.Set("sse_role_arn", tableResp.SSEDetails.RoleArn)

}

return nil
Expand All @@ -267,7 +303,7 @@ func resourceAliyunOtsTableUpdate(d *schema.ResourceData, meta interface{}) erro
}
client := meta.(*connectivity.AliyunClient)

if d.HasChange("time_to_live") || d.HasChange("max_version") || d.HasChange("deviation_cell_version_in_sec") {
if d.HasChange("time_to_live") || d.HasChange("max_version") || d.HasChange("deviation_cell_version_in_sec") || d.HasChange("allow_update") {
request := new(tablestore.UpdateTableRequest)
request.TableName = tableName
tableOption := new(tablestore.TableOption)
Expand All @@ -277,6 +313,8 @@ func resourceAliyunOtsTableUpdate(d *schema.ResourceData, meta interface{}) erro
if deviation, ok := d.GetOk("deviation_cell_version_in_sec"); ok {
tableOption.DeviationCellVersionInSec, _ = strconv.ParseInt(deviation.(string), 10, 64)
}
allowUpdate := d.Get("allow_update").(bool)
tableOption.AllowUpdate = &allowUpdate

request.TableOption = tableOption
var requestinfo *tablestore.TableStoreClient
Expand Down
Loading

0 comments on commit ad3994c

Please sign in to comment.