-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
36980f7
commit 5ab7b8f
Showing
8 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
opentelekomcloud/acceptance/rms/resource_opentelekomcloud_rms_advanced_query_v1_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
162 changes: 162 additions & 0 deletions
162
opentelekomcloud/services/rms/resource_opentelekomcloud_rms_advanced_query_v1.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`_) |