Skip to content

Commit

Permalink
RMS: advanced queries (#2698)
Browse files Browse the repository at this point in the history
RMS: advanced queries

Summary of the Pull Request
Advanced queries resource
PR Checklist

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

Acceptance Steps Performed
2024/10/28 13:53:06 [INFO] Building Swift S3 auth structure
2024/10/28 13:53:06 [INFO] Setting AWS metadata API timeout to 100ms
2024/10/28 13:53:07 [INFO] Ignoring AWS metadata API endpoint at default location as it doesn't return any instance-id
2024/10/28 13:53:07 [INFO] Swift S3 Auth provider used: "StaticProvider"
=== RUN   TestAccAdvancedQuery_basic
=== PAUSE TestAccAdvancedQuery_basic
=== CONT  TestAccAdvancedQuery_basic
--- PASS: TestAccAdvancedQuery_basic (198.97s)
PASS

Process finished with the exit code 0

Reviewed-by: Anton Sidelnikov
  • Loading branch information
artem-lifshits authored Oct 28, 2024
1 parent 36980f7 commit 5ab7b8f
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 8 deletions.
56 changes: 56 additions & 0 deletions docs/resources/rms_advanced_query_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
subcategory: "Config"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_rms_advanced_query_v1"
sidebar_current: "docs-opentelekomcloud-rms-advanced-query-v1"
description: |-
Manages an RMS Advanced query resource within OpenTelekomCloud.
---

Up-to-date reference of API arguments for RDS replica you can get at
[documentation portal](https://docs.otc.t-systems.com/config/api-ref/apis/advanced_queries/index.html)

# opentelekomcloud_rms_advanced_query_v1

Manages an RMS advanced query resource within OpenTelekomCloud.

## Example Usage

```hcl
resource "opentelekomcloud_rms_advanced_query_v1" "test" {
name = "advanced_query_name"
expression = "select * from table_test"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required, String, ForceNew) Specifies the advanced query name. It contains 1 to 64 characters.

Changing this parameter will create a new resource.

* `expression` - (Required, String) Specifies the advanced query expression. It contains 1 to 4096 characters.

* `description` - (Optional, String) Specifies the advanced query description. It contains 1 to 512 characters.

## Attribute Reference

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

* `id` - The resource ID.

* `type` - The resource type.

* `created_at` - The resource creation time.

* `updated_at` - The resource update time.

## Import

The RMS advanced query can be imported using the `id`, e.g.

```bash
$ terraform import opentelekomcloud_rms_advanced_query_v1.test <id>
```
7 changes: 4 additions & 3 deletions docs/resources/rms_resource_recorder_v1.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
subcategory: "Config"
layout: "opentelekomcloud"
page_title: "OpenTelekomCloud: opentelekomcloud_resource_recorder_v1"
sidebar_current: "docs-opentelekomcloud-resource-recorder-v1"
description: ""
page_title: "OpenTelekomCloud: opentelekomcloud_rms_resource_recorder_v1"
sidebar_current: "docs-opentelekomcloud-rms-resource-recorder-v1"
description: |-
Manages an RMS Resource Recorder within OpenTelekomCloud.
---

Up-to-date reference of API arguments for RMS Resource Recorder you can get at
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package rms

import (
"fmt"
"testing"

"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/rms/advanced"
"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"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/rms"
)

func getAdvancedQueryResourceFunc(conf *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := conf.RmsV1Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating RMS V1 client: %s", err)
}

domainId := rms.GetRmsDomainId(client, conf)
return advanced.GetQuery(client, domainId, state.Primary.ID)
}

func TestAccAdvancedQuery_basic(t *testing.T) {
var obj interface{}

name := "rms-adv-query-" + acctest.RandString(5)
rName := "opentelekomcloud_rms_advanced_query_v1.test"

rc := common.InitResourceCheck(
rName,
&obj,
getAdvancedQueryResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAdvancedQuery_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "expression", "select colume_1 from table_1"),
resource.TestCheckResourceAttr(rName, "description", "test_description"),
),
},
{
Config: testAdvancedQuery_basic_update(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "expression", "update table_1 set volume_1 = 5"),
resource.TestCheckResourceAttr(rName, "description", "test_description_update"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAdvancedQuery_basic(name string) string {
return fmt.Sprintf(`
resource "opentelekomcloud_rms_advanced_query_v1" "test" {
name = "%s"
expression = "select colume_1 from table_1"
description = "test_description"
}
`, name)
}

func testAdvancedQuery_basic_update(name string) string {
return fmt.Sprintf(`
resource "opentelekomcloud_rms_advanced_query_v1" "test" {
name = "%s"
expression = "update table_1 set volume_1 = 5"
description = "test_description_update"
}
`, name)
}
1 change: 1 addition & 0 deletions opentelekomcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ func Provider() *schema.Provider {
"opentelekomcloud_rds_maintenance_v3": rds.ResourceRdsMaintenanceV3(),
"opentelekomcloud_rds_parametergroup_v3": rds.ResourceRdsConfigurationV3(),
"opentelekomcloud_rds_read_replica_v3": rds.ResourceRdsReadReplicaV3(),
"opentelekomcloud_rms_advanced_query_v1": rms.ResourceRmsAdvancedQueryV1(),
"opentelekomcloud_rms_resource_recorder_v1": rms.ResourceRmsResourceRecorderV1(),
"opentelekomcloud_rts_software_deployment_v1": rts.ResourceRtsSoftwareDeploymentV1(),
"opentelekomcloud_rts_software_config_v1": rts.ResourceSoftwareConfigV1(),
Expand Down
15 changes: 15 additions & 0 deletions opentelekomcloud/services/rms/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package rms

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
)

const (
errCreationRMSV1Client = "error creating OpenTelekomCloud RMS v1 client: %w"
rmsClientV1 = "rms-v1-client"
)

func GetRmsDomainId(client *golangsdk.ServiceClient, config *cfg.Config) (domainID string) {
domainID = client.DomainID

if domainID == "" {
domainID = config.DomainClient.AKSKAuthOptions.DomainID
}

return
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package rms

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/rms/advanced"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/fmterr"
)

func ResourceRmsAdvancedQueryV1() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAdvancedQueryCreate,
UpdateContext: resourceAdvancedQueryUpdate,
ReadContext: resourceAdvancedQueryRead,
DeleteContext: resourceAdvancedQueryDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"expression": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAdvancedQueryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := common.ClientFromCtx(ctx, rmsClientV1, func() (*golangsdk.ServiceClient, error) {
return config.RmsV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationRMSV1Client, err)
}

domainID := GetRmsDomainId(client, config)

createOpts := advanced.CreateQueryOpts{
DomainId: domainID,
Name: d.Get("name").(string),
Expression: d.Get("expression").(string),
Description: d.Get("description").(string),
}

resp, err := advanced.CreateQuery(client, createOpts)
if err != nil {
return diag.Errorf("error creating RMS advanced query: %s", err)
}

d.SetId(resp.Id)

clientCtx := common.CtxWithClient(ctx, client, errCreationRMSV1Client)
return resourceAdvancedQueryRead(clientCtx, d, meta)
}

func resourceAdvancedQueryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := common.ClientFromCtx(ctx, rmsClientV1, func() (*golangsdk.ServiceClient, error) {
return config.RmsV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationRMSV1Client, err)
}

domainID := GetRmsDomainId(client, config)

resp, err := advanced.GetQuery(client, domainID, d.Id())
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving RMS advanced query")
}

mErr := multierror.Append(nil,
d.Set("name", resp.Name),
d.Set("expression", resp.Expression),
d.Set("description", resp.Description),
d.Set("type", resp.Type),
d.Set("created_at", resp.Created),
d.Set("updated_at", resp.Updated),
)

return diag.FromErr(mErr.ErrorOrNil())
}

func resourceAdvancedQueryUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
updateAdvancedQueryChanges := []string{
"expression",
"description",
}

if d.HasChanges(updateAdvancedQueryChanges...) {
config := meta.(*cfg.Config)
client, err := common.ClientFromCtx(ctx, rmsClientV1, func() (*golangsdk.ServiceClient, error) {
return config.RmsV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationRMSV1Client, err)
}

domainID := GetRmsDomainId(client, config)

updateOpts := advanced.UpdateQueryOpts{
DomainId: domainID,
QueryId: d.Id(),
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Expression: d.Get("expression").(string),
}

_, err = advanced.UpdateQuery(client, updateOpts)
if err != nil {
return diag.Errorf("error updating RMS advanced query: %s", err)
}
}
return resourceAdvancedQueryRead(ctx, d, meta)
}

func resourceAdvancedQueryDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*cfg.Config)
client, err := common.ClientFromCtx(ctx, rmsClientV1, func() (*golangsdk.ServiceClient, error) {
return config.RmsV1Client(config.GetRegion(d))
})
if err != nil {
return fmterr.Errorf(errCreationRMSV1Client, err)
}

domainID := GetRmsDomainId(client, config)

err = advanced.DeleteQuery(client, domainID, d.Id())
if err != nil {
return diag.Errorf("error deleting RMS advanced query: %s", err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ func resourceRecorderUpdate(ctx context.Context, d *schema.ResourceData, meta in
return fmterr.Errorf(errCreationRMSV1Client, err)
}

domainID := client.DomainID

if domainID == "" {
domainID = config.DomainClient.AKSKAuthOptions.DomainID
}
domainID := GetRmsDomainId(client, config)

resTypesRaw := d.Get("selector.0.resource_types").(*schema.Set).List()

Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/rms_advanced_query-aa1446d2edd578a1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
**[RMS]** Add new resource ``resource/opentelekomcloud_rms_advanced_query_v1`` (`#2698 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/2698>`_)

0 comments on commit 5ab7b8f

Please sign in to comment.