Skip to content

Commit

Permalink
refactor: keep code clean and comply with programming specifications (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance52259 committed Feb 27, 2021
1 parent 2c7ea50 commit 47d235f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
31 changes: 20 additions & 11 deletions docs/resources/compute_servergroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,45 @@ This is an alternative to `huaweicloud_compute_servergroup_v2`
## Example Usage

```hcl
data "huaweicloud_compute_instance" "instance_demo" {
name = "ecs-servergroup-demo"
}
resource "huaweicloud_compute_servergroup" "test-sg" {
name = "my-sg"
policies = ["anti-affinity"]
members = [
data.huaweicloud_compute_instance.instance_demo.id,
]
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) The region in which to create the server group resource. If omitted, the provider-level region will be used. Changing this creates a new server group resource.

* `name` - (Required, String, ForceNew) A unique name for the server group. Changing this creates
a new server group.
* `region` - (Optional, String, ForceNew) Specifies the region in which to create the server group resource.
If omitted, the provider-level region will be used.
Changing this creates a new server group.

* `policies` - (Required, List, ForceNew) The set of policies for the server group. Only two
policies are available right now, and both are mutually exclusive. Possible values are "affinity" and "anti-affinity".
"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.
* `name` - (Required, String, ForceNew) Specifies a unique name for the server group.
This parameter can contain a maximum of 255 characters, which may consist of
letters, digits, underscores (_), and hyphens (-).
Changing this creates a new server group.

* `value_specs` - (Optional, Map, ForceNew) Map of additional options.
* `policies` - (Required, List, ForceNew) Specifies the set of policies for the server group.
Only *anti-affinity* policies are supported.

* `anti-affinity`: All ECS in this group must be deployed on different hosts.
Changing this creates a new server group.

* `members` - (Optional, Set) Specifies the IDs of the an server group.
* `members` - (Optional, Set) Specifies an array of one or more instance ID to attach server group.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - Specifies a resource ID in UUID format.
* `id` - A resource ID in UUID format.

## Import

Expand Down
29 changes: 7 additions & 22 deletions huaweicloud/resource_huaweicloud_compute_servergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ func ResourceComputeServerGroupV2() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"value_specs": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
Expand All @@ -66,12 +60,9 @@ func resourceComputeServerGroupV2Create(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error creating HuaweiCloud compute client: %s", err)
}

createOpts := ServerGroupCreateOpts{
servergroups.CreateOpts{
Name: d.Get("name").(string),
Policies: resourceServerGroupPoliciesV2(d),
},
MapValueSpecs(d),
createOpts := servergroups.CreateOpts{
Name: d.Get("name").(string),
Policies: resourceServerGroupPoliciesV2(d),
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
Expand Down Expand Up @@ -112,20 +103,14 @@ func resourceComputeServerGroupV2Read(d *schema.ResourceData, meta interface{})

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

// Set the name
d.Set("name", sg.Name)

// Set the policies
policies := []string{}
for _, p := range sg.Policies {
policies = append(policies, p)
policies := make([]string, len(sg.Policies))
for i, p := range sg.Policies {
policies[i] = p
}
d.Set("policies", policies)

// Set the members & fault_domains
d.Set("name", sg.Name)
d.Set("members", sg.Members)
d.Set("fault_domains", sg.FaultDomain.Names)

d.Set("region", GetRegion(d, config))

return nil
Expand Down
13 changes: 0 additions & 13 deletions huaweicloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"time"

"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/servergroups"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/recordsets"
"github.com/huaweicloud/golangsdk/openstack/dns/v2/zones"
"github.com/huaweicloud/golangsdk/openstack/networking/v1/eips"
Expand Down Expand Up @@ -328,18 +327,6 @@ func (opts RuleCreateOpts) ToRuleCreateMap() (map[string]interface{}, error) {
return b, nil
}

// ServerGroupCreateOpts represents the attributes used when creating a new router.
type ServerGroupCreateOpts struct {
servergroups.CreateOpts
ValueSpecs map[string]string `json:"value_specs,omitempty"`
}

// ToServerGroupCreateMap casts a CreateOpts struct to a map.
// It overrides routers.ToServerGroupCreateMap to add the ValueSpecs field.
func (opts ServerGroupCreateOpts) ToServerGroupCreateMap() (map[string]interface{}, error) {
return BuildRequest(opts, "server_group")
}

// SubnetCreateOpts represents the attributes used when creating a new subnet.
type SubnetCreateOpts struct {
subnets.CreateOpts
Expand Down

0 comments on commit 47d235f

Please sign in to comment.