From d85725b45a91c76041039a49567408ff4d23afc1 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 10 Nov 2019 12:57:35 +0000 Subject: [PATCH] Fixed time setting for udr posting; added product put to custoemrdb --- customerdb/client.go | 48 ++++++++++++++++++++++++++++++++++++++++---- udr/udr.go | 11 ++++++++-- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/customerdb/client.go b/customerdb/client.go index 2f75bf9..cf62966 100644 --- a/customerdb/client.go +++ b/customerdb/client.go @@ -11,10 +11,12 @@ package customerdbclient import ( "bytes" "encoding/json" - l "gitlab.com/cyclops-utilities/logging" - "gitlab.com/cyclops-utilities/types" + "errors" "io/ioutil" "net/http" + + l "gitlab.com/cyclops-utilities/logging" + "gitlab.com/cyclops-utilities/types" ) // CustomerDBService contains the endpoint where the customer db service lives... @@ -28,7 +30,7 @@ type CustomerDBService struct { // correctly. func (s *CustomerDBService) CreateReseller(r types.Reseller) (success bool, err error) { val, _ := json.Marshal(r) - // fmt.Printf("r= %+v\n", string(val)) + l.Debug.Printf("r= %+v\n", string(val)) newCustomerEndpoint := s.Endpoint + "/reseller/" req, err := http.NewRequest("POST", newCustomerEndpoint, bytes.NewBuffer(val)) req.Header.Set("Content-Type", "application/json") @@ -47,7 +49,7 @@ func (s *CustomerDBService) CreateReseller(r types.Reseller) (success bool, err defer resp.Body.Close() if err == nil { - if resp.StatusCode != http.StatusCreated { + if (resp.StatusCode != http.StatusCreated) && (resp.StatusCode != http.StatusAccepted) { l.Warning.Printf("Unexpected response Creating new Reseller record - Response Status: %v\n", resp.Status) body, _ := ioutil.ReadAll(resp.Body) l.Debug.Printf("Response Body: %v\n", string(body)) @@ -62,3 +64,41 @@ func (s *CustomerDBService) CreateReseller(r types.Reseller) (success bool, err } return } + +// PutProduct puts a product under a given reseller - it takses as input the product +// which includes the customer id and posts it into the customer entry in the customerdb +// accordingly +func (s *CustomerDBService) PutProduct(p types.Product) (err error) { + val, _ := json.Marshal(p) + l.Debug.Printf("r= %+v\n", string(val)) + productEndpoint := s.Endpoint + "/product/" + p.ProductId + req, err := http.NewRequest("PUT", productEndpoint, bytes.NewBuffer(val)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + //panic(err) + l.Error.Printf("error = %v\n", err.Error()) + return + } + + //TODO(murp) - fixme...) + defer resp.Body.Close() + + if err == nil { + if (resp.StatusCode != http.StatusCreated) && (resp.StatusCode != http.StatusAccepted) { + l.Warning.Printf("Unexpected response Creating new Reseller record - Response Status: %v\n", resp.Status) + body, _ := ioutil.ReadAll(resp.Body) + l.Debug.Printf("Response Body: %v\n", string(body)) + err = errors.New("Unexpected response posting product to customerdb") + } else { + // this is the successful case - only dump output if in debug mode as this + // should be the default... + body, _ := ioutil.ReadAll(resp.Body) + l.Debug.Printf("Sending Cyclops Customer DB service - Response Status: %v, Response Body: %v\n", resp.Status, string(body)) + } + } + return +} diff --git a/udr/udr.go b/udr/udr.go index d514818..1ce3704 100644 --- a/udr/udr.go +++ b/udr/udr.go @@ -3,9 +3,11 @@ package udrclient import ( "bytes" "encoding/json" - l "gitlab.com/cyclops-utilities/logging" "io/ioutil" "net/http" + "time" + + l "gitlab.com/cyclops-utilities/logging" ) // The UDRRecord is reasonably self-explanaroy - the one point that is a little @@ -42,6 +44,7 @@ func (u *UDRService) PostRecords(records []UDRRecord) (success bool, recordsWrit recordsWritten = 0 for _, r := range records { success, err = u.PostRecord(r) + // TODO(murp) - check if this works if !success { return } @@ -57,6 +60,10 @@ func (u *UDRService) PostRecord(r UDRRecord) (success bool, err error) { req, err := http.NewRequest("POST", u.Endpoint, bytes.NewBuffer(val)) req.Header.Set("Content-Type", "application/json") + if r.Time == 0 { + r.Time = time.Now().UnixNano() / int64(time.Millisecond) + } + client := &http.Client{} resp, err := client.Do(req) @@ -71,7 +78,7 @@ func (u *UDRService) PostRecord(r UDRRecord) (success bool, err error) { if err == nil { if resp.StatusCode != http.StatusCreated { - l.Warning.Printf("Unexpected response POSTing to UDR - Response Status: %v\n", resp.Status) + l.Warning.Printf("Unexpected response POSTing to UDR - Usage record: %v, Response Status: %v\n", r, resp.Status) body, _ := ioutil.ReadAll(resp.Body) l.Debug.Printf("Response Body: %v\n", string(body)) success = false