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

make whitelists be optional for Redis 4.0 and 5.0 instance #588

Merged
merged 1 commit into from
Oct 14, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/resources/dcs_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ The following arguments are supported:
This parameter is mandatory for Memcached and Redis 3.0 versions.

* `whitelists` - (Optional) Specifies the IP addresses which can access the instance.
This parameter is mandatory for Redis 4.0 and 5.0 versions. The structure is described below.
This parameter is valid for Redis 4.0 and 5.0 versions. The structure is described below.

* `whitelist_enable` - (Optional) Enable or disable the IP addresse whitelists. Default to true.
If the whitelist is disabled, all IP addresses connected to the VPC can access the instance.
Expand Down
35 changes: 31 additions & 4 deletions huaweicloud/resource_huaweicloud_dcs_instance_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func resourceDcsInstanceV1() *schema.Resource {
ForceNew: true,
},
"security_group_id": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"security_group_id", "whitelists"},
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"whitelists"},
},
"available_zones": {
Type: schema.TypeList,
Expand Down Expand Up @@ -195,6 +195,25 @@ 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 Redis 4.0 and 5.0
if engineVersion == "4.0" || engineVersion == "5.0" {
if secGroupID != "" {
return fmt.Errorf("security_group_id is not supported for Redis 4.0 and 5.0. please configure the whitelists alternatively")
}
} else {
// check for Memcached and Redis 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 @@ -264,6 +283,10 @@ func resourceDcsInstancesV1Create(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Error creating HuaweiCloud 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 Down Expand Up @@ -336,7 +359,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 Down Expand Up @@ -389,6 +412,10 @@ func resourceDcsInstancesV1Read(d *schema.ResourceData, meta interface{}) error
func resourceDcsInstancesV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

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

if d.HasChanges("name", "description", "security_group_id", "maintain_begin", "maintain_end") {
dcsV1Client, err := config.dcsV1Client(GetRegion(d, config))
if err != nil {
Expand Down