Skip to content

Commit

Permalink
improve on dcs instance resource (#471)
Browse files Browse the repository at this point in the history
* remove subnet_id in dcs instance resource

* make security_group_id be optional for Redis 4.0 and 5.0 instance

* [DCS] change type of capacity from int to float

* deprecate instance_type and update docs
  • Loading branch information
ShiChangkuo authored Feb 8, 2021
1 parent 1e03541 commit 8eab829
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 97 deletions.
76 changes: 43 additions & 33 deletions flexibleengine/resource_flexibleengine_dcs_instance_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package flexibleengine
import (
"fmt"
"log"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -43,7 +44,7 @@ func resourceDcsInstanceV1() *schema.Resource {
ForceNew: true,
},
"capacity": {
Type: schema.TypeInt,
Type: schema.TypeFloat,
Required: true,
ForceNew: true,
},
Expand All @@ -63,22 +64,14 @@ func resourceDcsInstanceV1() *schema.Resource {
Required: true,
ForceNew: true,
},
"security_group_id": {
"network_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"network_id"},
Deprecated: "use network_id instead",
},
"network_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"subnet_id"},
"security_group_id": {
Type: schema.TypeString,
Optional: true,
},
"available_zones": {
Type: schema.TypeList,
Expand All @@ -88,9 +81,10 @@ func resourceDcsInstanceV1() *schema.Resource {
},
"instance_type": {
Type: schema.TypeString,
ConflictsWith: []string{"product_id"},
ForceNew: true,
Optional: true,
ConflictsWith: []string{"product_id"},
Deprecated: "use product_id instead",
},
"product_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -183,6 +177,20 @@ func resourceDcsInstanceV1() *schema.Resource {
}
}

func resourceDcsInstancesCheck(d *schema.ResourceData) error {
engineVersion := d.Get("engine_version").(string)
secGroupID := d.Get("security_group_id").(string)

// check for Memcached and Redis 3.0
if engineVersion == "3.0" {
if secGroupID == "" {
return fmt.Errorf("security_group_id is mandatory for this DCS instance")
}
}

return nil
}

func getInstanceBackupPolicy(d *schema.ResourceData) *instances.InstanceBackupPolicy {
backupAts := d.Get("backup_at").([]interface{})
ats := make([]int, len(backupAts))
Expand Down Expand Up @@ -233,6 +241,10 @@ func resourceDcsInstancesV1Create(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Error creating FlexibleEngine dcs instance client: %s", err)
}

if err := resourceDcsInstancesCheck(d); err != nil {
return err
}

no_password_access := "true"
if d.Get("access_user").(string) != "" || d.Get("password").(string) != "" {
no_password_access = "false"
Expand All @@ -243,28 +255,18 @@ func resourceDcsInstancesV1Create(d *schema.ResourceData, meta interface{}) erro
Description: d.Get("description").(string),
Engine: d.Get("engine").(string),
EngineVersion: d.Get("engine_version").(string),
Capacity: d.Get("capacity").(int),
Capacity: d.Get("capacity").(float64),
NoPasswordAccess: no_password_access,
Password: d.Get("password").(string),
AccessUser: d.Get("access_user").(string),
VPCID: d.Get("vpc_id").(string),
SubnetID: d.Get("network_id").(string),
SecurityGroupID: d.Get("security_group_id").(string),
AvailableZones: getAllAvailableZones(d),
MaintainBegin: d.Get("maintain_begin").(string),
MaintainEnd: d.Get("maintain_end").(string),
}

subnet_id, subnet_ok := d.GetOk("subnet_id")
network_id, network_ok := d.GetOk("network_id")
if !subnet_ok && !network_ok {
return fmt.Errorf("one of subnet_id or network_id must be configured")
}
if subnet_ok {
createOpts.SubnetID = subnet_id.(string)
} else {
createOpts.SubnetID = network_id.(string)
}

product_id, product_ok := d.GetOk("product_id")
instance_type, type_ok := d.GetOk("instance_type")
if !product_ok && !type_ok {
Expand Down Expand Up @@ -321,7 +323,7 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
}
v, err := instances.Get(dcsV1Client, d.Id()).Extract()
if err != nil {
return err
return CheckDeleted(d, err, "DCS instance")
}

log.Printf("[DEBUG] Dcs instance %s: %+v", d.Id(), v)
Expand All @@ -330,7 +332,6 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
d.Set("name", v.Name)
d.Set("engine", v.Engine)
d.Set("engine_version", v.EngineVersion)
d.Set("capacity", v.Capacity)
d.Set("used_memory", v.UsedMemory)
d.Set("max_memory", v.MaxMemory)
d.Set("port", v.Port)
Expand All @@ -339,6 +340,7 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
d.Set("resource_spec_code", v.ResourceSpecCode)
d.Set("internal_version", v.InternalVersion)
d.Set("vpc_id", v.VPCID)
d.Set("network_id", v.SubnetID)
d.Set("vpc_name", v.VPCName)
d.Set("created_at", v.CreatedAt)
d.Set("product_id", v.ProductID)
Expand All @@ -353,11 +355,14 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
d.Set("access_user", v.AccessUser)
d.Set("ip", v.IP)

if _, ok := d.GetOk("subnet_id"); ok {
d.Set("subnet_id", v.SubnetID)
} else {
d.Set("network_id", v.SubnetID)
// set capacity by Capacity and CapacityMinor
var capacity float64 = float64(v.Capacity)
if v.CapacityMinor != "" {
if minor, err := strconv.ParseFloat(v.CapacityMinor, 64); err == nil {
capacity += minor
}
}
d.Set("capacity", capacity)

return nil
}
Expand All @@ -368,6 +373,11 @@ func resourceDcsInstancesV1Update(d *schema.ResourceData, meta interface{}) erro
if err != nil {
return fmt.Errorf("Error updating FlexibleEngine dcs instance client: %s", err)
}

if err := resourceDcsInstancesCheck(d); err != nil {
return err
}

var updateOpts instances.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
Expand Down
40 changes: 21 additions & 19 deletions flexibleengine/resource_flexibleengine_dcs_instance_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,37 +87,39 @@ func testAccCheckDcsV1InstanceExists(n string, instance instances.Instance) reso
func testAccDcsV1Instance_basic(instanceName string) string {
return fmt.Sprintf(`
resource "flexibleengine_networking_secgroup_v2" "secgroup_1" {
name = "secgroup_1"
name = "secgroup_1"
description = "secgroup_1"
}
resource "flexibleengine_vpc_v1" "vpc_3" {
name = "terraform_provider_vpc3"
cidr= "192.168.0.0/16"
resource "flexibleengine_vpc_v1" "vpc_1" {
name = "terraform_vpc1"
cidr = "192.168.0.0/16"
}
resource "flexibleengine_vpc_subnet_v1" "subnet_1" {
name = "flexibleengine_subnet"
cidr = "192.168.0.0/16"
name = "terraform_subnet"
cidr = "192.168.0.0/24"
gateway_ip = "192.168.0.1"
vpc_id = flexibleengine_vpc_v1.vpc_3.id
vpc_id = flexibleengine_vpc_v1.vpc_1.id
}
resource "flexibleengine_dcs_instance_v1" "instance_1" {
name = "%s"
engine_version = "3.0"
password = "Huawei_test"
engine = "Redis"
capacity = 2
vpc_id = flexibleengine_vpc_v1.vpc_3.id
name = "%s"
engine = "Redis"
engine_version = "3.0"
password = "Huawei_test"
product_id = "dcs.master_standby-h"
capacity = 2
vpc_id = flexibleengine_vpc_v1.vpc_1.id
network_id = flexibleengine_vpc_subnet_v1.subnet_1.id
security_group_id = flexibleengine_networking_secgroup_v2.secgroup_1.id
subnet_id = flexibleengine_vpc_subnet_v1.subnet_1.id
available_zones = ["eu-west-0a"]
instance_type = "dcs.master_standby"
save_days = 1
available_zones = ["eu-west-0a"]
save_days = 1
backup_type = "manual"
begin_at = "00:00-01:00"
begin_at = "00:00-01:00"
period_type = "weekly"
backup_at = [1]
backup_at = [1]
}
`, instanceName)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.0.0
github.com/huaweicloud/golangsdk v0.0.0-20210205050659-642d3daa3d9f
github.com/huaweicloud/golangsdk v0.0.0-20210207050553-158e5bc3ef64
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ github.com/huaweicloud/golangsdk v0.0.0-20210116064948-5bfe83790eb2 h1:S1zh1Z7Ja
github.com/huaweicloud/golangsdk v0.0.0-20210116064948-5bfe83790eb2/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20210205050659-642d3daa3d9f h1:Ru5t1Mf0JLu0KzlDQ0V9grsBhf0BqxBgoEDIij3sa38=
github.com/huaweicloud/golangsdk v0.0.0-20210205050659-642d3daa3d9f/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/huaweicloud/golangsdk v0.0.0-20210207050553-158e5bc3ef64 h1:CHIzZqEyYFRY+Tro+AVR6EsnHrqs1AnxV1ol+ZCv8RY=
github.com/huaweicloud/golangsdk v0.0.0-20210207050553-158e5bc3ef64/go.mod h1:WQBcHRNX9shz3928lWEvstQJtAtYI7ks6XlgtRT9Tcw=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a h1:FyS/ubzBR5xJlnJGRTwe7GUHpJOR4ukYK3y+LFNffuA=
github.com/jen20/awspolicyequivalence v0.0.0-20170831201602-3d48364a137a/go.mod h1:uoIMjNxUfXi48Ci40IXkPRbghZ1vbti6v9LCbNqRgHY=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ github.com/hashicorp/terraform-plugin-sdk/plugin
github.com/hashicorp/terraform-plugin-sdk/terraform
# github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d
github.com/hashicorp/yamux
# github.com/huaweicloud/golangsdk v0.0.0-20210205050659-642d3daa3d9f
# github.com/huaweicloud/golangsdk v0.0.0-20210207050553-158e5bc3ef64
## explicit
github.com/huaweicloud/golangsdk
github.com/huaweicloud/golangsdk/internal
Expand Down
Loading

0 comments on commit 8eab829

Please sign in to comment.