Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions util/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

var TimeValidity int64
var MAX_ALLOWED_TIME int64 = 24 // in hrs
var ACC_API = "https://accapi.appbase.io/"

type ArcUsage struct {
ArcID string `json:"arc_id"`
Expand All @@ -32,10 +33,31 @@ type ArcUsageResponse struct {
TimeValidity int64 `json:"time_validity"`
}

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"`
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"
subscriptionID = "SUBSCRIPTION_ID"
envEsURL = "ES_CLUSTER_URL"
arcIdentifier = "ARC_ID"
)

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

func getArcInstance(arcID string) (ArcInstance, error) {
arcInstance := ArcInstance{}
response := ArcInstanceResponse{}
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 arcInstance, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println("error reading res body: ", err)
return arcInstance, err
}
err = json.Unmarshal(body, &response)
arcInstance.SubscriptionID = response.ArcRecords[0].SubscriptionID

if err != nil {
log.Println("error while unmarshalling res body: ", err)
return arcInstance, err
}
return arcInstance, nil
}

func ReportUsageRequest(arcUsage ArcUsage) (ArcUsageResponse, error) {
response := ArcUsageResponse{}
url := "https://accapi.appbase.io/arc/" + 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)
Expand Down Expand Up @@ -99,7 +150,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")
}
Expand All @@ -112,9 +169,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
Expand Down