util.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. if cp, ok := info[ClusterInfoProfileKey]; ok {
  25. clusterProfile = cp
  26. }
  27. if pvdr, ok := info[ClusterInfoProviderKey]; ok {
  28. provider = pvdr
  29. }
  30. if acct, ok := info[ClusterInfoAccountKey]; ok {
  31. account = acct
  32. }
  33. if proj, ok := info[ClusterInfoProjectKey]; ok {
  34. project = proj
  35. }
  36. if reg, ok := info[ClusterInfoRegionKey]; ok {
  37. region = reg
  38. }
  39. if pvsr, ok := info[ClusterInfoProvisionerKey]; ok {
  40. provisioner = pvsr
  41. }
  42. return &ClusterInfo{
  43. ID: id,
  44. Name: name,
  45. Profile: clusterProfile,
  46. Provider: provider,
  47. Account: account,
  48. Project: project,
  49. Region: region,
  50. Provisioner: provisioner,
  51. }, nil
  52. }