stack.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. Reason string
  41. Message string
  42. Resources []StackResource
  43. SourceConfigs []StackSourceConfig
  44. }
  45. func (s StackRevision) ToStackRevisionMetaType(stackID string) types.StackRevisionMeta {
  46. return types.StackRevisionMeta{
  47. CreatedAt: s.CreatedAt,
  48. ID: s.RevisionNumber,
  49. Status: types.StackRevisionStatus(s.Status),
  50. StackID: stackID,
  51. }
  52. }
  53. func (s StackRevision) ToStackRevisionType(stackID string) *types.StackRevision {
  54. metaType := s.ToStackRevisionMetaType(stackID)
  55. sourceConfigs := make([]types.StackSourceConfig, 0)
  56. for _, sourceConfig := range s.SourceConfigs {
  57. sourceConfigs = append(sourceConfigs, *sourceConfig.ToStackSourceConfigType(stackID, s.RevisionNumber))
  58. }
  59. resources := make([]types.StackResource, 0)
  60. for _, stackResource := range s.Resources {
  61. resources = append(resources, *stackResource.ToStackResource(stackID, s.RevisionNumber, s.SourceConfigs))
  62. }
  63. return &types.StackRevision{
  64. StackRevisionMeta: &metaType,
  65. SourceConfigs: sourceConfigs,
  66. Resources: resources,
  67. Reason: s.Reason,
  68. Message: s.Message,
  69. }
  70. }
  71. type StackResource struct {
  72. gorm.Model
  73. Name string
  74. UID string
  75. StackRevisionID uint
  76. StackSourceConfigUID string
  77. HelmRevisionID uint
  78. Values []byte
  79. TemplateRepoURL string
  80. TemplateName string
  81. TemplateVersion string
  82. }
  83. func (s StackResource) ToStackResource(stackID string, stackRevisionID uint, sourceConfigs []StackSourceConfig) *types.StackResource {
  84. // find the relevant source config
  85. var linkedSourceConfig StackSourceConfig
  86. for _, sourceConfig := range sourceConfigs {
  87. if sourceConfig.UID == s.StackSourceConfigUID {
  88. linkedSourceConfig = sourceConfig
  89. break
  90. }
  91. }
  92. return &types.StackResource{
  93. CreatedAt: s.CreatedAt,
  94. UpdatedAt: s.UpdatedAt,
  95. Name: s.Name,
  96. ID: s.UID,
  97. StackSourceConfig: linkedSourceConfig.ToStackSourceConfigType(stackID, stackRevisionID),
  98. StackID: stackID,
  99. // Note that `StackRevisionID` on the API refers to the numerical auto-incremented revision ID, not
  100. // the stack_revision_id in the database.
  101. StackRevisionID: stackRevisionID,
  102. StackAppData: &types.StackResourceAppData{
  103. TemplateRepoURL: s.TemplateRepoURL,
  104. TemplateName: s.TemplateName,
  105. TemplateVersion: s.TemplateVersion,
  106. },
  107. }
  108. }
  109. type StackSourceConfig struct {
  110. gorm.Model
  111. StackRevisionID uint
  112. Name string
  113. UID string
  114. ImageRepoURI string
  115. ImageTag string
  116. // TODO: add git-specific information
  117. }
  118. func (s StackSourceConfig) ToStackSourceConfigType(stackID string, stackRevisionID uint) *types.StackSourceConfig {
  119. return &types.StackSourceConfig{
  120. CreatedAt: s.CreatedAt,
  121. UpdatedAt: s.UpdatedAt,
  122. StackID: stackID,
  123. StackRevisionID: stackRevisionID,
  124. Name: s.Name,
  125. ID: s.UID,
  126. ImageRepoURI: s.ImageRepoURI,
  127. ImageTag: s.ImageTag,
  128. }
  129. }