github.com/Axway/agent-sdk@v1.1.101/pkg/apic/provisioning/quota.go (about)

     1  package provisioning
     2  
     3  import (
     4  	catalog "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/catalog/v1alpha1"
     5  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     6  )
     7  
     8  // Quota - interface for accessing an access requests quota
     9  type Quota interface {
    10  	// GetInterval returns the quota interval from within the access request
    11  	GetInterval() QuotaInterval
    12  	// GetIntervalString returns the string representation of the quota interval from within the access request
    13  	GetIntervalString() string
    14  	// GetLimit returns the quota limit from within the access request
    15  	GetLimit() int64
    16  	// GetPlanName returns the product plan name from within the access request
    17  	GetPlanName() string
    18  }
    19  
    20  // QuotaInterval is the quota limit
    21  type QuotaInterval int
    22  
    23  const (
    24  	// Daily -
    25  	Daily QuotaInterval = iota + 1
    26  	// Weekly -
    27  	Weekly
    28  	// Monthly -
    29  	Monthly
    30  	// Annually -
    31  	Annually
    32  )
    33  
    34  // String returns the string value of the State
    35  func (q QuotaInterval) String() string {
    36  	return map[QuotaInterval]string{
    37  		Daily:    "daily",
    38  		Weekly:   "weekly",
    39  		Monthly:  "monthly",
    40  		Annually: "annually",
    41  	}[q]
    42  }
    43  
    44  // quotaIntervalFromString returns the quota limit represented by the string sent in
    45  func quotaIntervalFromString(limit string) QuotaInterval {
    46  	if q, ok := map[string]QuotaInterval{
    47  		"daily":    Daily,
    48  		"weekly":   Weekly,
    49  		"monthly":  Monthly,
    50  		"annually": Annually,
    51  	}[limit]; ok {
    52  		return q
    53  	}
    54  	return -1
    55  }
    56  
    57  type quota struct {
    58  	interval QuotaInterval
    59  	limit    int64
    60  	planName string
    61  }
    62  
    63  // NewQuotaFromAccessRequest create a Quota interface from an access request or nil if no quota on access request
    64  func NewQuotaFromAccessRequest(ar *management.AccessRequest) Quota {
    65  	if ar.Spec.Quota == nil {
    66  		return nil
    67  	}
    68  	interval := quotaIntervalFromString(ar.Spec.Quota.Interval)
    69  	if interval == -1 {
    70  		return nil
    71  	}
    72  
    73  	planName := ""
    74  	planData := ar.GetReferenceByGVK(catalog.ProductPlanGVK())
    75  	if planData.Name != "" {
    76  		planName = planData.Name
    77  	}
    78  	return &quota{
    79  		limit:    int64(ar.Spec.Quota.Limit),
    80  		interval: interval,
    81  		planName: planName,
    82  	}
    83  }
    84  
    85  func (q *quota) GetInterval() QuotaInterval {
    86  	return q.interval
    87  }
    88  
    89  func (q *quota) GetIntervalString() string {
    90  	return q.interval.String()
    91  }
    92  
    93  func (q *quota) GetLimit() int64 {
    94  	return q.limit
    95  }
    96  
    97  func (q *quota) GetPlanName() string {
    98  	return q.planName
    99  }