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

[compute v2]: handle unexpected nova server groups API response #1524

Merged
merged 2 commits into from
Mar 21, 2023
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
53 changes: 19 additions & 34 deletions openstack/resource_openstack_compute_servergroup_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,41 +94,36 @@ func resourceComputeServerGroupV2Create(ctx context.Context, d *schema.ResourceD
policy = policies[0]
}

createOpts := ComputeServerGroupV2CreateOpts{
servergroups.CreateOpts{
Name: name,
Policy: policy,
},
MapValueSpecs(d),
}

rulesVal, rulesPresent := d.GetOk("rules")
var createOpts ComputeServerGroupV2CreateOpts
computeClient.Microversion = "2.64"
if policy == "anti-affinity" && rulesPresent {
maxServerPerHost := expandComputeServerGroupV2RulesMaxServerPerHost(rulesVal.([]interface{}))
createOpts = ComputeServerGroupV2CreateOpts{
servergroups.CreateOpts{
Name: name,
Policy: policy,
Rules: &servergroups.Rules{
MaxServerPerHost: maxServerPerHost,
},
},
MapValueSpecs(d),
}
} else {
createOpts = ComputeServerGroupV2CreateOpts{
servergroups.CreateOpts{
Name: name,
Policy: policy,
},
MapValueSpecs(d),
computeClient.Microversion = "2.64"
createOpts.CreateOpts.Rules = &servergroups.Rules{
MaxServerPerHost: expandComputeServerGroupV2RulesMaxServerPerHost(rulesVal.([]interface{})),
}
}

log.Printf("[DEBUG] openstack_compute_servergroup_v2 create options: %#v", createOpts)
newSG, err := servergroups.Create(computeClient, createOpts).Extract()
if err != nil {
// return an error right away
if createOpts.CreateOpts.Rules != nil {
return diag.Errorf("Error creating openstack_compute_servergroup_v2 %s: %s", name, err)
}

log.Printf("[DEBUG] Falling back to legacy API call due to: %#v", err)
// fallback to legacy microversion
computeClient.Microversion = ""
createOpts = ComputeServerGroupV2CreateOpts{
servergroups.CreateOpts{
Name: name,
Policies: policies,
Policies: expandComputeServerGroupV2Policies(computeClient, rawPolicies),
},
MapValueSpecs(d),
}
Expand Down Expand Up @@ -163,27 +158,17 @@ func resourceComputeServerGroupV2Read(_ context.Context, d *schema.ResourceData,
if err != nil {
return diag.FromErr(CheckDeleted(d, err, "Error retrieving openstack_compute_servergroup_v2"))
}

log.Printf("[DEBUG] Retrieved openstack_compute_servergroup_v2 %s: %#v", d.Id(), sg)

d.Set("name", sg.Name)
d.Set("members", sg.Members)
d.Set("region", GetRegion(d, config))
d.Set("policies", sg.Policies)
d.Set("rules", nil)

return nil
}

log.Printf("[DEBUG] Retrieved openstack_compute_servergroup_v2 %s: %#v", d.Id(), sg)

d.Set("name", sg.Name)
d.Set("members", sg.Members)
d.Set("region", GetRegion(d, config))
if sg.Policy != nil {
if sg.Policy != nil && *sg.Policy != "" {
d.Set("policies", []string{*sg.Policy})
} else {
d.Set("policies", nil)
d.Set("policies", sg.Policies)
}
if sg.Rules != nil {
d.Set("rules", []map[string]interface{}{{"max_server_per_host": sg.Rules.MaxServerPerHost}})
Expand Down