Kaynağa Gözat

Generate api/types:Port from swagger spec.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
Daniel Nephin 8 yıl önce
ebeveyn
işleme
f06d8d6db9
6 değiştirilmiş dosya ile 44 ekleme ve 21 silme
  1. 2 1
      Makefile
  2. 5 5
      api/common.go
  3. 11 3
      api/swagger.yaml
  4. 23 0
      api/types/port.go
  5. 0 9
      api/types/types.go
  6. 3 3
      daemon/list.go

+ 2 - 1
Makefile

@@ -142,4 +142,5 @@ win: build ## cross build the binary for windows
 swagger-gen:
 	docker run --rm -v $(PWD):/work -w /work quay.io/goswagger/swagger \
 		generate model -m "types" -f api/swagger.yaml -t api/ --skip-validator \
-			-n Volume
+			-n Volume \
+			-n Port

+ 5 - 5
api/common.go

@@ -20,7 +20,7 @@ import (
 
 // Common constants for daemon and client.
 const (
-	// Version of Current REST API
+	// DefaultVersion of Current REST API
 	DefaultVersion string = "1.25"
 
 	// MinVersion represents Minimum REST API version supported
@@ -57,8 +57,8 @@ func (r byPortInfo) Less(i, j int) bool {
 // it's used by command 'docker ps'
 func DisplayablePorts(ports []types.Port) string {
 	type portGroup struct {
-		first int
-		last  int
+		first uint16
+		last  uint16
 	}
 	groupMap := make(map[string]*portGroup)
 	var result []string
@@ -99,7 +99,7 @@ func DisplayablePorts(ports []types.Port) string {
 	return strings.Join(result, ", ")
 }
 
-func formGroup(key string, start, last int) string {
+func formGroup(key string, start, last uint16) string {
 	parts := strings.Split(key, "/")
 	groupType := parts[0]
 	var ip string
@@ -107,7 +107,7 @@ func formGroup(key string, start, last int) string {
 		ip = parts[0]
 		groupType = parts[1]
 	}
-	group := strconv.Itoa(start)
+	group := strconv.Itoa(int(start))
 	if start != last {
 		group = fmt.Sprintf("%s-%d", group, last)
 	}

+ 11 - 3
api/swagger.yaml

@@ -62,20 +62,28 @@ definitions:
   Port:
     type: "object"
     description: "An open port on a container"
+    required: [PrivatePort, Type]
     properties:
       IP:
         type: "string"
+        format: "ip-address"
       PrivatePort:
         type: "integer"
+        format: "uint16"
+        x-nullable: false
         description: "Port on the container"
       PublicPort:
         type: "integer"
+        format: "uint16"
         description: "Port exposed on the host"
       Type:
         type: "string"
-        enum:
-          - "tcp"
-          - "udp"
+        x-nullable: false
+        enum: ["tcp", "udp"]
+    example:
+      PrivatePort: 8080
+      PublicPort: 80
+      Type: "tcp"
   MountPoint:
     type: "object"
     description: "A mount point inside a container"

+ 23 - 0
api/types/port.go

@@ -0,0 +1,23 @@
+package types
+
+// This file was generated by the swagger tool.
+// Editing this file might prove futile when you re-run the swagger generate command
+
+// Port An open port on a container
+// swagger:model Port
+type Port struct {
+
+	// IP
+	IP string `json:"IP,omitempty"`
+
+	// Port on the container
+	// Required: true
+	PrivatePort uint16 `json:"PrivatePort"`
+
+	// Port exposed on the host
+	PublicPort uint16 `json:"PublicPort,omitempty"`
+
+	// type
+	// Required: true
+	Type string `json:"Type"`
+}

+ 0 - 9
api/types/types.go

@@ -138,15 +138,6 @@ type ImageInspect struct {
 	RootFS          RootFS
 }
 
-// Port stores open ports info of container
-// e.g. {"PrivatePort": 8080, "PublicPort": 80, "Type": "tcp"}
-type Port struct {
-	IP          string `json:",omitempty"`
-	PrivatePort int
-	PublicPort  int `json:",omitempty"`
-	Type        string
-}
-
 // Container contains response of Remote API:
 // GET "/containers/json"
 type Container struct {

+ 3 - 3
daemon/list.go

@@ -519,7 +519,7 @@ func (daemon *Daemon) transformContainer(container *container.Container, ctx *li
 		}
 		if len(bindings) == 0 {
 			newC.Ports = append(newC.Ports, types.Port{
-				PrivatePort: p,
+				PrivatePort: uint16(p),
 				Type:        port.Proto(),
 			})
 			continue
@@ -530,8 +530,8 @@ func (daemon *Daemon) transformContainer(container *container.Container, ctx *li
 				return nil, err
 			}
 			newC.Ports = append(newC.Ports, types.Port{
-				PrivatePort: p,
-				PublicPort:  h,
+				PrivatePort: uint16(p),
+				PublicPort:  uint16(h),
 				Type:        port.Proto(),
 				IP:          binding.HostIP,
 			})