stack.go 3.8 KB

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