2
0

utils.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package stack
  2. import (
  3. "context"
  4. "fmt"
  5. api "github.com/porter-dev/porter/api/client"
  6. "github.com/porter-dev/porter/api/types"
  7. switchboardTypes "github.com/porter-dev/switchboard/pkg/types"
  8. )
  9. type MessageLevel string
  10. const (
  11. Warning MessageLevel = "WARN"
  12. Error MessageLevel = "ERR"
  13. Success MessageLevel = "OK"
  14. Info MessageLevel = "INFO"
  15. )
  16. func composePreviewMessage(msg string, level MessageLevel) string {
  17. return fmt.Sprintf("[porter.yaml stack][%s] -- %s", level, msg)
  18. }
  19. func buildStackValues(apps *switchboardTypes.ResourceGroup) (map[string]interface{}, error) {
  20. values := make(map[string]interface{})
  21. for _, app := range apps.Resources {
  22. if app.Config == nil {
  23. continue
  24. }
  25. if helm_values, ok := app.Config["Values"]; ok {
  26. values[app.Name] = helm_values
  27. }
  28. }
  29. return values, nil
  30. }
  31. func buildStackDependencies(apps *switchboardTypes.ResourceGroup, client *api.Client, projectID uint) ([]types.Dependency, error) {
  32. deps := make([]types.Dependency, 0)
  33. for _, app := range apps.Resources {
  34. source, ok := app.Source["name"]
  35. if !ok {
  36. return nil, fmt.Errorf("app %s does not have a source", app.Name)
  37. }
  38. chartName, ok := source.(string)
  39. if !ok {
  40. return nil, fmt.Errorf("unable to parse source name for app %s", app.Name)
  41. }
  42. selectedRepo := "https://charts.getporter.dev"
  43. selectedVersion, err := getLatestTemplateVersion(chartName, client, projectID)
  44. if err != nil {
  45. return nil, err
  46. }
  47. deps = append(deps, types.Dependency{
  48. Name: chartName,
  49. Alias: app.Name,
  50. Version: selectedVersion,
  51. Repository: selectedRepo,
  52. })
  53. }
  54. return deps, nil
  55. }
  56. // getLatestTemplateVersion retrieves the latest template version for a specific
  57. // Porter template from the chart repository.
  58. func getLatestTemplateVersion(templateName string, client *api.Client, projectID uint) (string, error) {
  59. resp, err := client.ListTemplates(
  60. context.Background(),
  61. projectID,
  62. &types.ListTemplatesRequest{},
  63. )
  64. if err != nil {
  65. return "", err
  66. }
  67. templates := *resp
  68. var version string
  69. // find the matching template name
  70. for _, template := range templates {
  71. if templateName == template.Name {
  72. version = template.Versions[0]
  73. break
  74. }
  75. }
  76. if version == "" {
  77. return "", fmt.Errorf("matching template version not found")
  78. }
  79. return version, nil
  80. }