Пример #1
0
// HandlePath is part of the Explorer interface
func (ex ZkExplorer) HandlePath(actionRepo proto.ActionRepository, zkPath string, r *http.Request) interface{} {
	result := NewZkResult(zkPath)

	if zkPath == "/zk" {
		cells, err := zk.ResolveWildcards(ex.zconn, []string{"/zk/*"})
		if err != nil {
			result.Error = err.Error()
			return result
		}
		for i, cell := range cells {
			cells[i] = cell[4:] // cut off "/zk/"
		}
		result.Children = cells
		sort.Strings(result.Children)
		return result
	}

	data, _, err := ex.zconn.Get(zkPath)
	if err != nil {
		result.Error = err.Error()
		return result
	}
	if m, _ := path.Match("/zk/global/vt/keyspaces/*", zkPath); m {
		keyspace := path.Base(zkPath)
		actionRepo.PopulateKeyspaceActions(result.Actions, keyspace)
	} else if m, _ := path.Match("/zk/global/vt/keyspaces/*/shards/*", zkPath); m {
		zkPathParts := strings.Split(zkPath, "/")
		keyspace := zkPathParts[5]
		shard := zkPathParts[7]
		actionRepo.PopulateShardActions(result.Actions, keyspace, shard)
	} else if m, _ := path.Match("/zk/*/vt/tablets/*", result.Path); m {
		zkPathParts := strings.Split(result.Path, "/")
		alias := zkPathParts[2] + "-" + zkPathParts[5]
		actionRepo.PopulateTabletActions(result.Actions, alias, r)
		ex.addTabletLinks(data, result)
	}
	result.Data = data
	children, _, err := ex.zconn.Children(zkPath)
	if err != nil {
		result.Error = err.Error()
		return result
	}
	result.Children = children
	sort.Strings(result.Children)
	return result
}
Пример #2
0
// HandlePath implements vtctld Explorer.
func (ex Explorer) HandlePath(actionRepo ctlproto.ActionRepository, rPath string, r *http.Request) interface{} {
	result := newExplorerResult(rPath)

	// Cut off explorerRoot prefix.
	if !strings.HasPrefix(rPath, explorerRoot) {
		result.Error = "invalid etcd explorer path: " + rPath
		return result
	}
	rPath = rPath[len(explorerRoot):]

	// Root is a list of cells.
	if rPath == "" {
		cells, err := ex.ts.getCellList()
		if err != nil {
			result.Error = err.Error()
			return result
		}
		result.Children = append([]string{globalCell}, cells...)
		return result
	}

	// Get a client for the requested cell.
	var client Client
	cell, rPath, err := splitCellPath(rPath)
	if err != nil {
		result.Error = err.Error()
		return result
	}
	if cell == globalCell {
		client = ex.ts.getGlobal()
	} else {
		client, err = ex.ts.getCell(cell)
		if err != nil {
			result.Error = "Can't get cell: " + err.Error()
			return result
		}
	}

	// Get the requested node data.
	resp, err := client.Get(rPath, true /* sort */, false /* recursive */)
	if err != nil {
		result.Error = err.Error()
		return result
	}
	if resp.Node == nil {
		result.Error = ErrBadResponse.Error()
		return result
	}
	result.Data = getNodeData(client, resp.Node)

	// Populate children.
	for _, node := range resp.Node.Nodes {
		result.Children = append(result.Children, path.Base(node.Key))
	}

	// Populate actions.
	if m, _ := path.Match(keyspaceDirPath("*"), rPath); m {
		actionRepo.PopulateKeyspaceActions(result.Actions, path.Base(rPath))
	} else if m, _ := path.Match(shardDirPath("*", "*"), rPath); m {
		if keyspace, shard, err := splitShardDirPath(rPath); err == nil {
			actionRepo.PopulateShardActions(result.Actions, keyspace, shard)
		}
	} else if m, _ := path.Match(path.Join(tabletsDirPath, "*"), rPath); m {
		actionRepo.PopulateTabletActions(result.Actions, path.Base(rPath), r)
		addTabletLinks(result, result.Data)
	}
	return result
}