pbm_util.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Copyright (c) 2017 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pbm
  14. import (
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "github.com/vmware/govmomi/pbm/types"
  19. )
  20. // A struct to capture pbm create spec details.
  21. type CapabilityProfileCreateSpec struct {
  22. Name string
  23. Description string
  24. Category string
  25. CapabilityList []Capability
  26. }
  27. // A struct to capture pbm capability instance details.
  28. type Capability struct {
  29. ID string
  30. Namespace string
  31. PropertyList []Property
  32. }
  33. // A struct to capture pbm property instance details.
  34. type Property struct {
  35. ID string
  36. Operator string
  37. Value string
  38. DataType string
  39. }
  40. func CreateCapabilityProfileSpec(pbmCreateSpec CapabilityProfileCreateSpec) (*types.PbmCapabilityProfileCreateSpec, error) {
  41. capabilities, err := createCapabilityInstances(pbmCreateSpec.CapabilityList)
  42. if err != nil {
  43. return nil, err
  44. }
  45. pbmCapabilityProfileSpec := types.PbmCapabilityProfileCreateSpec{
  46. Name: pbmCreateSpec.Name,
  47. Description: pbmCreateSpec.Description,
  48. Category: pbmCreateSpec.Category,
  49. ResourceType: types.PbmProfileResourceType{
  50. ResourceType: string(types.PbmProfileResourceTypeEnumSTORAGE),
  51. },
  52. Constraints: &types.PbmCapabilitySubProfileConstraints{
  53. SubProfiles: []types.PbmCapabilitySubProfile{
  54. types.PbmCapabilitySubProfile{
  55. Capability: capabilities,
  56. },
  57. },
  58. },
  59. }
  60. return &pbmCapabilityProfileSpec, nil
  61. }
  62. func createCapabilityInstances(rules []Capability) ([]types.PbmCapabilityInstance, error) {
  63. var capabilityInstances []types.PbmCapabilityInstance
  64. for _, capabilityRule := range rules {
  65. capability := types.PbmCapabilityInstance{
  66. Id: types.PbmCapabilityMetadataUniqueId{
  67. Namespace: capabilityRule.Namespace,
  68. Id: capabilityRule.ID,
  69. },
  70. }
  71. var propertyInstances []types.PbmCapabilityPropertyInstance
  72. for _, propertyRule := range capabilityRule.PropertyList {
  73. property := types.PbmCapabilityPropertyInstance{
  74. Id: propertyRule.ID,
  75. }
  76. if propertyRule.Operator != "" {
  77. property.Operator = propertyRule.Operator
  78. }
  79. var err error
  80. switch strings.ToLower(propertyRule.DataType) {
  81. case "int":
  82. // Go int32 is marshalled to xsi:int whereas Go int is marshalled to xsi:long when sending down the wire.
  83. var val int32
  84. val, err = verifyPropertyValueIsInt(propertyRule.Value, propertyRule.DataType)
  85. property.Value = val
  86. case "bool":
  87. var val bool
  88. val, err = verifyPropertyValueIsBoolean(propertyRule.Value, propertyRule.DataType)
  89. property.Value = val
  90. case "string":
  91. property.Value = propertyRule.Value
  92. case "set":
  93. set := types.PbmCapabilityDiscreteSet{}
  94. for _, val := range strings.Split(propertyRule.Value, ",") {
  95. set.Values = append(set.Values, val)
  96. }
  97. property.Value = set
  98. default:
  99. return nil, fmt.Errorf("invalid value: %q with datatype: %q", propertyRule.Value, propertyRule.Value)
  100. }
  101. if err != nil {
  102. return nil, fmt.Errorf("invalid value: %q with datatype: %q", propertyRule.Value, propertyRule.Value)
  103. }
  104. propertyInstances = append(propertyInstances, property)
  105. }
  106. constraintInstances := []types.PbmCapabilityConstraintInstance{
  107. types.PbmCapabilityConstraintInstance{
  108. PropertyInstance: propertyInstances,
  109. },
  110. }
  111. capability.Constraint = constraintInstances
  112. capabilityInstances = append(capabilityInstances, capability)
  113. }
  114. return capabilityInstances, nil
  115. }
  116. // Verify if the capability value is of type integer.
  117. func verifyPropertyValueIsInt(propertyValue string, dataType string) (int32, error) {
  118. val, err := strconv.ParseInt(propertyValue, 10, 32)
  119. if err != nil {
  120. return -1, err
  121. }
  122. return int32(val), nil
  123. }
  124. // Verify if the capability value is of type integer.
  125. func verifyPropertyValueIsBoolean(propertyValue string, dataType string) (bool, error) {
  126. val, err := strconv.ParseBool(propertyValue)
  127. if err != nil {
  128. return false, err
  129. }
  130. return val, nil
  131. }