|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func DataSourceAlloydbSupportedDatabaseFlags() *schema.Resource { |
| 10 | + |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceAlloydbSupportedDatabaseFlagsRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "project": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Optional: true, |
| 18 | + Description: `Project ID of the project.`, |
| 19 | + }, |
| 20 | + "location": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + Description: `The canonical id for the location. For example: "us-east1".`, |
| 24 | + }, |
| 25 | + "supported_database_flags": { |
| 26 | + Type: schema.TypeList, |
| 27 | + Computed: true, |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Optional: true, |
| 34 | + Description: `The name of the flag resource, following Google Cloud conventions, e.g.: * projects/{project}/locations/{location}/flags/{flag} This field currently has no semantic meaning.`, |
| 35 | + }, |
| 36 | + "flag_name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + Optional: true, |
| 40 | + Description: `The name of the database flag, e.g. "max_allowed_packets". The is a possibly key for the Instance.database_flags map field.`, |
| 41 | + }, |
| 42 | + "value_type": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Optional: true, |
| 46 | + Description: `ValueType describes the semantic type of the value that the flag accepts. The supported values are:- 'VALUE_TYPE_UNSPECIFIED', 'STRING', 'INTEGER', 'FLOAT', 'NONE'.`, |
| 47 | + }, |
| 48 | + "accepts_multiple_values": { |
| 49 | + Type: schema.TypeBool, |
| 50 | + Computed: true, |
| 51 | + Optional: true, |
| 52 | + Description: `Whether the database flag accepts multiple values. If true, a comma-separated list of stringified values may be specified.`, |
| 53 | + }, |
| 54 | + "supported_db_versions": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Optional: true, |
| 58 | + Description: `Major database engine versions for which this flag is supported. Supported values are:- 'DATABASE_VERSION_UNSPECIFIED', and 'POSTGRES_14'.`, |
| 59 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 60 | + }, |
| 61 | + "requires_db_restart": { |
| 62 | + Type: schema.TypeBool, |
| 63 | + Computed: true, |
| 64 | + Optional: true, |
| 65 | + Description: `Whether setting or updating this flag on an Instance requires a database restart. If a flag that requires database restart is set, the backend will automatically restart the database (making sure to satisfy any availability SLO's).`, |
| 66 | + }, |
| 67 | + "string_restrictions": { |
| 68 | + Type: schema.TypeList, |
| 69 | + Computed: true, |
| 70 | + Optional: true, |
| 71 | + Description: `Restriction on STRING type value.`, |
| 72 | + MaxItems: 1, |
| 73 | + Elem: &schema.Resource{ |
| 74 | + Schema: map[string]*schema.Schema{ |
| 75 | + "allowed_values": { |
| 76 | + Type: schema.TypeList, |
| 77 | + Computed: true, |
| 78 | + Optional: true, |
| 79 | + Description: `The list of allowed values, if bounded. This field will be empty if there is a unbounded number of allowed values.`, |
| 80 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "integer_restrictions": { |
| 86 | + Type: schema.TypeList, |
| 87 | + Computed: true, |
| 88 | + Optional: true, |
| 89 | + Description: `Restriction on INTEGER type value.`, |
| 90 | + MaxItems: 1, |
| 91 | + Elem: &schema.Resource{ |
| 92 | + Schema: map[string]*schema.Schema{ |
| 93 | + "min_value": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Optional: true, |
| 97 | + Description: `The minimum value that can be specified, if applicable.`, |
| 98 | + }, |
| 99 | + "max_value": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + Optional: true, |
| 103 | + Description: `The maximum value that can be specified, if applicable.`, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func dataSourceAlloydbSupportedDatabaseFlagsRead(d *schema.ResourceData, meta interface{}) error { |
| 116 | + config := meta.(*Config) |
| 117 | + userAgent, err := generateUserAgentString(d, config.UserAgent) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + location := d.Get("location").(string) |
| 122 | + |
| 123 | + billingProject := "" |
| 124 | + |
| 125 | + project, err := getProject(d, config) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("Error fetching project for Cluster: %s", err) |
| 128 | + } |
| 129 | + billingProject = project |
| 130 | + |
| 131 | + // err == nil indicates that the billing_project value was found |
| 132 | + if bp, err := getBillingProject(d, config); err == nil { |
| 133 | + billingProject = bp |
| 134 | + } |
| 135 | + |
| 136 | + url, err := ReplaceVars(d, config, "{{AlloydbBasePath}}projects/{{project}}/locations/{{location}}/supportedDatabaseFlags") |
| 137 | + if err != nil { |
| 138 | + return fmt.Errorf("Error setting api endpoint") |
| 139 | + } |
| 140 | + res, err := SendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 141 | + if err != nil { |
| 142 | + return handleNotFoundError(err, d, fmt.Sprintf("SupportedDatabaseFlags %q", d.Id())) |
| 143 | + } |
| 144 | + var supportedDatabaseFlags []map[string]interface{} |
| 145 | + for { |
| 146 | + result := res["supportedDatabaseFlags"].([]interface{}) |
| 147 | + for _, dbFlag := range result { |
| 148 | + supportedDatabaseFlag := make(map[string]interface{}) |
| 149 | + flag := dbFlag.(map[string]interface{}) |
| 150 | + if flag["name"] != nil { |
| 151 | + supportedDatabaseFlag["name"] = flag["name"].(string) |
| 152 | + } |
| 153 | + if flag["flagName"] != nil { |
| 154 | + supportedDatabaseFlag["flag_name"] = flag["flagName"].(string) |
| 155 | + } |
| 156 | + if flag["valueType"] != nil { |
| 157 | + supportedDatabaseFlag["value_type"] = flag["valueType"].(string) |
| 158 | + } |
| 159 | + if flag["acceptsMultipleValues"] != nil { |
| 160 | + supportedDatabaseFlag["accepts_multiple_values"] = flag["acceptsMultipleValues"].(bool) |
| 161 | + } |
| 162 | + if flag["requiresDbRestart"] != nil { |
| 163 | + supportedDatabaseFlag["requires_db_restart"] = flag["requiresDbRestart"].(bool) |
| 164 | + } |
| 165 | + if flag["supportedDbVersions"] != nil { |
| 166 | + dbVersions := make([]string, 0, len(flag["supportedDbVersions"].([]interface{}))) |
| 167 | + for _, supDbVer := range flag["supportedDbVersions"].([]interface{}) { |
| 168 | + dbVersions = append(dbVersions, supDbVer.(string)) |
| 169 | + } |
| 170 | + supportedDatabaseFlag["supported_db_versions"] = dbVersions |
| 171 | + } |
| 172 | + |
| 173 | + if flag["stringRestrictions"] != nil { |
| 174 | + restrictions := make([]map[string][]string, 0, 1) |
| 175 | + fetchedAllowedValues := flag["stringRestrictions"].(map[string]interface{})["allowedValues"] |
| 176 | + if fetchedAllowedValues != nil { |
| 177 | + allowedValues := make([]string, 0, len(fetchedAllowedValues.([]interface{}))) |
| 178 | + for _, val := range fetchedAllowedValues.([]interface{}) { |
| 179 | + allowedValues = append(allowedValues, val.(string)) |
| 180 | + } |
| 181 | + stringRestrictions := map[string][]string{ |
| 182 | + "allowed_values": allowedValues, |
| 183 | + } |
| 184 | + restrictions = append(restrictions, stringRestrictions) |
| 185 | + supportedDatabaseFlag["string_restrictions"] = restrictions |
| 186 | + } |
| 187 | + } |
| 188 | + if flag["integerRestrictions"] != nil { |
| 189 | + restrictions := make([]map[string]string, 0, 1) |
| 190 | + minValue := flag["integerRestrictions"].(map[string]interface{})["minValue"].(string) |
| 191 | + maxValue := flag["integerRestrictions"].(map[string]interface{})["maxValue"].(string) |
| 192 | + integerRestrictions := map[string]string{ |
| 193 | + "min_value": minValue, |
| 194 | + "max_value": maxValue, |
| 195 | + } |
| 196 | + restrictions = append(restrictions, integerRestrictions) |
| 197 | + supportedDatabaseFlag["integer_restrictions"] = restrictions |
| 198 | + } |
| 199 | + supportedDatabaseFlags = append(supportedDatabaseFlags, supportedDatabaseFlag) |
| 200 | + } |
| 201 | + if res["pageToken"] == nil || res["pageToken"].(string) == "" { |
| 202 | + break |
| 203 | + } |
| 204 | + url, err = ReplaceVars(d, config, "{{AlloydbBasePath}}projects/{{project}}/locations/{{location}}/supportedDatabaseFlags?pageToken="+res["nextPageToken"].(string)) |
| 205 | + if err != nil { |
| 206 | + return fmt.Errorf("Error setting api endpoint") |
| 207 | + } |
| 208 | + res, err = SendRequest(config, "GET", billingProject, url, userAgent, nil) |
| 209 | + if err != nil { |
| 210 | + return handleNotFoundError(err, d, fmt.Sprintf("SupportedDatabaseFlags %q", d.Id())) |
| 211 | + } |
| 212 | + } |
| 213 | + if err := d.Set("supported_database_flags", supportedDatabaseFlags); err != nil { |
| 214 | + return fmt.Errorf("Error setting supported_database_flags: %s", err) |
| 215 | + } |
| 216 | + d.SetId(fmt.Sprintf("projects/%s/locations/%s/supportedDbFlags", project, location)) |
| 217 | + return nil |
| 218 | +} |
0 commit comments