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

Add date validator to data_source_storage_account_sas #4064

Merged
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
15 changes: 9 additions & 6 deletions azurerm/data_source_storage_account_sas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/go-azure-helpers/storage"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

const (
Expand Down Expand Up @@ -99,16 +100,18 @@ func dataSourceArmStorageAccountSharedAccessSignature() *schema.Resource {

// Always in UTC and must be ISO-8601 format
"start": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ISO8601DateTime,
},

// Always in UTC and must be ISO-8601 format
"expiry": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ISO8601DateTime,
},

"permissions": {
Expand Down
15 changes: 15 additions & 0 deletions azurerm/helpers/validate/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/Azure/go-autorest/autorest/date"
iso8601 "github.com/btubbs/datetime"
Copy link
Contributor

Choose a reason for hiding this comment

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

internal heads up: licence is MIT ✅

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
Expand Down Expand Up @@ -41,6 +42,20 @@ func RFC3339Time(i interface{}, k string) (warnings []string, errors []error) {
return warnings, errors
}

func ISO8601DateTime(i interface{}, k string) (warnings []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 := iso8601.Parse(v, time.UTC); err != nil {
errors = append(errors, fmt.Errorf("%q has the invalid ISO8601 date format %q: %+v", k, i, err))
}

return warnings, errors
}

// RFC3339 date is duration d or greater into the future
func RFC3339DateInFutureBy(d time.Duration) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {
Expand Down
54 changes: 54 additions & 0 deletions azurerm/helpers/validate/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,60 @@ func TestRFC3339Time(t *testing.T) {
}
}

func TestISO8601DateTime(t *testing.T) {
cases := []struct {
Time string
Errors int
}{
{
Time: "",
Errors: 1,
},
{
Time: "this is not a date",
Errors: 1,
},
{
Time: "2000-06-31", // No 31st of 6th
Errors: 1,
},
{
Time: "01/21/2015", // not valid US date with slashes
Errors: 1,
},
{
Time: "01-21-2015", // not valid US date with dashes
Errors: 1,
},
{
Time: "2000-01-01",
Errors: 0,
},
{
Time: "2000-01-01T01:23:45",
Errors: 0,
},
{
Time: "2000-01-01T01:23:45Z",
Errors: 0,
},
{
Time: "2000-01-01T01:23:45+00:00",
Errors: 0,
},
}

for _, tc := range cases {
t.Run(tc.Time, func(t *testing.T) {
_, errors := ISO8601DateTime(tc.Time, "test")

if len(errors) != tc.Errors {
t.Fatalf("Expected ISO8601DateTime to have %d but got %d errors for %q", tc.Errors, len(errors), tc.Time)
}
})
}
}

func TestRFC3339DateInFutureBy(t *testing.T) {
cases := []struct {
Name string
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require (
github.com/Azure/go-autorest/autorest v0.3.0
github.com/Azure/go-autorest/autorest/adal v0.1.0
github.com/Azure/go-autorest/autorest/date v0.1.0
github.com/btubbs/datetime v0.1.0
github.com/davecgh/go-spew v1.1.1
github.com/dnaeon/go-vcr v1.0.1 // indirect
github.com/google/uuid v1.1.1
Expand All @@ -15,6 +16,7 @@ require (
github.com/hashicorp/go-version v1.1.0
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform v0.12.6
github.com/relvacode/iso8601 v0.0.0-20181221151331-e9cae14c704e // indirect
github.com/satori/go.uuid v1.2.0
github.com/satori/uuid v0.0.0-20160927100844-b061729afc07
github.com/terraform-providers/terraform-provider-azuread v0.4.1-0.20190610202312-5a179146b9f9
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k=
github.com/btubbs/datetime v0.1.0 h1:183iHRjmNAokYM5D8V3wbEOOEe/HYEYpm7E2oom3vhM=
github.com/btubbs/datetime v0.1.0/go.mod h1:n2BZ/2ltnRzNiz27aE3wUb2onNttQdC+WFxAoks5jJM=
github.com/census-instrumentation/opencensus-proto v0.1.0-0.20181214143942-ba49f56771b8/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.0 h1:LzQXZOgg4CQfE6bFvXGM30YZL1WW/M337pXml+GrcZ4=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -403,6 +405,8 @@ github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/relvacode/iso8601 v0.0.0-20181221151331-e9cae14c704e h1:NQHGYUZfr1cCsFZwC31Thmhuc9K6eUUmeNI9J9QMfB0=
github.com/relvacode/iso8601 v0.0.0-20181221151331-e9cae14c704e/go.mod h1:Kn09ZkOSv8PRg6gPzFTGF6xIk6p7UFCVqGmS/+uUZ3Y=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/btubbs/datetime/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/btubbs/datetime/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions vendor/github.com/btubbs/datetime/Gopkg.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/btubbs/datetime/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/btubbs/datetime/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions vendor/github.com/btubbs/datetime/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/btubbs/datetime/parse.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading