Browse code

Generate api/types:Port from swagger spec.

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2016/10/04 03:49:49
Showing 6 changed files
... ...
@@ -142,4 +142,5 @@ win: build ## cross build the binary for windows
142 142
 swagger-gen:
143 143
 	docker run --rm -v $(PWD):/work -w /work quay.io/goswagger/swagger \
144 144
 		generate model -m "types" -f api/swagger.yaml -t api/ --skip-validator \
145
-			-n Volume
145
+			-n Volume \
146
+			-n Port
... ...
@@ -20,7 +20,7 @@ import (
20 20
 
21 21
 // Common constants for daemon and client.
22 22
 const (
23
-	// Version of Current REST API
23
+	// DefaultVersion of Current REST API
24 24
 	DefaultVersion string = "1.25"
25 25
 
26 26
 	// MinVersion represents Minimum REST API version supported
... ...
@@ -57,8 +57,8 @@ func (r byPortInfo) Less(i, j int) bool {
57 57
 // it's used by command 'docker ps'
58 58
 func DisplayablePorts(ports []types.Port) string {
59 59
 	type portGroup struct {
60
-		first int
61
-		last  int
60
+		first uint16
61
+		last  uint16
62 62
 	}
63 63
 	groupMap := make(map[string]*portGroup)
64 64
 	var result []string
... ...
@@ -99,7 +99,7 @@ func DisplayablePorts(ports []types.Port) string {
99 99
 	return strings.Join(result, ", ")
100 100
 }
101 101
 
102
-func formGroup(key string, start, last int) string {
102
+func formGroup(key string, start, last uint16) string {
103 103
 	parts := strings.Split(key, "/")
104 104
 	groupType := parts[0]
105 105
 	var ip string
... ...
@@ -107,7 +107,7 @@ func formGroup(key string, start, last int) string {
107 107
 		ip = parts[0]
108 108
 		groupType = parts[1]
109 109
 	}
110
-	group := strconv.Itoa(start)
110
+	group := strconv.Itoa(int(start))
111 111
 	if start != last {
112 112
 		group = fmt.Sprintf("%s-%d", group, last)
113 113
 	}
... ...
@@ -62,20 +62,28 @@ definitions:
62 62
   Port:
63 63
     type: "object"
64 64
     description: "An open port on a container"
65
+    required: [PrivatePort, Type]
65 66
     properties:
66 67
       IP:
67 68
         type: "string"
69
+        format: "ip-address"
68 70
       PrivatePort:
69 71
         type: "integer"
72
+        format: "uint16"
73
+        x-nullable: false
70 74
         description: "Port on the container"
71 75
       PublicPort:
72 76
         type: "integer"
77
+        format: "uint16"
73 78
         description: "Port exposed on the host"
74 79
       Type:
75 80
         type: "string"
76
-        enum:
77
-          - "tcp"
78
-          - "udp"
81
+        x-nullable: false
82
+        enum: ["tcp", "udp"]
83
+    example:
84
+      PrivatePort: 8080
85
+      PublicPort: 80
86
+      Type: "tcp"
79 87
   MountPoint:
80 88
     type: "object"
81 89
     description: "A mount point inside a container"
82 90
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+package types
1
+
2
+// This file was generated by the swagger tool.
3
+// Editing this file might prove futile when you re-run the swagger generate command
4
+
5
+// Port An open port on a container
6
+// swagger:model Port
7
+type Port struct {
8
+
9
+	// IP
10
+	IP string `json:"IP,omitempty"`
11
+
12
+	// Port on the container
13
+	// Required: true
14
+	PrivatePort uint16 `json:"PrivatePort"`
15
+
16
+	// Port exposed on the host
17
+	PublicPort uint16 `json:"PublicPort,omitempty"`
18
+
19
+	// type
20
+	// Required: true
21
+	Type string `json:"Type"`
22
+}
... ...
@@ -138,15 +138,6 @@ type ImageInspect struct {
138 138
 	RootFS          RootFS
139 139
 }
140 140
 
141
-// Port stores open ports info of container
142
-// e.g. {"PrivatePort": 8080, "PublicPort": 80, "Type": "tcp"}
143
-type Port struct {
144
-	IP          string `json:",omitempty"`
145
-	PrivatePort int
146
-	PublicPort  int `json:",omitempty"`
147
-	Type        string
148
-}
149
-
150 141
 // Container contains response of Remote API:
151 142
 // GET "/containers/json"
152 143
 type Container struct {
... ...
@@ -519,7 +519,7 @@ func (daemon *Daemon) transformContainer(container *container.Container, ctx *li
519 519
 		}
520 520
 		if len(bindings) == 0 {
521 521
 			newC.Ports = append(newC.Ports, types.Port{
522
-				PrivatePort: p,
522
+				PrivatePort: uint16(p),
523 523
 				Type:        port.Proto(),
524 524
 			})
525 525
 			continue
... ...
@@ -530,8 +530,8 @@ func (daemon *Daemon) transformContainer(container *container.Container, ctx *li
530 530
 				return nil, err
531 531
 			}
532 532
 			newC.Ports = append(newC.Ports, types.Port{
533
-				PrivatePort: p,
534
-				PublicPort:  h,
533
+				PrivatePort: uint16(p),
534
+				PublicPort:  uint16(h),
535 535
 				Type:        port.Proto(),
536 536
 				IP:          binding.HostIP,
537 537
 			})