github.com/kubeshop/testkube@v1.17.23/pkg/tcl/checktcl/subscription.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package checktcl
    10  
    11  import (
    12  	"context"
    13  	"encoding/json"
    14  	"fmt"
    15  
    16  	"github.com/pkg/errors"
    17  	"google.golang.org/grpc"
    18  
    19  	"github.com/kubeshop/testkube/internal/config"
    20  	"github.com/kubeshop/testkube/pkg/cloud"
    21  	cloudconfig "github.com/kubeshop/testkube/pkg/cloud/data/config"
    22  	"github.com/kubeshop/testkube/pkg/cloud/data/executor"
    23  )
    24  
    25  type SubscriptionChecker struct {
    26  	proContext config.ProContext
    27  	orgPlan    OrganizationPlan
    28  }
    29  
    30  // NewSubscriptionChecker creates a new subscription checker using the agent token
    31  func NewSubscriptionChecker(ctx context.Context, proContext config.ProContext, cloudClient cloud.TestKubeCloudAPIClient, grpcConn *grpc.ClientConn) (SubscriptionChecker, error) {
    32  	executor := executor.NewCloudGRPCExecutor(cloudClient, grpcConn, proContext.APIKey)
    33  
    34  	req := GetOrganizationPlanRequest{}
    35  	response, err := executor.Execute(ctx, cloudconfig.CmdConfigGetOrganizationPlan, req)
    36  	if err != nil {
    37  		return SubscriptionChecker{}, err
    38  	}
    39  
    40  	var commandResponse GetOrganizationPlanResponse
    41  	if err := json.Unmarshal(response, &commandResponse); err != nil {
    42  		return SubscriptionChecker{}, err
    43  	}
    44  
    45  	subscription := OrganizationPlan{
    46  		TestkubeMode: OrganizationPlanTestkubeMode(commandResponse.TestkubeMode),
    47  		IsTrial:      commandResponse.IsTrial,
    48  		PlanStatus:   PlanStatus(commandResponse.PlanStatus),
    49  	}
    50  
    51  	return SubscriptionChecker{proContext: proContext, orgPlan: subscription}, nil
    52  }
    53  
    54  // GetCurrentOrganizationPlan returns current organization plan
    55  func (c *SubscriptionChecker) GetCurrentOrganizationPlan() (OrganizationPlan, error) {
    56  	if c.orgPlan.IsEmpty() {
    57  		return OrganizationPlan{}, errors.New("organization plan is not set")
    58  	}
    59  	return c.orgPlan, nil
    60  }
    61  
    62  // IsOrgPlanEnterprise checks if organization plan is enterprise
    63  func (c *SubscriptionChecker) IsOrgPlanEnterprise() (bool, error) {
    64  	if c.orgPlan.IsEmpty() {
    65  		return false, errors.New("organization plan is not set")
    66  	}
    67  	return c.orgPlan.IsEnterprise(), nil
    68  }
    69  
    70  // IsOrgPlanCloud checks if organization plan is cloud
    71  func (c *SubscriptionChecker) IsOrgPlanPro() (bool, error) {
    72  	if c.orgPlan.IsEmpty() {
    73  		return false, errors.New("organization plan is not set")
    74  	}
    75  	return c.orgPlan.IsPro(), nil
    76  }
    77  
    78  // IsOrgPlanActive checks if organization plan is active
    79  func (c *SubscriptionChecker) IsOrgPlanActive() (bool, error) {
    80  	if c.orgPlan.IsEmpty() {
    81  		return false, errors.New("organization plan is not set")
    82  	}
    83  	return c.orgPlan.IsActive(), nil
    84  }
    85  
    86  // IsActiveOrgPlanEnterpriseForFeature checks if organization plan is active and enterprise for feature
    87  func (c *SubscriptionChecker) IsActiveOrgPlanEnterpriseForFeature(featureName string) error {
    88  	plan, err := c.GetCurrentOrganizationPlan()
    89  	if err != nil {
    90  		return errors.Wrap(err, fmt.Sprintf("%s is a commercial feature", featureName))
    91  	}
    92  
    93  	if !plan.IsActive() {
    94  		return errors.New(fmt.Sprintf("%s is not available: inactive subscription plan", featureName))
    95  	}
    96  
    97  	if !plan.IsEnterprise() {
    98  		return errors.New(fmt.Sprintf("%s is not allowed: wrong subscription plan", featureName))
    99  	}
   100  
   101  	return nil
   102  }