@@ -15,6 +15,8 @@ import (
1515
1616var TimeValidity int64
1717var MAX_ALLOWED_TIME int64 = 24 // in hrs
18+ // var ACC_API = "https://accapi.appbase.io/"
19+ var ACC_API = "http://localhost:3000/"
1820
1921type ArcUsage struct {
2022 ArcID string `json:"arc_id"`
@@ -32,10 +34,13 @@ type ArcUsageResponse struct {
3234 TimeValidity int64 `json:"time_validity"`
3335}
3436
37+ type ArcInstance struct {
38+ SubscriptionID string `json:"subscription_id"`
39+ }
40+
3541const (
36- envEsURL = "ES_CLUSTER_URL"
37- arcIdentifier = "ARC_ID"
38- subscriptionID = "SUBSCRIPTION_ID"
42+ envEsURL = "ES_CLUSTER_URL"
43+ arcIdentifier = "ARC_ID"
3944)
4045
4146// Middleware function, which will be called for each request
@@ -58,9 +63,36 @@ func BillingMiddleware(next http.Handler) http.Handler {
5863 })
5964}
6065
66+ func getArcInstance (arcID string ) (ArcInstance , error ) {
67+ response := ArcInstance {}
68+ url := ACC_API + "arc/instance?arcid=" + arcID
69+ req , _ := http .NewRequest ("GET" , url , nil )
70+ req .Header .Add ("Content-Type" , "application/json" )
71+ req .Header .Add ("cache-control" , "no-cache" )
72+
73+ res , err := http .DefaultClient .Do (req )
74+ if err != nil {
75+ log .Println ("error while sending request: " , err )
76+ return response , err
77+ }
78+ defer res .Body .Close ()
79+ body , err := ioutil .ReadAll (res .Body )
80+ if err != nil {
81+ log .Println ("error reading res body: " , err )
82+ return response , err
83+ }
84+ err = json .Unmarshal (body , & response )
85+
86+ if err != nil {
87+ log .Println ("error while unmarshalling res body: " , err )
88+ return response , err
89+ }
90+ return response , nil
91+ }
92+
6193func ReportUsageRequest (arcUsage ArcUsage ) (ArcUsageResponse , error ) {
6294 response := ArcUsageResponse {}
63- url := "http://localhost:3000 /" + arcUsage .ArcID + "/report_usage"
95+ url := ACC_API + "arc /" + arcUsage .ArcID + "/report_usage"
6496 marshalledRequest , err := json .Marshal (arcUsage )
6597 if err != nil {
6698 log .Println ("error while marshalling req body: " , err )
@@ -99,7 +131,13 @@ func ReportUsage() {
99131 if arcID == "" {
100132 log .Fatalln ("ARC_ID not found" )
101133 }
102- subID := os .Getenv (subscriptionID )
134+
135+ result , err := getArcInstance (arcID )
136+ if err != nil {
137+ log .Println ("Unable to fetch arc instance" )
138+ }
139+
140+ subID := result .SubscriptionID
103141 if subID == "" {
104142 log .Println ("SUBSCRIPTION_ID not found. Initializing in trial mode" )
105143 }
@@ -112,9 +150,9 @@ func ReportUsage() {
112150 usageBody .SubscriptionID = subID
113151 usageBody .Timestamp = time .Now ().Unix ()
114152 usageBody .Quantity = nodeCount
115- response , err := ReportUsageRequest (usageBody )
116- if err != nil {
117- log .Println ("please contact support. Usage not getting reported: " , err )
153+ response , err1 := ReportUsageRequest (usageBody )
154+ if err1 != nil {
155+ log .Println ("please contact support. Usage not getting reported: " , err1 )
118156 }
119157
120158 TimeValidity = response .TimeValidity
0 commit comments