Skip to content

Commit 6924bf5

Browse files
Merge pull request #15 from appbaseio/test/billing
fix: remove subscription_id
2 parents d7692f1 + 4941359 commit 6924bf5

1 file changed

Lines changed: 65 additions & 8 deletions

File tree

util/billing.go

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
var TimeValidity int64
1717
var MAX_ALLOWED_TIME int64 = 24 // in hrs
18+
var ACC_API = "https://accapi.appbase.io/"
1819

1920
type ArcUsage struct {
2021
ArcID string `json:"arc_id"`
@@ -32,10 +33,31 @@ type ArcUsageResponse struct {
3233
TimeValidity int64 `json:"time_validity"`
3334
}
3435

36+
type ArcInstance struct {
37+
SubscriptionID string `json:"subscription_id"`
38+
}
39+
40+
type ArcInstanceResponse struct {
41+
ArcRecords []arcInstanceDetails `json:"arc_records"`
42+
}
43+
44+
type arcInstanceDetails struct {
45+
NodeCount int64 `json:"node_count"`
46+
Description string `json:"description"`
47+
SubscriptionID string `json:"subscription_id"`
48+
SubscriptionCanceled bool `json:"subscription_canceled"`
49+
Trial bool `json:"trial"`
50+
TrialValidity int64 `json:"trial_validity"`
51+
ArcID string `json:"arc_id"`
52+
CreatedAt int64 `json:"created_at"`
53+
Tier string `json:"tier"`
54+
TierValidity int64 `json:"tier_validity"`
55+
Metadata map[string]interface{} `json:"metadata"`
56+
}
57+
3558
const (
36-
envEsURL = "ES_CLUSTER_URL"
37-
arcIdentifier = "ARC_ID"
38-
subscriptionID = "SUBSCRIPTION_ID"
59+
envEsURL = "ES_CLUSTER_URL"
60+
arcIdentifier = "ARC_ID"
3961
)
4062

4163
// Middleware function, which will be called for each request
@@ -58,9 +80,38 @@ func BillingMiddleware(next http.Handler) http.Handler {
5880
})
5981
}
6082

83+
func getArcInstance(arcID string) (ArcInstance, error) {
84+
arcInstance := ArcInstance{}
85+
response := ArcInstanceResponse{}
86+
url := ACC_API + "arc/instance?arcid=" + arcID
87+
req, _ := http.NewRequest("GET", url, nil)
88+
req.Header.Add("Content-Type", "application/json")
89+
req.Header.Add("cache-control", "no-cache")
90+
91+
res, err := http.DefaultClient.Do(req)
92+
if err != nil {
93+
log.Println("error while sending request: ", err)
94+
return arcInstance, err
95+
}
96+
defer res.Body.Close()
97+
body, err := ioutil.ReadAll(res.Body)
98+
if err != nil {
99+
log.Println("error reading res body: ", err)
100+
return arcInstance, err
101+
}
102+
err = json.Unmarshal(body, &response)
103+
arcInstance.SubscriptionID = response.ArcRecords[0].SubscriptionID
104+
105+
if err != nil {
106+
log.Println("error while unmarshalling res body: ", err)
107+
return arcInstance, err
108+
}
109+
return arcInstance, nil
110+
}
111+
61112
func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) {
62113
response := ArcUsageResponse{}
63-
url := "https://accapi.appbase.io/arc/" + arcUsage.ArcID + "/report_usage"
114+
url := ACC_API + "arc/" + arcUsage.ArcID + "/report_usage"
64115
marshalledRequest, err := json.Marshal(arcUsage)
65116
if err != nil {
66117
log.Println("error while marshalling req body: ", err)
@@ -99,7 +150,13 @@ func ReportUsage() {
99150
if arcID == "" {
100151
log.Fatalln("ARC_ID not found")
101152
}
102-
subID := os.Getenv(subscriptionID)
153+
154+
result, err := getArcInstance(arcID)
155+
if err != nil {
156+
log.Println("Unable to fetch arc instance")
157+
}
158+
159+
subID := result.SubscriptionID
103160
if subID == "" {
104161
log.Println("SUBSCRIPTION_ID not found. Initializing in trial mode")
105162
}
@@ -112,9 +169,9 @@ func ReportUsage() {
112169
usageBody.SubscriptionID = subID
113170
usageBody.Timestamp = time.Now().Unix()
114171
usageBody.Quantity = nodeCount
115-
response, err := ReportUsageRequest(usageBody)
116-
if err != nil {
117-
log.Println("please contact support. Usage not getting reported: ", err)
172+
response, err1 := ReportUsageRequest(usageBody)
173+
if err1 != nil {
174+
log.Println("please contact support. Usage not getting reported: ", err1)
118175
}
119176

120177
TimeValidity = response.TimeValidity

0 commit comments

Comments
 (0)