project.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package models
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
  6. "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/porter/internal/features"
  8. ints "github.com/porter-dev/porter/internal/models/integrations"
  9. )
  10. // FeatureFlagLabel strongly types project feature flags
  11. type FeatureFlagLabel string
  12. const (
  13. // APITokensEnabled allows users to create Bearer tokens for use with the Porter API
  14. // #nosec G101 - Not actually an api token
  15. APITokensEnabled FeatureFlagLabel = "api_tokens_enabled"
  16. // AzureEnabled enables Azure Provisioning
  17. AzureEnabled FeatureFlagLabel = "azure_enabled"
  18. // CapiProvisionerEnabled enables the CAPI Provisioning flow
  19. CapiProvisionerEnabled FeatureFlagLabel = "capi_provisioner_enabled"
  20. // DBEnabled enables the "Databases" tab
  21. DBEnabled FeatureFlagLabel = "db_enabled"
  22. // EnableReprovision enables the provisioning button after initial creation of the cluster
  23. EnableReprovision FeatureFlagLabel = "enable_reprovision"
  24. // FullAddOns shows all addons, not just curated
  25. FullAddOns FeatureFlagLabel = "full_add_ons"
  26. // HelmValuesEnabled shows the helm values tab for porter apps (when simplified_view_enabled=true)
  27. HelmValuesEnabled FeatureFlagLabel = "helm_values_enabled"
  28. // ManagedInfraEnabled uses terraform provisioning instead of capi
  29. ManagedInfraEnabled FeatureFlagLabel = "managed_infra_enabled"
  30. // MultiCluster allows multiple clusters in simplified view (simplified_view_enabled=true)
  31. MultiCluster FeatureFlagLabel = "multi_cluster"
  32. // PreviewEnvsEnabled allows legacy user the ability to see preview environments in sidebar (simplified_view_enabled=false)
  33. PreviewEnvsEnabled FeatureFlagLabel = "preview_envs_enabled"
  34. // RDSDatabasesEnabled allows for users to provision RDS instances within their cluster vpc
  35. RDSDatabasesEnabled FeatureFlagLabel = "rds_databases_enabled"
  36. // QuotaIncrease enables whether we allow for auto increase of quota_increase
  37. QuotaIncrease FeatureFlagLabel = "quota_increase"
  38. // SimplifiedViewEnabled shows the new UI dashboard or not
  39. SimplifiedViewEnabled FeatureFlagLabel = "simplified_view_enabled"
  40. // StacksEnabled uses stack view for legacy (simplified_view_enabled=false)
  41. StacksEnabled FeatureFlagLabel = "stacks_enabled"
  42. // ValidateApplyV2 controls whether apps deploys use a porter app revision contract vs helm
  43. ValidateApplyV2 FeatureFlagLabel = "validate_apply_v2"
  44. )
  45. // ProjectFeatureFlags keeps track of all project-related feature flags
  46. var ProjectFeatureFlags = map[FeatureFlagLabel]bool{
  47. APITokensEnabled: false,
  48. AzureEnabled: false,
  49. CapiProvisionerEnabled: true,
  50. DBEnabled: false,
  51. EnableReprovision: false,
  52. FullAddOns: false,
  53. HelmValuesEnabled: false,
  54. ManagedInfraEnabled: false,
  55. MultiCluster: false,
  56. PreviewEnvsEnabled: false,
  57. RDSDatabasesEnabled: false,
  58. QuotaIncrease: false,
  59. SimplifiedViewEnabled: true,
  60. StacksEnabled: false,
  61. ValidateApplyV2: true,
  62. }
  63. type ProjectPlan string
  64. const (
  65. ProjectPlanBasic ProjectPlan = "basic"
  66. ProjectPlanTeam ProjectPlan = "team"
  67. ProjectPlanGrowth ProjectPlan = "growth"
  68. ProjectPlanEnterprise ProjectPlan = "enterprise"
  69. )
  70. // Project type that extends gorm.Model
  71. type Project struct {
  72. gorm.Model `gorm:"embedded"`
  73. Name string `json:"name"`
  74. Roles []Role `json:"roles"`
  75. ProjectUsageID uint
  76. ProjectUsageCacheID uint
  77. // linked repos
  78. GitRepos []GitRepo `json:"git_repos,omitempty"`
  79. // linked registries
  80. Registries []Registry `json:"registries,omitempty"`
  81. // linked clusters
  82. Clusters []Cluster `json:"clusters"`
  83. ClusterCandidates []ClusterCandidate `json:"cluster_candidates"`
  84. // linked databases
  85. Databases []Database `json:"databases"`
  86. // linked helm repos
  87. HelmRepos []HelmRepo `json:"helm_repos"`
  88. // invitations to the project
  89. Invites []Invite `json:"invites"`
  90. // provisioned aws infra
  91. Infras []Infra `json:"infras"`
  92. // auth mechanisms
  93. KubeIntegrations []ints.KubeIntegration `json:"kube_integrations"`
  94. BasicIntegrations []ints.BasicIntegration `json:"basic_integrations"`
  95. OIDCIntegrations []ints.OIDCIntegration `json:"oidc_integrations"`
  96. OAuthIntegrations []ints.OAuthIntegration `json:"oauth_integrations"`
  97. AWSIntegrations []ints.AWSIntegration `json:"aws_integrations"`
  98. GCPIntegrations []ints.GCPIntegration `json:"gcp_integrations"`
  99. AzureIntegrations []ints.AzureIntegration `json:"azure_integrations"`
  100. GitlabIntegrations []ints.GitlabIntegration `json:"gitlab_integrations"`
  101. // Deprecated: use p.GetFeatureFlag(PreviewEnvsEnabled, *features.Client) instead
  102. PreviewEnvsEnabled bool
  103. // Deprecated: use p.GetFeatureFlag(RDSDatabasesEnabled, *features.Client) instead
  104. RDSDatabasesEnabled bool
  105. // Deprecated: use p.GetFeatureFlag(ManagedInfraEnabled, *features.Client) instead
  106. ManagedInfraEnabled bool
  107. // Deprecated: use p.GetFeatureFlag(StacksEnabled, *features.Client) instead
  108. StacksEnabled bool
  109. // Deprecated: use p.GetFeatureFlag(APITokensEnabled, *features.Client) instead
  110. APITokensEnabled bool
  111. // Deprecated: use p.GetFeatureFlag(CapiProvisionerEnabled, *features.Client) instead
  112. CapiProvisionerEnabled bool
  113. // Deprecated: use p.GetFeatureFlag(SimplifiedViewEnabled, *features.Client) instead
  114. SimplifiedViewEnabled bool
  115. // Deprecated: use p.GetFeatureFlag(AzureEnabled, *features.Client) instead
  116. AzureEnabled bool
  117. // Deprecated: use p.GetFeatureFlag(HelmValuesEnabled, *features.Client) instead
  118. HelmValuesEnabled bool
  119. // Deprecated: use p.GetFeatureFlag(MultiCluster, *features.Client) instead
  120. MultiCluster bool `gorm:"default:false"`
  121. // Deprecated: use p.GetFeatureFlag(FullAddOns, *features.Client) instead
  122. FullAddOns bool `gorm:"default:false"`
  123. // Deprecated: use p.GetFeatureFlag(ValidateApplyV2, *features.Client) instead
  124. ValidateApplyV2 bool `gorm:"default:false"`
  125. // Deprecated: use p.GetFeatureFlag(EnableReprovision, *features.Client) instead
  126. EnableReprovision bool `gorm:"default:false"`
  127. }
  128. // GetFeatureFlag calls launchdarkly for the specified flag
  129. // and returns the configured value
  130. func (p *Project) GetFeatureFlag(flagName FeatureFlagLabel, launchDarklyClient *features.Client) bool {
  131. if launchDarklyClient.UseDatabase() {
  132. // case switch things
  133. switch flagName {
  134. case "api_tokens_enabled":
  135. return p.APITokensEnabled
  136. case "azure_enabled":
  137. return p.AzureEnabled
  138. case "capi_provisioner_enabled":
  139. return p.CapiProvisionerEnabled
  140. case "db_enabled":
  141. return false
  142. case "enable_reprovision":
  143. return p.EnableReprovision
  144. case "full_add_ons":
  145. return p.FullAddOns
  146. case "helm_values_enabled":
  147. return p.HelmValuesEnabled
  148. case "managed_infra_enabled":
  149. return p.ManagedInfraEnabled
  150. case "multi_cluster":
  151. return p.MultiCluster
  152. case "preview_envs_enabled":
  153. return p.PreviewEnvsEnabled
  154. case "quota_increase":
  155. return false
  156. case "rds_databases_enabled":
  157. return p.RDSDatabasesEnabled
  158. case "simplified_view_enabled":
  159. return p.SimplifiedViewEnabled
  160. case "stacks_enabled":
  161. return p.StacksEnabled
  162. case "validate_apply_v2":
  163. return p.ValidateApplyV2
  164. }
  165. }
  166. projectID := p.ID
  167. projectName := p.Name
  168. ldContext := getProjectContext(projectID, projectName)
  169. defaultValue := ProjectFeatureFlags[flagName]
  170. value, _ := launchDarklyClient.BoolVariation(string(flagName), ldContext, defaultValue)
  171. return value
  172. }
  173. // ToProjectType generates an external types.Project to be shared over REST
  174. func (p *Project) ToProjectType(launchDarklyClient *features.Client) types.Project {
  175. roles := make([]*types.Role, 0)
  176. for _, role := range p.Roles {
  177. roles = append(roles, role.ToRoleType())
  178. }
  179. projectID := p.ID
  180. projectName := p.Name
  181. return types.Project{
  182. ID: projectID,
  183. Name: projectName,
  184. Roles: roles,
  185. PreviewEnvsEnabled: p.GetFeatureFlag(PreviewEnvsEnabled, launchDarklyClient),
  186. RDSDatabasesEnabled: p.GetFeatureFlag(RDSDatabasesEnabled, launchDarklyClient),
  187. ManagedInfraEnabled: p.GetFeatureFlag(ManagedInfraEnabled, launchDarklyClient),
  188. StacksEnabled: p.GetFeatureFlag(StacksEnabled, launchDarklyClient),
  189. APITokensEnabled: p.GetFeatureFlag(APITokensEnabled, launchDarklyClient),
  190. CapiProvisionerEnabled: p.GetFeatureFlag(CapiProvisionerEnabled, launchDarklyClient),
  191. DBEnabled: p.GetFeatureFlag(DBEnabled, launchDarklyClient),
  192. SimplifiedViewEnabled: p.GetFeatureFlag(SimplifiedViewEnabled, launchDarklyClient),
  193. AzureEnabled: p.GetFeatureFlag(AzureEnabled, launchDarklyClient),
  194. HelmValuesEnabled: p.GetFeatureFlag(HelmValuesEnabled, launchDarklyClient),
  195. MultiCluster: p.GetFeatureFlag(MultiCluster, launchDarklyClient),
  196. EnableReprovision: p.GetFeatureFlag(EnableReprovision, launchDarklyClient),
  197. ValidateApplyV2: p.GetFeatureFlag(ValidateApplyV2, launchDarklyClient),
  198. FullAddOns: p.GetFeatureFlag(FullAddOns, launchDarklyClient),
  199. QuotaIncrease: p.GetFeatureFlag(QuotaIncrease, launchDarklyClient),
  200. }
  201. }
  202. // ToProjectListType returns a "minified" version of a Project
  203. // suitable for api responses to GET /projects
  204. // TODO: update this in the future to use default values for all
  205. // the feature flags instead of trying to retrieve them from the database
  206. func (p *Project) ToProjectListType() *types.ProjectList {
  207. var roles []types.Role
  208. for _, role := range p.Roles {
  209. roles = append(roles, *role.ToRoleType())
  210. }
  211. return &types.ProjectList{
  212. ID: p.ID,
  213. Name: p.Name,
  214. // note: all of these fields should be considered deprecated
  215. // in an api response
  216. Roles: roles,
  217. PreviewEnvsEnabled: p.PreviewEnvsEnabled,
  218. RDSDatabasesEnabled: p.RDSDatabasesEnabled,
  219. ManagedInfraEnabled: p.ManagedInfraEnabled,
  220. StacksEnabled: p.StacksEnabled,
  221. APITokensEnabled: p.APITokensEnabled,
  222. DBEnabled: false,
  223. CapiProvisionerEnabled: p.CapiProvisionerEnabled,
  224. SimplifiedViewEnabled: p.SimplifiedViewEnabled,
  225. AzureEnabled: p.AzureEnabled,
  226. HelmValuesEnabled: p.HelmValuesEnabled,
  227. MultiCluster: p.MultiCluster,
  228. EnableReprovision: p.EnableReprovision,
  229. ValidateApplyV2: p.ValidateApplyV2,
  230. FullAddOns: p.FullAddOns,
  231. }
  232. }
  233. func getProjectContext(projectID uint, projectName string) ldcontext.Context {
  234. projectIdentifier := fmt.Sprintf("project-%d", projectID)
  235. launchDarklyName := fmt.Sprintf("%s: %s", projectIdentifier, projectName)
  236. return ldcontext.NewBuilder(projectIdentifier).
  237. Kind("project").
  238. Name(launchDarklyName).
  239. SetInt("project_id", int(projectID)).
  240. Build()
  241. }