github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/daemon/cluster/convert/node.go (about) 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 "github.com/docker/swarmkit/protobuf/ptypes" 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.Role.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 }, 24 } 25 26 // Meta 27 node.Version.Index = n.Meta.Version.Index 28 node.CreatedAt, _ = ptypes.Timestamp(n.Meta.CreatedAt) 29 node.UpdatedAt, _ = ptypes.Timestamp(n.Meta.UpdatedAt) 30 31 //Annotations 32 node.Spec.Name = n.Spec.Annotations.Name 33 node.Spec.Labels = n.Spec.Annotations.Labels 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 } 54 55 //Manager 56 if n.ManagerStatus != nil { 57 node.ManagerStatus = &types.ManagerStatus{ 58 Leader: n.ManagerStatus.Leader, 59 Reachability: types.Reachability(strings.ToLower(n.ManagerStatus.Reachability.String())), 60 Addr: n.ManagerStatus.Addr, 61 } 62 } 63 64 return node 65 } 66 67 // NodeSpecToGRPC converts a NodeSpec to a grpc NodeSpec. 68 func NodeSpecToGRPC(s types.NodeSpec) (swarmapi.NodeSpec, error) { 69 spec := swarmapi.NodeSpec{ 70 Annotations: swarmapi.Annotations{ 71 Name: s.Name, 72 Labels: s.Labels, 73 }, 74 } 75 if role, ok := swarmapi.NodeRole_value[strings.ToUpper(string(s.Role))]; ok { 76 spec.Role = swarmapi.NodeRole(role) 77 } else { 78 return swarmapi.NodeSpec{}, fmt.Errorf("invalid Role: %q", s.Role) 79 } 80 81 if availability, ok := swarmapi.NodeSpec_Availability_value[strings.ToUpper(string(s.Availability))]; ok { 82 spec.Availability = swarmapi.NodeSpec_Availability(availability) 83 } else { 84 return swarmapi.NodeSpec{}, fmt.Errorf("invalid Availability: %q", s.Availability) 85 } 86 87 return spec, nil 88 }