helpers.go 2.7 KB

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