From 36d8def0ef59dff589080576fb20abbe6e49e7fd Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sat, 3 Aug 2019 20:21:59 +0530 Subject: [PATCH 1/9] update accapi --- util/billing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/billing.go b/util/billing.go index abb9fce8..43f7541e 100644 --- a/util/billing.go +++ b/util/billing.go @@ -60,7 +60,7 @@ func BillingMiddleware(next http.Handler) http.Handler { func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) { response := ArcUsageResponse{} - url := "https://accapi.appbase.io/arc/" + arcUsage.ArcID + "/report_usage" + url := "http://localhost:3000/" + arcUsage.ArcID + "/report_usage" marshalledRequest, err := json.Marshal(arcUsage) if err != nil { log.Println("error while marshalling req body: ", err) From 300fd3315e8033b6941078896f951dc593d823e8 Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 00:55:42 +0530 Subject: [PATCH 2/9] fix: remove subscription id --- util/billing.go | 54 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/util/billing.go b/util/billing.go index 43f7541e..6ab2b927 100644 --- a/util/billing.go +++ b/util/billing.go @@ -15,6 +15,8 @@ import ( var TimeValidity int64 var MAX_ALLOWED_TIME int64 = 24 // in hrs +// var ACC_API = "https://accapi.appbase.io/" +var ACC_API = "http://localhost:3000/" type ArcUsage struct { ArcID string `json:"arc_id"` @@ -32,10 +34,13 @@ type ArcUsageResponse struct { TimeValidity int64 `json:"time_validity"` } +type ArcInstance struct { + SubscriptionID string `json:"subscription_id"` +} + const ( - envEsURL = "ES_CLUSTER_URL" - arcIdentifier = "ARC_ID" - subscriptionID = "SUBSCRIPTION_ID" + envEsURL = "ES_CLUSTER_URL" + arcIdentifier = "ARC_ID" ) // Middleware function, which will be called for each request @@ -58,9 +63,36 @@ func BillingMiddleware(next http.Handler) http.Handler { }) } +func getArcInstance(arcID string) (ArcInstance, error) { + response := ArcInstance{} + url := ACC_API + "arc/instance?arcid=" + arcID + req, _ := http.NewRequest("GET", url, nil) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("cache-control", "no-cache") + + res, err := http.DefaultClient.Do(req) + if err != nil { + log.Println("error while sending request: ", err) + return response, err + } + defer res.Body.Close() + body, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Println("error reading res body: ", err) + return response, err + } + err = json.Unmarshal(body, &response) + + if err != nil { + log.Println("error while unmarshalling res body: ", err) + return response, err + } + return response, nil +} + func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) { response := ArcUsageResponse{} - url := "http://localhost:3000/" + arcUsage.ArcID + "/report_usage" + url := ACC_API + "arc/" + arcUsage.ArcID + "/report_usage" marshalledRequest, err := json.Marshal(arcUsage) if err != nil { log.Println("error while marshalling req body: ", err) @@ -99,7 +131,13 @@ func ReportUsage() { if arcID == "" { log.Fatalln("ARC_ID not found") } - subID := os.Getenv(subscriptionID) + + result, err := getArcInstance(arcID) + if err != nil { + log.Println("Unable to fetch arc instance") + } + + subID := result.SubscriptionID if subID == "" { log.Println("SUBSCRIPTION_ID not found. Initializing in trial mode") } @@ -112,9 +150,9 @@ func ReportUsage() { usageBody.SubscriptionID = subID usageBody.Timestamp = time.Now().Unix() usageBody.Quantity = nodeCount - response, err := ReportUsageRequest(usageBody) - if err != nil { - log.Println("please contact support. Usage not getting reported: ", err) + response, err1 := ReportUsageRequest(usageBody) + if err1 != nil { + log.Println("please contact support. Usage not getting reported: ", err1) } TimeValidity = response.TimeValidity From 59da89b48f8c6aead72dcee87cb34152e4c6ec73 Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 01:10:18 +0530 Subject: [PATCH 3/9] fix: minor fixes --- util/billing.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/util/billing.go b/util/billing.go index 6ab2b927..19421961 100644 --- a/util/billing.go +++ b/util/billing.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io/ioutil" "log" "net/http" @@ -136,6 +137,7 @@ func ReportUsage() { if err != nil { log.Println("Unable to fetch arc instance") } + fmt.Println("THIS IS RESULT", result) subID := result.SubscriptionID if subID == "" { From 5a0f3c60cb11d292a1598c18c554a9c8d60e99be Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 01:14:34 +0530 Subject: [PATCH 4/9] add logs --- util/billing.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/util/billing.go b/util/billing.go index 19421961..4595cae6 100644 --- a/util/billing.go +++ b/util/billing.go @@ -72,17 +72,20 @@ func getArcInstance(arcID string) (ArcInstance, error) { req.Header.Add("cache-control", "no-cache") res, err := http.DefaultClient.Do(req) + fmt.Println("Requesting:", url) if err != nil { log.Println("error while sending request: ", err) return response, err } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) + fmt.Println("bODY:", body) if err != nil { log.Println("error reading res body: ", err) return response, err } err = json.Unmarshal(body, &response) + fmt.Println("RESPONSE:", response) if err != nil { log.Println("error while unmarshalling res body: ", err) From 704b0dc398f4d17ef55fbcd464d1184abe76a98e Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 01:20:57 +0530 Subject: [PATCH 5/9] fix: minor fixes --- util/billing.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/util/billing.go b/util/billing.go index 4595cae6..b48ba707 100644 --- a/util/billing.go +++ b/util/billing.go @@ -39,6 +39,20 @@ type ArcInstance struct { SubscriptionID string `json:"subscription_id"` } +type arcInstanceDetails struct { + NodeCount int64 `json:"node_count"` + Description string `json:"description"` + SubscriptionID string `json:"subscription_id"` + SubscriptionCanceled bool `json:"subscription_canceled"` + Trial bool `json:"trial"` + TrialValidity int64 `json:"trial_validity"` + ArcID string `json:"arc_id"` + CreatedAt int64 `json:"created_at"` + Tier string `json:"tier"` + TierValidity int64 `json:"tier_validity"` + Metadata map[string]interface{} `json:"metadata"` +} + const ( envEsURL = "ES_CLUSTER_URL" arcIdentifier = "ARC_ID" @@ -65,7 +79,8 @@ func BillingMiddleware(next http.Handler) http.Handler { } func getArcInstance(arcID string) (ArcInstance, error) { - response := ArcInstance{} + arcInstance := ArcInstance{} + response := []arcInstanceDetails{} url := ACC_API + "arc/instance?arcid=" + arcID req, _ := http.NewRequest("GET", url, nil) req.Header.Add("Content-Type", "application/json") @@ -75,23 +90,24 @@ func getArcInstance(arcID string) (ArcInstance, error) { fmt.Println("Requesting:", url) if err != nil { log.Println("error while sending request: ", err) - return response, err + return arcInstance, err } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) fmt.Println("bODY:", body) if err != nil { log.Println("error reading res body: ", err) - return response, err + return arcInstance, err } err = json.Unmarshal(body, &response) fmt.Println("RESPONSE:", response) + arcInstance.SubscriptionID = response[0].SubscriptionID if err != nil { log.Println("error while unmarshalling res body: ", err) - return response, err + return arcInstance, err } - return response, nil + return arcInstance, nil } func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) { From 17cf6a8f1929dad7cb3df29c35ce8e1751f2b2e0 Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 01:26:28 +0530 Subject: [PATCH 6/9] fix: update request --- util/billing.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/util/billing.go b/util/billing.go index b48ba707..7359d777 100644 --- a/util/billing.go +++ b/util/billing.go @@ -39,6 +39,10 @@ type ArcInstance struct { SubscriptionID string `json:"subscription_id"` } +type ArcInstanceResponse struct { + ArcRecords []arcInstanceDetails `json:"arc_records"` +} + type arcInstanceDetails struct { NodeCount int64 `json:"node_count"` Description string `json:"description"` @@ -80,7 +84,7 @@ func BillingMiddleware(next http.Handler) http.Handler { func getArcInstance(arcID string) (ArcInstance, error) { arcInstance := ArcInstance{} - response := []arcInstanceDetails{} + response := ArcInstanceResponse{} url := ACC_API + "arc/instance?arcid=" + arcID req, _ := http.NewRequest("GET", url, nil) req.Header.Add("Content-Type", "application/json") @@ -101,7 +105,7 @@ func getArcInstance(arcID string) (ArcInstance, error) { } err = json.Unmarshal(body, &response) fmt.Println("RESPONSE:", response) - arcInstance.SubscriptionID = response[0].SubscriptionID + arcInstance.SubscriptionID = response.ArcRecords[0].SubscriptionID if err != nil { log.Println("error while unmarshalling res body: ", err) From 6bb258b2d2348af15ffabd4d9388c0fd9ba017c2 Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 01:29:33 +0530 Subject: [PATCH 7/9] fix: remove subscription id --- util/billing.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/util/billing.go b/util/billing.go index 7359d777..bdb136b2 100644 --- a/util/billing.go +++ b/util/billing.go @@ -98,13 +98,11 @@ func getArcInstance(arcID string) (ArcInstance, error) { } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) - fmt.Println("bODY:", body) if err != nil { log.Println("error reading res body: ", err) return arcInstance, err } err = json.Unmarshal(body, &response) - fmt.Println("RESPONSE:", response) arcInstance.SubscriptionID = response.ArcRecords[0].SubscriptionID if err != nil { @@ -160,7 +158,6 @@ func ReportUsage() { if err != nil { log.Println("Unable to fetch arc instance") } - fmt.Println("THIS IS RESULT", result) subID := result.SubscriptionID if subID == "" { From c70a705cb48f121bee2ec95c07736c5fbc26ba8e Mon Sep 17 00:00:00 2001 From: kuldeep Date: Sun, 4 Aug 2019 02:03:24 +0530 Subject: [PATCH 8/9] fix: update URL --- util/billing.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/util/billing.go b/util/billing.go index bdb136b2..2815e469 100644 --- a/util/billing.go +++ b/util/billing.go @@ -16,8 +16,7 @@ import ( var TimeValidity int64 var MAX_ALLOWED_TIME int64 = 24 // in hrs -// var ACC_API = "https://accapi.appbase.io/" -var ACC_API = "http://localhost:3000/" +var ACC_API = "https://accapi.appbase.io/" type ArcUsage struct { ArcID string `json:"arc_id"` From 4941359d01b2cfc04ca4aa35688c73346d2e6015 Mon Sep 17 00:00:00 2001 From: Siddharth Kothari Date: Sun, 4 Aug 2019 02:42:48 +0530 Subject: [PATCH 9/9] fix: remove fmt.Println(..) log --- util/billing.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/util/billing.go b/util/billing.go index 2815e469..6c628fc1 100644 --- a/util/billing.go +++ b/util/billing.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io/ioutil" "log" "net/http" @@ -90,7 +89,6 @@ func getArcInstance(arcID string) (ArcInstance, error) { req.Header.Add("cache-control", "no-cache") res, err := http.DefaultClient.Do(req) - fmt.Println("Requesting:", url) if err != nil { log.Println("error while sending request: ", err) return arcInstance, err