Skip to content

Commit 89aeda9

Browse files
committed
Merge branch 'feat/es7' of github.com:appbaseio/arc into feat/es7
2 parents 7574b1c + c0aa7e8 commit 89aeda9

7 files changed

Lines changed: 54 additions & 18 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ build
66
*.http
77

88
tags
9+
10+
config

Dockerfile

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ ENV HOSTED_BILLING="${HOSTED_BILLING}"
1313
ARG CLUSTER_BILLING=false
1414
ENV CLUSTER_BILLING="${CLUSTER_BILLING}"
1515

16+
# Run `--build-arg IGNORE_BILLING_MIDDLEWARE=true` to disable billing middleware for testing
17+
ARG IGNORE_BILLING_MIDDLEWARE=false
18+
ENV IGNORE_BILLING_MIDDLEWARE="${IGNORE_BILLING_MIDDLEWARE}"
19+
1620
# Run `--build-arg PLAN_REFRESH_INTERVAL=X` to change the default interval of 1 hour, where 'X' is an integer represent the hours unit
1721
ARG PLAN_REFRESH_INTERVAL=1
1822
ENV PLAN_REFRESH_INTERVAL="${PLAN_REFRESH_INTERVAL}"
@@ -36,9 +40,17 @@ RUN make
3640
# Final stage: Create the running container
3741
FROM alpine:3.10.1 AS final
3842

43+
# Get ca certs, for making api calls
44+
RUN apk add --no-cache ca-certificates
45+
46+
47+
# Create env folder
48+
RUN mkdir /arc-data && touch /arc-data/.env && chmod 777 /arc-data/.env
49+
3950
# Import the compiled executable from the first stage.
4051
COPY --from=builder /arc /arc
4152
WORKDIR /arc
4253

4354
EXPOSE 8000
44-
CMD ["build/arc", "--log", "stdout", "--plugins"]
55+
ENTRYPOINT ["build/arc", "--log", "stdout", "--plugins"]
56+
CMD ["--env", "config/docker.env"]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ PLUGIN_MAIN_LOC_FUNC=plugins/$(1)/main/$(1).$(2)
99
PLUGIN_LOC_FUNC=$(foreach PLUGIN,$(PLUGINS),$(call PLUGIN_MAIN_LOC_FUNC,$(PLUGIN),$(1)))
1010

1111
cmd: plugins
12-
$(GC) -ldflags "-X main.Billing=$(BILLING) -X main.HostedBilling=$(HOSTED_BILLING) -X main.ClusterBilling=$(CLUSTER_BILLING) -X main.PlanRefreshInterval=$(PLAN_REFRESH_INTERVAL)" -o $(BUILD_DIR)/arc main.go
12+
$(GC) -ldflags "-X main.Billing=$(BILLING) -X main.HostedBilling=$(HOSTED_BILLING) -X main.ClusterBilling=$(CLUSTER_BILLING) -X main.PlanRefreshInterval=$(PLAN_REFRESH_INTERVAL) -X main.IgnoreBillingMiddleware=$(IGNORE_BILLING_MIDDLEWARE)" -o $(BUILD_DIR)/arc main.go
1313

1414
plugins: $(call PLUGIN_LOC_FUNC,so)
1515

main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ var (
4343
HostedBilling string
4444
// ClusterBilling is a build time flag
4545
ClusterBilling string
46+
// IgnoreBillingMiddleware ignores the billing middleware
47+
IgnoreBillingMiddleware string
4648
)
4749

4850
func init() {
@@ -103,20 +105,28 @@ func main() {
103105
cronjob := cron.New()
104106
cronjob.AddFunc(interval, util.ReportUsage)
105107
cronjob.Start()
106-
router.Use(util.BillingMiddleware)
108+
if IgnoreBillingMiddleware != "true" {
109+
router.Use(util.BillingMiddleware)
110+
}
107111
} else if HostedBilling == "true" {
108112
log.Println("You're running Arc with hosted billing module enabled.")
109113
util.ReportHostedArcUsage()
110114
cronjob := cron.New()
111115
cronjob.AddFunc(interval, util.ReportHostedArcUsage)
112116
cronjob.Start()
117+
if IgnoreBillingMiddleware != "true" {
118+
router.Use(util.BillingMiddleware)
119+
}
113120
} else if ClusterBilling == "true" {
114121
log.Println("You're running Arc with cluster billing module enabled.")
115122
util.SetClusterPlan()
116123
// refresh plan
117124
cronjob := cron.New()
118125
cronjob.AddFunc(interval, util.SetClusterPlan)
119126
cronjob.Start()
127+
if IgnoreBillingMiddleware != "true" {
128+
router.Use(util.BillingMiddleware)
129+
}
120130
} else {
121131
var plan = util.ArcEnterprise
122132
util.Tier = &plan

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: 20 additions & 14 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
@@ -100,7 +105,7 @@ func BillingMiddleware(next http.Handler) http.Handler {
100105
log.Println("current time validity value: ", TimeValidity)
101106
if TimeValidity > 0 { // Valid plan
102107
next.ServeHTTP(w, r)
103-
} else if TimeValidity <= 0 && TimeValidity < -3600*MaxErrorTime { // Negative validity, plan has been expired
108+
} else if TimeValidity <= 0 && -TimeValidity < 3600*MaxErrorTime { // Negative validity, plan has been expired
104109
// Print warning message if remaining time is less than max allowed time
105110
log.Println("Warning: Payment is required. Arc will start sending out error messages in next", MaxErrorTime, "hours")
106111
next.ServeHTTP(w, r)
@@ -187,12 +192,12 @@ func getArcClusterInstance(clusterID string) (ArcInstance, error) {
187192
return arcInstance, err
188193
}
189194
if len(response.ArcInstances) != 0 {
190-
arcInstanceByID := getArcInstanceByID(clusterID, response.ArcInstances)
191-
arcInstance.SubscriptionID = arcInstanceByID.SubscriptionID
192-
TimeValidity = arcInstanceByID.TimeValidity
193-
Tier = arcInstanceByID.Tier
194-
FeatureCustomEvents = arcInstanceByID.FeatureCustomEvents
195-
FeatureSuggestions = arcInstanceByID.FeatureSuggestions
195+
arcInstanceDetails := response.ArcInstances[0]
196+
arcInstance.SubscriptionID = arcInstanceDetails.SubscriptionID
197+
TimeValidity = arcInstanceDetails.TimeValidity
198+
Tier = arcInstanceDetails.Tier
199+
FeatureCustomEvents = arcInstanceDetails.FeatureCustomEvents
200+
FeatureSuggestions = arcInstanceDetails.FeatureSuggestions
196201
} else {
197202
return arcInstance, errors.New("No valid instance found for the provided CLUSTER_ID")
198203
}
@@ -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)