events.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. package aliyun
  15. import (
  16. "strings"
  17. "time"
  18. "github.com/pkg/errors"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/utils"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. )
  23. const (
  24. EVENT_REGION_HANGZHOU = "cn-hangzhou"
  25. )
  26. type SAttributes struct {
  27. CreationDate time.Time
  28. MfaAuthenticated bool
  29. }
  30. type SSessionContext struct {
  31. Attributes SAttributes
  32. }
  33. type SUserIdentity struct {
  34. AccessKeyId string
  35. AccountId string
  36. PrincipalId string
  37. SessionContext SSessionContext
  38. Type string
  39. UserName string
  40. }
  41. type SEvent struct {
  42. region *SRegion
  43. AdditionalEventData map[string]string
  44. ApiVersion string
  45. EventId string
  46. EventName string
  47. EventSource string
  48. EventTime time.Time
  49. EventType string
  50. EventVersion string
  51. RequestId string
  52. RequestParameters map[string]string
  53. ServiceName string
  54. SourceIpAddress string
  55. UserAgent string
  56. UserIdentity SUserIdentity
  57. ResponseElements map[string]string
  58. IsGlobal bool
  59. }
  60. func (event *SEvent) GetCreatedAt() time.Time {
  61. return event.EventTime
  62. }
  63. func (event *SEvent) GetName() string {
  64. return event.EventName
  65. }
  66. func (event *SEvent) GetAction() string {
  67. return event.ServiceName
  68. }
  69. func (event *SEvent) GetResourceType() string {
  70. return ""
  71. }
  72. func (event *SEvent) GetRequestId() string {
  73. return event.RequestId
  74. }
  75. func (event *SEvent) GetRequest() jsonutils.JSONObject {
  76. return jsonutils.Marshal(event)
  77. }
  78. func (event *SEvent) GetAccount() string {
  79. if account, ok := event.AdditionalEventData["loginAccount"]; ok {
  80. return account
  81. }
  82. if len(event.UserIdentity.AccessKeyId) > 0 {
  83. return event.UserIdentity.AccessKeyId
  84. }
  85. return event.UserIdentity.UserName
  86. }
  87. func (event *SEvent) GetService() string {
  88. return event.ServiceName
  89. }
  90. func (event *SEvent) IsSuccess() bool {
  91. return true
  92. }
  93. func (region *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) {
  94. var (
  95. events []SEvent
  96. err error
  97. token string
  98. _events []SEvent
  99. iEvents []cloudprovider.ICloudEvent
  100. eventRW string
  101. )
  102. eventRW = "Write"
  103. if withReadEvent {
  104. eventRW = ""
  105. }
  106. for {
  107. _events, token, err = region.GetEvents(start, end, token, eventRW, "")
  108. if err != nil {
  109. return nil, errors.Wrap(err, "region.GetEvents")
  110. }
  111. events = append(events, _events...)
  112. if len(token) == 0 || len(_events) == 0 {
  113. break
  114. }
  115. }
  116. for i := range events {
  117. if events[i].IsGlobal && region.RegionId != ALIYUN_DEFAULT_REGION {
  118. continue
  119. }
  120. if !withReadEvent && strings.HasPrefix(events[i].EventName, "Query") { // QueryInstanceBill QueryBill
  121. continue
  122. }
  123. iEvents = append(iEvents, &events[i])
  124. }
  125. return iEvents, nil
  126. }
  127. func (region *SRegion) GetEvents(start time.Time, end time.Time, token string, eventRW string, requestId string) ([]SEvent, string, error) {
  128. params := map[string]string{
  129. "RegionId": region.RegionId,
  130. }
  131. if !start.IsZero() {
  132. params["StartTime"] = start.Format("2006-01-02T15:04:05Z")
  133. }
  134. if !end.IsZero() {
  135. params["EndTime"] = end.Format("2006-01-02T15:04:05Z")
  136. }
  137. if utils.IsInStringArray(eventRW, []string{"Read", "Write"}) {
  138. params["LookupAttribute.1.Key"] = "EventRW"
  139. params["LookupAttribute.1.Value"] = eventRW
  140. }
  141. if len(token) > 0 {
  142. params["NextToken"] = token
  143. }
  144. if len(requestId) > 0 {
  145. params["Request"] = requestId
  146. }
  147. resp, err := region.trialRequest("LookupEvents", params)
  148. if err != nil {
  149. if strings.Contains(err.Error(), "no such host") { // cn-wulanchabu not support
  150. return []SEvent{}, "", nil
  151. }
  152. return nil, "", err
  153. }
  154. events := []SEvent{}
  155. err = resp.Unmarshal(&events, "Events")
  156. if err != nil {
  157. return nil, "", err
  158. }
  159. nextToken, _ := resp.GetString("NextToken")
  160. return events, nextToken, nil
  161. }