Skip to content

Commit

Permalink
[Feat] New resource opentelekomcloud_css_configuration_v1 (#2792)
Browse files Browse the repository at this point in the history
[Feat] New resource opentelekomcloud_css_configuration_v1

Summary of the Pull Request
Resource for changing cluster configurations
PR Checklist

 Refers to: #2789
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestAccCssConfiguration_basic
=== PAUSE TestAccCssConfiguration_basic
=== CONT  TestAccCssConfiguration_basic
--- PASS: TestAccCssConfiguration_basic (210.78s)
PASS

Process finished with the exit code 0

Reviewed-by: Muneeb H. Jan <[email protected]>
  • Loading branch information
anton-sidelnikov authored Jan 21, 2025
1 parent 45c9b00 commit c25c896
Show file tree
Hide file tree
Showing 10 changed files with 552 additions and 2 deletions.
93 changes: 93 additions & 0 deletions docs/resources/css_configuration_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
subcategory: "Cloud Search Service (CSS)"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_css_configuration_v1"
sidebar_current: "docs-opentelekomcloud-resource-css-configuration-v1"
description: |-
Manage CSS cluster configurations in OpenTelekomCloud.
---

Up-to-date reference for API arguments and details can be found at the [documentation portal](https://docs.otc.t-systems.com/cloud-search-service/api-ref/parameter_configuration/index.html).

# opentelekomcloud_css_configuration_v1

Manage the configuration settings of a CSS cluster in OpenTelekomCloud.

## Example Usage

```hcl
resource "opentelekomcloud_css_configuration_v1" "example" {
cluster_id = "your-cluster-id"
http_cors_allow_credentials = "true"
http_cors_allow_origin = "122.122.122.122:9200"
indices_queries_cache_size = "50"
auto_create_index = "true"
}
```

## Argument Reference

The following arguments are supported:

* `cluster_id` - (Required, String, ForceNew) The CSS cluster ID.

Changing this parameter will create a new resource.

* `http_cors_allow_credetials` - (Optional, String) Whether to return the Access-Control-Allow-Credentials of
the header during cross-domain access.
The value can be `true` or `false`. Default value: `false`.

* `http_cors_allow_origin` - (Optional, String) Origin IP address allowed for cross-domain access, for example, `122.122.122.122:9200`.

* `http_cors_max_age` - (Optional, String) Cache duration of the browser. The cache is automatically cleared
after the time range you specify.
Unit: s, Default value: `1,728,000`.

* `http_cors_allow_headers` - (Optional, String) Headers allowed for cross-domain access.
Including `X-Requested-With`, `Content-Type`, and `Content-Length`.
Use commas (,) and spaces to separate headers.

* `http_cors_enabled` - (Optional, String) Whether to allow cross-domain access.
The value can be `true` or `false`. Default value: `false`.

* `http_cors_allow_methods` - (Optional, String) Methods allowed for cross-domain access.
Including `OPTIONS`, `HEAD`, `GET`, `POST`, `PUT`, and `DELETE`.
Use commas (,) and spaces to separate methods.

* `reindex_remote_whitelist` - (Optional, String) Configured for migrating data from the current cluster to
the target cluster through the reindex API.
The example value is `122.122.122.122:9200`.

* `indices_queries_cache_size` - (Optional, String) Cache size in the query phase. Value range: `1%` to `100%`.
Unit: %, Default value: `10%`.

* `thread_pool_force_merge_size` - (Optional, String) Queue size in the force merge thread pool.
Default value: `1`.

* `auto_create_index` - (Optional, String) Whether to auto-create an index.
The value can be `true` or `false`.


## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID which equals the `cluster_id`.

* `region` - The region where the CSS cluster is deployed.


## Timeouts

This resource provides the following timeouts configuration options:

* `create` - Default is 20 minutes.
* `delete` - Default is 20 minutes.

## Import

The CSS configuration can be imported using the `id` which equals the `cluster_id`, e.g.

```bash
$ terraform import opentelekomcloud_css_configuration_v1.test <id>
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20250115161007-a7fae3c659fc
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20250117142924-89e7b723fcc7
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.31.0
golang.org/x/sync v0.10.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20250115161007-a7fae3c659fc h1:JDr/sgKTh98agnyd5aEZp7EiYKTjwHAG5FW+bGFboig=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20250115161007-a7fae3c659fc/go.mod h1:la8cQVYopRoEbNe2L7HlGTdLxUQOwIqHp1VHtjE/5qA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20250117142924-89e7b723fcc7 h1:MGGtjRYekil3cch//l2gQBZWv32d/4ztF9GlO6FfCBI=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20250117142924-89e7b723fcc7/go.mod h1:la8cQVYopRoEbNe2L7HlGTdLxUQOwIqHp1VHtjE/5qA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package acceptance

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
pc "github.com/opentelekomcloud/gophertelekomcloud/openstack/css/v1/parameter-configuration"
"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"
)

func getCssConfigurationV1ResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
c, err := cfg.CssV1Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating APIG v2 client: %s", err)
}
configurations, err := pc.List(c, state.Primary.ID)
if err != nil {
return nil, fmt.Errorf("error retrieving OpenTelekomCloud CSS configuration: %s", err)
}
for _, template := range configurations.Templates {
if template.Value != template.DefaultValue {
return configurations, nil
}
}
return nil, golangsdk.ErrDefault404{}
}

func TestAccCssConfiguration_basic(t *testing.T) {
clusterID := os.Getenv("OS_CSS_CLUSTER_ID")
if clusterID == "" {
t.Skip("OS_CSS_CLUSTER_ID env var is not set")
}

var obj pc.Configurations
rName := "opentelekomcloud_css_configuration_v1.config"
rc := common.InitResourceCheck(
rName,
&obj,
getCssConfigurationV1ResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testCssConfigurationV1_basic(clusterID),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "thread_pool_force_merge_size", "3"),
resource.TestCheckResourceAttr(rName, "http_cors_allow_credentials", "true"),
),
},
{
Config: testCssConfigurationV1_update(clusterID),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "thread_pool_force_merge_size", "4"),
resource.TestCheckResourceAttr(rName, "http_cors_allow_credentials", "true"),
resource.TestCheckResourceAttr(rName, "http_cors_allow_headers", "X-Requested-With, Content-Type"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCssConfigurationV1_basic(clusterID string) string {
return fmt.Sprintf(`
resource "opentelekomcloud_css_configuration_v1" "config" {
cluster_id = "%s"
thread_pool_force_merge_size = "3"
http_cors_allow_credentials = true
}
`, clusterID)
}

func testCssConfigurationV1_update(clusterID string) string {
return fmt.Sprintf(`
resource "opentelekomcloud_css_configuration_v1" "config" {
cluster_id = "%s"
thread_pool_force_merge_size = "4"
http_cors_allow_credentials = true
http_cors_allow_headers = "X-Requested-With, Content-Type"
auto_create_index = true
}
`, clusterID)
}
58 changes: 58 additions & 0 deletions opentelekomcloud/common/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package common

import (
"encoding/json"
"errors"
"fmt"
"log"
"reflect"

golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
)

// ConvertExpected403ErrInto404Err is a method used to parsing 403 error and try to convert it to 404 error according
// to the right error code.
// Arguments:
// + err: The error response obtained through HTTP/HTTPS request.
// + errCodeKey: The key name of the error code in the error response body, e.g. 'error_code', 'err_code'.
// + specErrCodes: One or more error codes that you wish to match against the current error, e.g. 'APIGW.0001'.
// Notes: If you missing specErrCodes input, this function will convert all 403 errors into 404 errors.
// How to use it:
// + For the general cases, their error code key is 'error_code', and we should call as follows:
// - utils.ConvertExpected403ErrInto404Err(err, "error_code")
// - utils.ConvertExpected403ErrInto404Err(err, "error_code", "DWS.0001")
// - utils.ConvertExpected403ErrInto404Err(err, "error_code", []string{"DWS.0001", "DLM.3028"}...)
func ConvertExpected403ErrInto404Err(err error, errCodeKey string, specErrCodes ...string) error {
var err403 golangsdk.ErrDefault403
if !errors.As(err, &err403) {
log.Printf("[WARN] Unable to recognize expected error type, want 'golangsdk.ErrDefault403', but got '%s'",
reflect.TypeOf(err).String())
return err
}
var apiError interface{}
if jsonErr := json.Unmarshal(err403.Body, &apiError); jsonErr != nil {
return err
}

errCode := PathSearch(errCodeKey, apiError, nil)
if errCode == nil {
// 4xx means the client parsing was failed.
return golangsdk.ErrDefault400{
ErrUnexpectedResponseCode: golangsdk.ErrUnexpectedResponseCode{
Body: []byte(fmt.Sprintf("Unable to find the error code from the error body using given error code key (%s), the error is: %#v",
errCodeKey, apiError)),
},
}
}

if len(specErrCodes) < 1 {
log.Printf("[INFO] Identified 403 error parsed it as 404 error (without the error code control)")
return golangsdk.ErrDefault404{}
}
if StrSliceContains(specErrCodes, fmt.Sprint(errCode)) {
log.Printf("[INFO] Identified 403 error with code '%v' and parsed it as 404 error", errCode)
return golangsdk.ErrDefault404{}
}
log.Printf("[WARN] Unable to recognize expected error code (%v), want %v", errCode, specErrCodes)
return err
}
7 changes: 7 additions & 0 deletions opentelekomcloud/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ func ValueIgnoreEmpty(v interface{}) interface{} {
return v
}

// SetIfNotEmpty to set key if value is not empty
func SetIfNotEmpty(target map[string]interface{}, key string, value interface{}) {
if value != nil && value != "" {
target[key] = value
}
}

// PathSearch evaluates a JMESPath expression against input data and returns the result.
func PathSearch(expression string, obj interface{}, defaultValue interface{}) interface{} {
v, err := jmespath.Search(expression, obj)
Expand Down
1 change: 1 addition & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_cts_tracker_v1": cts.ResourceCTSTrackerV1(),
"opentelekomcloud_cts_tracker_v3": cts.ResourceCTSTrackerV3(),
"opentelekomcloud_css_cluster_v1": css.ResourceCssClusterV1(),
"opentelekomcloud_css_configuration_v1": css.ResourceCssConfigurationV1(),
"opentelekomcloud_css_snapshot_configuration_v1": css.ResourceCssSnapshotConfigurationV1(),
"opentelekomcloud_direct_connect_v2": dcaas.ResourceDirectConnectV2(),
"opentelekomcloud_dc_endpoint_group_v2": dcaas.ResourceDCEndpointGroupV2(),
Expand Down
Loading

0 comments on commit c25c896

Please sign in to comment.