Skip to content

Commit c699be3

Browse files
committed
fix: immediately starts thhrowing error for invalid ARC_ID
1 parent af708fb commit c699be3

1 file changed

Lines changed: 51 additions & 9 deletions

File tree

util/billing.go

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ var maxErrorTime int64 = 24 // in hrs
5050
// NodeCount is the current node count, defaults to 1
5151
var NodeCount = 1
5252

53+
// isInvalidArcIDUsed flag is being used to determine that if the arc id is invalid.
54+
// If it is `true` then `Arc` will start throwing errors immediately with a status code of `400` instead of `402`
55+
var isInvalidArcIDUsed = false
56+
5357
// ArcUsage struct is used to report time usage
5458
type ArcUsage struct {
5559
ArcID string `json:"arc_id"`
@@ -59,14 +63,18 @@ type ArcUsage struct {
5963
}
6064

6165
type ClusterPlan struct {
62-
Tier *Plan `json:"tier"`
63-
FeatureCustomEvents bool `json:"feature_custom_events"`
64-
FeatureSuggestions bool `json:"feature_suggestions"`
65-
Trial bool `json:"trial"`
66-
TrialValidity int64 `json:"trial_validity"`
67-
TierValidity int64 `json:"tier_validity"`
68-
TimeValidity int64 `json:"time_validity"`
69-
SubscriptionID string `json:"subscription_id"`
66+
Tier *Plan `json:"tier"`
67+
FeatureCustomEvents bool `json:"feature_custom_events"`
68+
FeatureSuggestions bool `json:"feature_suggestions"`
69+
FeatureRules bool `json:"feature_rules"`
70+
FeatureTemplates bool `json:"feature_templates"`
71+
FeatureFunctions bool `json:"feature_functions"`
72+
FeatureSearchSettings bool `json:"feature_search_settings"`
73+
Trial bool `json:"trial"`
74+
TrialValidity int64 `json:"trial_validity"`
75+
TierValidity int64 `json:"tier_validity"`
76+
TimeValidity int64 `json:"time_validity"`
77+
SubscriptionID string `json:"subscription_id"`
7078
}
7179

7280
// ArcUsageResponse stores the response from ACCAPI
@@ -121,6 +129,16 @@ func SetDefaultTier() {
121129
SetTier(&plan)
122130
}
123131

132+
// ValidateArcID validates the ARC_ID by checking the response returned from the ACCAPI
133+
func ValidateArcID(statusCode int) {
134+
if statusCode == http.StatusBadRequest {
135+
// Set the flag to `true` so `Arc` can start throwing errors immediately
136+
isInvalidArcIDUsed = true
137+
} else {
138+
isInvalidArcIDUsed = false
139+
}
140+
}
141+
124142
func validateTimeValidity() bool {
125143
if GetTimeValidity() > 0 { // Valid plan
126144
return true
@@ -136,14 +154,21 @@ func validateTimeValidity() bool {
136154
func BillingMiddleware(next http.Handler) http.Handler {
137155
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
138156
log.Println("current time validity value: ", GetTimeValidity())
157+
158+
if isInvalidArcIDUsed {
159+
// throw invalid ARC_ID usage error
160+
WriteBackError(w, "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.", http.StatusBadRequest)
161+
return
162+
}
139163
// Blacklist subscription routes
140164
if strings.HasPrefix(r.RequestURI, "/arc/subscription") || strings.HasPrefix(r.RequestURI, "/arc/plan") {
141165
next.ServeHTTP(w, r)
142166
} else if validateTimeValidity() {
143167
next.ServeHTTP(w, r)
144168
} else {
145169
// Write an error and stop the handler chain
146-
http.Error(w, "payment required", http.StatusPaymentRequired)
170+
WriteBackError(w, "Payment required", http.StatusPaymentRequired)
171+
return
147172
}
148173
})
149174
}
@@ -167,6 +192,9 @@ func getArcInstance(arcID string) (ArcInstance, error) {
167192
log.Errorln("error reading res body:", err)
168193
return arcInstance, err
169194
}
195+
// Validate the ACCAPI response
196+
ValidateArcID(res.StatusCode)
197+
170198
err = json.Unmarshal(body, &response)
171199
if len(response.ArcInstances) != 0 {
172200
arcInstanceByID := response.ArcInstances[0]
@@ -204,6 +232,9 @@ func getArcClusterInstance(clusterID string) (ArcInstance, error) {
204232
return arcInstance, err
205233
}
206234
defer res.Body.Close()
235+
// Validate the ACCAPI response
236+
ValidateArcID(res.StatusCode)
237+
207238
body, err := ioutil.ReadAll(res.Body)
208239
if err != nil {
209240
log.Errorln("error reading res body:", err)
@@ -222,6 +253,10 @@ func getArcClusterInstance(clusterID string) (ArcInstance, error) {
222253
SetTier(arcInstanceDetails.Tier)
223254
SetFeatureSuggestions(arcInstanceDetails.FeatureSuggestions)
224255
SetFeatureCustomEvents(arcInstanceDetails.FeatureCustomEvents)
256+
SetFeatureRules(arcInstanceDetails.FeatureRules)
257+
SetFeatureFunctions(arcInstanceDetails.FeatureFunctions)
258+
SetFeatureSearchSettings(arcInstanceDetails.FeatureSearchSettings)
259+
SetFeatureTemplates(arcInstanceDetails.FeatureTemplates)
225260
} else {
226261
return arcInstance, errors.New("No valid instance found for the provided CLUSTER_ID")
227262
}
@@ -248,6 +283,9 @@ func getClusterPlan(clusterID string) (ClusterPlan, error) {
248283
log.Errorln("error reading res body:", err)
249284
return clusterPlan, err
250285
}
286+
// Validate the ACCAPI response
287+
ValidateArcID(res.StatusCode)
288+
251289
err = json.Unmarshal(body, &response)
252290

253291
if err != nil {
@@ -263,6 +301,10 @@ func getClusterPlan(clusterID string) (ClusterPlan, error) {
263301
SetTimeValidity(response.Plan.TimeValidity)
264302
SetFeatureSuggestions(response.Plan.FeatureSuggestions)
265303
SetFeatureCustomEvents(response.Plan.FeatureCustomEvents)
304+
SetFeatureRules(response.Plan.FeatureRules)
305+
SetFeatureFunctions(response.Plan.FeatureFunctions)
306+
SetFeatureSearchSettings(response.Plan.FeatureSearchSettings)
307+
SetFeatureTemplates(response.Plan.FeatureTemplates)
266308

267309
return clusterPlan, nil
268310
}

0 commit comments

Comments
 (0)