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