assetprops.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package kubecost
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // AssetProperty is a kind of property belonging to an Asset
  7. type AssetProperty string
  8. const (
  9. // AssetNilProp is the zero-value of AssetProperty
  10. AssetNilProp AssetProperty = ""
  11. // AssetAccountProp describes the account of the Asset
  12. AssetAccountProp AssetProperty = "account"
  13. // AssetCategoryProp describes the category of the Asset
  14. AssetCategoryProp AssetProperty = "category"
  15. // AssetClusterProp describes the cluster of the Asset
  16. AssetClusterProp AssetProperty = "cluster"
  17. // AssetNameProp describes the name of the Asset
  18. AssetNameProp AssetProperty = "name"
  19. // AssetNodeProp describes the node of the Asset
  20. AssetNodeProp AssetProperty = "node"
  21. // AssetProjectProp describes the project of the Asset
  22. AssetProjectProp AssetProperty = "project"
  23. // AssetProviderProp describes the provider of the Asset
  24. AssetProviderProp AssetProperty = "provider"
  25. // AssetProviderIDProp describes the providerID of the Asset
  26. AssetProviderIDProp AssetProperty = "providerID"
  27. // AssetServiceProp describes the service of the Asset
  28. AssetServiceProp AssetProperty = "service"
  29. // AssetTypeProp describes the type of the Asset
  30. AssetTypeProp AssetProperty = "type"
  31. )
  32. // ParseAssetProperty attempts to parse a string into an AssetProperty
  33. func ParseAssetProperty(text string) (AssetProperty, error) {
  34. switch strings.TrimSpace(strings.ToLower(text)) {
  35. case "account":
  36. return AssetAccountProp, nil
  37. case "category":
  38. return AssetCategoryProp, nil
  39. case "cluster":
  40. return AssetClusterProp, nil
  41. case "name":
  42. return AssetNameProp, nil
  43. case "project":
  44. return AssetProjectProp, nil
  45. case "provider":
  46. return AssetProviderProp, nil
  47. case "providerid":
  48. return AssetProviderIDProp, nil
  49. case "service":
  50. return AssetServiceProp, nil
  51. case "type":
  52. return AssetTypeProp, nil
  53. }
  54. return AssetNilProp, fmt.Errorf("invalid asset property: %s", text)
  55. }
  56. // Category options
  57. // ComputeCategory signifies the Compute Category
  58. const ComputeCategory = "Compute"
  59. // StorageCategory signifies the Storage Category
  60. const StorageCategory = "Storage"
  61. // NetworkCategory signifies the Network Category
  62. const NetworkCategory = "Network"
  63. // ManagementCategory signifies the Management Category
  64. const ManagementCategory = "Management"
  65. // SharedCategory signifies an unassigned Category
  66. const SharedCategory = "Shared"
  67. // OtherCategory signifies an unassigned Category
  68. const OtherCategory = "Other"
  69. // Provider options
  70. // AWSProvider describes the provider AWS
  71. const AWSProvider = "AWS"
  72. // GCPProvider describes the provider GCP
  73. const GCPProvider = "GCP"
  74. // AzureProvider describes the provider Azure
  75. const AzureProvider = "Azure"
  76. // NilProvider describes unknown provider
  77. const NilProvider = "-"
  78. // Service options
  79. const KubernetesService = "Kubernetes"
  80. // ParseProvider attempts to parse and return a known provider, given a string
  81. func ParseProvider(str string) string {
  82. switch strings.ToLower(strings.TrimSpace(str)) {
  83. case "aws", "eks", "amazon":
  84. return AWSProvider
  85. case "gcp", "gke", "google":
  86. return GCPProvider
  87. case "azure":
  88. return AzureProvider
  89. default:
  90. return NilProvider
  91. }
  92. }
  93. // AssetProperties describes all properties assigned to an Asset.
  94. type AssetProperties struct {
  95. Category string `json:"category,omitempty"`
  96. Provider string `json:"provider,omitempty"`
  97. Account string `json:"account,omitempty"`
  98. Project string `json:"project,omitempty"`
  99. Service string `json:"service,omitempty"`
  100. Cluster string `json:"cluster,omitempty"`
  101. Name string `json:"name,omitempty"`
  102. ProviderID string `json:"providerID,omitempty"`
  103. }
  104. // Clone returns a cloned instance of the given AssetProperties
  105. func (ap *AssetProperties) Clone() *AssetProperties {
  106. if ap == nil {
  107. return nil
  108. }
  109. clone := &AssetProperties{}
  110. clone.Category = ap.Category
  111. clone.Provider = ap.Provider
  112. clone.Account = ap.Account
  113. clone.Project = ap.Project
  114. clone.Service = ap.Service
  115. clone.Cluster = ap.Cluster
  116. clone.Name = ap.Name
  117. clone.ProviderID = ap.ProviderID
  118. return clone
  119. }
  120. // Equal returns true only if both AssetProperties are non-nil exact matches
  121. func (ap *AssetProperties) Equal(that *AssetProperties) bool {
  122. if ap == nil || that == nil {
  123. return false
  124. }
  125. if ap.Category != that.Category {
  126. return false
  127. }
  128. if ap.Provider != that.Provider {
  129. return false
  130. }
  131. if ap.Account != that.Account {
  132. return false
  133. }
  134. if ap.Project != that.Project {
  135. return false
  136. }
  137. if ap.Service != that.Service {
  138. return false
  139. }
  140. if ap.Cluster != that.Cluster {
  141. return false
  142. }
  143. if ap.Name != that.Name {
  144. return false
  145. }
  146. if ap.ProviderID != that.ProviderID {
  147. return false
  148. }
  149. return true
  150. }
  151. // Keys returns the list of string values used to key the Asset based on the
  152. // list of properties provided.
  153. func (ap *AssetProperties) Keys(props []AssetProperty) []string {
  154. keys := []string{}
  155. if ap == nil {
  156. return keys
  157. }
  158. if (props == nil || hasProp(props, AssetCategoryProp)) && ap.Category != "" {
  159. keys = append(keys, ap.Category)
  160. }
  161. if (props == nil || hasProp(props, AssetProviderProp)) && ap.Provider != "" {
  162. keys = append(keys, ap.Provider)
  163. }
  164. if (props == nil || hasProp(props, AssetAccountProp)) && ap.Account != "" {
  165. keys = append(keys, ap.Account)
  166. }
  167. if (props == nil || hasProp(props, AssetProjectProp)) && ap.Project != "" {
  168. keys = append(keys, ap.Project)
  169. }
  170. if (props == nil || hasProp(props, AssetServiceProp)) && ap.Service != "" {
  171. keys = append(keys, ap.Service)
  172. }
  173. if (props == nil || hasProp(props, AssetClusterProp)) && ap.Cluster != "" {
  174. keys = append(keys, ap.Cluster)
  175. }
  176. if (props == nil || hasProp(props, AssetNameProp)) && ap.Name != "" {
  177. keys = append(keys, ap.Name)
  178. }
  179. if (props == nil || hasProp(props, AssetProviderIDProp)) && ap.ProviderID != "" {
  180. keys = append(keys, ap.ProviderID)
  181. }
  182. return keys
  183. }
  184. // Merge retains only the properties shared with the given AssetProperties
  185. func (ap *AssetProperties) Merge(that *AssetProperties) *AssetProperties {
  186. if ap == nil || that == nil {
  187. return nil
  188. }
  189. result := &AssetProperties{}
  190. if ap.Category == that.Category {
  191. result.Category = ap.Category
  192. }
  193. if ap.Provider == that.Provider {
  194. result.Provider = ap.Provider
  195. }
  196. if ap.Account == that.Account {
  197. result.Account = ap.Account
  198. }
  199. if ap.Project == that.Project {
  200. result.Project = ap.Project
  201. }
  202. if ap.Service == that.Service {
  203. result.Service = ap.Service
  204. }
  205. if ap.Cluster == that.Cluster {
  206. result.Cluster = ap.Cluster
  207. }
  208. if ap.Name == that.Name {
  209. result.Name = ap.Name
  210. }
  211. if ap.ProviderID == that.ProviderID {
  212. result.ProviderID = ap.ProviderID
  213. }
  214. return result
  215. }
  216. // String represents the properties as a string
  217. func (ap *AssetProperties) String() string {
  218. if ap == nil {
  219. return "<nil>"
  220. }
  221. strs := []string{}
  222. if ap.Category != "" {
  223. strs = append(strs, "Category:"+ap.Category)
  224. }
  225. if ap.Provider != "" {
  226. strs = append(strs, "Provider:"+ap.Provider)
  227. }
  228. if ap.Account != "" {
  229. strs = append(strs, "Account:"+ap.Account)
  230. }
  231. if ap.Project != "" {
  232. strs = append(strs, "Project:"+ap.Project)
  233. }
  234. if ap.Service != "" {
  235. strs = append(strs, "Service:"+ap.Service)
  236. }
  237. if ap.Cluster != "" {
  238. strs = append(strs, "Cluster:"+ap.Cluster)
  239. }
  240. if ap.Name != "" {
  241. strs = append(strs, "Name:"+ap.Name)
  242. }
  243. if ap.ProviderID != "" {
  244. strs = append(strs, "ProviderID:"+ap.ProviderID)
  245. }
  246. return strings.Join(strs, ",")
  247. }
  248. func hasProp(props []AssetProperty, prop AssetProperty) bool {
  249. for _, p := range props {
  250. if p == prop {
  251. return true
  252. }
  253. }
  254. return false
  255. }