Skip to content

Commit

Permalink
add db parameter group (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnmssu authored Apr 15, 2020
1 parent 34708f5 commit 4aa2708
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 40 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
## 1.19.0 (Unreleased)

FEATURES:

* **New Datasource:** `ucloud_db_parameter_groups` [GH-66]

ENHANCEMENTS:

* resource/ucloud_db_instance: add `parameter_group` to argument [GH-66]
* resource/ucloud_db_instance: add `timeouts` to argument [GH-66]
* resource/ucloud_instance: add `timeouts` to argument [GH-66]

## 1.18.0 (April 10, 2020)

ENHANCEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion ucloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Config) Client() (*UCloudClient, error) {
// enable auto retry with http/connection error
cfg.MaxRetries = c.MaxRetries
cfg.LogLevel = log.PanicLevel
cfg.UserAgent = "Terraform-UCloud/1.18.0"
cfg.UserAgent = "Terraform-UCloud/1.19.0"
cfg.BaseUrl = c.BaseURL

if isAcc() {
Expand Down
3 changes: 3 additions & 0 deletions ucloud/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const (

// statusStopped is the general status when remote resource is stopped
statusStopped = "Stopped"

// statusShutdown is the general status when remote resource is Shutdown
statusShutdown = "Shutdown"
)

const (
Expand Down
182 changes: 182 additions & 0 deletions ucloud/data_source_ucloud_db_parameter_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package ucloud

import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/ucloud/ucloud-sdk-go/services/udb"
"github.com/ucloud/ucloud-sdk-go/ucloud"
)

func dataSourceUCloudDBParameterGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceUCloudDBParameterGroupsRead,

Schema: map[string]*schema.Schema{
"availability_zone": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"multi_az"},
},

"multi_az": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"availability_zone"},
},

"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.ValidateRegexp,
},

"output_file": {
Type: schema.TypeString,
Optional: true,
},

"total_count": {
Type: schema.TypeInt,
Computed: true,
},

"parameter_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},

"availability_zone": {
Type: schema.TypeString,
Computed: true,
},

"name": {
Type: schema.TypeString,
Computed: true,
},

"engine": {
Type: schema.TypeString,
Computed: true,
},

"engine_version": {
Type: schema.TypeString,
Computed: true,
},

"is_default": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}

func dataSourceUCloudDBParameterGroupsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*UCloudClient)
conn := client.udbconn

req := conn.NewDescribeUDBParamGroupRequest()
req.ClassType = ucloud.String("sql")
if val, ok := d.GetOk("availability_zone"); ok {
req.Zone = ucloud.String(val.(string))
}
if val, ok := d.GetOkExists("multi_az"); ok {
req.RegionFlag = ucloud.Bool(val.(bool))
}

var paramGroups []udb.UDBParamGroupSet
var allParamGroups []udb.UDBParamGroupSet
var limit = 100
var offset int
for {
req.Limit = ucloud.Int(limit)
req.Offset = ucloud.Int(offset)

resp, err := conn.DescribeUDBParamGroup(req)
if err != nil {
return fmt.Errorf("error on reading db parameter groups, %s", err)
}

if resp == nil || len(resp.DataSet) < 1 {
break
}

allParamGroups = append(allParamGroups, resp.DataSet...)

if len(resp.DataSet) < limit {
break
}

offset = offset + limit
}

if nameRegex, ok := d.GetOk("name_regex"); ok {
r := regexp.MustCompile(nameRegex.(string))
for _, v := range allParamGroups {
if r != nil && !r.MatchString(v.GroupName) {
continue
}

paramGroups = append(paramGroups, v)
}
} else {
paramGroups = allParamGroups
}

err := dataSourceUCloudDBParameterGroupsSave(d, paramGroups)
if err != nil {
return fmt.Errorf("error on reading parameter group list, %s", err)
}

return nil
}

func dataSourceUCloudDBParameterGroupsSave(d *schema.ResourceData, parameterGroups []udb.UDBParamGroupSet) error {
var ids []string
var id string
var data []map[string]interface{}
for _, parameterGroup := range parameterGroups {
if parameterGroup.Zone != "" {
id = fmt.Sprintf("%s:%s", parameterGroup.Zone, strconv.Itoa(parameterGroup.GroupId))
} else {
id = strconv.Itoa(parameterGroup.GroupId)
}
ids = append(ids, id)
arr := strings.Split(parameterGroup.DBTypeId, "-")
data = append(data, map[string]interface{}{
"id": strconv.Itoa(parameterGroup.GroupId),
"name": parameterGroup.GroupName,
"engine": arr[0],
"engine_version": arr[1],
"is_default": !parameterGroup.Modifiable,
"availability_zone": parameterGroup.Zone,
})
}

d.SetId(hashStringArray(ids))
d.Set("total_count", len(data))
if err := d.Set("parameter_groups", data); err != nil {
return err
}

if outputFile, ok := d.GetOk("output_file"); ok && outputFile.(string) != "" {
writeToFile(outputFile.(string), data)
}

return nil
}
35 changes: 35 additions & 0 deletions ucloud/data_source_ucloud_db_parameter_groups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ucloud

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccUCloudDBParameterGroupsDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataDBParameterGroupsConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckIDExists("data.ucloud_db_parameter_groups.foo"),
resource.TestCheckResourceAttr("data.ucloud_db_parameter_groups.foo", "parameter_groups.0.name", "mysql5.6默认配置"),
),
},
},
})
}

const testAccDataDBParameterGroupsConfig = `
data "ucloud_zones" "default" {
}
data "ucloud_db_parameter_groups" "foo" {
availability_zone = data.ucloud_zones.default.zones[0].id
name_regex = "mysql5.6默认配置"
}
`
12 changes: 6 additions & 6 deletions ucloud/data_source_ucloud_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func dataSourceUCloudInstancesRead(d *schema.ResourceData, meta interface{}) err

var allInstances []uhost.UHostInstanceSet
var instances []uhost.UHostInstanceSet
var limit int = 100
var limit = 100
var offset int
for {
req.Limit = ucloud.Int(limit)
Expand Down Expand Up @@ -255,14 +255,14 @@ func dataSourceUCloudInstancesRead(d *schema.ResourceData, meta interface{}) err
}

func dataSourceUCloudInstancesSave(d *schema.ResourceData, instances []uhost.UHostInstanceSet) error {
ids := []string{}
data := []map[string]interface{}{}
var ids []string
var data []map[string]interface{}

for _, instance := range instances {
ids = append(ids, string(instance.UHostId))
ids = append(ids, instance.UHostId)
var vpcId, subnetId, privateIp string

ipSet := []map[string]interface{}{}
var ipSet []map[string]interface{}
for _, item := range instance.IPSet {
ipSet = append(ipSet, map[string]interface{}{
"ip": item.IP,
Expand All @@ -276,7 +276,7 @@ func dataSourceUCloudInstancesSave(d *schema.ResourceData, instances []uhost.UHo
}
}

diskSet := []map[string]interface{}{}
var diskSet []map[string]interface{}
for _, item := range instance.DiskSet {
diskSet = append(diskSet, map[string]interface{}{
"type": upperCvt.convert(item.DiskType),
Expand Down
1 change: 1 addition & 0 deletions ucloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func Provider() terraform.ResourceProvider {
"ucloud_vpn_gateways": dateSourceUCloudVPNGateways(),
"ucloud_vpn_customer_gateways": dateSourceUCloudVPNCustomerGateways(),
"ucloud_vpn_connections": dateSourceUCloudVPNConnections(),
"ucloud_db_parameter_groups": dataSourceUCloudDBParameterGroups(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
Loading

0 comments on commit 4aa2708

Please sign in to comment.