Hi,
Line line 147 of register.go is status code check in the second for loop
but now solved.
if res.StatusCode != http.StatusNoContent {
But i still could not find out why the SendNFInstanceRegistration() message
is send twice even though i have check
the responses here
fmt.Print("status:", status)
if status == http.StatusOK {
logrus.Println("PCF Profile Update SUCCESS")
} else if status == http.StatusCreated {
logrus.Println("PCF Profile Registration SUCCESS")
} else {
logrus.Println(fmt.Errorf("Wrong status code returned by nrf %d", status))
}
Response codes:
status:201INFO[0000] PCF Profile Registration SUCCESS
status:200INFO[0000] PCF Profile Update SUCCESS
I have check all errors and this is the final code but the problem now is
the PUT message being sent twice.
Could you help find out what am doing wrong
func SendNFInstanceRegistration() (int32, string, *http.Response, error) {
var (
profile Profile
err error
)
profilebytes, err := ioutil.ReadFile("./profile.json")
if err != nil {
logrus.Println("Cannot read json file")
}
// unmarshall it
err = json.Unmarshal(profilebytes, &profile)
if err != nil {
logrus.Errorf("Read File profile Unmarshal failed %s", err.Error())
}
profile.NfInstanceId = contx.ID
id := contx.ID
locProfilebytes, err := json.Marshal(profile)
if err != nil {
logrus.Error(err)
}
locVarNRFUrl := NewConfig.BasePath + "/nf-instances/" + id
htbt, contentLoc, resp, err :=
HandleNFInstanceRegistration(locProfilebytes, locVarUrl)
if err != nil {
logrus.Error("could not register profile")
return htbt, contentLoc, resp, err
}
if resp == nil {
logrus.Error(err)
return htbt, contentLoc, resp, err
}
status := resp.StatusCode
fmt.Print("status:", status)
if status == http.StatusOK {
logrus.Println("Profile Update SUCCESS")
} else if status == http.StatusCreated {
logrus.Println("Profile Registration SUCCESS")
} else {
logrus.Println(fmt.Errorf("Wrong status code returned %d", status))
}
heartBeatTimer := htbt
return heartBeatTimer, contentLoc, resp, nil
}
func HandleNFInstanceRegistration(nfprofilebyte []byte, VarPath string)
(int32, string, *http.Response, error) {
var (
// Set client and set url
localVarHTTPMethod = http.MethodPut
nfprofile models.NfProfile
heartBeatTimer int32
contentloc string
)
req, err := http.NewRequest(localVarHTTPMethod, VarPath,
bytes.NewBuffer(nfprofilebyte))
if err != nil {
logrus.Error(err)
}
req.Close = true
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
backoff := 1
for {
res, err := transport.Client.Do(req) //Use for dev
if err != nil || res == nil {
logrus.Println("trying to register profile ...")
backoff *= 2
if backoff > 20 {
backoff = 20
}
time.Sleep(time.Duration(backoff) * time.Second)
continue
}
if res == nil {
logrus.Errorf("Registration failed %s", err.Error())
return heartBeatTimer, contentloc, res, err
}
defer func() {
if resCloseErr := res.Body.Close(); resCloseErr != nil {
logrus.Errorf("RegisterNFInstance response body cannot
close: %+v", resCloseErr)
}
}()
bodybytes, err := ioutil.ReadAll(res.Body)
if err != nil {
logrus.Error(err)
return heartBeatTimer, contentloc, res, err
}
err = json.Unmarshal(bodybytes, &nfprofile)
if err != nil {
logrus.Errorf("Profile Unmarshal failed %s", err.Error())
return heartBeatTimer, contentloc, res, err
}
heartBeatTimer = nfprofile.HeartBeatTimer
contentloc := res.Header.Get("Location")
return heartBeatTimer, contentloc, res, nil
}
}
func SendHeartbeat(ContentLocation string) (response *http.Response, err
error) {
// Preapare Heart-Beat message
patchitem := []models.PatchItem{
models.PatchItem{
Op: "replace",
Path: "/load",
From: ContentLocation,
Value: "REG",
}
}
patchitembytes, err := json.Marshal(patchitem)
if err != nil {
logrus.Error(err)
}
req, err := http.NewRequest("PATCH", ContentLocation,
bytes.NewBuffer(patchitembytes))
req.Header.Set("Content-Type", "application/json-patch+json")
response, err = transport.Client.Do(req) // for dev
if err != nil {
logrus.Debug("Heart-Beat Request FAILED")
return response, err
}
defer response.Body.Close()
status := response.StatusCode
if status == http.StatusNoContent {
logrus.Info("Heart-Beat Message SUCCESS")
} else if status == http.StatusNotFound {
logrus.Println("Heart-Beat Message FAILED")
}
return response, err
}
func PutAndPatch() {
var (
interval int32
resp *http.Response
cl string
err error
)
for {
for {
interval, cl, resp, err = SendNFInstanceRegistration()
if err != nil {
logrus.Error(err)
return
}
status := resp.StatusCode
if status == http.StatusCreated || status == http.StatusOK {
break
}
}
ticker := time.NewTicker(time.Duration(interval) * time.Second)
for range ticker.C {
res, err := SendHeartbeat(cl)
if err != nil {
logrus.Error("Heat-Beat Message FAILED")
break
}
if res.StatusCode != http.StatusNoContent {
break
}
}
}
}
On Sunday, March 21, 2021 at 1:47:07 PM UTC+2 Brian Candler wrote:
> On Sunday, 21 March 2021 at 11:42:37 UTC Brian Candler wrote:
>
>> Also: somebody has to be responsible for closing the response. You're
>> not explicitly returning an error from SendNFInstanceRegistration() to
>> HandleNFInstanceRegistration(), so the only way you can indicate that the
>> body is invalid is to return nil as the response pointer.
>>
>>
> Sorry, I got that the wrong way round. SendNFInstanceRegistration calls
> HandleNFInstanceRegistration, and you do return an error there.
>
> However, PutAndPatch calls SendNFInstanceRegistration, and you don't
> return an error. The caller then checks resp.StatusCode, but doesn't
> account for the fact that there might have been some error, and so resp is
> nil.
>
> I would suggest that instead of returning a resp, you return bool "ok"
> which just says whether the request was successful or not.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/70f4feb3-8b0f-4b87-944d-4d59ef2799acn%40googlegroups.com.