From 5ab7b8fba53a10a720f9c69cd9e6e8af4c6da178 Mon Sep 17 00:00:00 2001 From: Artem Lifshits <55093318+artem-lifshits@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:48:48 +0100 Subject: [PATCH] RMS: advanced queries (#2698) 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 --- docs/resources/rms_advanced_query_v1.md | 56 ++++++ docs/resources/rms_resource_recorder_v1.md | 7 +- ...telekomcloud_rms_advanced_query_v1_test.go | 89 ++++++++++ opentelekomcloud/provider.go | 1 + opentelekomcloud/services/rms/common.go | 15 ++ ..._opentelekomcloud_rms_advanced_query_v1.go | 162 ++++++++++++++++++ ...entelekomcloud_rms_resource_recorder_v1.go | 6 +- .../rms_advanced_query-aa1446d2edd578a1.yaml | 4 + 8 files changed, 332 insertions(+), 8 deletions(-) create mode 100644 docs/resources/rms_advanced_query_v1.md create mode 100644 opentelekomcloud/acceptance/rms/resource_opentelekomcloud_rms_advanced_query_v1_test.go create mode 100644 opentelekomcloud/services/rms/resource_opentelekomcloud_rms_advanced_query_v1.go create mode 100644 releasenotes/notes/rms_advanced_query-aa1446d2edd578a1.yaml diff --git a/docs/resources/rms_advanced_query_v1.md b/docs/resources/rms_advanced_query_v1.md new file mode 100644 index 000000000..09d931a54 --- /dev/null +++ b/docs/resources/rms_advanced_query_v1.md @@ -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 +``` diff --git a/docs/resources/rms_resource_recorder_v1.md b/docs/resources/rms_resource_recorder_v1.md index c43902163..ed6de5e82 100644 --- a/docs/resources/rms_resource_recorder_v1.md +++ b/docs/resources/rms_resource_recorder_v1.md @@ -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 diff --git a/opentelekomcloud/acceptance/rms/resource_opentelekomcloud_rms_advanced_query_v1_test.go b/opentelekomcloud/acceptance/rms/resource_opentelekomcloud_rms_advanced_query_v1_test.go new file mode 100644 index 000000000..4a72daba2 --- /dev/null +++ b/opentelekomcloud/acceptance/rms/resource_opentelekomcloud_rms_advanced_query_v1_test.go @@ -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) +} diff --git a/opentelekomcloud/provider.go b/opentelekomcloud/provider.go index 009d99b4f..4a94d6b26 100644 --- a/opentelekomcloud/provider.go +++ b/opentelekomcloud/provider.go @@ -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(), diff --git a/opentelekomcloud/services/rms/common.go b/opentelekomcloud/services/rms/common.go index 250460399..abc1c4622 100644 --- a/opentelekomcloud/services/rms/common.go +++ b/opentelekomcloud/services/rms/common.go @@ -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 +} diff --git a/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_advanced_query_v1.go b/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_advanced_query_v1.go new file mode 100644 index 000000000..ff74dd8e6 --- /dev/null +++ b/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_advanced_query_v1.go @@ -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 +} diff --git a/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_resource_recorder_v1.go b/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_resource_recorder_v1.go index 904c6ddf0..9a0cb7b2c 100644 --- a/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_resource_recorder_v1.go +++ b/opentelekomcloud/services/rms/resource_opentelekomcloud_rms_resource_recorder_v1.go @@ -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() diff --git a/releasenotes/notes/rms_advanced_query-aa1446d2edd578a1.yaml b/releasenotes/notes/rms_advanced_query-aa1446d2edd578a1.yaml new file mode 100644 index 000000000..e6a30c1cf --- /dev/null +++ b/releasenotes/notes/rms_advanced_query-aa1446d2edd578a1.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + **[RMS]** Add new resource ``resource/opentelekomcloud_rms_advanced_query_v1`` (`#2698 `_)