Skip to content

Commit 9c937e4

Browse files
authored
Merge pull request #40 from appbaseio/fix/cluster-plan-test
fix: segmentation error + apply billing middleware for appbase.io clusters and hosted Arc
2 parents 744e511 + 0a428dc commit 9c937e4

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ func main() {
110110
cronjob := cron.New()
111111
cronjob.AddFunc(interval, util.ReportHostedArcUsage)
112112
cronjob.Start()
113+
router.Use(util.BillingMiddleware)
113114
} else if ClusterBilling == "true" {
114115
log.Println("You're running Arc with cluster billing module enabled.")
115116
util.SetClusterPlan()
116117
// refresh plan
117118
cronjob := cron.New()
118119
cronjob.AddFunc(interval, util.SetClusterPlan)
119120
cronjob.Start()
121+
router.Use(util.BillingMiddleware)
120122
} else {
121123
log.Println("You're running Arc with billing module disabled.")
122124
}

middleware/validate/plan.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ func Plan(validPlans []util.Plan, byPassValidation bool) middleware.Middleware {
1919
// Throws the payment required error
2020
func invalidPlan(h http.HandlerFunc) http.HandlerFunc {
2121
return func(w http.ResponseWriter, req *http.Request) {
22-
msg := "This feature is not available for the " + util.Tier.String() + " plan users."
22+
msg := "This feature is not available for the free plan users, please upgrade to a paid plan."
23+
if util.Tier != nil {
24+
msg = "This feature is not available for the " + util.Tier.String() + " plan users, please upgrade to a higher plan."
25+
}
2326
util.WriteBackError(w, msg, http.StatusPaymentRequired)
2427
}
2528
}

util/billing.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ type ArcUsage struct {
4646
}
4747

4848
type ClusterPlan struct {
49-
Tier *Plan `json:"pricing_plan"`
50-
FeatureCustomEvents bool `json:"feature_custom_events"`
51-
FeatureSuggestions bool `json:"feature_suggestions"`
49+
Tier *Plan `json:"tier"`
50+
FeatureCustomEvents bool `json:"feature_custom_events"`
51+
FeatureSuggestions bool `json:"feature_suggestions"`
52+
Trial bool `json:"trial"`
53+
TrialValidity int64 `json:"trial_validity"`
54+
TierValidity int64 `json:"tier_validity"`
55+
TimeValidity int64 `json:"time_validity"`
56+
SubscriptionID string `json:"subscription_id"`
5257
}
5358

5459
// ArcUsageResponse stores the response from ACCAPI
@@ -231,13 +236,14 @@ func getClusterPlan(clusterID string) (ClusterPlan, error) {
231236
}
232237
// Set the plan for clusters
233238
Tier = response.Plan.Tier
239+
TimeValidity = response.Plan.TimeValidity
234240
FeatureCustomEvents = response.Plan.FeatureCustomEvents
235241
FeatureSuggestions = response.Plan.FeatureSuggestions
236242

237243
return clusterPlan, nil
238244
}
239245

240-
// GetClusterPlan fetches the cluster plan & sets the Tier value
246+
// SetClusterPlan fetches the cluster plan & sets the Tier value
241247
func SetClusterPlan() {
242248
log.Printf("=> Getting cluster plan details")
243249
clusterID := os.Getenv("CLUSTER_ID")
@@ -247,7 +253,7 @@ func SetClusterPlan() {
247253
}
248254
_, err := getClusterPlan(clusterID)
249255
if err != nil {
250-
log.Println("Unable to fetch the cluster plan. Please make sure that you're using a valid CLUSTER_ID.", err)
256+
log.Fatalln("Unable to fetch the cluster plan. Please make sure that you're using a valid CLUSTER_ID. If the issue persists please contact support@appbase.io with your ARC_ID or registered e-mail address.", err)
251257
return
252258
}
253259
}
@@ -333,7 +339,7 @@ func ReportUsage() {
333339

334340
result, err := getArcInstance(arcID)
335341
if err != nil {
336-
log.Println("Unable to fetch the arc instance. Please make sure that you're using a valid ARC_ID.")
342+
log.Fatalln("Unable to fetch the arc instance. Please make sure that you're using a valid ARC_ID. If the issue persists please contact support@appbase.io with your ARC_ID or registered e-mail address.")
337343
return
338344
}
339345

@@ -383,7 +389,7 @@ func ReportHostedArcUsage() {
383389
// getArcClusterInstance(clusterId)
384390
result, err := getArcClusterInstance(clusterID)
385391
if err != nil {
386-
log.Println("Unable to fetch the arc instance. Please make sure that you're using a valid CLUSTER_ID.", err)
392+
log.Fatalln("Unable to fetch the arc instance. Please make sure that you're using a valid CLUSTER_ID. If the issue persists please contact support@appbase.io with your ARC_ID or registered e-mail address.", err)
387393
return
388394
}
389395

util/plans.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func ValidatePlans(validPlans []Plan, byPassValidation bool) bool {
154154
if byPassValidation {
155155
return true
156156
}
157+
if Tier == nil {
158+
return false
159+
}
157160
for _, validPlan := range validPlans {
158161
if (ClusterBilling == "true" || Billing == "true" || HostedBilling == "true") && Tier.String() == validPlan.String() {
159162
return true

0 commit comments

Comments
 (0)