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

[ECS]: opentelekomcloud_compute_servergroup_v2 update #1933

Merged
merged 2 commits into from Sep 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/resources/compute_servergroup_v2.md
Expand Up @@ -31,9 +31,6 @@ The following arguments are supported:

## Policies

* `affinity` - All instances/servers launched in this group will be hosted on
the same compute node.

* `anti-affinity` - All instances/servers launched in this group will be
hosted on different compute nodes.

Expand Down
Expand Up @@ -2,6 +2,7 @@ package acceptance

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -80,6 +81,23 @@ func TestAccComputeV2ServerGroup_affinity(t *testing.T) {
})
}

func TestAccComputeV2ServerGroup_policyValidation(t *testing.T) {
qts := serverQuotas(4, env.OsFlavorID)
t.Parallel()
quotas.BookMany(t, qts)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccComputeV2ServerGroupPolicyCheck,
ExpectError: regexp.MustCompile(`Error: only 'anti-affinity' policies are supported`),
},
},
})
}

func testAccCheckComputeV2ServerGroupDestroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.ComputeV2Client(env.OS_REGION_NAME)
Expand Down Expand Up @@ -150,7 +168,7 @@ func testAccCheckComputeV2InstanceInServerGroup(instance *servers.Server, sg *se
const testAccComputeV2ServerGroupBasic = `
resource "opentelekomcloud_compute_servergroup_v2" "sg_1" {
name = "sg_1"
policies = ["affinity"]
policies = ["anti-affinity"]
}
`

Expand All @@ -159,7 +177,7 @@ var testAccComputeV2ServerGroupAffinity = fmt.Sprintf(`

resource "opentelekomcloud_compute_servergroup_v2" "sg_1" {
name = "sg_1"
policies = ["affinity"]
policies = ["anti-affinity"]
}

resource "opentelekomcloud_compute_instance_v2" "instance_1" {
Expand All @@ -173,3 +191,10 @@ resource "opentelekomcloud_compute_instance_v2" "instance_1" {
}
}
`, common.DataSourceSubnet)

const testAccComputeV2ServerGroupPolicyCheck = `
resource "opentelekomcloud_compute_servergroup_v2" "sg_1" {
name = "sg_1"
policies = ["affinity"]
}
`
Expand Up @@ -2,6 +2,7 @@ package ecs

import (
"context"
"fmt"
"log"

"github.com/hashicorp/go-multierror"
Expand All @@ -24,6 +25,8 @@ func ResourceComputeServerGroupV2() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

CustomizeDiff: validatePolicy,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Expand All @@ -39,7 +42,7 @@ func ResourceComputeServerGroupV2() *schema.Resource {
},
"policies": {
Type: schema.TypeList,
Optional: true,
Required: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -134,3 +137,11 @@ func resourceServerGroupPoliciesV2(d *schema.ResourceData) []string {
}
return policies
}

func validatePolicy(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
policyData := d.Get("policies").([]interface{})
if policyData[0] != "anti-affinity" {
return fmt.Errorf("only 'anti-affinity' policies are supported")
}
return nil
}
7 changes: 7 additions & 0 deletions releasenotes/notes/compute_sg_fix-452802bb1d5b67cf.yaml
@@ -0,0 +1,7 @@
---
fixes:
- |
**[ECS]** Documentation fixed for ``policy`` parameter in ``resource/opentelekomcloud_compute_servergroup_v2`` (`#1933 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/1933>`_)
other:
- |
**[ECS]** Validation/tests added for ``policy`` parameter ``resource/opentelekomcloud_compute_servergroup_v2`` (`#1933 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/pull/1933>`_)