stack.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package models
  2. import (
  3. "github.com/porter-dev/porter/api/types"
  4. "gorm.io/gorm"
  5. )
  6. // Stack represents the metadata for a stack on Porter
  7. type Stack struct {
  8. gorm.Model
  9. ProjectID uint
  10. ClusterID uint
  11. Namespace string
  12. Name string
  13. UID string `gorm:"unique"`
  14. Revisions []StackRevision
  15. }
  16. func (s *Stack) ToStackType() *types.Stack {
  17. revisions := []types.StackRevisionMeta{}
  18. for _, rev := range s.Revisions {
  19. revisions = append(revisions, rev.ToStackRevisionMetaType(s.UID))
  20. }
  21. var latestRevision *types.StackRevision
  22. if len(s.Revisions) > 0 {
  23. latestRevision = s.Revisions[0].ToStackRevisionType(s.UID)
  24. }
  25. return &types.Stack{
  26. CreatedAt: s.CreatedAt,
  27. UpdatedAt: s.UpdatedAt,
  28. Name: s.Name,
  29. ID: s.UID,
  30. LatestRevision: latestRevision,
  31. Revisions: revisions,
  32. }
  33. }
  34. // StackRevision represents the revision information for the stack
  35. type StackRevision struct {
  36. gorm.Model
  37. RevisionNumber uint
  38. StackID uint
  39. Status string
  40. Resources []StackResource
  41. SourceConfigs []StackSourceConfig
  42. }
  43. func (s StackRevision) ToStackRevisionMetaType(stackID string) types.StackRevisionMeta {
  44. return types.StackRevisionMeta{
  45. CreatedAt: s.CreatedAt,
  46. ID: s.RevisionNumber,
  47. Status: types.StackRevisionStatus(s.Status),
  48. StackID: stackID,
  49. }
  50. }
  51. func (s StackRevision) ToStackRevisionType(stackID string) *types.StackRevision {
  52. metaType := s.ToStackRevisionMetaType(stackID)
  53. sourceConfigs := make([]types.StackSourceConfig, 0)
  54. for _, sourceConfig := range s.SourceConfigs {
  55. sourceConfigs = append(sourceConfigs, *sourceConfig.ToStackSourceConfigType(stackID, s.RevisionNumber))
  56. }
  57. resources := make([]types.StackResource, 0)
  58. for _, stackResource := range s.Resources {
  59. resources = append(resources, *stackResource.ToStackResource(stackID, s.RevisionNumber, s.SourceConfigs))
  60. }
  61. return &types.StackRevision{
  62. StackRevisionMeta: &metaType,
  63. SourceConfigs: sourceConfigs,
  64. Resources: resources,
  65. }
  66. }
  67. type StackResource struct {
  68. gorm.Model
  69. Name string
  70. UID string
  71. StackRevisionID uint
  72. StackSourceConfigUID string
  73. HelmRevisionID uint
  74. Values []byte
  75. TemplateRepoURL string
  76. TemplateName string
  77. TemplateVersion string
  78. }
  79. func (s StackResource) ToStackResource(stackID string, stackRevisionID uint, sourceConfigs []StackSourceConfig) *types.StackResource {
  80. // find the relevant source config
  81. var linkedSourceConfig StackSourceConfig
  82. for _, sourceConfig := range sourceConfigs {
  83. if sourceConfig.UID == s.StackSourceConfigUID {
  84. linkedSourceConfig = sourceConfig
  85. break
  86. }
  87. }
  88. return &types.StackResource{
  89. CreatedAt: s.CreatedAt,
  90. UpdatedAt: s.UpdatedAt,
  91. Name: s.Name,
  92. ID: s.UID,
  93. StackSourceConfig: linkedSourceConfig.ToStackSourceConfigType(stackID, stackRevisionID),
  94. StackID: stackID,
  95. // Note that `StackRevisionID` on the API refers to the numerical auto-incremented revision ID, not
  96. // the stack_revision_id in the database.
  97. StackRevisionID: stackRevisionID,
  98. StackAppData: &types.StackResourceAppData{
  99. TemplateRepoURL: s.TemplateRepoURL,
  100. TemplateName: s.TemplateName,
  101. TemplateVersion: s.TemplateVersion,
  102. },
  103. }
  104. }
  105. type StackSourceConfig struct {
  106. gorm.Model
  107. StackRevisionID uint
  108. Name string
  109. UID string
  110. ImageRepoURI string
  111. ImageTag string
  112. // TODO: add git-specific information
  113. }
  114. func (s StackSourceConfig) ToStackSourceConfigType(stackID string, stackRevisionID uint) *types.StackSourceConfig {
  115. return &types.StackSourceConfig{
  116. CreatedAt: s.CreatedAt,
  117. UpdatedAt: s.UpdatedAt,
  118. StackID: stackID,
  119. StackRevisionID: stackRevisionID,
  120. Name: s.Name,
  121. ID: s.UID,
  122. ImageRepoURI: s.ImageRepoURI,
  123. ImageTag: s.ImageTag,
  124. }
  125. }