From 1a800c99c71e313702ecff9fdfd3e04be905c728 Mon Sep 17 00:00:00 2001 From: zhaoxiangchun Date: Thu, 7 May 2020 10:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=B7=E6=AD=8C=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 直接返回调用RestAPI的json格式数据 --- pkg/multicloud/google/google.go | 8 ++++---- pkg/multicloud/google/monitor.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/multicloud/google/google.go b/pkg/multicloud/google/google.go index 22e515c5f0..e2753bde39 100644 --- a/pkg/multicloud/google/google.go +++ b/pkg/multicloud/google/google.go @@ -652,7 +652,7 @@ func (self *SGoogleClient) monitorList(resource string, params map[string]string return jsonRequest(self.client, "GET", GOOGLE_MONITOR_DOMAIN, GOOGLE_MONITOR_API_VERSION, resource, params, nil, self.debug) } -func (self *SGoogleClient) monitorListAll(resource string, params map[string]string, retval interface{}) error { +func (self *SGoogleClient) monitorListAll(resource string, params map[string]string) (*jsonutils.JSONArray, error) { if params == nil { params = map[string]string{} } @@ -663,12 +663,12 @@ func (self *SGoogleClient) monitorListAll(resource string, params map[string]str params["pageToken"] = nextPageToken resp, err := self.monitorList(resource, params) if err != nil { - return errors.Wrap(err, "monitorList") + return nil, errors.Wrap(err, "monitorList") } if resp.Contains("timeSeries") { _series, err := resp.GetArray("timeSeries") if err != nil { - return errors.Wrap(err, "resp.GetArray") + return nil, errors.Wrap(err, "resp.GetTimeSeries") } timeSeries.Add(_series...) } @@ -677,7 +677,7 @@ func (self *SGoogleClient) monitorListAll(resource string, params map[string]str break } } - return timeSeries.Unmarshal(retval) + return timeSeries, nil } func rawRequest(client *http.Client, method httputils.THttpMethod, domain, apiVersion string, resource string, header http.Header, body io.Reader, debug bool) (*http.Response, error) { diff --git a/pkg/multicloud/google/monitor.go b/pkg/multicloud/google/monitor.go index 1b3084f5f3..ec1518f361 100644 --- a/pkg/multicloud/google/monitor.go +++ b/pkg/multicloud/google/monitor.go @@ -19,12 +19,13 @@ import ( "strconv" "time" - monitoring "cloud.google.com/go/monitoring/apiv3" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" + + "yunion.io/x/jsonutils" ) func (region *SRegion) GetMonitorData(id, serverName, metricName string, since time.Time, - until time.Time) (*monitoring.TimeSeriesIterator, error) { + until time.Time) (*jsonutils.JSONArray, error) { params := map[string]string{ "filter": `metric.type="` + metricName + `" AND metric.labels.instance_name="` + serverName + `"`, //"filter": "metric.type=" + metricName + " AND metric.labels.instance_name=" + serverName, @@ -33,11 +34,10 @@ func (region *SRegion) GetMonitorData(id, serverName, metricName string, since t "view": strconv.FormatInt(int64(monitoringpb.ListTimeSeriesRequest_FULL), 10), } resource := fmt.Sprintf("%s/%s/%s", "projects", id, "timeSeries") - //client, _ := monitoring.NewMetricClient(context.Background()) - rtn := monitoring.TimeSeriesIterator{} - err := region.client.monitorListAll(resource, params, &rtn) + + timeSeries, err := region.client.monitorListAll(resource, params) if err != nil { return nil, err } - return &rtn, nil + return timeSeries, nil }