Migrate to go-acme/lego.
This commit is contained in:
parent
4a68d29ce2
commit
87da7520de
286 changed files with 14021 additions and 2501 deletions
438
vendor/github.com/exoscale/egoscale/serialization.go
generated
vendored
438
vendor/github.com/exoscale/egoscale/serialization.go
generated
vendored
|
@ -54,7 +54,9 @@ func info(command interface{}) (*CommandInfo, error) {
|
|||
// prepareValues uses a command to build a POST request
|
||||
//
|
||||
// command is not a Command so it's easier to Test
|
||||
func prepareValues(prefix string, params url.Values, command interface{}) error {
|
||||
func prepareValues(prefix string, command interface{}) (url.Values, error) {
|
||||
params := url.Values{}
|
||||
|
||||
value := reflect.ValueOf(command)
|
||||
typeof := reflect.TypeOf(command)
|
||||
|
||||
|
@ -66,7 +68,7 @@ func prepareValues(prefix string, params url.Values, command interface{}) error
|
|||
|
||||
// Checking for nil commands
|
||||
if !value.IsValid() {
|
||||
return fmt.Errorf("cannot serialize the invalid value %#v", command)
|
||||
return nil, fmt.Errorf("cannot serialize the invalid value %#v", command)
|
||||
}
|
||||
|
||||
for i := 0; i < typeof.NumField(); i++ {
|
||||
|
@ -77,217 +79,165 @@ func prepareValues(prefix string, params url.Values, command interface{}) error
|
|||
|
||||
val := value.Field(i)
|
||||
tag := field.Tag
|
||||
|
||||
var err error
|
||||
var name string
|
||||
var value interface{}
|
||||
|
||||
if json, ok := tag.Lookup("json"); ok {
|
||||
n, required := ExtractJSONTag(field.Name, json)
|
||||
name := prefix + n
|
||||
name = prefix + n
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
v := val.Int()
|
||||
if v == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got 0", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(name, strconv.FormatInt(v, 10))
|
||||
}
|
||||
value, err = prepareInt(val.Int(), required)
|
||||
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
v := val.Uint()
|
||||
if v == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got 0", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(name, strconv.FormatUint(v, 10))
|
||||
}
|
||||
value, err = prepareUint(val.Uint(), required)
|
||||
|
||||
case reflect.Float32, reflect.Float64:
|
||||
v := val.Float()
|
||||
if v == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got 0", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(name, strconv.FormatFloat(v, 'f', -1, 64))
|
||||
}
|
||||
value, err = prepareFloat(val.Float(), required)
|
||||
|
||||
case reflect.String:
|
||||
v := val.String()
|
||||
if v == "" {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got \"\"", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(name, v)
|
||||
}
|
||||
value, err = prepareString(val.String(), required)
|
||||
|
||||
case reflect.Bool:
|
||||
v := val.Bool()
|
||||
if !v {
|
||||
if required {
|
||||
params.Set(name, "false")
|
||||
}
|
||||
} else {
|
||||
params.Set(name, "true")
|
||||
}
|
||||
case reflect.Ptr:
|
||||
if val.IsNil() {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty ptr", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
switch field.Type.Elem().Kind() {
|
||||
case reflect.Bool:
|
||||
params.Set(name, strconv.FormatBool(val.Elem().Bool()))
|
||||
case reflect.Struct:
|
||||
i := val.Interface()
|
||||
s, ok := i.(fmt.Stringer)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s.%s (%v) is not a Stringer", typeof.Name(), field.Name, val.Kind())
|
||||
}
|
||||
if s != nil && s.String() == "" {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty value", typeof.Name(), field.Name, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(n, s.String())
|
||||
}
|
||||
default:
|
||||
log.Printf("[SKIP] %s.%s (%v) not supported", typeof.Name(), n, field.Type.Elem().Kind())
|
||||
}
|
||||
}
|
||||
case reflect.Slice:
|
||||
switch field.Type.Elem().Kind() {
|
||||
case reflect.Uint8:
|
||||
switch field.Type {
|
||||
case reflect.TypeOf(net.IPv4zero):
|
||||
ip := (net.IP)(val.Bytes())
|
||||
if ip == nil || ip.Equal(net.IPv4zero) {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got zero IPv4 address", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(name, ip.String())
|
||||
}
|
||||
case reflect.TypeOf(MAC48(0, 0, 0, 0, 0, 0)):
|
||||
mac := val.Interface().(MACAddress)
|
||||
s := mac.String()
|
||||
if s == "" {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty MAC address", typeof.Name(), field.Name, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(name, s)
|
||||
}
|
||||
default:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty slice", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
v := val.Bytes()
|
||||
params.Set(name, base64.StdEncoding.EncodeToString(v))
|
||||
}
|
||||
}
|
||||
case reflect.String:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty slice", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
elems := make([]string, 0, val.Len())
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
// XXX what if the value contains a comma? Double encode?
|
||||
s := val.Index(i).String()
|
||||
elems = append(elems, s)
|
||||
}
|
||||
params.Set(name, strings.Join(elems, ","))
|
||||
}
|
||||
default:
|
||||
switch field.Type.Elem() {
|
||||
case reflect.TypeOf(CIDR{}), reflect.TypeOf(UUID{}):
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty slice", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
value := reflect.ValueOf(val.Interface())
|
||||
ss := make([]string, val.Len())
|
||||
for i := 0; i < value.Len(); i++ {
|
||||
v := value.Index(i).Interface()
|
||||
s, ok := v.(fmt.Stringer)
|
||||
if !ok {
|
||||
return fmt.Errorf("not a String, %T", v)
|
||||
}
|
||||
ss[i] = s.String()
|
||||
}
|
||||
params.Set(name, strings.Join(ss, ","))
|
||||
}
|
||||
default:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty slice", typeof.Name(), n, val.Kind())
|
||||
}
|
||||
} else {
|
||||
err := prepareList(name, params, val.Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
value, err = prepareBool(val.Bool(), required)
|
||||
|
||||
case reflect.Map:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty map", typeof.Name(), field.Name, val.Kind())
|
||||
err = fmt.Errorf("field is required, got empty map")
|
||||
}
|
||||
} else {
|
||||
err := prepareMap(name, params, val.Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value, err = prepareMap(name, val.Interface())
|
||||
}
|
||||
|
||||
case reflect.Ptr:
|
||||
value, err = preparePtr(field.Type.Elem().Kind(), val, required)
|
||||
|
||||
case reflect.Slice:
|
||||
value, err = prepareSlice(name, field.Type, val, required)
|
||||
|
||||
case reflect.Struct:
|
||||
i := val.Interface()
|
||||
s, ok := i.(fmt.Stringer)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s.%s (%v) is not a Stringer", typeof.Name(), field.Name, val.Kind())
|
||||
}
|
||||
if s != nil && s.String() == "" {
|
||||
if required {
|
||||
return fmt.Errorf("%s.%s (%v) is required, got empty value", typeof.Name(), field.Name, val.Kind())
|
||||
}
|
||||
} else {
|
||||
params.Set(n, s.String())
|
||||
}
|
||||
value, err = prepareStruct(val.Interface(), required)
|
||||
|
||||
default:
|
||||
if required {
|
||||
return fmt.Errorf("unsupported type %s.%s (%v)", typeof.Name(), n, val.Kind())
|
||||
err = fmt.Errorf("unsupported type")
|
||||
}
|
||||
fmt.Printf("%s\n", val.Kind())
|
||||
}
|
||||
} else {
|
||||
log.Printf("[SKIP] %s.%s no json label found", typeof.Name(), field.Name)
|
||||
switch val.Kind() {
|
||||
case reflect.Struct:
|
||||
value, err = prepareEmbedStruct(val.Interface())
|
||||
default:
|
||||
log.Printf("[SKIP] %s.%s no json label found", typeof.Name(), field.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s.%s (%v) %s", typeof.Name(), field.Name, val.Kind(), err)
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case *string:
|
||||
if name != "" && v != nil {
|
||||
params.Set(name, *v)
|
||||
}
|
||||
case url.Values:
|
||||
for k, xs := range v {
|
||||
for _, x := range xs {
|
||||
params.Add(k, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func prepareList(prefix string, params url.Values, slice interface{}) error {
|
||||
func prepareInt(v int64, required bool) (*string, error) {
|
||||
if v == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got %d", v)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
value := strconv.FormatInt(v, 10)
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func prepareUint(v uint64, required bool) (*string, error) {
|
||||
if v == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got %d", v)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
value := strconv.FormatUint(v, 10)
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func prepareFloat(v float64, required bool) (*string, error) {
|
||||
if v == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got %f", v)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
value := strconv.FormatFloat(v, 'f', -1, 64)
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func prepareString(v string, required bool) (*string, error) {
|
||||
if v == "" {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got %q", v)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return &v, nil
|
||||
}
|
||||
|
||||
func prepareBool(v bool, required bool) (*string, error) {
|
||||
value := strconv.FormatBool(v)
|
||||
if !v {
|
||||
if required {
|
||||
return &value, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
func prepareList(prefix string, slice interface{}) (url.Values, error) {
|
||||
params := url.Values{}
|
||||
value := reflect.ValueOf(slice)
|
||||
|
||||
for i := 0; i < value.Len(); i++ {
|
||||
err := prepareValues(fmt.Sprintf("%s[%d].", prefix, i), params, value.Index(i).Interface())
|
||||
ps, err := prepareValues(fmt.Sprintf("%s[%d].", prefix, i), value.Index(i).Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for k, xs := range ps {
|
||||
for _, x := range xs {
|
||||
params.Add(k, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func prepareMap(prefix string, params url.Values, m interface{}) error {
|
||||
value := reflect.ValueOf(m)
|
||||
func prepareMap(prefix string, m interface{}) (url.Values, error) {
|
||||
value := url.Values{}
|
||||
v := reflect.ValueOf(m)
|
||||
|
||||
for i, key := range value.MapKeys() {
|
||||
for i, key := range v.MapKeys() {
|
||||
var keyName string
|
||||
var keyValue string
|
||||
|
||||
|
@ -295,19 +245,143 @@ func prepareMap(prefix string, params url.Values, m interface{}) error {
|
|||
case reflect.String:
|
||||
keyName = key.String()
|
||||
default:
|
||||
return fmt.Errorf("only map[string]string are supported (XXX)")
|
||||
return value, fmt.Errorf("only map[string]string are supported (XXX)")
|
||||
}
|
||||
|
||||
val := value.MapIndex(key)
|
||||
val := v.MapIndex(key)
|
||||
switch val.Kind() {
|
||||
case reflect.String:
|
||||
keyValue = val.String()
|
||||
default:
|
||||
return fmt.Errorf("only map[string]string are supported (XXX)")
|
||||
return value, fmt.Errorf("only map[string]string are supported (XXX)")
|
||||
}
|
||||
params.Set(fmt.Sprintf("%s[%d].%s", prefix, i, keyName), keyValue)
|
||||
|
||||
value.Set(fmt.Sprintf("%s[%d].%s", prefix, i, keyName), keyValue)
|
||||
}
|
||||
return nil
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func preparePtr(kind reflect.Kind, val reflect.Value, required bool) (*string, error) {
|
||||
if val.IsNil() {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got empty ptr")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case reflect.Bool:
|
||||
return prepareBool(val.Elem().Bool(), true)
|
||||
case reflect.Struct:
|
||||
return prepareStruct(val.Interface(), required)
|
||||
default:
|
||||
return nil, fmt.Errorf("kind %v is not supported as a ptr", kind)
|
||||
}
|
||||
}
|
||||
|
||||
func prepareSlice(name string, fieldType reflect.Type, val reflect.Value, required bool) (interface{}, error) {
|
||||
switch fieldType.Elem().Kind() {
|
||||
case reflect.Uint8:
|
||||
switch fieldType {
|
||||
case reflect.TypeOf(net.IPv4zero):
|
||||
ip := (net.IP)(val.Bytes())
|
||||
if ip == nil || ip.Equal(net.IP{}) {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got zero IPv4 address")
|
||||
}
|
||||
} else {
|
||||
value := ip.String()
|
||||
return &value, nil
|
||||
}
|
||||
|
||||
case reflect.TypeOf(MAC48(0, 0, 0, 0, 0, 0)):
|
||||
mac := val.Interface().(MACAddress)
|
||||
s := mac.String()
|
||||
if s == "" {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got empty MAC address")
|
||||
}
|
||||
} else {
|
||||
return &s, nil
|
||||
}
|
||||
default:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got empty slice")
|
||||
}
|
||||
} else {
|
||||
value := base64.StdEncoding.EncodeToString(val.Bytes())
|
||||
return &value, nil
|
||||
}
|
||||
}
|
||||
case reflect.String:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got empty slice")
|
||||
}
|
||||
} else {
|
||||
elems := make([]string, 0, val.Len())
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
// XXX what if the value contains a comma? Double encode?
|
||||
s := val.Index(i).String()
|
||||
elems = append(elems, s)
|
||||
}
|
||||
value := strings.Join(elems, ",")
|
||||
return &value, nil
|
||||
}
|
||||
default:
|
||||
switch fieldType.Elem() {
|
||||
case reflect.TypeOf(CIDR{}), reflect.TypeOf(UUID{}):
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got empty slice")
|
||||
}
|
||||
} else {
|
||||
v := reflect.ValueOf(val.Interface())
|
||||
ss := make([]string, val.Len())
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
e := v.Index(i).Interface()
|
||||
s, ok := e.(fmt.Stringer)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("not a String, %T", e)
|
||||
}
|
||||
ss[i] = s.String()
|
||||
}
|
||||
value := strings.Join(ss, ",")
|
||||
return &value, nil
|
||||
}
|
||||
default:
|
||||
if val.Len() == 0 {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got empty slice")
|
||||
}
|
||||
} else {
|
||||
return prepareList(name, val.Interface())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func prepareStruct(i interface{}, required bool) (*string, error) {
|
||||
s, ok := i.(fmt.Stringer)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("struct field not a Stringer")
|
||||
}
|
||||
|
||||
if s == nil {
|
||||
if required {
|
||||
return nil, fmt.Errorf("field is required, got %#v", s)
|
||||
}
|
||||
}
|
||||
|
||||
return prepareString(s.String(), required)
|
||||
}
|
||||
|
||||
func prepareEmbedStruct(i interface{}) (url.Values, error) {
|
||||
return prepareValues("", i)
|
||||
}
|
||||
|
||||
// ExtractJSONTag returns the variable name or defaultName as well as if the field is required (!omitempty)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue