Skip to content

Commit e199dd6

Browse files
Modernize the code base (#2225)
* Modernize the code base * More modernizations by copilot
1 parent 4de8789 commit e199dd6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+287
-331
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ run:
44

55
linters:
66
default: standard
7+
enable:
8+
- modernize
79
settings:
810
dupl:
911
threshold: 100

linode/acceptance/test_retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// This function wraps the given testing.T and handles errors accordingly.
1111
// This should only be used for flapping API tests.
1212
func RunTestWithRetries(t testing.TB, maxAttempts int, f func(t *WrappedT)) {
13-
for i := 0; i < maxAttempts; i++ {
13+
for i := range maxAttempts {
1414
wrappedT := &WrappedT{
1515
TB: t,
1616
failed: atomic.Bool{},

linode/acceptance/util.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func initOptInTests() {
6868
return
6969
}
7070

71-
for _, testName := range strings.Split(optInTestsValue, ",") {
71+
for testName := range strings.SplitSeq(optInTestsValue, ",") {
7272
optInTests[testName] = struct{}{}
7373
}
7474
}
@@ -308,7 +308,7 @@ func CheckResourceAttrListContains(resName, path, desiredValue string) resource.
308308
return fmt.Errorf("attribute %s does not exist", path)
309309
}
310310

311-
for i := 0; i < length; i++ {
311+
for i := range length {
312312
if rs.Primary.Attributes[path+"."+strconv.Itoa(i)] == desiredValue {
313313
return nil
314314
}
@@ -330,7 +330,7 @@ func LoopThroughStringList(resName, path string, listValidateFunc ListAttrValida
330330
return fmt.Errorf("attribute %s does not exist", path)
331331
}
332332

333-
for i := 0; i < length; i++ {
333+
for i := range length {
334334
err := listValidateFunc(resName, path+"."+strconv.Itoa(i), s)
335335
if err != nil {
336336
return fmt.Errorf("Value not found:%s", err)
@@ -354,7 +354,7 @@ func CheckListContains(resName, path, value string) resource.TestCheckFunc {
354354
return fmt.Errorf("attribute %s does not exist", path)
355355
}
356356

357-
for i := 0; i < length; i++ {
357+
for i := range length {
358358
foundValue, ok := rs.Primary.Attributes[path+"."+strconv.Itoa(i)]
359359
if !ok {
360360
return fmt.Errorf("index %d does not exist in attributes", i)
@@ -546,7 +546,7 @@ func AnyOfTestCheckFunc(funcs ...resource.TestCheckFunc) resource.TestCheckFunc
546546
}
547547
}
548548

549-
func ExecuteTemplate(t testing.TB, templateName string, data interface{}) string {
549+
func ExecuteTemplate(t testing.TB, templateName string, data any) string {
550550
t.Helper()
551551

552552
var b bytes.Buffer
@@ -591,7 +591,7 @@ type ProviderMetaModifier func(ctx context.Context, data *schema.ResourceData, c
591591
func ModifyProviderMeta(provider *schema.Provider, modifier ProviderMetaModifier) *schema.Provider {
592592
oldConfigure := provider.ConfigureContextFunc
593593

594-
provider.ConfigureContextFunc = func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
594+
provider.ConfigureContextFunc = func(ctx context.Context, data *schema.ResourceData) (any, diag.Diagnostics) {
595595
config, err := oldConfigure(ctx, data)
596596
if err != nil {
597597
return nil, err
@@ -715,13 +715,9 @@ func GetRegionsWithCaps(capabilities []string, regionType string, filters ...Reg
715715
return false
716716
})
717717

718-
result := make([]string, len(filteredRegions))
719-
720-
for i, r := range filteredRegions {
721-
result[i] = r.ID
722-
}
723-
724-
return result, nil
718+
return helper.MapSlice(filteredRegions, func(r linodego.Region) string {
719+
return r.ID
720+
}), nil
725721
}
726722

727723
// GetRandomRegionWithCaps gets a random region given a list of region capabilities.
@@ -766,10 +762,7 @@ func GetTestClient() (*linodego.Client, error) {
766762
return nil, fmt.Errorf("LINODE_TOKEN must be set for acceptance tests")
767763
}
768764

769-
apiVersion := os.Getenv("LINODE_API_VERSION")
770-
if apiVersion == "" {
771-
apiVersion = "v4beta"
772-
}
765+
apiVersion := cmp.Or(os.Getenv("LINODE_API_VERSION"), "v4beta")
773766

774767
config := &helper.Config{
775768
AccessToken: token,
@@ -791,10 +784,7 @@ func GetTestClientAlternateToken(tokenName string) (*linodego.Client, error) {
791784
return nil, fmt.Errorf("%s must be set for acceptance tests", tokenName)
792785
}
793786

794-
apiVersion := os.Getenv("LINODE_API_VERSION")
795-
if apiVersion == "" {
796-
apiVersion = "v4beta"
797-
}
787+
apiVersion := cmp.Or(os.Getenv("LINODE_API_VERSION"), "v4beta")
798788

799789
config := &helper.Config{
800790
AccessToken: token,

linode/childaccounts/framework_models.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ func (model *ChildAccountFilterModel) parseAccounts(accounts []linodego.ChildAcc
1717
result := make([]account.DataSourceModel, len(accounts))
1818

1919
for i, childAccount := range accounts {
20-
// Shadow to avoid implicit memory aliasing
21-
childAccount := childAccount
22-
2320
result[i].ParseAccount(&childAccount)
2421
}
2522

linode/databaseaccesscontrols/resource.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func Resource() *schema.Resource {
2626
}
2727
}
2828

29-
func readResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
29+
func readResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
3030
client := meta.(*helper.ProviderMeta).Client
3131

3232
dbID, dbType, err := parseID(d.Id())
@@ -52,7 +52,7 @@ func readResource(ctx context.Context, d *schema.ResourceData, meta interface{})
5252
return nil
5353
}
5454

55-
func createResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
55+
func createResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5656
dbID := d.Get("database_id").(int)
5757
dbType := d.Get("database_type").(string)
5858

@@ -61,7 +61,7 @@ func createResource(ctx context.Context, d *schema.ResourceData, meta interface{
6161
return updateResource(ctx, d, meta)
6262
}
6363

64-
func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
64+
func updateResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6565
client := meta.(*helper.ProviderMeta).Client
6666

6767
dbID, dbType, err := parseID(d.Id())
@@ -85,7 +85,7 @@ func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{
8585
return readResource(ctx, d, meta)
8686
}
8787

88-
func deleteResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
func deleteResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
8989
client := meta.(*helper.ProviderMeta).Client
9090

9191
dbID, dbType, err := parseID(d.Id())

linode/databasemysql/resource.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Resource() *schema.Resource {
4242
}
4343
}
4444

45-
func readResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
45+
func readResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
4646
client := meta.(*helper.ProviderMeta).Client
4747
id, err := strconv.Atoi(d.Id())
4848
if err != nil {
@@ -89,12 +89,12 @@ func readResource(ctx context.Context, d *schema.ResourceData, meta interface{})
8989
d.Set("updated", db.Updated.Format(time.RFC3339))
9090
d.Set("root_username", creds.Username)
9191
d.Set("version", db.Version)
92-
d.Set("updates", []interface{}{databaseshared.FlattenMaintenanceWindow(db.Updates)})
92+
d.Set("updates", []any{databaseshared.FlattenMaintenanceWindow(db.Updates)})
9393

9494
return nil
9595
}
9696

97-
func createResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
97+
func createResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
9898
client := meta.(*helper.ProviderMeta).Client
9999

100100
p, err := client.NewEventPollerWithoutEntity(linodego.EntityDatabase, linodego.ActionDatabaseCreate)
@@ -140,10 +140,10 @@ func createResource(ctx context.Context, d *schema.ResourceData, meta interface{
140140
return diag.Errorf("failed to wait for database active: %s", err)
141141
}
142142

143-
updateList := d.Get("updates").([]interface{})
143+
updateList := d.Get("updates").([]any)
144144

145145
if !d.GetRawConfig().GetAttr("updates").IsNull() && len(updateList) > 0 {
146-
updates, err := databaseshared.ExpandMaintenanceWindow(updateList[0].(map[string]interface{}))
146+
updates, err := databaseshared.ExpandMaintenanceWindow(updateList[0].(map[string]any))
147147
if err != nil {
148148
return diag.Errorf("failed to read maintenance window config: %s", err)
149149
}
@@ -172,7 +172,7 @@ func createResource(ctx context.Context, d *schema.ResourceData, meta interface{
172172
return readResource(ctx, d, meta)
173173
}
174174

175-
func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
175+
func updateResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
176176
client := meta.(*helper.ProviderMeta).Client
177177

178178
id, err := strconv.Atoi(d.Id())
@@ -199,8 +199,8 @@ func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{
199199
var updates *linodego.MySQLDatabaseMaintenanceWindow
200200

201201
updatesRaw := d.Get("updates")
202-
if updatesRaw != nil && len(updatesRaw.([]interface{})) > 0 {
203-
expanded, err := databaseshared.ExpandMaintenanceWindow(updatesRaw.([]interface{})[0].(map[string]interface{}))
202+
if updatesRaw != nil && len(updatesRaw.([]any)) > 0 {
203+
expanded, err := databaseshared.ExpandMaintenanceWindow(updatesRaw.([]any)[0].(map[string]any))
204204
if err != nil {
205205
return diag.Errorf("failed to update maintenance window: %s", err)
206206
}
@@ -239,7 +239,7 @@ func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{
239239
return readResource(ctx, d, meta)
240240
}
241241

242-
func deleteResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
242+
func deleteResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
243243
client := meta.(*helper.ProviderMeta).Client
244244
id, err := strconv.Atoi(d.Id())
245245
if err != nil {

linode/databasemysql/schema_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ var resourceSchema = map[string]*schema.Schema{
8989
Type: schema.TypeString,
9090
Description: "The day to perform maintenance.",
9191
Required: true,
92-
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
92+
ValidateDiagFunc: func(i any, path cty.Path) diag.Diagnostics {
9393
if _, err := databaseshared.ExpandDayOfWeek(i.(string)); err != nil {
9494
return diag.FromErr(err)
9595
}

linode/databasemysqlbackups/datasource.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func DataSource() *schema.Resource {
1717
}
1818
}
1919

20-
func readDataSource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
20+
func readDataSource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
2121
results, err := filterConfig.FilterDataSource(ctx, d, meta, listBackups, flattenBackup)
2222
if err != nil {
2323
return nil
@@ -33,15 +33,15 @@ func readDataSource(ctx context.Context, d *schema.ResourceData, meta interface{
3333
func listBackups(
3434
ctx context.Context, d *schema.ResourceData, client *linodego.Client,
3535
options *linodego.ListOptions,
36-
) ([]interface{}, error) {
36+
) ([]any, error) {
3737
dbID := d.Get("database_id").(int)
3838

3939
backups, err := client.ListMySQLDatabaseBackups(ctx, dbID, options)
4040
if err != nil {
4141
return nil, err
4242
}
4343

44-
result := make([]interface{}, len(backups))
44+
result := make([]any, len(backups))
4545

4646
for i, v := range backups {
4747
result[i] = v
@@ -50,10 +50,10 @@ func listBackups(
5050
return result, nil
5151
}
5252

53-
func flattenBackup(data interface{}) map[string]interface{} {
53+
func flattenBackup(data any) map[string]any {
5454
backup := data.(linodego.MySQLDatabaseBackup)
5555

56-
result := make(map[string]interface{})
56+
result := make(map[string]any)
5757

5858
result["id"] = backup.ID
5959
result["label"] = backup.Label

linode/databasepostgresql/resource.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Resource() *schema.Resource {
4242
}
4343
}
4444

45-
func readResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
45+
func readResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
4646
client := meta.(*helper.ProviderMeta).Client
4747
id, err := strconv.Atoi(d.Id())
4848
if err != nil {
@@ -91,12 +91,12 @@ func readResource(ctx context.Context, d *schema.ResourceData, meta interface{})
9191
d.Set("updated", db.Updated.Format(time.RFC3339))
9292
d.Set("root_username", creds.Username)
9393
d.Set("version", db.Version)
94-
d.Set("updates", []interface{}{databaseshared.FlattenMaintenanceWindow(db.Updates)})
94+
d.Set("updates", []any{databaseshared.FlattenMaintenanceWindow(db.Updates)})
9595

9696
return nil
9797
}
9898

99-
func createResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
99+
func createResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
100100
client := meta.(*helper.ProviderMeta).Client
101101

102102
p, err := client.NewEventPollerWithoutEntity(linodego.EntityDatabase, linodego.ActionDatabaseCreate)
@@ -143,10 +143,10 @@ func createResource(ctx context.Context, d *schema.ResourceData, meta interface{
143143
return diag.Errorf("failed to wait for database active: %s", err)
144144
}
145145

146-
updateList := d.Get("updates").([]interface{})
146+
updateList := d.Get("updates").([]any)
147147

148148
if !d.GetRawConfig().GetAttr("updates").IsNull() && len(updateList) > 0 {
149-
updates, err := databaseshared.ExpandMaintenanceWindow(updateList[0].(map[string]interface{}))
149+
updates, err := databaseshared.ExpandMaintenanceWindow(updateList[0].(map[string]any))
150150
if err != nil {
151151
return diag.Errorf("failed to read maintenance window config: %s", err)
152152
}
@@ -177,7 +177,7 @@ func createResource(ctx context.Context, d *schema.ResourceData, meta interface{
177177
return readResource(ctx, d, meta)
178178
}
179179

180-
func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
180+
func updateResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
181181
client := meta.(*helper.ProviderMeta).Client
182182

183183
id, err := strconv.Atoi(d.Id())
@@ -204,8 +204,8 @@ func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{
204204
var updates *linodego.DatabaseMaintenanceWindow
205205

206206
updatesRaw := d.Get("updates")
207-
if updatesRaw != nil && len(updatesRaw.([]interface{})) > 0 {
208-
expanded, err := databaseshared.ExpandMaintenanceWindow(updatesRaw.([]interface{})[0].(map[string]interface{}))
207+
if updatesRaw != nil && len(updatesRaw.([]any)) > 0 {
208+
expanded, err := databaseshared.ExpandMaintenanceWindow(updatesRaw.([]any)[0].(map[string]any))
209209
if err != nil {
210210
return diag.Errorf("failed to update maintenance window: %s", err)
211211
}
@@ -239,7 +239,7 @@ func updateResource(ctx context.Context, d *schema.ResourceData, meta interface{
239239
return readResource(ctx, d, meta)
240240
}
241241

242-
func deleteResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
242+
func deleteResource(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
243243
client := meta.(*helper.ProviderMeta).Client
244244
id, err := strconv.Atoi(d.Id())
245245
if err != nil {

linode/databasepostgresql/schema_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var resourceSchema = map[string]*schema.Schema{
103103
Type: schema.TypeString,
104104
Description: "The day to perform maintenance.",
105105
Required: true,
106-
ValidateDiagFunc: func(i interface{}, path cty.Path) diag.Diagnostics {
106+
ValidateDiagFunc: func(i any, path cty.Path) diag.Diagnostics {
107107
if _, err := databaseshared.ExpandDayOfWeek(i.(string)); err != nil {
108108
return diag.FromErr(err)
109109
}

0 commit comments

Comments
 (0)