yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/traces.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 huawei
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  	"yunion.io/x/pkg/errors"
    24  
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  )
    27  
    28  type SUser struct {
    29  	Domain map[string]string
    30  	Name   string
    31  	Id     string
    32  }
    33  
    34  type SEvent struct {
    35  	TraceId      string
    36  	Code         string
    37  	TraceName    string
    38  	ResourceType string
    39  	ApiVersion   string
    40  	SourceIp     string
    41  	TraceType    string
    42  	ServiceType  string
    43  	EventType    string
    44  	ProjectId    string
    45  	Request      string
    46  	Response     string
    47  	TrackerName  string
    48  	TraceStatus  string
    49  	Time         int64
    50  	ResourceId   string
    51  	ResourceName string
    52  	User         SUser
    53  	RecordTime   int64
    54  }
    55  
    56  func (event *SEvent) GetName() string {
    57  	if len(event.ResourceName) > 0 {
    58  		return event.ResourceName
    59  	}
    60  	if len(event.ResourceId) > 0 {
    61  		return event.ResourceId
    62  	}
    63  	return event.TraceName
    64  }
    65  
    66  func (event *SEvent) GetService() string {
    67  	return event.ServiceType
    68  }
    69  
    70  func (event *SEvent) GetAction() string {
    71  	return event.TraceName
    72  }
    73  
    74  func (event *SEvent) GetResourceType() string {
    75  	return event.ResourceType
    76  }
    77  
    78  func (event *SEvent) GetRequestId() string {
    79  	return event.TraceId
    80  }
    81  
    82  func (event *SEvent) GetRequest() jsonutils.JSONObject {
    83  	return jsonutils.Marshal(event)
    84  }
    85  
    86  func (event *SEvent) GetAccount() string {
    87  	return event.User.Name
    88  }
    89  
    90  func (event *SEvent) IsSuccess() bool {
    91  	code, _ := strconv.Atoi(event.Code)
    92  	return code < 400
    93  }
    94  
    95  func (event *SEvent) GetCreatedAt() time.Time {
    96  	return time.Unix(event.Time/1000, event.Time%1000)
    97  }
    98  
    99  func (self *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) {
   100  	if !self.client.isMainProject {
   101  		return nil, cloudprovider.ErrNotSupported
   102  	}
   103  	events, err := self.GetEvents(start, end)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	iEvents := []cloudprovider.ICloudEvent{}
   108  	for i := range events {
   109  		iEvents = append(iEvents, &events[i])
   110  	}
   111  	return iEvents, nil
   112  }
   113  
   114  func (self *SRegion) GetEvents(start time.Time, end time.Time) ([]SEvent, error) {
   115  	events := []SEvent{}
   116  	params := map[string]string{}
   117  	if start.IsZero() {
   118  		start = time.Now().AddDate(0, 0, -7)
   119  	}
   120  	if end.IsZero() {
   121  		end = time.Now()
   122  	}
   123  	params["from"] = fmt.Sprintf("%d000", start.Unix())
   124  	params["to"] = fmt.Sprintf("%d000", end.Unix())
   125  
   126  	err := doListAllWithMarker(self.ecsClient.Traces.List, params, &events)
   127  	if err != nil {
   128  		return nil, errors.Wrap(err, "doListAllWithMarker")
   129  	}
   130  	return events, nil
   131  }