Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RDS] Fix template application #1150

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -444,24 +444,24 @@ func testAccRdsInstanceV3ConfigTemplateBasic(postfix string) string {
resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
name = "pg-rds-test"
values = {
max_connections = "10"
max_connections = "100"
autocommit = "OFF"
}
datastore {
type = "postgresql"
version = "10"
version = "12"
}
}

resource "opentelekomcloud_rds_parametergroup_v3" "pg2" {
name = "pg-rds-test-2"
values = {
max_connections = "10"
max_connections = "100"
autocommit = "OFF"
}
datastore {
type = "postgresql"
version = "10"
version = "12"
}
}

Expand All @@ -475,7 +475,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "12"
}
security_group_id = opentelekomcloud_networking_secgroup_v2.sg.id
subnet_id = "%s"
Expand All @@ -495,7 +495,7 @@ func testAccRdsInstanceV3ConfigTemplateUpdate(postfix string) string {
resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
name = "pg-rds-test"
values = {
max_connections = "10"
max_connections = "100"
autocommit = "OFF"
}
datastore {
Expand All @@ -507,7 +507,7 @@ resource "opentelekomcloud_rds_parametergroup_v3" "pg" {
resource "opentelekomcloud_rds_parametergroup_v3" "pg2" {
name = "pg-rds-test-2"
values = {
max_connections = "10"
max_connections = "100"
autocommit = "OFF"
}
datastore {
Expand All @@ -526,7 +526,7 @@ resource "opentelekomcloud_rds_instance_v3" "instance" {
db {
password = "Postgres!120521"
type = "PostgreSQL"
version = "10"
version = "12"
}
security_group_id = opentelekomcloud_networking_secgroup_v2.sg.id
subnet_id = "%s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,95 @@ func resourceRdsInstanceV3Create(ctx context.Context, d *schema.ResourceData, me
}
}

if err := assureTemplateApplied(client, d); err != nil {
return fmterr.Errorf("error making sure configuration template is applied: %w", err)
}

return resourceRdsInstanceV3Read(ctx, d, meta)
}

func assureTemplateApplied(client *golangsdk.ServiceClient, d *schema.ResourceData) error {
templateID := d.Get("param_group_id").(string)
if templateID == "" {
return nil
}

applied, err := configurations.Get(client, templateID).Extract()
if err != nil {
return fmt.Errorf("error getting parameter template %s: %w", templateID, err)
}
// convert to the map
appliedParams := make(map[string]configurations.Parameter, len(applied.Parameters))
for _, param := range applied.Parameters {
appliedParams[param.Name] = param
}

current, err := configurations.GetForInstance(client, d.Id()).Extract()
if err != nil {
return fmt.Errorf("error getting configuration of instance %s: %w", d.Id(), err)
}

needsReapply := false
for _, val := range current.Parameters {
param, ok := appliedParams[val.Name]
if !ok { // than it's not from the template
continue
}
if val.Value != param.Value {
needsReapply = true
break // that's enough
}
}
if !needsReapply {
return nil
}

return applyAndRestart(client, d)
}

func applyAndRestart(client *golangsdk.ServiceClient, d *schema.ResourceData) error {
templateID := d.Get("param_group_id").(string)
applyResult, err := configurations.Apply(client, templateID, configurations.ApplyOpts{
InstanceIDs: []string{d.Id()},
}).Extract()
if err != nil {
return fmt.Errorf("error applying configuration %s to instance %s: %w", templateID, d.Id(), err)
}
restartRequired := false
switch l := len(applyResult.ApplyResults); l {
case 0:
return fmt.Errorf("empty appply results")
case 1:
result := applyResult.ApplyResults[0]
if !result.Success {
return fmt.Errorf("unsuccessful apply of template instance %s", result.InstanceID)
}
restartRequired = result.RestartRequired
default:
return fmt.Errorf("more that one apply result returned: %#v", applyResult.ApplyResults)
}

if !restartRequired {
return nil
}

waitSeconds := int(d.Timeout(schema.TimeoutCreate).Seconds())
err = instances.WaitForStateAvailable(client, waitSeconds, d.Id())
if err != nil {
return err
}

job, err := instances.Restart(client, instances.RestartRdsInstanceOpts{Restart: "{}"}, d.Id()).Extract()
if err != nil {
return fmt.Errorf("error restarting RDS instance: %w", err)
}
timeout := d.Timeout(schema.TimeoutCreate)
if err := instances.WaitForJobCompleted(client, int(timeout.Seconds()), job.JobId); err != nil {
return fmt.Errorf("error waiting for instance to reboot: %w", err)
}
return nil
}

func GetRdsInstance(rdsClient *golangsdk.ServiceClient, rdsId string) (*instances.RdsInstanceResponse, error) {
listOpts := instances.ListRdsInstanceOpts{
Id: rdsId,
Expand Down Expand Up @@ -688,7 +774,6 @@ func resourceRdsInstanceV3Update(ctx context.Context, d *schema.ResourceData, me
switch len(newIPs) {
case 0:
err = unAssignEipFromInstance(nwClient, oldIPs[0].(string)) // if it become 0, it was 1 before
break
case 1:
if len(oldIPs) > 0 {
err = unAssignEipFromInstance(nwClient, oldIPs[0].(string))
Expand All @@ -702,7 +787,6 @@ func resourceRdsInstanceV3Update(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}
err = assignEipToInstance(nwClient, newIPs[0].(string), privateIP, subnetID)
break
default:
return fmterr.Errorf("RDS instance can't have more than one public IP")
}
Expand Down