Browse code

Change API route for containers/ and images/ in order to avoid conflict

Guillaume J. Charmes authored on 2013/05/10 09:50:56
Showing 4 changed files
... ...
@@ -154,7 +154,6 @@ func getImagesHistory(srv *Server, w http.ResponseWriter, r *http.Request, vars
154 154
 		return nil, fmt.Errorf("Missing parameter")
155 155
 	}
156 156
 	name := vars["name"]
157
-	log.Printf("----------> %s\n", name)
158 157
 	outs, err := srv.ImageHistory(name)
159 158
 	if err != nil {
160 159
 		return nil, err
... ...
@@ -182,7 +181,7 @@ func getContainersChanges(srv *Server, w http.ResponseWriter, r *http.Request, v
182 182
 	return b, nil
183 183
 }
184 184
 
185
-func getContainers(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
185
+func getContainersPs(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
186 186
 	if err := parseForm(r); err != nil {
187 187
 		return nil, err
188 188
 	}
... ...
@@ -248,7 +247,8 @@ func postCommit(srv *Server, w http.ResponseWriter, r *http.Request, vars map[st
248 248
 	return b, nil
249 249
 }
250 250
 
251
-func postImages(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
251
+// Creates an image from Pull or from Import
252
+func postImagesCreate(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
252 253
 	if err := parseForm(r); err != nil {
253 254
 		return nil, err
254 255
 	}
... ...
@@ -355,21 +355,25 @@ func postBuild(srv *Server, w http.ResponseWriter, r *http.Request, vars map[str
355 355
 	return nil, nil
356 356
 }
357 357
 
358
-func postContainers(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
359
-	var config Config
360
-	if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
358
+func postContainersCreate(srv *Server, w http.ResponseWriter, r *http.Request, vars map[string]string) ([]byte, error) {
359
+	config := &Config{}
360
+	if err := json.NewDecoder(r.Body).Decode(config); err != nil {
361 361
 		return nil, err
362 362
 	}
363
-	id, memoryW, swapW, err := srv.ContainerCreate(config)
363
+	id, err := srv.ContainerCreate(config)
364 364
 	if err != nil {
365 365
 		return nil, err
366 366
 	}
367
-	var out ApiRun
368
-	out.Id = id
369
-	if memoryW {
367
+
368
+	out := &ApiRun{
369
+		Id: id,
370
+	}
371
+	if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
372
+		log.Println("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.")
370 373
 		out.Warnings = append(out.Warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
371 374
 	}
372
-	if swapW {
375
+	if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
376
+		log.Println("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.")
373 377
 		out.Warnings = append(out.Warnings, "Your kernel does not support memory swap capabilities. Limitation discarded.")
374 378
 	}
375 379
 	b, err := json.Marshal(out)
... ...
@@ -545,28 +549,27 @@ func ListenAndServe(addr string, srv *Server, logging bool) error {
545 545
 		"GET": {
546 546
 			"/auth":                         getAuth,
547 547
 			"/version":                      getVersion,
548
-			"/containers/{name:.*}/export":  getContainersExport,
549
-			"/images":                       getImages,
548
+			"/info":                         getInfo,
550 549
 			"/images/json":                  getImages,
551 550
 			"/images/viz":                   getImagesViz,
552
-			"/info":                         getInfo,
553 551
 			"/images/search":                getImagesSearch,
554 552
 			"/images/{name:.*}/history":     getImagesHistory,
555
-			"/containers/{name:.*}/changes": getContainersChanges,
556
-			"/containers":                   getContainers,
557 553
 			"/images/{name:.*}/json":        getImagesByName,
554
+			"/containers/ps":                getContainersPs,
555
+			"/containers/{name:.*}/export":  getContainersExport,
556
+			"/containers/{name:.*}/changes": getContainersChanges,
558 557
 			"/containers/{name:.*}/json":    getContainersByName,
559 558
 		},
560 559
 		"POST": {
561
-			"/auth": postAuth,
562
-			"/containers/{name:.*}/kill":    postContainersKill,
563
-			"/images/{name:.*}/tag":         postImagesTag,
560
+			"/auth":                         postAuth,
564 561
 			"/commit":                       postCommit,
565
-			"/images":                       postImages,
562
+			"/build":                        postBuild,
563
+			"/images/create":                postImagesCreate,
566 564
 			"/images/{name:*.}/insert":      postImagesInsert,
567 565
 			"/images/{name:*.}/push":        postImagesPush,
568
-			"/build":                        postBuild,
569
-			"/containers":                   postContainers,
566
+			"/images/{name:.*}/tag":         postImagesTag,
567
+			"/containers/create":            postContainersCreate,
568
+			"/containers/{name:.*}/kill":    postContainersKill,
570 569
 			"/containers/{name:.*}/restart": postContainersRestart,
571 570
 			"/containers/{name:.*}/start":   postContainersStart,
572 571
 			"/containers/{name:.*}/stop":    postContainersStop,
... ...
@@ -9,20 +9,6 @@ import (
9 9
 	"testing"
10 10
 )
11 11
 
12
-// func init() {
13
-// 	// Make it our Store root
14
-// 	runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
15
-// 	if err != nil {
16
-// 		panic(err)
17
-// 	}
18
-
19
-// 	// Create the "Server"
20
-// 	srv := &Server{
21
-// 		runtime: runtime,
22
-// 	}
23
-// 	go ListenAndServe("0.0.0.0:4243", srv, false)
24
-// }
25
-
26 12
 func TestAuth(t *testing.T) {
27 13
 	runtime, err := newTestRuntime()
28 14
 	if err != nil {
... ...
@@ -108,7 +94,11 @@ func TestVersion(t *testing.T) {
108 108
 	}
109 109
 }
110 110
 
111
-func TestImages(t *testing.T) {
111
+func TestContainersExport(t *testing.T) {
112
+	//FIXME: Implement this test
113
+}
114
+
115
+func TestGetImages(t *testing.T) {
112 116
 	runtime, err := newTestRuntime()
113 117
 	if err != nil {
114 118
 		t.Fatal(err)
... ...
@@ -274,7 +264,7 @@ func testCreateContainer(t *testing.T, srv *Server) {
274 274
 		t.Fatal(err)
275 275
 	}
276 276
 
277
-	body, err := postContainers(srv, r, req, nil)
277
+	body, err := postContainersCreate(srv, r, req, nil)
278 278
 	if err != nil {
279 279
 		t.Fatal(err)
280 280
 	}
... ...
@@ -297,7 +287,7 @@ func testListContainers(t *testing.T, srv *Server, expected int) []ApiContainers
297 297
 		t.Fatal(err)
298 298
 	}
299 299
 
300
-	body, err := getContainers(srv, r, req, nil)
300
+	body, err := getContainersPs(srv, r, req, nil)
301 301
 	if err != nil {
302 302
 		t.Fatal(err)
303 303
 	}
... ...
@@ -236,9 +236,10 @@ func (srv *Server) ContainerChanges(name string) ([]Change, error) {
236 236
 }
237 237
 
238 238
 func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, before string) []ApiContainers {
239
-	var outs []ApiContainers = []ApiContainers{} //produce [] when empty instead of 'null'
240 239
 	var foundBefore bool
241 240
 	var displayed int
241
+	retContainers := []ApiContainers{}
242
+
242 243
 	for _, container := range srv.runtime.List() {
243 244
 		if !container.State.Running && !all && n == -1 && since == "" && before == "" {
244 245
 			continue
... ...
@@ -258,23 +259,26 @@ func (srv *Server) Containers(all, trunc_cmd, only_ids bool, n int, since, befor
258 258
 		if container.ShortId() == since {
259 259
 			break
260 260
 		}
261
-		displayed += 1
262
-		var out ApiContainers
263
-		out.Id = container.ShortId()
261
+		displayed++
262
+
263
+		c := ApiContainers{
264
+			Id: container.ShortId(),
265
+		}
266
+
264 267
 		if !only_ids {
265 268
 			command := fmt.Sprintf("%s %s", container.Path, strings.Join(container.Args, " "))
266 269
 			if trunc_cmd {
267 270
 				command = Trunc(command, 20)
268 271
 			}
269
-			out.Image = srv.runtime.repositories.ImageName(container.Image)
270
-			out.Command = command
271
-			out.Created = container.Created.Unix()
272
-			out.Status = container.State.String()
273
-			out.Ports = container.NetworkSettings.PortMappingHuman()
272
+			c.Image = srv.runtime.repositories.ImageName(container.Image)
273
+			c.Command = command
274
+			c.Created = container.Created.Unix()
275
+			c.Status = container.State.String()
276
+			c.Ports = container.NetworkSettings.PortMappingHuman()
274 277
 		}
275
-		outs = append(outs, out)
278
+		retContainers = append(retContainers, c)
276 279
 	}
277
-	return outs
280
+	return retContainers
278 281
 }
279 282
 
280 283
 func (srv *Server) ContainerCommit(name, repo, tag, author, comment string, config *Config) (string, error) {
... ...
@@ -369,29 +373,24 @@ func (srv *Server) ImageImport(src, repo, tag string, in io.Reader, out io.Write
369 369
 	return nil
370 370
 }
371 371
 
372
-func (srv *Server) ContainerCreate(config Config) (string, bool, bool, error) {
373
-	var memoryW, swapW bool
372
+func (srv *Server) ContainerCreate(config *Config) (string, error) {
374 373
 
375 374
 	if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
376
-		memoryW = true
377
-		log.Println("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.")
378 375
 		config.Memory = 0
379 376
 	}
380 377
 
381 378
 	if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
382
-		swapW = true
383
-		log.Println("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.")
384 379
 		config.MemorySwap = -1
385 380
 	}
386 381
 	b := NewBuilder(srv.runtime)
387
-	container, err := b.Create(&config)
382
+	container, err := b.Create(config)
388 383
 	if err != nil {
389 384
 		if srv.runtime.graph.IsNotExist(err) {
390
-			return "", false, false, fmt.Errorf("No such image: %s", config.Image)
385
+			return "", fmt.Errorf("No such image: %s", config.Image)
391 386
 		}
392
-		return "", false, false, err
387
+		return "", err
393 388
 	}
394
-	return container.ShortId(), memoryW, swapW, nil
389
+	return container.ShortId(), nil
395 390
 }
396 391
 
397 392
 func (srv *Server) ImageCreateFromFile(dockerfile io.Reader, out io.Writer) error {
... ...
@@ -18,7 +18,7 @@ func TestCreateRm(t *testing.T) {
18 18
 		t.Fatal(err)
19 19
 	}
20 20
 
21
-	id, _, _, err := srv.ContainerCreate(*config)
21
+	id, err := srv.ContainerCreate(config)
22 22
 	if err != nil {
23 23
 		t.Fatal(err)
24 24
 	}
... ...
@@ -51,7 +51,7 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
51 51
 		t.Fatal(err)
52 52
 	}
53 53
 
54
-	id, _, _, err := srv.ContainerCreate(*config)
54
+	id, err := srv.ContainerCreate(config)
55 55
 	if err != nil {
56 56
 		t.Fatal(err)
57 57
 	}