helpers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package stacks
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/internal/encryption"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. func CloneSourceConfigs(sourceConfigs []models.StackSourceConfig) ([]models.StackSourceConfig, error) {
  8. res := make([]models.StackSourceConfig, 0)
  9. // for now, only write source configs which are deployed as a docker image
  10. // TODO: add parsing/writes for git-based sources
  11. for _, sourceConfig := range sourceConfigs {
  12. uid, err := encryption.GenerateRandomBytes(16)
  13. if err != nil {
  14. return nil, err
  15. }
  16. res = append(res, models.StackSourceConfig{
  17. UID: uid,
  18. Name: sourceConfig.Name,
  19. DisplayName: sourceConfig.DisplayName,
  20. ImageRepoURI: sourceConfig.ImageRepoURI,
  21. ImageTag: sourceConfig.ImageTag,
  22. })
  23. }
  24. return res, nil
  25. }
  26. func CloneAppResources(
  27. appResources []models.StackResource,
  28. prevSourceConfigs []models.StackSourceConfig,
  29. newSourceConfigs []models.StackSourceConfig,
  30. ) ([]models.StackResource, error) {
  31. res := make([]models.StackResource, 0)
  32. // for now, only write source configs which are deployed as a docker image
  33. // TODO: add parsing/writes for git-based sources
  34. for _, appResource := range appResources {
  35. uid, err := encryption.GenerateRandomBytes(16)
  36. if err != nil {
  37. return nil, err
  38. }
  39. var linkedSourceConfigUID string
  40. for _, prevSourceConfig := range prevSourceConfigs {
  41. if prevSourceConfig.UID == appResource.StackSourceConfigUID {
  42. // find the corresponding new source config
  43. for _, newSourceConfig := range newSourceConfigs {
  44. if newSourceConfig.Name == prevSourceConfig.Name {
  45. linkedSourceConfigUID = newSourceConfig.UID
  46. }
  47. }
  48. }
  49. }
  50. if linkedSourceConfigUID == "" {
  51. return nil, fmt.Errorf("source config does not exist in source config list")
  52. }
  53. res = append(res, models.StackResource{
  54. Name: appResource.Name,
  55. UID: uid,
  56. StackSourceConfigUID: linkedSourceConfigUID,
  57. TemplateRepoURL: appResource.TemplateRepoURL,
  58. TemplateName: appResource.TemplateName,
  59. TemplateVersion: appResource.TemplateVersion,
  60. HelmRevisionID: appResource.HelmRevisionID,
  61. })
  62. }
  63. return res, nil
  64. }
  65. func CloneEnvGroups(envGroups []models.StackEnvGroup) ([]models.StackEnvGroup, error) {
  66. res := make([]models.StackEnvGroup, 0)
  67. for _, envGroup := range envGroups {
  68. uid, err := encryption.GenerateRandomBytes(16)
  69. if err != nil {
  70. return nil, err
  71. }
  72. res = append(res, models.StackEnvGroup{
  73. UID: uid,
  74. Name: envGroup.Name,
  75. EnvGroupVersion: envGroup.EnvGroupVersion,
  76. Namespace: envGroup.Namespace,
  77. ProjectID: envGroup.ProjectID,
  78. ClusterID: envGroup.ClusterID,
  79. })
  80. }
  81. return res, nil
  82. }