forked from openshift/terraform-provider-azurerm
/
url.go
52 lines (42 loc) · 1.21 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package validate
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
func URLIsHTTPS(i interface{}, k string) (_ []string, errors []error) {
return URLWithScheme([]string{"https"})(i, k)
}
func URLIsHTTPOrHTTPS(i interface{}, k string) (_ []string, errors []error) {
return URLWithScheme([]string{"http", "https"})(i, k)
}
func URLWithScheme(validSchemes []string) 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
}
if v == "" {
errors = append(errors, fmt.Errorf("expected %q url to not be empty", k))
return
}
u, err := url.Parse(v)
if err != nil {
errors = append(errors, fmt.Errorf("%q url is in an invalid format: %q (%+v)", k, v, err))
return
}
if u.Host == "" {
errors = append(errors, fmt.Errorf("%q url has no host: %q", k, v))
return
}
for _, s := range validSchemes {
if u.Scheme == s {
return //last check so just return
}
}
errors = append(errors, fmt.Errorf("expected %q url %q to have a schema of: %q", k, v, strings.Join(validSchemes, ",")))
return
}
}