Browse code

refactor create to not expose internal data structures

- use existing exposed type

Signed-off-by: Morgan Bauer <mbauer@us.ibm.com>

Morgan Bauer authored on 2015/09/23 10:06:09
Showing 3 changed files
... ...
@@ -366,10 +366,8 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter
366 366
 	if err := checkForJSON(r); err != nil {
367 367
 		return err
368 368
 	}
369
-	var (
370
-		warnings []string
371
-		name     = r.Form.Get("name")
372
-	)
369
+
370
+	name := r.Form.Get("name")
373 371
 
374 372
 	config, hostConfig, err := runconfig.DecodeContainerConfig(r.Body)
375 373
 	if err != nil {
... ...
@@ -378,15 +376,12 @@ func (s *Server) postContainersCreate(ctx context.Context, w http.ResponseWriter
378 378
 	version := ctx.Version()
379 379
 	adjustCPUShares := version.LessThan("1.19")
380 380
 
381
-	container, warnings, err := s.daemon.ContainerCreate(ctx, name, config, hostConfig, adjustCPUShares)
381
+	ccr, err := s.daemon.ContainerCreate(ctx, name, config, hostConfig, adjustCPUShares)
382 382
 	if err != nil {
383 383
 		return err
384 384
 	}
385 385
 
386
-	return writeJSON(w, http.StatusCreated, &types.ContainerCreateResponse{
387
-		ID:       container.ID,
388
-		Warnings: warnings,
389
-	})
386
+	return writeJSON(w, http.StatusCreated, ccr)
390 387
 }
391 388
 
392 389
 func (s *Server) deleteContainers(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -233,10 +233,15 @@ func (b *builder) runContextCommand(ctx context.Context, args []string, allowRem
233 233
 		return nil
234 234
 	}
235 235
 
236
-	container, _, err := b.Daemon.ContainerCreate(ctx, "", b.Config, nil, true)
236
+	ccr, err := b.Daemon.ContainerCreate(ctx, "", b.Config, nil, true)
237 237
 	if err != nil {
238 238
 		return err
239 239
 	}
240
+	container, err := b.Daemon.Get(ctx, ccr.ID)
241
+	if err != nil {
242
+		return err
243
+	}
244
+
240 245
 	b.TmpContainers[container.ID] = struct{}{}
241 246
 
242 247
 	if err := container.Mount(ctx); err != nil {
... ...
@@ -621,13 +626,17 @@ func (b *builder) create(ctx context.Context) (*daemon.Container, error) {
621 621
 	config := *b.Config
622 622
 
623 623
 	// Create the container
624
-	c, warnings, err := b.Daemon.ContainerCreate(ctx, "", b.Config, hostConfig, true)
624
+	ccr, err := b.Daemon.ContainerCreate(ctx, "", b.Config, hostConfig, true)
625 625
 	if err != nil {
626 626
 		return nil, err
627 627
 	}
628
-	for _, warning := range warnings {
628
+	for _, warning := range ccr.Warnings {
629 629
 		fmt.Fprintf(b.OutStream, " ---> [Warning] %s\n", warning)
630 630
 	}
631
+	c, err := b.Daemon.Get(ctx, ccr.ID)
632
+	if err != nil {
633
+		return nil, err
634
+	}
631 635
 
632 636
 	b.TmpContainers[c.ID] = struct{}{}
633 637
 	fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))
... ...
@@ -16,14 +16,14 @@ import (
16 16
 )
17 17
 
18 18
 // ContainerCreate takes configs and creates a container.
19
-func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (*Container, []string, error) {
19
+func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (types.ContainerCreateResponse, error) {
20 20
 	if config == nil {
21
-		return nil, nil, derr.ErrorCodeEmptyConfig
21
+		return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
22 22
 	}
23 23
 
24 24
 	warnings, err := daemon.verifyContainerSettings(ctx, hostConfig, config)
25 25
 	if err != nil {
26
-		return nil, warnings, err
26
+		return types.ContainerCreateResponse{"", warnings}, err
27 27
 	}
28 28
 
29 29
 	daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
... ...
@@ -32,20 +32,20 @@ func (daemon *Daemon) ContainerCreate(ctx context.Context, name string, config *
32 32
 	if err != nil {
33 33
 		if daemon.Graph(ctx).IsNotExist(err, config.Image) {
34 34
 			if strings.Contains(config.Image, "@") {
35
-				return nil, warnings, derr.ErrorCodeNoSuchImageHash.WithArgs(config.Image)
35
+				return types.ContainerCreateResponse{"", warnings}, derr.ErrorCodeNoSuchImageHash.WithArgs(config.Image)
36 36
 			}
37 37
 			img, tag := parsers.ParseRepositoryTag(config.Image)
38 38
 			if tag == "" {
39 39
 				tag = tags.DefaultTag
40 40
 			}
41
-			return nil, warnings, derr.ErrorCodeNoSuchImageTag.WithArgs(img, tag)
41
+			return types.ContainerCreateResponse{"", warnings}, derr.ErrorCodeNoSuchImageTag.WithArgs(img, tag)
42 42
 		}
43
-		return nil, warnings, err
43
+		return types.ContainerCreateResponse{"", warnings}, err
44 44
 	}
45 45
 
46 46
 	warnings = append(warnings, buildWarnings...)
47 47
 
48
-	return container, warnings, nil
48
+	return types.ContainerCreateResponse{container.ID, warnings}, nil
49 49
 }
50 50
 
51 51
 // Create creates a new container from the given configuration with a given name.