Skip to content

Commit

Permalink
SCALRCORE-17568 Refactor event def parsing + disable version check
Browse files Browse the repository at this point in the history
  • Loading branch information
lbn committed Nov 20, 2020
1 parent ddda667 commit e9cea17
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform v0.12.0
github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/scalr/go-scalr v0.0.0-20201120165912-e824e20ebd32
github.com/scalr/go-scalr v0.0.0-20201120175216-bdac90743c0a
)

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ github.com/scalr/go-scalr v0.0.0-20201120103742-6fd5e98a9c5e h1:D9//8zdMEITlLC4D
github.com/scalr/go-scalr v0.0.0-20201120103742-6fd5e98a9c5e/go.mod h1:eEDwNsOdDKg5TJHljpJWfb4LhobfCkUHZo/uX/rPph8=
github.com/scalr/go-scalr v0.0.0-20201120165912-e824e20ebd32 h1:MM7QCfgWvqLePrTS2dRlUE8uxaR8R8XbLTcWU0ME0mY=
github.com/scalr/go-scalr v0.0.0-20201120165912-e824e20ebd32/go.mod h1:eEDwNsOdDKg5TJHljpJWfb4LhobfCkUHZo/uX/rPph8=
github.com/scalr/go-scalr v0.0.0-20201120175216-bdac90743c0a h1:NLKK5IoC859g+RMdxbkbAJ6Z4COXgkaUqW6EJoYEuc0=
github.com/scalr/go-scalr v0.0.0-20201120175216-bdac90743c0a/go.mod h1:eEDwNsOdDKg5TJHljpJWfb4LhobfCkUHZo/uX/rPph8=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
Expand Down
15 changes: 2 additions & 13 deletions scalr/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,6 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}

if providerVersion.ProviderVersion != "dev" {
// We purposefully ignore the error and return the previous error, as
// checking for version constraints is considered optional.
constraints, _ := host.VersionConstraints(scalrServiceIDs[0], "scalr-provider")

// First check any constraints we might have received.
if constraints != nil {
if err := checkConstraints(constraints); err != nil {
return nil, err
}
}
}

// When we don't have any constraints errors, also check for discovery
// errors before we continue.
if discoErr != nil {
Expand Down Expand Up @@ -261,6 +248,7 @@ func credentialsSource(config *Config) auth.CredentialsSource {
// checkConstraints checks service version constrains against our own
// version and returns rich and informational diagnostics in case any
// incompatibilities are detected.
// nolint:deadcode,unused
func checkConstraints(c *disco.Constraints) error {
if c == nil || c.Minimum == "" || c.Maximum == "" {
return nil
Expand Down Expand Up @@ -354,6 +342,7 @@ func checkConstraints(c *disco.Constraints) error {
return fmt.Errorf("%s\n\n%s", summary, details)
}

// nolint:unused
func checkConstraintsWarning(err error) error {
return fmt.Errorf(
"Failed to check version constraints: %v\n\n"+
Expand Down
32 changes: 18 additions & 14 deletions scalr/resource_scalr_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ func validateEventDefinitions(eventName string) error {
"Invalid value for events '%s'. Allowed values: %s", eventName, strings.Join(eventDefinitionsQuoted, ", "))
}

func parseEventDefinitions(d *schema.ResourceData) ([]*scalr.EventDefinition, error) {
events := d.Get("events").([]interface{})
var eventDefinitions []*scalr.EventDefinition
for _, eventID := range events {
if err := validateEventDefinitions(eventID.(string)); err != nil {
return nil, err
}
eventDefinitions = append(eventDefinitions, &scalr.EventDefinition{ID: eventID.(string)})
}
return eventDefinitions, nil
}

func resourceScalrWebhookCreate(d *schema.ResourceData, meta interface{}) error {
scalrClient := meta.(*scalr.Client)

Expand All @@ -137,13 +149,9 @@ func resourceScalrWebhookCreate(d *schema.ResourceData, meta interface{}) error
return err
}

events := d.Get("events").([]interface{})
var eventDefinitions []*scalr.EventDefinition
for _, eventID := range events {
if err := validateEventDefinitions(eventID.(string)); err != nil {
return err
}
eventDefinitions = append(eventDefinitions, &scalr.EventDefinition{ID: eventID.(string)})
eventDefinitions, err := parseEventDefinitions(d)
if err != nil {
return err
}

// Create a new options struct.
Expand Down Expand Up @@ -213,13 +221,9 @@ func resourceScalrWebhookUpdate(d *schema.ResourceData, meta interface{}) error
scalrClient := meta.(*scalr.Client)

var err error
events := d.Get("events").([]string)
var eventDefinitions []*scalr.EventDefinition
for _, eventID := range events {
if err = validateEventDefinitions(eventID); err != nil {
return err
}
eventDefinitions = append(eventDefinitions, &scalr.EventDefinition{ID: eventID})
eventDefinitions, err := parseEventDefinitions(d)
if err != nil {
return err
}

// Create a new options struct.
Expand Down

0 comments on commit e9cea17

Please sign in to comment.