1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Refactor endpoint*FromGRPC to limit duplication

endpointSpecFromGRPC and endpointFromGRPC do the exact same thing for
endpoint{,Spec}.Ports, let's extract that to a method.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-02-28 10:51:55 +01:00
parent 3c5932086a
commit a620c0172c
No known key found for this signature in database
GPG key ID: 083CC6FD6EB699A3

View file

@ -89,13 +89,7 @@ func endpointSpecFromGRPC(es *swarmapi.EndpointSpec) *types.EndpointSpec {
endpointSpec.Mode = types.ResolutionMode(strings.ToLower(es.Mode.String())) endpointSpec.Mode = types.ResolutionMode(strings.ToLower(es.Mode.String()))
for _, portState := range es.Ports { for _, portState := range es.Ports {
endpointSpec.Ports = append(endpointSpec.Ports, types.PortConfig{ endpointSpec.Ports = append(endpointSpec.Ports, swarmPortConfigToAPIPortConfig(portState))
Name: portState.Name,
Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portState.Protocol)])),
PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(portState.PublishMode)])),
TargetPort: portState.TargetPort,
PublishedPort: portState.PublishedPort,
})
} }
} }
return endpointSpec return endpointSpec
@ -109,13 +103,7 @@ func endpointFromGRPC(e *swarmapi.Endpoint) types.Endpoint {
} }
for _, portState := range e.Ports { for _, portState := range e.Ports {
endpoint.Ports = append(endpoint.Ports, types.PortConfig{ endpoint.Ports = append(endpoint.Ports, swarmPortConfigToAPIPortConfig(portState))
Name: portState.Name,
Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portState.Protocol)])),
PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(portState.PublishMode)])),
TargetPort: portState.TargetPort,
PublishedPort: portState.PublishedPort,
})
} }
for _, v := range e.VirtualIPs { for _, v := range e.VirtualIPs {
@ -129,6 +117,16 @@ func endpointFromGRPC(e *swarmapi.Endpoint) types.Endpoint {
return endpoint return endpoint
} }
func swarmPortConfigToAPIPortConfig(portConfig *swarmapi.PortConfig) types.PortConfig {
return types.PortConfig{
Name: portConfig.Name,
Protocol: types.PortConfigProtocol(strings.ToLower(swarmapi.PortConfig_Protocol_name[int32(portConfig.Protocol)])),
PublishMode: types.PortConfigPublishMode(strings.ToLower(swarmapi.PortConfig_PublishMode_name[int32(portConfig.PublishMode)])),
TargetPort: portConfig.TargetPort,
PublishedPort: portConfig.PublishedPort,
}
}
// BasicNetworkFromGRPC converts a grpc Network to a NetworkResource. // BasicNetworkFromGRPC converts a grpc Network to a NetworkResource.
func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource { func BasicNetworkFromGRPC(n swarmapi.Network) basictypes.NetworkResource {
spec := n.Spec spec := n.Spec