Tizen_4.0 base
[platform/upstream/docker-engine.git] / daemon / cluster / convert / node.go
1 package convert
2
3 import (
4         "fmt"
5         "strings"
6
7         types "github.com/docker/docker/api/types/swarm"
8         swarmapi "github.com/docker/swarmkit/api"
9         gogotypes "github.com/gogo/protobuf/types"
10 )
11
12 // NodeFromGRPC converts a grpc Node to a Node.
13 func NodeFromGRPC(n swarmapi.Node) types.Node {
14         node := types.Node{
15                 ID: n.ID,
16                 Spec: types.NodeSpec{
17                         Role:         types.NodeRole(strings.ToLower(n.Spec.DesiredRole.String())),
18                         Availability: types.NodeAvailability(strings.ToLower(n.Spec.Availability.String())),
19                 },
20                 Status: types.NodeStatus{
21                         State:   types.NodeState(strings.ToLower(n.Status.State.String())),
22                         Message: n.Status.Message,
23                         Addr:    n.Status.Addr,
24                 },
25         }
26
27         // Meta
28         node.Version.Index = n.Meta.Version.Index
29         node.CreatedAt, _ = gogotypes.TimestampFromProto(n.Meta.CreatedAt)
30         node.UpdatedAt, _ = gogotypes.TimestampFromProto(n.Meta.UpdatedAt)
31
32         //Annotations
33         node.Spec.Annotations = annotationsFromGRPC(n.Spec.Annotations)
34
35         //Description
36         if n.Description != nil {
37                 node.Description.Hostname = n.Description.Hostname
38                 if n.Description.Platform != nil {
39                         node.Description.Platform.Architecture = n.Description.Platform.Architecture
40                         node.Description.Platform.OS = n.Description.Platform.OS
41                 }
42                 if n.Description.Resources != nil {
43                         node.Description.Resources.NanoCPUs = n.Description.Resources.NanoCPUs
44                         node.Description.Resources.MemoryBytes = n.Description.Resources.MemoryBytes
45                 }
46                 if n.Description.Engine != nil {
47                         node.Description.Engine.EngineVersion = n.Description.Engine.EngineVersion
48                         node.Description.Engine.Labels = n.Description.Engine.Labels
49                         for _, plugin := range n.Description.Engine.Plugins {
50                                 node.Description.Engine.Plugins = append(node.Description.Engine.Plugins, types.PluginDescription{Type: plugin.Type, Name: plugin.Name})
51                         }
52                 }
53                 if n.Description.TLSInfo != nil {
54                         node.Description.TLSInfo.TrustRoot = string(n.Description.TLSInfo.TrustRoot)
55                         node.Description.TLSInfo.CertIssuerPublicKey = n.Description.TLSInfo.CertIssuerPublicKey
56                         node.Description.TLSInfo.CertIssuerSubject = n.Description.TLSInfo.CertIssuerSubject
57                 }
58         }
59
60         //Manager
61         if n.ManagerStatus != nil {
62                 node.ManagerStatus = &types.ManagerStatus{
63                         Leader:       n.ManagerStatus.Leader,
64                         Reachability: types.Reachability(strings.ToLower(n.ManagerStatus.Reachability.String())),
65                         Addr:         n.ManagerStatus.Addr,
66                 }
67         }
68
69         return node
70 }
71
72 // NodeSpecToGRPC converts a NodeSpec to a grpc NodeSpec.
73 func NodeSpecToGRPC(s types.NodeSpec) (swarmapi.NodeSpec, error) {
74         spec := swarmapi.NodeSpec{
75                 Annotations: swarmapi.Annotations{
76                         Name:   s.Name,
77                         Labels: s.Labels,
78                 },
79         }
80         if role, ok := swarmapi.NodeRole_value[strings.ToUpper(string(s.Role))]; ok {
81                 spec.DesiredRole = swarmapi.NodeRole(role)
82         } else {
83                 return swarmapi.NodeSpec{}, fmt.Errorf("invalid Role: %q", s.Role)
84         }
85
86         if availability, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(s.Availability))]; ok {
87                 spec.Availability = swarmapi.NodeSpec_Availability(availability)
88         } else {
89                 return swarmapi.NodeSpec{}, fmt.Errorf("invalid Availability: %q", s.Availability)
90         }
91
92         return spec, nil
93 }