Esempio n. 1
0
// Extract interprets any serverResult as a Server, if possible.
func (r serverResult) Extract() (*Server, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var response struct {
		Server Server `mapstructure:"server"`
	}

	config := &mapstructure.DecoderConfig{
		DecodeHook: toMapFromString,
		Result:     &response,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return nil, err
	}

	err = decoder.Decode(r.Body)
	if err != nil {
		return nil, err
	}

	return &response.Server, nil
}
Esempio n. 2
0
// Extract returns a pointer to a PreviewedStack object and is called after a
// Preview operation.
func (r PreviewResult) Extract() (*PreviewedStack, error) {
	if r.Err != nil {
		return nil, r.Err
	}

	var res struct {
		Stack *PreviewedStack `mapstructure:"stack"`
	}

	config := &mapstructure.DecoderConfig{
		Result:           &res,
		WeaklyTypedInput: true,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return nil, err
	}

	if err := decoder.Decode(r.Body); err != nil {
		return nil, err
	}

	b := r.Body.(map[string]interface{})["stack"].(map[string]interface{})

	if date, ok := b["creation_time"]; ok && date != nil {
		t, err := time.Parse(STACK_TIME_FMT, date.(string))
		if err != nil {
			return nil, err
		}
		res.Stack.CreationTime = t
	}

	if date, ok := b["updated_time"]; ok && date != nil {
		t, err := time.Parse(STACK_TIME_FMT, date.(string))
		if err != nil {
			return nil, err
		}
		res.Stack.UpdatedTime = t
	}

	return res.Stack, err
}
Esempio n. 3
0
// Extract provides access to the individual Flavor returned by the Get function.
func (gr GetResult) Extract() (*Flavor, error) {
	if gr.Err != nil {
		return nil, gr.Err
	}

	var result struct {
		Flavor Flavor `mapstructure:"flavor"`
	}

	cfg := &mapstructure.DecoderConfig{
		DecodeHook: defaulter,
		Result:     &result,
	}
	decoder, err := mapstructure.NewDecoder(cfg)
	if err != nil {
		return nil, err
	}
	err = decoder.Decode(gr.Body)
	return &result.Flavor, err
}
Esempio n. 4
0
// DecodeHeader is a function that decodes a header (usually of type map[string]interface{}) to
// another type (usually a struct). This function is used by the objectstorage package to give
// users access to response headers without having to query a map. A DecodeHookFunction is used,
// because OpenStack-based clients return header values as arrays (Go slices).
func DecodeHeader(from, to interface{}) error {
	config := &mapstructure.DecoderConfig{
		DecodeHook: func(from, to reflect.Kind, data interface{}) (interface{}, error) {
			if from == reflect.Slice {
				return data.([]string)[0], nil
			}
			return data, nil
		},
		Result:           to,
		WeaklyTypedInput: true,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return err
	}
	if err := decoder.Decode(from); err != nil {
		return err
	}
	return nil
}
Esempio n. 5
0
// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
func ExtractServers(page pagination.Page) ([]Server, error) {
	casted := page.(ServerPage).Body

	var response struct {
		Servers []Server `mapstructure:"servers"`
	}

	config := &mapstructure.DecoderConfig{
		DecodeHook: toMapFromString,
		Result:     &response,
	}
	decoder, err := mapstructure.NewDecoder(config)
	if err != nil {
		return nil, err
	}

	err = decoder.Decode(casted)

	return response.Servers, err
}
Esempio n. 6
0
// ExtractFlavors provides access to the list of flavors in a page acquired from the List operation.
func ExtractFlavors(page pagination.Page) ([]Flavor, error) {
	casted := page.(FlavorPage).Body
	var container struct {
		Flavors []Flavor `mapstructure:"flavors"`
	}

	cfg := &mapstructure.DecoderConfig{
		DecodeHook: defaulter,
		Result:     &container,
	}
	decoder, err := mapstructure.NewDecoder(cfg)
	if err != nil {
		return container.Flavors, err
	}
	err = decoder.Decode(casted)
	if err != nil {
		return container.Flavors, err
	}

	return container.Flavors, nil
}