|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + alloydb "cloud.google.com/go/alloydb/apiv1" |
| 5 | + alloydbpb "cloud.google.com/go/alloydb/apiv1/alloydbpb" |
| 6 | + "fmt" |
| 7 | + gax "github.com/googleapis/gax-go/v2" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "google.golang.org/api/iterator" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceAlloydbSupportedDatabaseFlags() *schema.Resource { |
| 13 | + |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceAlloydbSupportedDatabaseFlagsRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "project": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + Description: `Project ID of the project.`, |
| 22 | + }, |
| 23 | + "location": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: `The canonical id for the location. For example: "us-east1".`, |
| 27 | + }, |
| 28 | + "supported_database_flags": { |
| 29 | + Type: schema.TypeList, |
| 30 | + Computed: true, |
| 31 | + Elem: &schema.Resource{ |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Optional: true, |
| 37 | + 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.`, |
| 38 | + }, |
| 39 | + "flag_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Optional: true, |
| 43 | + Description: `The name of the database flag, e.g. "max_allowed_packets". The is a possibly key for the Instance.database_flags map field.`, |
| 44 | + }, |
| 45 | + "value_type": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Optional: true, |
| 49 | + Description: `ValueType describes the semantic type of the value that the flag accepts. The supported values are:- 'VALUE_TYPE_UNSPECIFIED', 'STRING', 'INTEGER', 'FLOAT', 'NONE'.`, |
| 50 | + }, |
| 51 | + "accepts_multiple_values": { |
| 52 | + Type: schema.TypeBool, |
| 53 | + Computed: true, |
| 54 | + Optional: true, |
| 55 | + Description: `Whether the database flag accepts multiple values. If true, a comma-separated list of stringified values may be specified.`, |
| 56 | + }, |
| 57 | + "supported_db_versions": { |
| 58 | + Type: schema.TypeList, |
| 59 | + Computed: true, |
| 60 | + Optional: true, |
| 61 | + Description: `Major database engine versions for which this flag is supported. Supported values are:- 'DATABASE_VERSION_UNSPECIFIED', and 'POSTGRES_14'.`, |
| 62 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 63 | + }, |
| 64 | + "requires_db_restart": { |
| 65 | + Type: schema.TypeBool, |
| 66 | + Computed: true, |
| 67 | + Optional: true, |
| 68 | + 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).`, |
| 69 | + }, |
| 70 | + "string_restrictions": { |
| 71 | + Type: schema.TypeList, |
| 72 | + Computed: true, |
| 73 | + Optional: true, |
| 74 | + Description: `Restriction on STRING type value.`, |
| 75 | + MaxItems: 1, |
| 76 | + Elem: &schema.Resource{ |
| 77 | + Schema: map[string]*schema.Schema{ |
| 78 | + "allowed_values": { |
| 79 | + Type: schema.TypeList, |
| 80 | + Computed: true, |
| 81 | + Optional: true, |
| 82 | + Description: `The list of allowed values, if bounded. This field will be empty if there is a unbounded number of allowed values.`, |
| 83 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + "integer_restrictions": { |
| 89 | + Type: schema.TypeList, |
| 90 | + Computed: true, |
| 91 | + Optional: true, |
| 92 | + Description: `Restriction on INTEGER type value.`, |
| 93 | + MaxItems: 1, |
| 94 | + Elem: &schema.Resource{ |
| 95 | + Schema: map[string]*schema.Schema{ |
| 96 | + "min_value": { |
| 97 | + Type: schema.TypeInt, |
| 98 | + Computed: true, |
| 99 | + Optional: true, |
| 100 | + Description: `The minimum value that can be specified, if applicable.`, |
| 101 | + }, |
| 102 | + "max_value": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + Optional: true, |
| 106 | + Description: `The maximum value that can be specified, if applicable.`, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func dataSourceAlloydbSupportedDatabaseFlagsRead(d *schema.ResourceData, meta interface{}) error { |
| 119 | + config := meta.(*Config) |
| 120 | + userAgent, err := generateUserAgentString(d, config.UserAgent) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + project, err := getProject(d, config) |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error fetching project: %s", err) |
| 127 | + } |
| 128 | + billingProject := project |
| 129 | + if bp, err := getBillingProject(d, config); err == nil { |
| 130 | + billingProject = bp |
| 131 | + } |
| 132 | + location := "" |
| 133 | + if v, ok := d.GetOk("location"); ok { |
| 134 | + location = v.(string) |
| 135 | + } |
| 136 | + if location == "" { |
| 137 | + return fmt.Errorf("Location cannot be empty") |
| 138 | + } |
| 139 | + var supportedDatabaseFlagIterator *alloydb.SupportedDatabaseFlagIterator |
| 140 | + dbFlagsReq := new(alloydbpb.ListSupportedDatabaseFlagsRequest) |
| 141 | + alloydbClient := config.NewAlloydbClient(userAgent) |
| 142 | + if alloydbClient == nil { |
| 143 | + return fmt.Errorf("Failed to call the API to fetch the supported database flags") |
| 144 | + } |
| 145 | + err = nil |
| 146 | + err = retryTime(func() error { |
| 147 | + url := fmt.Sprintf("v1/projects/%s/locations/%s/supportedDatabaseFlags", billingProject, location) |
| 148 | + supportedDatabaseFlagIterator = alloydbClient.ListSupportedDatabaseFlags(config.context, dbFlagsReq, gax.WithPath(url)) |
| 149 | + return nil |
| 150 | + }, 5) |
| 151 | + if err != nil { |
| 152 | + return handleNotFoundError(err, d, fmt.Sprintf("Supported Database flags %q", d.Id())) |
| 153 | + } |
| 154 | + |
| 155 | + var supportedDatabaseFlags []map[string]interface{} |
| 156 | + for { |
| 157 | + supportedDatabaseFlag := make(map[string]interface{}) |
| 158 | + flag, err := supportedDatabaseFlagIterator.Next() |
| 159 | + if err == iterator.Done { |
| 160 | + break |
| 161 | + } |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf(fmt.Sprintf("Failed to fetch the supported database flags for the provided location: %s", location)) |
| 164 | + } |
| 165 | + if flag.Name != "" { |
| 166 | + supportedDatabaseFlag["name"] = flag.Name |
| 167 | + } |
| 168 | + if flag.FlagName != "" { |
| 169 | + supportedDatabaseFlag["flag_name"] = flag.FlagName |
| 170 | + } |
| 171 | + supportedDatabaseFlag["value_type"] = flag.ValueType.String() |
| 172 | + supportedDatabaseFlag["accepts_multiple_values"] = flag.AcceptsMultipleValues |
| 173 | + supportedDatabaseFlag["requires_db_restart"] = flag.RequiresDbRestart |
| 174 | + if flag.SupportedDbVersions != nil { |
| 175 | + dbVersions := make([]string, 0, len(flag.SupportedDbVersions)) |
| 176 | + for _, supDbVer := range flag.SupportedDbVersions { |
| 177 | + dbVersions = append(dbVersions, supDbVer.String()) |
| 178 | + } |
| 179 | + supportedDatabaseFlag["supported_db_versions"] = dbVersions |
| 180 | + } |
| 181 | + |
| 182 | + if flag.Restrictions != nil { |
| 183 | + if stringRes, ok := flag.Restrictions.(*alloydbpb.SupportedDatabaseFlag_StringRestrictions_); ok { |
| 184 | + restrictions := make([]map[string][]string, 0, 1) |
| 185 | + fetchedAllowedValues := stringRes.StringRestrictions.AllowedValues |
| 186 | + if fetchedAllowedValues != nil { |
| 187 | + allowedValues := make([]string, 0, len(fetchedAllowedValues)) |
| 188 | + for _, val := range fetchedAllowedValues { |
| 189 | + allowedValues = append(allowedValues, val) |
| 190 | + } |
| 191 | + stringRestrictions := map[string][]string{ |
| 192 | + "allowed_values": allowedValues, |
| 193 | + } |
| 194 | + restrictions = append(restrictions, stringRestrictions) |
| 195 | + supportedDatabaseFlag["string_restrictions"] = restrictions |
| 196 | + } |
| 197 | + } |
| 198 | + if integerRes, ok := flag.Restrictions.(*alloydbpb.SupportedDatabaseFlag_IntegerRestrictions_); ok { |
| 199 | + restrictions := make([]map[string]int64, 0, 1) |
| 200 | + minValue := integerRes.IntegerRestrictions.MinValue |
| 201 | + maxValue := integerRes.IntegerRestrictions.MaxValue |
| 202 | + integerRestrictions := map[string]int64{ |
| 203 | + "min_value": minValue.GetValue(), |
| 204 | + "max_value": maxValue.GetValue(), |
| 205 | + } |
| 206 | + restrictions = append(restrictions, integerRestrictions) |
| 207 | + supportedDatabaseFlag["integer_restrictions"] = restrictions |
| 208 | + } |
| 209 | + } |
| 210 | + supportedDatabaseFlags = append(supportedDatabaseFlags, supportedDatabaseFlag) |
| 211 | + } |
| 212 | + if err := d.Set("supported_database_flags", supportedDatabaseFlags); err != nil { |
| 213 | + return fmt.Errorf("Error setting supported_database_flags: %s", err) |
| 214 | + } |
| 215 | + d.SetId(fmt.Sprintf("projects/%s/locations/%s/supportedDbFlags", project, location)) |
| 216 | + return nil |
| 217 | +} |
0 commit comments