Fix monitor data processing
Signed-off-by: Jingwen Peng <pengsrc@yunify.com>
This commit is contained in:
parent
e11223f056
commit
923cf83b10
|
@ -19,11 +19,8 @@ package request
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/yunify/qingcloud-sdk-go/logger"
|
||||
"github.com/yunify/qingcloud-sdk-go/request/data"
|
||||
|
@ -45,13 +42,6 @@ func (u *Unpacker) UnpackHTTPRequest(o *data.Operation, r *http.Response, x *ref
|
|||
u.httpResponse = r
|
||||
u.output = x
|
||||
|
||||
if strings.Contains(o.APIName, "Monitor") {
|
||||
err := u.preProcessMonitor(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err := u.parseResponse()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -65,37 +55,6 @@ func (u *Unpacker) UnpackHTTPRequest(o *data.Operation, r *http.Response, x *ref
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *Unpacker) preProcessMonitor(r *http.Response) error {
|
||||
buffer := bytes.Buffer{}
|
||||
buffer.ReadFrom(r.Body)
|
||||
r.Body.Close()
|
||||
|
||||
if !strings.Contains(buffer.String(), `"ret_code":0`) {
|
||||
r.Body = ioutil.NopCloser(bytes.NewReader(buffer.Bytes()))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
responseString := buffer.String()
|
||||
|
||||
for {
|
||||
dataString := regexp.MustCompile(`"data":\[\[[^}]+`).FindString(responseString)
|
||||
if dataString == "" {
|
||||
break
|
||||
}
|
||||
|
||||
dataValueString := regexp.MustCompile(`\[\[.+]`).FindString(dataString)
|
||||
dataValueStringAfter := "\"" + strings.Replace(dataValueString, "\"", "\\\"", -1) + "\""
|
||||
dataStringAfter := strings.Replace(dataString, dataValueString, dataValueStringAfter, -1)
|
||||
|
||||
responseString = strings.Replace(responseString, dataString, dataStringAfter, -1)
|
||||
}
|
||||
|
||||
r.Body = ioutil.NopCloser(bytes.NewReader([]byte(responseString)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Unpacker) parseResponse() error {
|
||||
if u.httpResponse.StatusCode == 200 {
|
||||
if u.httpResponse.Header.Get("Content-Type") == "application/json" {
|
||||
|
|
|
@ -1253,24 +1253,11 @@ func (v *LoadBalancerPolicyRule) Validate() error {
|
|||
}
|
||||
|
||||
type Meter struct {
|
||||
Data *string `json:"data" name:"data"`
|
||||
DataSet []*Data `json:"data_set" name:"data_set"`
|
||||
MeterID *string `json:"meter_id" name:"meter_id"`
|
||||
Sequence *int `json:"sequence" name:"sequence"`
|
||||
VxNetID *string `json:"vxnet_id" name:"vxnet_id"`
|
||||
}
|
||||
|
||||
func (v *Meter) Validate() error {
|
||||
|
||||
if len(v.DataSet) > 0 {
|
||||
for _, property := range v.DataSet {
|
||||
if err := property.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
Data interface{} `json:"data" name:"data"`
|
||||
DataSet []interface{} `json:"data_set" name:"data_set"`
|
||||
MeterID *string `json:"meter_id" name:"meter_id"`
|
||||
Sequence *int `json:"sequence" name:"sequence"`
|
||||
VxNetID *string `json:"vxnet_id" name:"vxnet_id"`
|
||||
}
|
||||
|
||||
type Mongo struct {
|
||||
|
|
Loading…
Reference in New Issue