Generate api/types:Port from swagger spec.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-10-03 14:49:49 -04:00
parent 0243936d92
commit f06d8d6db9
6 changed files with 44 additions and 21 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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
api/types/port.go Normal file
View file

@ -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"`
}

View file

@ -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 {

View file

@ -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,
})