forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcepolicy.go
174 lines (146 loc) · 4.49 KB
/
resourcepolicy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/pkg/jsonutils"
"github.com/nyl1001/cloudmux/pkg/apis"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type SDailySchedule struct {
DaysInCycle int
StartTime string
Duration string
}
type SDayOfWeek struct {
Day string
StartTime string
Duration string
}
type SHourlySchedule struct {
HoursInCycle int
StartTime string
Duration string
}
type SWeeklySchedule struct {
DayOfWeeks []SDayOfWeek
}
type SSchedule struct {
WeeklySchedule SWeeklySchedule
DailySchedule SDailySchedule
HourlySchedule SHourlySchedule
}
type SRetentionPolicy struct {
MaxRetentionDays int
OnSourceDiskDelete string
}
type SSnapshotProperties struct {
StorageLocations []string
GuestFlush bool
}
type SSnapshotSchedulePolicy struct {
Schedule SSchedule
RetentionPolicy SRetentionPolicy
SnapshotProperties SSnapshotProperties
}
type SResourcePolicy struct {
region *SRegion
SResourceBase
GoogleTags
CreationTimestamp time.Time
Region string
Status string
Kind string
SnapshotSchedulePolicy SSnapshotSchedulePolicy `json:"snapshotSchedulePolicy"`
}
func (region *SRegion) GetResourcePolicies(maxResults int, pageToken string) ([]SResourcePolicy, error) {
policies := []SResourcePolicy{}
resource := fmt.Sprintf("regions/%s/resourcePolicies", region.Name)
params := map[string]string{}
return policies, region.List(resource, params, maxResults, pageToken, &policies)
}
func (region *SRegion) GetResourcePolicy(id string) (*SResourcePolicy, error) {
policy := &SResourcePolicy{region: region}
return policy, region.Get("resourcePolicies", id, policy)
}
func (policy *SResourcePolicy) GetStatus() string {
switch policy.Status {
case "READY":
return apis.STATUS_AVAILABLE
default:
return apis.STATUS_UNKNOWN
}
}
func (policy *SResourcePolicy) GetCreatedAt() time.Time {
return policy.CreationTimestamp
}
func (policy *SResourcePolicy) Refresh() error {
_policy, err := policy.region.GetResourcePolicy(policy.Id)
if err != nil {
return err
}
return jsonutils.Update(policy, _policy)
}
func (policy *SResourcePolicy) IsEmulated() bool {
return false
}
func (policy *SResourcePolicy) GetProjectId() string {
return policy.region.GetProjectId()
}
func (policy *SResourcePolicy) GetRetentionDays() int {
return policy.SnapshotSchedulePolicy.RetentionPolicy.MaxRetentionDays
}
func (policy *SResourcePolicy) ApplyDisks(ids []string) error {
return cloudprovider.ErrNotImplemented
}
func (policy *SResourcePolicy) GetApplyDiskIds() ([]string, error) {
return nil, cloudprovider.ErrNotImplemented
}
func (policy *SResourcePolicy) CancelDisks(ids []string) error {
return cloudprovider.ErrNotImplemented
}
func (policy *SResourcePolicy) Delete() error {
return cloudprovider.ErrNotImplemented
}
func (policy *SResourcePolicy) GetRepeatWeekdays() ([]int, error) {
result := []int{1, 2, 3, 4, 5, 6, 7}
if len(policy.SnapshotSchedulePolicy.Schedule.WeeklySchedule.DayOfWeeks) > 0 {
return nil, fmt.Errorf("current not support dayOfWeeks")
}
if policy.SnapshotSchedulePolicy.Schedule.HourlySchedule.HoursInCycle != 0 {
return nil, fmt.Errorf("current not support hourlySchedule")
}
return result, nil
}
func (policy *SResourcePolicy) GetTimePoints() ([]int, error) {
result := []int{}
if len(policy.SnapshotSchedulePolicy.Schedule.DailySchedule.StartTime) == 0 {
return nil, fmt.Errorf("current only support dailySchedule")
}
if startInfo := strings.Split(policy.SnapshotSchedulePolicy.Schedule.DailySchedule.StartTime, ":"); len(startInfo) >= 2 {
point, err := strconv.Atoi(startInfo[0])
if err != nil {
return nil, errors.Wrapf(err, "convert %s", policy.SnapshotSchedulePolicy.Schedule.DailySchedule.StartTime)
}
result = append(result, point)
if startInfo[1] != "00" {
result = append(result, point+1)
}
}
return result, nil
}