Use to the stable version of Lego
This commit is contained in:
parent
36e273714d
commit
b2cf03fa5c
108 changed files with 3847 additions and 1152 deletions
369
vendor/gopkg.in/ns1/ns1-go.v2/rest/model/data/meta.go
generated
vendored
369
vendor/gopkg.in/ns1/ns1-go.v2/rest/model/data/meta.go
generated
vendored
|
@ -1,12 +1,23 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FeedPtr represents the dynamic metadata value in which a feed is providing the value.
|
||||
type FeedPtr struct {
|
||||
FeedID string `json:"feed,omitempty"`
|
||||
}
|
||||
|
||||
// Meta contains information on an entities metadata table. Metadata key/value
|
||||
// pairs are used by a records' filter pipeline during a dns query.
|
||||
// Meta contains information on an entity's metadata table. Metadata key/value
|
||||
// pairs are used by a record's filter pipeline during a dns query.
|
||||
// All values can be a feed id as well, indicating real-time updates of these values.
|
||||
// Structure/Precendence of metadata tables:
|
||||
// - Record
|
||||
|
@ -125,3 +136,357 @@ type Meta struct {
|
|||
// int or FeedPtr.
|
||||
HighWatermark interface{} `json:"high_watermark,omitempty"`
|
||||
}
|
||||
|
||||
// StringMap returns a map[string]interface{} representation of metadata (for use with terraform in nested structures)
|
||||
func (meta *Meta) StringMap() map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
v := reflect.Indirect(reflect.ValueOf(meta))
|
||||
t := v.Type()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
fv := v.Field(i)
|
||||
if fv.IsNil() {
|
||||
continue
|
||||
}
|
||||
tag := f.Tag.Get("json")
|
||||
|
||||
tag = strings.Split(tag, ",")[0]
|
||||
|
||||
m[tag] = FormatInterface(fv.Interface())
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// FormatInterface takes an interface of types: string, bool, int, float64, []string, and FeedPtr, and returns a string representation of said interface
|
||||
func FormatInterface(i interface{}) string {
|
||||
switch v := i.(type) {
|
||||
case string:
|
||||
return v
|
||||
case bool:
|
||||
if v {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
case int:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64)
|
||||
case []string:
|
||||
return strings.Join(v, ",")
|
||||
case []interface{}:
|
||||
slc := make([]string, 0)
|
||||
for _, s := range v {
|
||||
slc = append(slc, s.(string))
|
||||
}
|
||||
return strings.Join(slc, ",")
|
||||
case FeedPtr:
|
||||
data, _ := json.Marshal(v)
|
||||
return string(data)
|
||||
default:
|
||||
panic(fmt.Sprintf("expected v to be convertible to a string, got: %+v, %T", v, v))
|
||||
}
|
||||
}
|
||||
|
||||
// ParseType returns an interface containing a string, bool, int, float64, []string, or FeedPtr
|
||||
// float64 values with no decimal may be returned as integers, but that should be ok because the api won't know the difference
|
||||
// when it's json encoded
|
||||
func ParseType(s string) interface{} {
|
||||
slc := strings.Split(s, ",")
|
||||
if len(slc) > 1 {
|
||||
sort.Strings(slc)
|
||||
return slc
|
||||
}
|
||||
|
||||
feedptr := FeedPtr{}
|
||||
err := json.Unmarshal([]byte(s), &feedptr)
|
||||
if err == nil {
|
||||
return feedptr
|
||||
}
|
||||
|
||||
f, err := strconv.ParseFloat(s, 64)
|
||||
if err == nil {
|
||||
if !isIntegral(f) {
|
||||
return f
|
||||
}
|
||||
return int(f)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func isIntegral(f float64) bool {
|
||||
return f == float64(int(f))
|
||||
}
|
||||
|
||||
// MetaFromMap creates a *Meta and uses reflection to set fields from a map. This will panic if a value for a key is not a string.
|
||||
// This it to ensure compatibility with terraform
|
||||
func MetaFromMap(m map[string]interface{}) *Meta {
|
||||
meta := &Meta{}
|
||||
mv := reflect.Indirect(reflect.ValueOf(meta))
|
||||
mt := mv.Type()
|
||||
for k, v := range m {
|
||||
name := ToCamel(k)
|
||||
if _, ok := mt.FieldByName(name); ok {
|
||||
fv := mv.FieldByName(name)
|
||||
if name == "Up" {
|
||||
if v.(string) == "1" {
|
||||
fv.Set(reflect.ValueOf(true))
|
||||
} else {
|
||||
fv.Set(reflect.ValueOf(false))
|
||||
}
|
||||
} else {
|
||||
fv.Set(reflect.ValueOf(ParseType(v.(string))))
|
||||
}
|
||||
}
|
||||
}
|
||||
return meta
|
||||
}
|
||||
|
||||
// metaValidation is a validation struct for a metadata field.
|
||||
// It contains the kinds of types that the field can be, and a list of check functions that will run on the field
|
||||
type metaValidation struct {
|
||||
kinds []reflect.Kind
|
||||
checkFuncs []func(v reflect.Value) error
|
||||
}
|
||||
|
||||
// validateLatLong makes sure that the given lat/long is within the range 180.0 to -180.0
|
||||
func validateLatLong(v reflect.Value) error {
|
||||
if v.Kind() == reflect.Float64 {
|
||||
f := v.Interface().(float64)
|
||||
if f < -180.0 || f > 180.0 {
|
||||
return fmt.Errorf("latitude/longitude values must be between -180.0 and 180.0, got %f", f)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateCidr makes sure that the given string is a valid cidr
|
||||
func validateCidr(v reflect.Value) error {
|
||||
if v.Kind() == reflect.String {
|
||||
s := v.Interface().(string)
|
||||
_, _, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var last error
|
||||
if v.Kind() == reflect.Slice {
|
||||
slc := v.Interface().([]interface{})
|
||||
for _, s := range slc {
|
||||
_, _, err := net.ParseCIDR(s.(string))
|
||||
last = err
|
||||
}
|
||||
}
|
||||
return last
|
||||
}
|
||||
|
||||
// validatePositiveNumber makes sure that the given number (float or int) is positive
|
||||
func validatePositiveNumber(fieldName string, v reflect.Value) error {
|
||||
i := 0
|
||||
if v.Kind() == reflect.Int {
|
||||
i = v.Interface().(int)
|
||||
|
||||
}
|
||||
|
||||
if v.Kind() == reflect.Float64 {
|
||||
i = int(v.Interface().(float64))
|
||||
}
|
||||
|
||||
if i < 0 {
|
||||
return fmt.Errorf("%s must be a positive number, was %+v", fieldName, v.Interface())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// geoMap is a map of all of the georegions
|
||||
var geoMap = map[string]struct{}{
|
||||
"US-EAST": {}, "US-CENTRAL": {}, "US-WEST": {},
|
||||
"EUROPE": {}, "ASIAPAC": {}, "SOUTH-AMERICA": {}, "AFRICA": {},
|
||||
}
|
||||
|
||||
// geoKeyString returns a string representation of all of the georegions
|
||||
func geoKeyString() string {
|
||||
length := 0
|
||||
slc := make([]string, 0)
|
||||
for k := range geoMap {
|
||||
slc = append(slc, k)
|
||||
length += len(k) + 1
|
||||
}
|
||||
sort.Strings(slc)
|
||||
|
||||
b := bytes.NewBuffer(make([]byte, 0, length-1))
|
||||
|
||||
for _, k := range slc {
|
||||
b.WriteString(k + ",")
|
||||
}
|
||||
|
||||
return strings.TrimRight(b.String(), ",")
|
||||
}
|
||||
|
||||
// validateGeoregion makes sure that the given georegion is correct
|
||||
func validateGeoregion(v reflect.Value) error {
|
||||
if v.Kind() == reflect.String {
|
||||
s := v.String()
|
||||
if _, ok := geoMap[s]; !ok {
|
||||
return fmt.Errorf("georegion must be one or more of %s, found %s", geoKeyString(), s)
|
||||
}
|
||||
}
|
||||
|
||||
if v.Kind() == reflect.Slice {
|
||||
if slc, ok := v.Interface().([]string); ok {
|
||||
for _, s := range slc {
|
||||
if _, ok := geoMap[s]; !ok {
|
||||
return fmt.Errorf("georegion must be one or more of %s, found %s", geoKeyString(), s)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
slc := v.Interface().([]interface{})
|
||||
for _, s := range slc {
|
||||
if _, ok := geoMap[s.(string)]; !ok {
|
||||
return fmt.Errorf("georegion must be one or more of %s, found %s", geoKeyString(), s)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateCountryStateProvince makes sure that the given field only has two characters
|
||||
func validateCountryStateProvince(v reflect.Value) error {
|
||||
if v.Kind() == reflect.String {
|
||||
s := v.String()
|
||||
if len(s) != 2 {
|
||||
return fmt.Errorf("country/state/province codes must be 2 digits as specified in ISO3166/ISO3166-2, got: %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
if v.Kind() == reflect.Slice {
|
||||
if slc, ok := v.Interface().([]string); ok {
|
||||
for _, s := range slc {
|
||||
if len(s) != 2 {
|
||||
return fmt.Errorf("country/state/province codes must be 2 digits as specified in ISO3166/ISO3166-2, got: %s", s)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
slc := v.Interface().([]interface{})
|
||||
for _, s := range slc {
|
||||
if len(s.(string)) != 2 {
|
||||
return fmt.Errorf("country/state/province codes must be 2 digits as specified in ISO3166/ISO3166-2, got: %s", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateNoteLength validates that a note's length is less than 256 characters
|
||||
func validateNoteLength(v reflect.Value) error {
|
||||
if v.Kind() == reflect.String {
|
||||
s := v.String()
|
||||
if len(s) > 256 {
|
||||
return fmt.Errorf("note length must be less than 256 characters, was %d", len(s))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkFuncs is shorthand for returning a slice of functions that take a reflect.Value and return an error
|
||||
func checkFuncs(f ...func(v reflect.Value) error) []func(v reflect.Value) error {
|
||||
return f
|
||||
}
|
||||
|
||||
// kinds is shorthand for returning a slice of reflect.Kind
|
||||
func kinds(k ...reflect.Kind) []reflect.Kind {
|
||||
return k
|
||||
}
|
||||
|
||||
// validationMap is a map of meta fields to validation types and functions
|
||||
var validationMap = map[string]metaValidation{
|
||||
"Up": {kinds(reflect.Bool), nil},
|
||||
"Connections": {kinds(reflect.Int), checkFuncs(
|
||||
func(v reflect.Value) error {
|
||||
return validatePositiveNumber("Connections", v)
|
||||
})},
|
||||
"Requests": {kinds(reflect.Int), checkFuncs(
|
||||
func(v reflect.Value) error {
|
||||
return validatePositiveNumber("Requests", v)
|
||||
})},
|
||||
"LoadAvg": {kinds(reflect.Float64, reflect.Int), checkFuncs(
|
||||
func(v reflect.Value) error {
|
||||
return validatePositiveNumber("LoadAvg", v)
|
||||
})},
|
||||
"Pulsar": {kinds(reflect.String), nil},
|
||||
"Latitude": {kinds(reflect.Float64, reflect.Int), checkFuncs(validateLatLong)},
|
||||
"Longitude": {kinds(reflect.Float64, reflect.Int), checkFuncs(validateLatLong)},
|
||||
"Georegion": {kinds(reflect.String, reflect.Slice), checkFuncs(validateGeoregion)},
|
||||
"Country": {kinds(reflect.String, reflect.Slice), checkFuncs(validateCountryStateProvince)},
|
||||
"USState": {kinds(reflect.String, reflect.Slice), checkFuncs(validateCountryStateProvince)},
|
||||
"CAProvince": {kinds(reflect.String, reflect.Slice), checkFuncs(validateCountryStateProvince)},
|
||||
"Note": {kinds(reflect.String), checkFuncs(validateNoteLength)},
|
||||
"IPPrefixes": {kinds(reflect.String, reflect.Slice), checkFuncs(validateCidr)},
|
||||
"ASN": {kinds(reflect.String, reflect.Slice), nil},
|
||||
"Priority": {kinds(reflect.Int), checkFuncs(
|
||||
func(v reflect.Value) error {
|
||||
return validatePositiveNumber("Priority", v)
|
||||
})},
|
||||
"Weight": {kinds(reflect.Float64, reflect.Int), checkFuncs(
|
||||
func(v reflect.Value) error {
|
||||
return validatePositiveNumber("Weight", v)
|
||||
})},
|
||||
"LowWatermark": {kinds(reflect.Int), nil},
|
||||
"HighWatermark": {kinds(reflect.Int), nil},
|
||||
}
|
||||
|
||||
// validate takes a field name, a reflect value, and metaValidation and validates the given field
|
||||
func validate(name string, v reflect.Value, m metaValidation) (errs []error) {
|
||||
|
||||
check := true
|
||||
// if this is a FeedPtr or a *FeedPtr then we're ok, skip checking the rest of the types
|
||||
if v.Kind() == reflect.Struct || v.Kind() == reflect.Invalid {
|
||||
check = false
|
||||
}
|
||||
|
||||
if check {
|
||||
match := false
|
||||
for _, k := range m.kinds {
|
||||
if k == v.Kind() {
|
||||
match = true
|
||||
}
|
||||
}
|
||||
|
||||
if !match {
|
||||
errs = append(errs, fmt.Errorf("found type mismatch for meta field '%s'. expected %+v, got: %+v", name, m.kinds, v.Kind()))
|
||||
}
|
||||
|
||||
for _, f := range m.checkFuncs {
|
||||
err := f(v)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if v.Kind() == reflect.Struct {
|
||||
if _, ok := v.Interface().(FeedPtr); !ok {
|
||||
errs = append(errs, fmt.Errorf("if a meta field is a struct, it must be a FeedPtr, got: %s", v.Type()))
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Validate validates metadata fields and returns a list of errors if any are found
|
||||
func (meta *Meta) Validate() (errs []error) {
|
||||
mv := reflect.Indirect(reflect.ValueOf(meta))
|
||||
mt := mv.Type()
|
||||
for i := 0; i < mt.NumField(); i++ {
|
||||
fv := mt.Field(i)
|
||||
err := validate(fv.Name, mv.Field(i).Elem(), validationMap[fv.Name])
|
||||
if err != nil {
|
||||
errs = append(errs, err...)
|
||||
}
|
||||
}
|
||||
|
||||
return errs
|
||||
}
|
||||
|
|
48
vendor/gopkg.in/ns1/ns1-go.v2/rest/model/data/string.go
generated
vendored
Normal file
48
vendor/gopkg.in/ns1/ns1-go.v2/rest/model/data/string.go
generated
vendored
Normal file
|
@ -0,0 +1,48 @@
|
|||
package data
|
||||
|
||||
// The following code is a modified copy of functions found in strcase, found here: https://github.com/iancoleman/strcase
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// ToCamel converts a string to CamelCase
|
||||
func ToCamel(s string) string {
|
||||
s = addWordBoundariesToNumbers(s)
|
||||
s = strings.TrimSpace(s)
|
||||
b := bytes.NewBuffer(make([]byte, 0))
|
||||
c := true
|
||||
for _, v := range s {
|
||||
if v >= 'A' && v <= 'Z' {
|
||||
b.WriteString(string(v))
|
||||
}
|
||||
if v >= '0' && v <= '9' {
|
||||
b.WriteString(string(v))
|
||||
}
|
||||
if v >= 'a' && v <= 'z' {
|
||||
if c {
|
||||
b.WriteString(string(unicode.ToUpper(v)))
|
||||
} else {
|
||||
b.WriteString(string(v))
|
||||
}
|
||||
}
|
||||
if v == '_' || v == ' ' || v == '-' {
|
||||
c = true
|
||||
} else {
|
||||
c = false
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(b.String())
|
||||
}
|
||||
|
||||
var numberSequence = regexp.MustCompile(`([a-zA-Z])(\d+)([a-zA-Z]?)`)
|
||||
var numberReplacement = []byte(`$1 $2 $3`)
|
||||
|
||||
func addWordBoundariesToNumbers(s string) string {
|
||||
b := []byte(s)
|
||||
b = numberSequence.ReplaceAll(b, numberReplacement)
|
||||
return string(b)
|
||||
}
|
15
vendor/gopkg.in/ns1/ns1-go.v2/rest/model/dns/zone.go
generated
vendored
15
vendor/gopkg.in/ns1/ns1-go.v2/rest/model/dns/zone.go
generated
vendored
|
@ -1,5 +1,6 @@
|
|||
package dns
|
||||
|
||||
import "encoding/json"
|
||||
import "gopkg.in/ns1/ns1-go.v2/rest/model/data"
|
||||
|
||||
// Zone wraps an NS1 /zone resource
|
||||
|
@ -44,13 +45,13 @@ func (z Zone) String() string {
|
|||
|
||||
// ZoneRecord wraps Zone's "records" attribute
|
||||
type ZoneRecord struct {
|
||||
Domain string `json:"Domain,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Link string `json:"link,omitempty"`
|
||||
ShortAns []string `json:"short_answers,omitempty"`
|
||||
Tier int `json:"tier,omitempty"`
|
||||
TTL int `json:"ttl,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Domain string `json:"Domain,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Link string `json:"link,omitempty"`
|
||||
ShortAns []string `json:"short_answers,omitempty"`
|
||||
Tier json.Number `json:"tier,omitempty"`
|
||||
TTL int `json:"ttl,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
}
|
||||
|
||||
// ZonePrimary wraps a Zone's "primary" attribute
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue