resourcepolicy.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 google
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/cloudmux/pkg/apis"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. )
  25. type SDailySchedule struct {
  26. DaysInCycle int
  27. StartTime string
  28. Duration string
  29. }
  30. type SDayOfWeek struct {
  31. Day string
  32. StartTime string
  33. Duration string
  34. }
  35. type SHourlySchedule struct {
  36. HoursInCycle int
  37. StartTime string
  38. Duration string
  39. }
  40. type SWeeklySchedule struct {
  41. DayOfWeeks []SDayOfWeek
  42. }
  43. type SSchedule struct {
  44. WeeklySchedule SWeeklySchedule
  45. DailySchedule SDailySchedule
  46. HourlySchedule SHourlySchedule
  47. }
  48. type SRetentionPolicy struct {
  49. MaxRetentionDays int
  50. OnSourceDiskDelete string
  51. }
  52. type SSnapshotProperties struct {
  53. StorageLocations []string
  54. GuestFlush bool
  55. }
  56. type SSnapshotSchedulePolicy struct {
  57. Schedule SSchedule
  58. RetentionPolicy SRetentionPolicy
  59. SnapshotProperties SSnapshotProperties
  60. }
  61. type SResourcePolicy struct {
  62. region *SRegion
  63. SResourceBase
  64. GoogleTags
  65. CreationTimestamp time.Time
  66. Region string
  67. Status string
  68. Kind string
  69. SnapshotSchedulePolicy SSnapshotSchedulePolicy `json:"snapshotSchedulePolicy"`
  70. }
  71. func (region *SRegion) GetResourcePolicies(maxResults int, pageToken string) ([]SResourcePolicy, error) {
  72. policies := []SResourcePolicy{}
  73. resource := fmt.Sprintf("regions/%s/resourcePolicies", region.Name)
  74. params := map[string]string{}
  75. return policies, region.List(resource, params, maxResults, pageToken, &policies)
  76. }
  77. func (region *SRegion) GetResourcePolicy(id string) (*SResourcePolicy, error) {
  78. policy := &SResourcePolicy{region: region}
  79. return policy, region.Get("resourcePolicies", id, policy)
  80. }
  81. func (policy *SResourcePolicy) GetStatus() string {
  82. switch policy.Status {
  83. case "READY":
  84. return apis.STATUS_AVAILABLE
  85. default:
  86. return apis.STATUS_UNKNOWN
  87. }
  88. }
  89. func (policy *SResourcePolicy) GetCreatedAt() time.Time {
  90. return policy.CreationTimestamp
  91. }
  92. func (policy *SResourcePolicy) Refresh() error {
  93. _policy, err := policy.region.GetResourcePolicy(policy.Id)
  94. if err != nil {
  95. return err
  96. }
  97. return jsonutils.Update(policy, _policy)
  98. }
  99. func (policy *SResourcePolicy) IsEmulated() bool {
  100. return false
  101. }
  102. func (policy *SResourcePolicy) GetProjectId() string {
  103. return policy.region.GetProjectId()
  104. }
  105. func (policy *SResourcePolicy) GetRetentionDays() int {
  106. return policy.SnapshotSchedulePolicy.RetentionPolicy.MaxRetentionDays
  107. }
  108. func (policy *SResourcePolicy) ApplyDisks(ids []string) error {
  109. return cloudprovider.ErrNotImplemented
  110. }
  111. func (policy *SResourcePolicy) GetApplyDiskIds() ([]string, error) {
  112. return nil, cloudprovider.ErrNotImplemented
  113. }
  114. func (policy *SResourcePolicy) CancelDisks(ids []string) error {
  115. return cloudprovider.ErrNotImplemented
  116. }
  117. func (policy *SResourcePolicy) Delete() error {
  118. return cloudprovider.ErrNotImplemented
  119. }
  120. func (policy *SResourcePolicy) GetRepeatWeekdays() ([]int, error) {
  121. result := []int{1, 2, 3, 4, 5, 6, 7}
  122. if len(policy.SnapshotSchedulePolicy.Schedule.WeeklySchedule.DayOfWeeks) > 0 {
  123. return nil, fmt.Errorf("current not support dayOfWeeks")
  124. }
  125. if policy.SnapshotSchedulePolicy.Schedule.HourlySchedule.HoursInCycle != 0 {
  126. return nil, fmt.Errorf("current not support hourlySchedule")
  127. }
  128. return result, nil
  129. }
  130. func (policy *SResourcePolicy) GetTimePoints() ([]int, error) {
  131. result := []int{}
  132. if len(policy.SnapshotSchedulePolicy.Schedule.DailySchedule.StartTime) == 0 {
  133. return nil, fmt.Errorf("current only support dailySchedule")
  134. }
  135. if startInfo := strings.Split(policy.SnapshotSchedulePolicy.Schedule.DailySchedule.StartTime, ":"); len(startInfo) >= 2 {
  136. point, err := strconv.Atoi(startInfo[0])
  137. if err != nil {
  138. return nil, errors.Wrapf(err, "convert %s", policy.SnapshotSchedulePolicy.Schedule.DailySchedule.StartTime)
  139. }
  140. result = append(result, point)
  141. if startInfo[1] != "00" {
  142. result = append(result, point+1)
  143. }
  144. }
  145. return result, nil
  146. }