Skip to content

Commit

Permalink
Replace usage of Create field with CreateContext in resource_integer (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Apr 12, 2022
1 parent 3bb503a commit ebf4911
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions internal/provider/resource_integer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package provider

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"strconv"
"strings"

Expand All @@ -17,9 +19,9 @@ func resourceInteger() *schema.Resource {
"This resource can be used in conjunction with resources that have the `create_before_destroy` " +
"lifecycle flag set, to avoid conflicts with unique names during the brief period where both the " +
"old and new resources exist concurrently.",
Create: CreateInteger,
Read: schema.Noop,
Delete: schema.RemoveFromState,
CreateContext: CreateInteger,
Read: schema.Noop,
Delete: schema.RemoveFromState,
Importer: &schema.ResourceImporter{
State: ImportInteger,
},
Expand Down Expand Up @@ -70,13 +72,17 @@ func resourceInteger() *schema.Resource {
}
}

func CreateInteger(d *schema.ResourceData, meta interface{}) error {
func CreateInteger(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
min := d.Get("min").(int)
max := d.Get("max").(int)
seed := d.Get("seed").(string)

if max <= min {
return fmt.Errorf("Minimum value needs to be smaller than maximum value")
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "minimum value needs to be smaller than maximum value",
})
}
rand := NewRand(seed)
number := rand.Intn((max+1)-min) + min
Expand Down

0 comments on commit ebf4911

Please sign in to comment.