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

fix(Cloud-Databases): RR & PITR Groups Attribute is Fixed for Source Formations with Multiple Members #5871

Merged
merged 5 commits into from
Dec 13, 2024
Merged
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
108 changes: 76 additions & 32 deletions ibm/service/database/resource_ibm_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,40 +1177,8 @@ func resourceIBMDatabaseInstanceCreate(context context.Context, d *schema.Resour
rsInst.ResourceGroup = &defaultRg
}

initialNodeCount, err := getInitialNodeCount(serviceName, plan, meta)
if err != nil {
return diag.FromErr(err)
}

params := Params{}
if group, ok := d.GetOk("group"); ok {
groups := expandGroups(group.(*schema.Set).List())
var memberGroup *Group
for _, g := range groups {
if g.ID == "member" {
memberGroup = g
break
}
}

if memberGroup != nil {
if memberGroup.Memory != nil {
params.Memory = memberGroup.Memory.Allocation * initialNodeCount
}

if memberGroup.Disk != nil {
params.Disk = memberGroup.Disk.Allocation * initialNodeCount
}

if memberGroup.CPU != nil {
params.CPU = memberGroup.CPU.Allocation * initialNodeCount
}

if memberGroup.HostFlavor != nil {
params.HostFlavor = memberGroup.HostFlavor.ID
}
}
}
if version, ok := d.GetOk("version"); ok {
params.Version = version.(string)
}
Expand Down Expand Up @@ -1248,6 +1216,64 @@ func resourceIBMDatabaseInstanceCreate(context context.Context, d *schema.Resour
params.OfflineRestore = offlineRestore.(bool)
}

var initialNodeCount int
var sourceCRN string

if params.PITRDeploymentID != "" {
sourceCRN = params.PITRDeploymentID
}

if params.RemoteLeaderID != "" {
sourceCRN = params.RemoteLeaderID
}

if sourceCRN != "" {
group, err := getMemberGroup(sourceCRN, meta)

if err != nil {
return diag.FromErr(
fmt.Errorf("[ERROR] Error fetching source formation group: %s", err)) // raise error
}

if group != nil {
initialNodeCount = group.Members.Allocation
}
} else {
initialNodeCount, err = getInitialNodeCount(serviceName, plan, meta)
if err != nil {
return diag.FromErr(err)
}
}

var memberGroup *Group

if group, ok := d.GetOk("group"); ok {
groups := expandGroups(group.(*schema.Set).List())

for _, g := range groups {
if g.ID == "member" {
memberGroup = g
break
}
}
}

if memberGroup != nil && memberGroup.Memory != nil {
params.Memory = memberGroup.Memory.Allocation * initialNodeCount
}

if memberGroup != nil && memberGroup.Disk != nil {
params.Disk = memberGroup.Disk.Allocation * initialNodeCount
}

if memberGroup != nil && memberGroup.CPU != nil {
params.CPU = memberGroup.CPU.Allocation * initialNodeCount
}

if memberGroup != nil && memberGroup.HostFlavor != nil {
params.HostFlavor = memberGroup.HostFlavor.ID
}

serviceEndpoint := d.Get("service_endpoints").(string)
params.ServiceEndpoints = serviceEndpoint
parameters, _ := json.Marshal(params)
Expand Down Expand Up @@ -2964,6 +2990,24 @@ func getCpuEnforcementRatios(service string, plan string, hostFlavor string, met
return nil, 0, 0
}

func getMemberGroup(instanceCRN string, meta interface{}) (*Group, error) {
groupsResponse, err := getGroups(instanceCRN, meta)

if err != nil {
return nil, err
}

currentGroups := normalizeGroups(groupsResponse)

for _, cg := range currentGroups {
if cg.ID == "member" {
return &cg, nil
}
}

return nil, nil
}

func validateUsersDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) (err error) {
service := diff.Get("service").(string)

Expand Down
Loading