billing.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package billing
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. // BillingManager contains methods for managing billing for a project
  8. type BillingManager interface {
  9. // CreateTeam creates the concept of a billing "team". This is currently a one-to-one
  10. // mapping with projects, but this may change in the future (i.e. multiple projects
  11. // per same team)
  12. CreateTeam(proj *models.Project) (teamID string, err error)
  13. // DeleteTeam deletes a billing team.
  14. DeleteTeam(proj *models.Project) (err error)
  15. // GetTeamID gets the billing team id for a project
  16. GetTeamID(proj *models.Project) (teamID string, err error)
  17. // CreatePlan creates a new plan based on the requested limits
  18. CreatePlan(teamID string, proj *models.Project, planSpec *types.AddProjectBillingRequest) (string, error)
  19. // CreateOrUpdateSubscription creates or updates a new subscription to a plan, based on a team and plan ID
  20. CreateOrUpdateSubscription(teamID, planID string) error
  21. // GetExistingPublicPlan returns an existing public plan based on a name
  22. GetExistingPublicPlan(planName string) (string, error)
  23. // AddUserToTeam adds a user to a team, and cases on whether the user can view
  24. // billing based on the role.
  25. AddUserToTeam(teamID string, user *models.User, role *models.Role) error
  26. // UpdateUserInTeam updates a user's role in a team, and cases on whether the user can view
  27. // billing based on the role.
  28. UpdateUserInTeam(role *models.Role) error
  29. // RemoveUserFromTeam removes a user from a team
  30. RemoveUserFromTeam(role *models.Role) error
  31. // GetIDToken retrieves a billing token for a user. The billing token can be exchanged
  32. // to view billing information.
  33. GetIDToken(proj *models.Project, user *models.User) (token string, teamID string, err error)
  34. // ParseProjectUsageFromWebhook parses the project usage from a webhook payload sent
  35. // from a billing agent
  36. ParseProjectUsageFromWebhook(payload []byte) (*models.ProjectUsage, error)
  37. // VerifySignature verifies the signature for a webhook
  38. VerifySignature(signature string, body []byte) bool
  39. }
  40. // NoopBillingManager performs no billing operations
  41. type NoopBillingManager struct{}
  42. func (n *NoopBillingManager) CreateTeam(proj *models.Project) (teamID string, err error) {
  43. return fmt.Sprintf("%d", proj.ID), nil
  44. }
  45. func (n *NoopBillingManager) DeleteTeam(proj *models.Project) (err error) {
  46. return nil
  47. }
  48. func (n *NoopBillingManager) GetTeamID(proj *models.Project) (teamID string, err error) {
  49. return fmt.Sprintf("%d", proj.ID), nil
  50. }
  51. func (n *NoopBillingManager) CreatePlan(teamID string, proj *models.Project, planSpec *types.AddProjectBillingRequest) (string, error) {
  52. return "", nil
  53. }
  54. func (n *NoopBillingManager) CreateOrUpdateSubscription(teamID, planID string) error {
  55. return nil
  56. }
  57. func (n *NoopBillingManager) GetExistingPublicPlan(planName string) (string, error) {
  58. return "", nil
  59. }
  60. func (n *NoopBillingManager) AddUserToTeam(teamID string, user *models.User, role *models.Role) error {
  61. return nil
  62. }
  63. func (n *NoopBillingManager) UpdateUserInTeam(role *models.Role) error {
  64. return nil
  65. }
  66. func (n *NoopBillingManager) RemoveUserFromTeam(role *models.Role) error {
  67. return nil
  68. }
  69. func (n *NoopBillingManager) GetIDToken(proj *models.Project, user *models.User) (token string, teamID string, err error) {
  70. return "", "", nil
  71. }
  72. func (n *NoopBillingManager) ParseProjectUsageFromWebhook(payload []byte) (*models.ProjectUsage, error) {
  73. return nil, nil
  74. }
  75. func (n *NoopBillingManager) VerifySignature(signature string, body []byte) bool {
  76. return false
  77. }