yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/client/modules/mod_ces.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package modules
    16  
    17  import (
    18  	"time"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/onecloud/pkg/httperrors"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/manager"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/hcso/client/requests"
    26  	hw_mod "yunion.io/x/cloudmux/pkg/multicloud/huawei/client/modules"
    27  )
    28  
    29  type SCloudEyeManager struct {
    30  	SResourceManager
    31  }
    32  
    33  func NewCloudEyeManager(cfg manager.IManagerConfig) *SCloudEyeManager {
    34  	return &SCloudEyeManager{SResourceManager: SResourceManager{
    35  		SBaseManager:    NewBaseManager(cfg),
    36  		ServiceName:     ServiceNameCES,
    37  		Region:          cfg.GetRegionId(),
    38  		ProjectId:       cfg.GetProjectId(),
    39  		version:         "V1.0",
    40  		Keyword:         "",
    41  		KeywordPlural:   "metrics",
    42  		ResourceKeyword: "metrics",
    43  	}}
    44  }
    45  
    46  func (ces *SCloudEyeManager) ListMetrics() ([]hw_mod.SMetricMeta, error) {
    47  	metrics := make([]hw_mod.SMetricMeta, 0)
    48  	next := ""
    49  	for {
    50  		marker, data, err := ces.listMetricsInternal(next)
    51  		if err != nil {
    52  			return nil, errors.Wrap(err, "ces.listMetricsInternal")
    53  		}
    54  		if len(data) == 0 {
    55  			break
    56  		}
    57  		metrics = append(metrics, data...)
    58  		next = marker
    59  	}
    60  	return metrics, nil
    61  }
    62  
    63  func (ces *SCloudEyeManager) listMetricsInternal(start string) (string, []hw_mod.SMetricMeta, error) {
    64  	request := requests.NewResourceRequest(ces.GetEndpoint(), "GET", string(ces.ServiceName), ces.version, ces.Region, ces.ProjectId, ces.ResourceKeyword)
    65  	request.AddQueryParam("limit", "1000")
    66  	if len(start) > 0 {
    67  		request.AddQueryParam("start", start)
    68  	}
    69  	_, resp, err := ces.jsonRequest(request)
    70  	if err != nil {
    71  		return "", nil, errors.Wrap(err, "ces.jsonRequest")
    72  	}
    73  	marker, _ := resp.GetString("meta_data", "marker")
    74  	metrics := make([]hw_mod.SMetricMeta, 0)
    75  	err = resp.Unmarshal(&metrics, "metrics")
    76  	if err != nil {
    77  		return "", nil, errors.Wrap(err, "resp.Unmarshal metrics")
    78  	}
    79  	return marker, metrics, nil
    80  }
    81  
    82  type SBatchQueryMetricDataInput struct {
    83  	Metrics []hw_mod.SMetric `json:"metrics"`
    84  
    85  	From   int64  `json:"from"`
    86  	To     int64  `json:"to"`
    87  	Period string `json:"period"`
    88  	Filter string `json:"filter"`
    89  }
    90  
    91  func (ces *SCloudEyeManager) GetMetricsData(metrics []hw_mod.SMetricMeta, since time.Time, until time.Time) ([]hw_mod.SMetricData, error) {
    92  	if len(metrics) > 10 {
    93  		return nil, errors.Wrap(httperrors.ErrTooLarge, "request more than 10 metrics")
    94  	}
    95  	metricReq := make([]hw_mod.SMetric, len(metrics))
    96  	for i := range metrics {
    97  		metricReq[i] = metrics[i].SMetric
    98  	}
    99  	request := requests.NewResourceRequest(ces.GetEndpoint(), "POST", string(ces.ServiceName), ces.version, ces.Region, ces.ProjectId, "batch-query-metric-data")
   100  	input := SBatchQueryMetricDataInput{
   101  		Metrics: metricReq,
   102  		From:    since.Unix() * 1000,
   103  		To:      until.Unix() * 1000,
   104  		Period:  "1",
   105  		Filter:  "average",
   106  	}
   107  	body := jsonutils.Marshal(&input).String()
   108  	request.SetContent([]byte(body))
   109  	_, resp, err := ces.jsonRequest(request)
   110  	if err != nil {
   111  		return nil, errors.Wrap(err, "ces.jsonRequest")
   112  	}
   113  	//log.Debugf("%s", resp)
   114  	result := make([]hw_mod.SMetricData, 0)
   115  	err = resp.Unmarshal(&result, "metrics")
   116  	if err != nil {
   117  		return nil, errors.Wrap(err, "resp.Unmarshal")
   118  	}
   119  	return result, nil
   120  }