From 869f85e4bbd4fec3fddbe32efc62725b9c2c6e45 Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Wed, 5 Oct 2016 11:55:45 -0700 Subject: [PATCH] Add support for specifying container short IDs Signed-off-by: Mrunal Patel --- server/container.go | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/server/container.go b/server/container.go index 5833d4cc..c62b2032 100644 --- a/server/container.go +++ b/server/container.go @@ -329,11 +329,16 @@ func (s *Server) createSandboxContainer(containerID string, containerName string // StartContainer starts the container. func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (*pb.StartContainerResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil { return nil, fmt.Errorf("specified container not found: %s", containerID) @@ -348,11 +353,16 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque // StopContainer stops a running container with a grace period (i.e., timeout). func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest) (*pb.StopContainerResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil { return nil, fmt.Errorf("specified container not found: %s", containerID) @@ -368,11 +378,16 @@ func (s *Server) StopContainer(ctx context.Context, req *pb.StopContainerRequest // RemoveContainer removes the container. If the container is running, the container // should be force removed. func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*pb.RemoveContainerResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil { return nil, fmt.Errorf("specified container not found: %s", containerID) @@ -447,11 +462,16 @@ func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersReque // ContainerStatus returns status of the container. func (s *Server) ContainerStatus(ctx context.Context, req *pb.ContainerStatusRequest) (*pb.ContainerStatusResponse, error) { - containerID := req.GetContainerId() - - if containerID == "" { + ctrID := req.GetContainerId() + if ctrID == "" { return nil, fmt.Errorf("container ID should not be empty") } + + containerID, err := s.ctrIDIndex.Get(ctrID) + if err != nil { + return nil, fmt.Errorf("container with ID starting with %s not found: %v", ctrID, err) + } + c := s.state.containers.Get(containerID) if c == nil {