project.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package models
  2. import (
  3. "gorm.io/gorm"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/features"
  6. ints "github.com/porter-dev/porter/internal/models/integrations"
  7. )
  8. type ProjectPlan string
  9. const (
  10. ProjectPlanBasic ProjectPlan = "basic"
  11. ProjectPlanTeam ProjectPlan = "team"
  12. ProjectPlanGrowth ProjectPlan = "growth"
  13. ProjectPlanEnterprise ProjectPlan = "enterprise"
  14. )
  15. // Project type that extends gorm.Model
  16. type Project struct {
  17. gorm.Model `gorm:"embedded"`
  18. Name string `json:"name"`
  19. Roles []Role `json:"roles"`
  20. ProjectUsageID uint
  21. ProjectUsageCacheID uint
  22. // linked repos
  23. GitRepos []GitRepo `json:"git_repos,omitempty"`
  24. // linked registries
  25. Registries []Registry `json:"registries,omitempty"`
  26. // linked clusters
  27. Clusters []Cluster `json:"clusters"`
  28. ClusterCandidates []ClusterCandidate `json:"cluster_candidates"`
  29. // linked databases
  30. Databases []Database `json:"databases"`
  31. // linked helm repos
  32. HelmRepos []HelmRepo `json:"helm_repos"`
  33. // invitations to the project
  34. Invites []Invite `json:"invites"`
  35. // provisioned aws infra
  36. Infras []Infra `json:"infras"`
  37. // auth mechanisms
  38. KubeIntegrations []ints.KubeIntegration `json:"kube_integrations"`
  39. BasicIntegrations []ints.BasicIntegration `json:"basic_integrations"`
  40. OIDCIntegrations []ints.OIDCIntegration `json:"oidc_integrations"`
  41. OAuthIntegrations []ints.OAuthIntegration `json:"oauth_integrations"`
  42. AWSIntegrations []ints.AWSIntegration `json:"aws_integrations"`
  43. GCPIntegrations []ints.GCPIntegration `json:"gcp_integrations"`
  44. AzureIntegrations []ints.AzureIntegration `json:"azure_integrations"`
  45. GitlabIntegrations []ints.GitlabIntegration `json:"gitlab_integrations"`
  46. PreviewEnvsEnabled bool
  47. RDSDatabasesEnabled bool
  48. ManagedInfraEnabled bool
  49. StacksEnabled bool
  50. APITokensEnabled bool
  51. CapiProvisionerEnabled bool
  52. SimplifiedViewEnabled bool
  53. AzureEnabled bool
  54. HelmValuesEnabled bool
  55. MultiCluster bool `gorm:"default:false"`
  56. FullAddOns bool `gorm:"default:false"`
  57. ValidateApplyV2 bool `gorm:"default:false"`
  58. EnableReprovision bool `gorm:"default:false"`
  59. }
  60. // ToProjectType generates an external types.Project to be shared over REST
  61. func (p *Project) ToProjectType(launchDarklyClient *features.Client) types.Project {
  62. roles := make([]*types.Role, 0)
  63. for _, role := range p.Roles {
  64. roles = append(roles, role.ToRoleType())
  65. }
  66. projectID := p.ID
  67. projectName := p.Name
  68. ldContext := getProjectContext(projectID, projectName)
  69. return types.Project{
  70. ID: projectID,
  71. Name: projectName,
  72. Roles: roles,
  73. PreviewEnvsEnabled: getPreviewEnvsEnabled(ldContext, launchDarklyClient),
  74. RDSDatabasesEnabled: getRdsDatabasesEnabled(ldContext, launchDarklyClient),
  75. ManagedInfraEnabled: getManagedInfraEnabled(ldContext, launchDarklyClient),
  76. StacksEnabled: getStacksEnabled(ldContext, launchDarklyClient),
  77. APITokensEnabled: getAPITokensEnabled(ldContext, launchDarklyClient),
  78. CapiProvisionerEnabled: getCapiProvisionerEnabled(ldContext, launchDarklyClient),
  79. SimplifiedViewEnabled: getSimplifiedViewEnabled(ldContext, launchDarklyClient),
  80. AzureEnabled: getAzureEnabled(ldContext, launchDarklyClient),
  81. HelmValuesEnabled: getHelmValuesEnabled(ldContext, launchDarklyClient),
  82. MultiCluster: getMultiCluster(ldContext, launchDarklyClient),
  83. EnableReprovision: getEnableReprovision(ldContext, launchDarklyClient),
  84. ValidateApplyV2: getValidateApplyV2(ldContext, launchDarklyClient),
  85. FullAddOns: getFullAddOns(ldContext, launchDarklyClient),
  86. }
  87. }
  88. // ToProjectListType returns a "minified" version of a Project
  89. // suitable for api responses to GET /projects
  90. // TODO: update this in the future to use default values for all
  91. // the feature flags instead of trying to retrieve them from the database
  92. func (p *Project) ToProjectListType() *types.ProjectList {
  93. var roles []types.Role
  94. for _, role := range p.Roles {
  95. roles = append(roles, *role.ToRoleType())
  96. }
  97. return &types.ProjectList{
  98. ID: p.ID,
  99. Name: p.Name,
  100. // note: all of these fields should be considered deprecated
  101. // in an api response
  102. Roles: roles,
  103. PreviewEnvsEnabled: p.PreviewEnvsEnabled,
  104. RDSDatabasesEnabled: p.RDSDatabasesEnabled,
  105. ManagedInfraEnabled: p.ManagedInfraEnabled,
  106. StacksEnabled: p.StacksEnabled,
  107. APITokensEnabled: p.APITokensEnabled,
  108. CapiProvisionerEnabled: p.CapiProvisionerEnabled,
  109. SimplifiedViewEnabled: p.SimplifiedViewEnabled,
  110. AzureEnabled: p.AzureEnabled,
  111. HelmValuesEnabled: p.HelmValuesEnabled,
  112. MultiCluster: p.MultiCluster,
  113. EnableReprovision: p.EnableReprovision,
  114. ValidateApplyV2: p.ValidateApplyV2,
  115. FullAddOns: p.FullAddOns,
  116. }
  117. }