2
0

util.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package clusters
  2. import "fmt"
  3. // MapToClusterInfo returns a ClusterInfo using parsed data from a string map. If
  4. // parsing the map fails for id and/or name, an error is returned.
  5. func MapToClusterInfo(info map[string]string) (*ClusterInfo, error) {
  6. var id string
  7. var name string
  8. if i, ok := info[ClusterInfoIdKey]; ok {
  9. id = i
  10. } else {
  11. return nil, fmt.Errorf("cluster info missing id")
  12. }
  13. if n, ok := info[ClusterInfoNameKey]; ok {
  14. name = n
  15. } else {
  16. name = id
  17. }
  18. var clusterProfile string
  19. var provider string
  20. var account string
  21. var project string
  22. var region string
  23. var provisioner string
  24. var version string
  25. if cp, ok := info[ClusterInfoProfileKey]; ok {
  26. clusterProfile = cp
  27. }
  28. if pvdr, ok := info[ClusterInfoProviderKey]; ok {
  29. provider = pvdr
  30. }
  31. if acct, ok := info[ClusterInfoAccountKey]; ok {
  32. account = acct
  33. }
  34. if proj, ok := info[ClusterInfoProjectKey]; ok {
  35. project = proj
  36. }
  37. if reg, ok := info[ClusterInfoRegionKey]; ok {
  38. region = reg
  39. }
  40. if pvsr, ok := info[ClusterInfoProvisionerKey]; ok {
  41. provisioner = pvsr
  42. }
  43. if ver, ok := info[ClusterInfoVersionKey]; ok {
  44. version = ver
  45. }
  46. return &ClusterInfo{
  47. ID: id,
  48. Name: name,
  49. Profile: clusterProfile,
  50. Provider: provider,
  51. Account: account,
  52. Project: project,
  53. Region: region,
  54. Provisioner: provisioner,
  55. Version: version,
  56. }, nil
  57. }