Пример #1
0
// ObjectReaction returns a ReactionFunc that takes a generic action string of the form
// <verb>-<resource> or <verb>-<subresource>-<resource> and attempts to return a runtime
// Object or error that matches the requested action. For instance, list-replicationControllers
// should attempt to return a list of replication controllers. This method delegates to the
// ObjectRetriever interface to satisfy retrieval of lists or retrieval of single items.
// TODO: add support for sub resources
func ObjectReaction(o ObjectRetriever, mapper meta.RESTMapper) ReactionFunc {
	return func(action Action) (runtime.Object, error) {
		_, kind, err := mapper.VersionAndKindForResource(action.GetResource())
		if err != nil {
			return nil, fmt.Errorf("unrecognized action %s: %v", action.GetResource(), err)
		}

		// TODO: have mapper return a Kind for a subresource?
		switch castAction := action.(type) {
		case ListAction:
			return o.Kind(kind+"List", "")
		case GetAction:
			return o.Kind(kind, castAction.GetName())
		case DeleteAction:
			return o.Kind(kind, castAction.GetName())
		case CreateAction:
			meta, err := api.ObjectMetaFor(castAction.GetObject())
			if err != nil {
				return nil, err
			}
			return o.Kind(kind, meta.Name)
		case UpdateAction:
			meta, err := api.ObjectMetaFor(castAction.GetObject())
			if err != nil {
				return nil, err
			}
			return o.Kind(kind, meta.Name)
		default:
			return nil, fmt.Errorf("no reaction implemented for %s", action)
		}
	}
}
Пример #2
0
// ObjectReaction returns a ReactionFunc that takes a generic action string of the form
// <verb>-<resource> or <verb>-<subresource>-<resource> and attempts to return a runtime
// Object or error that matches the requested action. For instance, list-replicationControllers
// should attempt to return a list of replication controllers. This method delegates to the
// ObjectRetriever interface to satisfy retrieval of lists or retrieval of single items.
// TODO: add support for sub resources
func ObjectReaction(o ObjectRetriever, mapper meta.RESTMapper) ReactionFunc {
	return func(action FakeAction) (runtime.Object, error) {
		segments := strings.Split(action.Action, "-")
		var verb, resource string
		switch len(segments) {
		case 3:
			verb, _, resource = segments[0], segments[1], segments[2]
		case 2:
			verb, resource = segments[0], segments[1]
		default:
			return nil, fmt.Errorf("unrecognized action, need two or three segments <verb>-<resource> or <verb>-<subresource>-<resource>: %s", action.Action)
		}
		_, kind, err := mapper.VersionAndKindForResource(resource)
		if err != nil {
			return nil, fmt.Errorf("unrecognized action %s: %v", resource, err)
		}
		// TODO: have mapper return a Kind for a subresource?
		switch verb {
		case "list", "search":
			return o.Kind(kind+"List", "")
		case "get", "create", "update", "delete":
			// TODO: handle sub resources
			if s, ok := action.Value.(string); ok && action.Value != nil {
				return o.Kind(kind, s)
			}
			return o.Kind(kind, "unknown")
		default:
			return nil, fmt.Errorf("no reaction implemented for %s", action.Action)
		}
		return nil, nil
	}
}
Пример #3
0
// ResolveResource returns the resource type and name of the resourceString.
// If the resource string has no specified type, defaultResource will be returned.
func ResolveResource(defaultResource, resourceString string, mapper meta.RESTMapper) (string, string, error) {
	if mapper == nil {
		return "", "", errors.New("mapper cannot be nil")
	}

	var name string
	parts := strings.Split(resourceString, "/")
	switch len(parts) {
	case 1:
		name = parts[0]
	case 2:
		_, kind, err := mapper.VersionAndKindForResource(parts[0])
		if err != nil {
			return "", "", err
		}
		name = parts[1]
		resource, _ := meta.KindToResource(kind, false)
		return resource, name, nil
	default:
		return "", "", fmt.Errorf("invalid resource format: %s", resourceString)
	}

	return defaultResource, name, nil
}