github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/plugins/device/util.go (about)

     1  package device
     2  
     3  import "github.com/hashicorp/nomad/plugins/device/proto"
     4  import "github.com/golang/protobuf/ptypes"
     5  
     6  // convertProtoDeviceGroups converts between a list of proto and structs DeviceGroup
     7  func convertProtoDeviceGroups(in []*proto.DeviceGroup) []*DeviceGroup {
     8  	if in == nil {
     9  		return nil
    10  	}
    11  
    12  	out := make([]*DeviceGroup, len(in))
    13  	for i, group := range in {
    14  		out[i] = convertProtoDeviceGroup(group)
    15  	}
    16  
    17  	return out
    18  }
    19  
    20  // convertProtoDeviceGroup converts between a proto and structs DeviceGroup
    21  func convertProtoDeviceGroup(in *proto.DeviceGroup) *DeviceGroup {
    22  	if in == nil {
    23  		return nil
    24  	}
    25  
    26  	return &DeviceGroup{
    27  		Vendor:     in.Vendor,
    28  		Type:       in.DeviceType,
    29  		Name:       in.DeviceName,
    30  		Devices:    convertProtoDevices(in.Devices),
    31  		Attributes: in.Attributes,
    32  	}
    33  }
    34  
    35  // convertProtoDevices converts between a list of proto and structs Device
    36  func convertProtoDevices(in []*proto.DetectedDevice) []*Device {
    37  	if in == nil {
    38  		return nil
    39  	}
    40  
    41  	out := make([]*Device, len(in))
    42  	for i, d := range in {
    43  		out[i] = convertProtoDevice(d)
    44  	}
    45  
    46  	return out
    47  }
    48  
    49  // convertProtoDevice converts between a proto and structs Device
    50  func convertProtoDevice(in *proto.DetectedDevice) *Device {
    51  	if in == nil {
    52  		return nil
    53  	}
    54  
    55  	return &Device{
    56  		ID:         in.ID,
    57  		Healthy:    in.Healthy,
    58  		HealthDesc: in.HealthDescription,
    59  		HwLocality: convertProtoDeviceLocality(in.HwLocality),
    60  	}
    61  }
    62  
    63  // convertProtoDeviceLocality converts between a proto and structs DeviceLocality
    64  func convertProtoDeviceLocality(in *proto.DeviceLocality) *DeviceLocality {
    65  	if in == nil {
    66  		return nil
    67  	}
    68  
    69  	return &DeviceLocality{
    70  		PciBusID: in.PciBusId,
    71  	}
    72  }
    73  
    74  // convertProtoContainerReservation is used to convert between a proto and struct
    75  // ContainerReservation
    76  func convertProtoContainerReservation(in *proto.ContainerReservation) *ContainerReservation {
    77  	if in == nil {
    78  		return nil
    79  	}
    80  
    81  	return &ContainerReservation{
    82  		Envs:    in.Envs,
    83  		Mounts:  convertProtoMounts(in.Mounts),
    84  		Devices: convertProtoDeviceSpecs(in.Devices),
    85  	}
    86  }
    87  
    88  // convertProtoMount converts between a list of proto and structs Mount
    89  func convertProtoMounts(in []*proto.Mount) []*Mount {
    90  	if in == nil {
    91  		return nil
    92  	}
    93  
    94  	out := make([]*Mount, len(in))
    95  	for i, d := range in {
    96  		out[i] = convertProtoMount(d)
    97  	}
    98  
    99  	return out
   100  }
   101  
   102  // convertProtoMount converts between a proto and structs Mount
   103  func convertProtoMount(in *proto.Mount) *Mount {
   104  	if in == nil {
   105  		return nil
   106  	}
   107  
   108  	return &Mount{
   109  		TaskPath: in.TaskPath,
   110  		HostPath: in.HostPath,
   111  		ReadOnly: in.ReadOnly,
   112  	}
   113  }
   114  
   115  // convertProtoDeviceSpecs converts between a list of proto and structs DeviceSpecs
   116  func convertProtoDeviceSpecs(in []*proto.DeviceSpec) []*DeviceSpec {
   117  	if in == nil {
   118  		return nil
   119  	}
   120  
   121  	out := make([]*DeviceSpec, len(in))
   122  	for i, d := range in {
   123  		out[i] = convertProtoDeviceSpec(d)
   124  	}
   125  
   126  	return out
   127  }
   128  
   129  // convertProtoDeviceSpec converts between a proto and structs DeviceSpec
   130  func convertProtoDeviceSpec(in *proto.DeviceSpec) *DeviceSpec {
   131  	if in == nil {
   132  		return nil
   133  	}
   134  
   135  	return &DeviceSpec{
   136  		TaskPath:    in.TaskPath,
   137  		HostPath:    in.HostPath,
   138  		CgroupPerms: in.Permissions,
   139  	}
   140  }
   141  
   142  // convertStructDeviceGroup converts between a list of struct and proto DeviceGroup
   143  func convertStructDeviceGroups(in []*DeviceGroup) []*proto.DeviceGroup {
   144  	if in == nil {
   145  		return nil
   146  	}
   147  
   148  	out := make([]*proto.DeviceGroup, len(in))
   149  	for i, g := range in {
   150  		out[i] = convertStructDeviceGroup(g)
   151  	}
   152  
   153  	return out
   154  }
   155  
   156  // convertStructDeviceGroup converts between a struct and proto DeviceGroup
   157  func convertStructDeviceGroup(in *DeviceGroup) *proto.DeviceGroup {
   158  	if in == nil {
   159  		return nil
   160  	}
   161  
   162  	return &proto.DeviceGroup{
   163  		Vendor:     in.Vendor,
   164  		DeviceType: in.Type,
   165  		DeviceName: in.Name,
   166  		Devices:    convertStructDevices(in.Devices),
   167  		Attributes: in.Attributes,
   168  	}
   169  }
   170  
   171  // convertStructDevices converts between a list of struct and proto Device
   172  func convertStructDevices(in []*Device) []*proto.DetectedDevice {
   173  	if in == nil {
   174  		return nil
   175  	}
   176  
   177  	out := make([]*proto.DetectedDevice, len(in))
   178  	for i, d := range in {
   179  		out[i] = convertStructDevice(d)
   180  	}
   181  
   182  	return out
   183  }
   184  
   185  // convertStructDevice converts between a struct and proto Device
   186  func convertStructDevice(in *Device) *proto.DetectedDevice {
   187  	if in == nil {
   188  		return nil
   189  	}
   190  
   191  	return &proto.DetectedDevice{
   192  		ID:                in.ID,
   193  		Healthy:           in.Healthy,
   194  		HealthDescription: in.HealthDesc,
   195  		HwLocality:        convertStructDeviceLocality(in.HwLocality),
   196  	}
   197  }
   198  
   199  // convertStructDeviceLocality converts between a struct and proto DeviceLocality
   200  func convertStructDeviceLocality(in *DeviceLocality) *proto.DeviceLocality {
   201  	if in == nil {
   202  		return nil
   203  	}
   204  
   205  	return &proto.DeviceLocality{
   206  		PciBusId: in.PciBusID,
   207  	}
   208  }
   209  
   210  // convertStructContainerReservation is used to convert between a struct and
   211  // proto ContainerReservation
   212  func convertStructContainerReservation(in *ContainerReservation) *proto.ContainerReservation {
   213  	if in == nil {
   214  		return nil
   215  	}
   216  
   217  	return &proto.ContainerReservation{
   218  		Envs:    in.Envs,
   219  		Mounts:  convertStructMounts(in.Mounts),
   220  		Devices: convertStructDeviceSpecs(in.Devices),
   221  	}
   222  }
   223  
   224  // convertStructMount converts between a list of structs and proto Mount
   225  func convertStructMounts(in []*Mount) []*proto.Mount {
   226  	if in == nil {
   227  		return nil
   228  	}
   229  
   230  	out := make([]*proto.Mount, len(in))
   231  	for i, m := range in {
   232  		out[i] = convertStructMount(m)
   233  	}
   234  
   235  	return out
   236  }
   237  
   238  // convertStructMount converts between a struct and proto Mount
   239  func convertStructMount(in *Mount) *proto.Mount {
   240  	if in == nil {
   241  		return nil
   242  	}
   243  
   244  	return &proto.Mount{
   245  		TaskPath: in.TaskPath,
   246  		HostPath: in.HostPath,
   247  		ReadOnly: in.ReadOnly,
   248  	}
   249  }
   250  
   251  // convertStructDeviceSpecs converts between a list of struct and proto DeviceSpecs
   252  func convertStructDeviceSpecs(in []*DeviceSpec) []*proto.DeviceSpec {
   253  	if in == nil {
   254  		return nil
   255  	}
   256  
   257  	out := make([]*proto.DeviceSpec, len(in))
   258  	for i, d := range in {
   259  		out[i] = convertStructDeviceSpec(d)
   260  	}
   261  
   262  	return out
   263  }
   264  
   265  // convertStructDeviceSpec converts between a struct and proto DeviceSpec
   266  func convertStructDeviceSpec(in *DeviceSpec) *proto.DeviceSpec {
   267  	if in == nil {
   268  		return nil
   269  	}
   270  
   271  	return &proto.DeviceSpec{
   272  		TaskPath:    in.TaskPath,
   273  		HostPath:    in.HostPath,
   274  		Permissions: in.CgroupPerms,
   275  	}
   276  }
   277  
   278  // convertProtoDeviceGroupsStats converts between a list of struct and proto
   279  // DeviceGroupStats
   280  func convertProtoDeviceGroupsStats(in []*proto.DeviceGroupStats) []*DeviceGroupStats {
   281  	if in == nil {
   282  		return nil
   283  	}
   284  
   285  	out := make([]*DeviceGroupStats, len(in))
   286  	for i, m := range in {
   287  		out[i] = convertProtoDeviceGroupStats(m)
   288  	}
   289  
   290  	return out
   291  }
   292  
   293  // convertProtoDeviceGroupStats converts between a proto and struct
   294  // DeviceGroupStats
   295  func convertProtoDeviceGroupStats(in *proto.DeviceGroupStats) *DeviceGroupStats {
   296  	if in == nil {
   297  		return nil
   298  	}
   299  
   300  	out := &DeviceGroupStats{
   301  		Vendor:        in.Vendor,
   302  		Type:          in.Type,
   303  		Name:          in.Name,
   304  		InstanceStats: make(map[string]*DeviceStats, len(in.InstanceStats)),
   305  	}
   306  
   307  	for k, v := range in.InstanceStats {
   308  		out.InstanceStats[k] = convertProtoDeviceStats(v)
   309  	}
   310  
   311  	return out
   312  }
   313  
   314  // convertProtoDeviceStats converts between a proto and struct DeviceStats
   315  func convertProtoDeviceStats(in *proto.DeviceStats) *DeviceStats {
   316  	if in == nil {
   317  		return nil
   318  	}
   319  
   320  	ts, err := ptypes.Timestamp(in.Timestamp)
   321  	if err != nil {
   322  		return nil
   323  	}
   324  
   325  	return &DeviceStats{
   326  		Summary:   convertProtoStatValue(in.Summary),
   327  		Stats:     convertProtoStatObject(in.Stats),
   328  		Timestamp: ts,
   329  	}
   330  }
   331  
   332  // convertProtoStatObject converts between a proto and struct StatObject
   333  func convertProtoStatObject(in *proto.StatObject) *StatObject {
   334  	if in == nil {
   335  		return nil
   336  	}
   337  
   338  	out := &StatObject{
   339  		Nested:     make(map[string]*StatObject, len(in.Nested)),
   340  		Attributes: make(map[string]*StatValue, len(in.Attributes)),
   341  	}
   342  
   343  	for k, v := range in.Nested {
   344  		out.Nested[k] = convertProtoStatObject(v)
   345  	}
   346  
   347  	for k, v := range in.Attributes {
   348  		out.Attributes[k] = convertProtoStatValue(v)
   349  	}
   350  
   351  	return out
   352  }
   353  
   354  // convertProtoStatValue converts between a proto and struct StatValue
   355  func convertProtoStatValue(in *proto.StatValue) *StatValue {
   356  	if in == nil {
   357  		return nil
   358  	}
   359  
   360  	return &StatValue{
   361  		FloatNumeratorVal:   in.FloatNumeratorVal,
   362  		FloatDenominatorVal: in.FloatDenominatorVal,
   363  		IntNumeratorVal:     in.IntNumeratorVal,
   364  		IntDenominatorVal:   in.IntDenominatorVal,
   365  		StringVal:           in.StringVal,
   366  		BoolVal:             in.BoolVal,
   367  		Unit:                in.Unit,
   368  		Desc:                in.Desc,
   369  	}
   370  }
   371  
   372  // convertStructDeviceGroupsStats converts between a list of struct and proto
   373  // DeviceGroupStats
   374  func convertStructDeviceGroupsStats(in []*DeviceGroupStats) []*proto.DeviceGroupStats {
   375  	if in == nil {
   376  		return nil
   377  	}
   378  
   379  	out := make([]*proto.DeviceGroupStats, len(in))
   380  	for i, m := range in {
   381  		out[i] = convertStructDeviceGroupStats(m)
   382  	}
   383  
   384  	return out
   385  }
   386  
   387  // convertStructDeviceGroupStats converts between a struct and proto
   388  // DeviceGroupStats
   389  func convertStructDeviceGroupStats(in *DeviceGroupStats) *proto.DeviceGroupStats {
   390  	if in == nil {
   391  		return nil
   392  	}
   393  
   394  	out := &proto.DeviceGroupStats{
   395  		Vendor:        in.Vendor,
   396  		Type:          in.Type,
   397  		Name:          in.Name,
   398  		InstanceStats: make(map[string]*proto.DeviceStats, len(in.InstanceStats)),
   399  	}
   400  
   401  	for k, v := range in.InstanceStats {
   402  		out.InstanceStats[k] = convertStructDeviceStats(v)
   403  	}
   404  
   405  	return out
   406  }
   407  
   408  // convertStructDeviceStats converts between a struct and proto DeviceStats
   409  func convertStructDeviceStats(in *DeviceStats) *proto.DeviceStats {
   410  	if in == nil {
   411  		return nil
   412  	}
   413  
   414  	ts, err := ptypes.TimestampProto(in.Timestamp)
   415  	if err != nil {
   416  		return nil
   417  	}
   418  
   419  	return &proto.DeviceStats{
   420  		Summary:   convertStructStatValue(in.Summary),
   421  		Stats:     convertStructStatObject(in.Stats),
   422  		Timestamp: ts,
   423  	}
   424  }
   425  
   426  // convertStructStatObject converts between a struct and proto StatObject
   427  func convertStructStatObject(in *StatObject) *proto.StatObject {
   428  	if in == nil {
   429  		return nil
   430  	}
   431  
   432  	out := &proto.StatObject{
   433  		Nested:     make(map[string]*proto.StatObject, len(in.Nested)),
   434  		Attributes: make(map[string]*proto.StatValue, len(in.Attributes)),
   435  	}
   436  
   437  	for k, v := range in.Nested {
   438  		out.Nested[k] = convertStructStatObject(v)
   439  	}
   440  
   441  	for k, v := range in.Attributes {
   442  		out.Attributes[k] = convertStructStatValue(v)
   443  	}
   444  
   445  	return out
   446  }
   447  
   448  // convertStructStatValue converts between a struct and proto StatValue
   449  func convertStructStatValue(in *StatValue) *proto.StatValue {
   450  	if in == nil {
   451  		return nil
   452  	}
   453  
   454  	return &proto.StatValue{
   455  		FloatNumeratorVal:   in.FloatNumeratorVal,
   456  		FloatDenominatorVal: in.FloatDenominatorVal,
   457  		IntNumeratorVal:     in.IntNumeratorVal,
   458  		IntDenominatorVal:   in.IntDenominatorVal,
   459  		StringVal:           in.StringVal,
   460  		BoolVal:             in.BoolVal,
   461  		Unit:                in.Unit,
   462  		Desc:                in.Desc,
   463  	}
   464  }