Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements to automation account & schedule (interval property for recurring) #1384

Merged
merged 6 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions azurerm/helpers/suppress/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package suppress

import (
"github.com/hashicorp/terraform/helper/schema"
"strings"
)

func CaseDifference(_, old, new string, _ *schema.ResourceData) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get this added into helper/schema 😄

return strings.ToLower(old) == strings.ToLower(new)
}
43 changes: 43 additions & 0 deletions azurerm/helpers/suppress/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package suppress

import "testing"

func TestHelper_Suppress_CaseDifference(t *testing.T) {
cases := []struct {
StringA string
StringB string
Suppress bool
}{
{
StringA: "",
StringB: "",
Suppress: true,
},
{
StringA: "ye old text",
StringB: "",
Suppress: false,
},
{
StringA: "ye old text?",
StringB: "ye different text",
Suppress: false,
},
{
StringA: "ye same text!",
StringB: "ye same text!",
Suppress: true,
},
{
StringA: "ye old text?",
StringB: "Ye OLD texT?",
Suppress: true,
},
}

for _, tc := range cases {
if CaseDifference("test", tc.StringA, tc.StringB, nil) != tc.Suppress {
t.Fatalf("Expected CaseDifference to return %t for '%q' == '%q'", tc.Suppress, tc.StringA, tc.StringB)
}
}
}
18 changes: 18 additions & 0 deletions azurerm/helpers/suppress/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package suppress

import (
"time"

"github.com/hashicorp/terraform/helper/schema"
)

func Rfc3339Time(_, old, new string, _ *schema.ResourceData) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh this too! 👍

ot, oerr := time.Parse(time.RFC3339, old)
nt, nerr := time.Parse(time.RFC3339, new)

if oerr != nil || nerr != nil {
return false
}

return nt.Equal(ot)
}
48 changes: 48 additions & 0 deletions azurerm/helpers/suppress/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package suppress

import "testing"

func TestHelper_Supress_Rfc3339Time(t *testing.T) {
cases := []struct {
TimeA string
TimeB string
Suppress bool
}{
{
TimeA: "",
TimeB: "",
Suppress: false,
},
{
TimeA: "this is not a time",
TimeB: "neither is this",
Suppress: false,
},
{
TimeA: "2000-01-01T01:23:45+00:00",
TimeB: "that is a valid time",
Suppress: false,
},
{
TimeA: "2000-01-01T01:23:45+00:00",
TimeB: "1984-07-07T01:23:45+00:00",
Suppress: false,
},
{
TimeA: "2000-01-01T01:23:45+00:00",
TimeB: "2000-01-01T01:23:45Z",
Suppress: true,
},
{
TimeA: "2000-01-01T01:23:45-08:00",
TimeB: "2000-01-01T09:23:45Z",
Suppress: true,
},
}

for _, tc := range cases {
if Rfc3339Time("test", tc.TimeA, tc.TimeB, nil) != tc.Suppress {
t.Fatalf("Expected Rfc3339Time to return %t for '%q' == '%q'", tc.Suppress, tc.TimeA, tc.TimeB)
}
}
}
47 changes: 47 additions & 0 deletions azurerm/helpers/validate/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package validate

import (
"fmt"
"time"

"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/terraform/helper/schema"
)

//todo, now in terraform helper, switch over once vended
// -> https://github.com/hashicorp/terraform/blob/master/helper/validation/validation.go#L263
func Rfc3339Time(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := date.ParseTime(time.RFC3339, v); err != nil {
errors = append(errors, fmt.Errorf("%q has the invalid RFC3339 date format %q: %+v", k, i, err))
}

return
}

func Rfc3339DateInFutureBy(d time.Duration) schema.SchemaValidateFunc {
return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

t, err := date.ParseTime(time.RFC3339, v)
if err != nil {
errors = append(errors, fmt.Errorf("%q has the invalid RFC3339 date format %q: %+v", k, i, err))
return
}

if time.Until(t) < d {
errors = append(errors, fmt.Errorf("%q is %q and should be at least %q in the future", k, i, d))
}

return
}
}
88 changes: 88 additions & 0 deletions azurerm/helpers/validate/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package validate

import (
"testing"
"time"
)

func TestHelper_Validate_RFC3339Time(t *testing.T) {
cases := []struct {
Time string
Errors int
}{
{
Time: "",
Errors: 1,
},
{
Time: "this is not a date",
Errors: 1,
},
{
Time: "2000-01-01",
Errors: 1,
},
{
Time: "2000-01-01T01:23:45",
Errors: 1,
},
{
Time: "2000-01-01T01:23:45Z",
Errors: 0,
},
{
Time: "2000-01-01T01:23:45+00:00",
Errors: 0,
},
}

for _, tc := range cases {
_, errors := Rfc3339Time(tc.Time, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected Rfc3339Time to have an error for '%q'", tc.Time)
}
}
}

func TestHelper_Validate_Rfc3339DateInFutureBy(t *testing.T) {
cases := []struct {
Time string
Duration time.Duration
Errors int
}{
{
Time: "",
Duration: time.Hour,
Errors: 1,
},
{
Time: "not a time",
Duration: time.Hour,
Errors: 1,
},
{
Time: time.Now().String(),
Duration: time.Hour,
Errors: 1,
},
{
Time: time.Now().Add(time.Hour * 7).String(),
Duration: time.Hour,
Errors: 1,
},
{
Time: time.Now().Add(time.Minute).String(),
Duration: time.Minute * 7,
Errors: 0,
},
}

for _, tc := range cases {
_, errors := Rfc3339DateInFutureBy(tc.Duration)(tc.Time, "test")

if len(errors) < tc.Errors {
t.Fatalf("Expected Rfc3339DateInFutureBy to have an error for '%q' in future by `%q`", tc.Time, tc.Duration.String())
}
}
}
56 changes: 0 additions & 56 deletions azurerm/import_arm_automation_account_test.go

This file was deleted.

33 changes: 0 additions & 33 deletions azurerm/import_arm_automation_credential_test.go

This file was deleted.

33 changes: 0 additions & 33 deletions azurerm/import_arm_automation_runbook_test.go

This file was deleted.

Loading