stack.go 3.6 KB

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