|
| 1 | +/* |
| 2 | +Provide a datasource to query default mysql parameters. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mysql_default_params" "mysql_57" { |
| 8 | + db_version = "5.7" |
| 9 | +} |
| 10 | +``` |
| 11 | +
|
| 12 | +
|
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "math/rand" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 22 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 23 | +) |
| 24 | + |
| 25 | +func datasourceTencentCloudMysqlDefaultParams() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: datasourceTencentCloudMysqlDefaultParamsRead, |
| 28 | + Importer: &schema.ResourceImporter{ |
| 29 | + State: schema.ImportStatePassthrough, |
| 30 | + }, |
| 31 | + Schema: map[string]*schema.Schema{ |
| 32 | + "db_version": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + Description: "MySQL database version.", |
| 36 | + }, |
| 37 | + //"template_type": { |
| 38 | + // Type: schema.TypeString, |
| 39 | + // Optional: true, |
| 40 | + // Description: "", |
| 41 | + //}, |
| 42 | + "result_output_file": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + Description: "Used for save results.", |
| 46 | + }, |
| 47 | + "param_list": { |
| 48 | + Type: schema.TypeList, |
| 49 | + Computed: true, |
| 50 | + Description: "List of param detail.", |
| 51 | + Elem: &schema.Resource{ |
| 52 | + Schema: map[string]*schema.Schema{ |
| 53 | + "current_value": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Param current value.", |
| 57 | + }, |
| 58 | + "default": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Param default value.", |
| 62 | + }, |
| 63 | + "description": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Param description.", |
| 67 | + }, |
| 68 | + "enum_value": { |
| 69 | + Type: schema.TypeList, |
| 70 | + Computed: true, |
| 71 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 72 | + Description: "Params available values if type of param is enum.", |
| 73 | + }, |
| 74 | + "max": { |
| 75 | + Type: schema.TypeInt, |
| 76 | + Computed: true, |
| 77 | + Description: "Param maximum value if type of param is integer.", |
| 78 | + }, |
| 79 | + "min": { |
| 80 | + Type: schema.TypeInt, |
| 81 | + Computed: true, |
| 82 | + Description: "Param minimum value if type of param is integer.", |
| 83 | + }, |
| 84 | + "name": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "Param key name.", |
| 88 | + }, |
| 89 | + "need_reboot": { |
| 90 | + Type: schema.TypeInt, |
| 91 | + Computed: true, |
| 92 | + Description: "Indicates weather the database instance reboot if param modified.", |
| 93 | + }, |
| 94 | + "param_type": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "Type of param.", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func datasourceTencentCloudMysqlDefaultParamsRead(d *schema.ResourceData, meta interface{}) error { |
| 107 | + defer logElapsed("datasource.tencentcloud_mysql_default_params.read")() |
| 108 | + defer inconsistentCheck(d, meta)() |
| 109 | + |
| 110 | + logId := getLogId(contextNil) |
| 111 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 112 | + |
| 113 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 114 | + service := MysqlService{client: client} |
| 115 | + |
| 116 | + engineVersion := d.Get("db_version").(string) |
| 117 | + |
| 118 | + params, err := service.DescribeDefaultParameters(ctx, engineVersion) |
| 119 | + |
| 120 | + if err != nil { |
| 121 | + d.SetId("") |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + d.SetId(fmt.Sprintf("mysql-params-%d", rand.Intn(1000))) |
| 126 | + |
| 127 | + paramList := make([]map[string]interface{}, 0, len(params)) |
| 128 | + |
| 129 | + for i := range params { |
| 130 | + item := params[i] |
| 131 | + param := map[string]interface{}{ |
| 132 | + "current_value": item.CurrentValue, |
| 133 | + "default": item.Default, |
| 134 | + "description": item.Description, |
| 135 | + "max": item.Max, |
| 136 | + "min": item.Min, |
| 137 | + "name": item.Name, |
| 138 | + "need_reboot": item.NeedReboot, |
| 139 | + "param_type": item.ParamType, |
| 140 | + } |
| 141 | + |
| 142 | + if item.EnumValue != nil { |
| 143 | + param["enum_value"] = helper.StringsInterfaces(item.EnumValue) |
| 144 | + } |
| 145 | + paramList = append(paramList, param) |
| 146 | + } |
| 147 | + |
| 148 | + if len(paramList) > 0 { |
| 149 | + err = d.Set("param_list", paramList) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + output, ok := d.GetOk("result_output_file") |
| 156 | + if ok && output.(string) != "" { |
| 157 | + if e := writeToFile(output.(string), params); e != nil { |
| 158 | + return e |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | +} |
0 commit comments