Add name param

Docker-DCO-1.1-Signed-off-by: Yutaka Matsubara <yutaka.matsubara@gmail.com> (github: mopemope)
This commit is contained in:
Yutaka Matsubara 2014-07-03 22:52:15 +09:00
parent 4652b01762
commit 88b75781eb
3 changed files with 56 additions and 2 deletions

View File

@ -112,7 +112,24 @@ func (b *dockerClientBackend) createContainer(cmd ...string) (libswarm.Sender, e
return nil, fmt.Errorf("dockerclient: spawn takes exactly 1 argument, got %d", len(cmd)) return nil, fmt.Errorf("dockerclient: spawn takes exactly 1 argument, got %d", len(cmd))
} }
resp, err := b.client.call("POST", "/containers/create", cmd[0]) param := cmd[0]
containerValues := url.Values{}
var postParam map[string]interface{}
if err := json.Unmarshal([]byte(param), &postParam); err != nil {
return nil, err
}
if name, ok := postParam["name"]; ok {
containerValues.Set("name", name.(string))
delete(postParam, "name")
tmp, err := json.Marshal(postParam)
if err != nil {
return nil, err
}
param = string(tmp)
}
resp, err := b.client.call("POST", "/containers/create?"+containerValues.Encode(), param)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -79,6 +79,25 @@ func TestSpawn(t *testing.T) {
server.Check() server.Check()
} }
func TestSpawnWithName(t *testing.T) {
name := "foo"
server := makeServer(t, &requestStub{
reqMethod: "POST",
reqPath: "/containers/create?name=foo",
reqBody: "{}",
resStatus: 201,
resBody: "{}",
},
)
i := instance(t, server)
_, err := i.Spawn(fmt.Sprintf("{\"name\":\"%s\"}", name))
if err != nil {
t.Fatal(err)
}
server.Check()
}
func TestAttachToChild(t *testing.T) { func TestAttachToChild(t *testing.T) {
name := "foo" name := "foo"
server := makeServer(t, &requestStub{ server := makeServer(t, &requestStub{
@ -210,7 +229,12 @@ func (s *stubServer) Check() {
func (s *stubServer) ServeRequest(w http.ResponseWriter, r *http.Request) { func (s *stubServer) ServeRequest(w http.ResponseWriter, r *http.Request) {
for _, record := range s.stubs { for _, record := range s.stubs {
stub := record.stub stub := record.stub
if r.Method == stub.reqMethod && r.URL.Path == stub.reqPath { path := r.URL.Path
q := r.URL.RawQuery
if q != "" {
path += "?" + q
}
if r.Method == stub.reqMethod && path == stub.reqPath {
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
s.t.Fatal(err) s.t.Fatal(err)

View File

@ -225,6 +225,19 @@ func postContainersCreate(out libswarm.Sender, version version.Version, w http.R
return err return err
} }
name := r.Form.Get("name")
if name != "" {
var reqJson map[string]interface{}
if err = json.Unmarshal(body, &reqJson); err != nil {
return err
}
reqJson["name"] = name
body, err = json.Marshal(reqJson)
if err != nil {
return err
}
}
container, err := libswarm.AsClient(out).Spawn(string(body)) container, err := libswarm.AsClient(out).Spawn(string(body))
if err != nil { if err != nil {
return err return err