yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ctyun/diskbackup_policy.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 ctyun
    16  
    17  import (
    18  	"strconv"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SDiskBacupPolicy struct {
    29  	multicloud.SResourceBase
    30  	CtyunTags
    31  	region *SRegion
    32  
    33  	PolicyResourceCount int64           `json:"policy_resource_count"`
    34  	BackupPolicyName    string          `json:"backup_policy_name"`
    35  	ScheduledPolicy     ScheduledPolicy `json:"scheduled_policy"`
    36  	BackupPolicyID      string          `json:"backup_policy_id"`
    37  }
    38  
    39  type ScheduledPolicy struct {
    40  	RententionNum               int    `json:"rentention_num"`
    41  	StartTime                   string `json:"start_time"`
    42  	Status                      string `json:"status"`
    43  	RemainFirstBackupOfCurMonth string `json:"remain_first_backup_of_curMonth"`
    44  	Frequency                   int    `json:"frequency"`
    45  }
    46  
    47  func (self *SDiskBacupPolicy) GetId() string {
    48  	return self.BackupPolicyID
    49  }
    50  
    51  func (self *SDiskBacupPolicy) GetName() string {
    52  	return self.BackupPolicyName
    53  }
    54  
    55  func (self *SDiskBacupPolicy) GetGlobalId() string {
    56  	return self.GetId()
    57  }
    58  
    59  func (self *SDiskBacupPolicy) GetStatus() string {
    60  	if self.ScheduledPolicy.Status == "ON" || self.ScheduledPolicy.Status == "OFF" {
    61  		return api.SNAPSHOT_POLICY_READY
    62  	}
    63  	return api.SNAPSHOT_POLICY_UNKNOWN
    64  }
    65  
    66  func (self *SDiskBacupPolicy) Refresh() error {
    67  	policy, err := self.region.GetDiskBackupPolicy(self.GetId())
    68  	if err != nil {
    69  		return errors.Wrap(err, "SDiskBacupPolicy.Refresh")
    70  	}
    71  
    72  	err = jsonutils.Update(self, policy)
    73  	if err != nil {
    74  		return errors.Wrap(err, "SDiskBacupPolicy.Refresh.Update")
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func (self *SDiskBacupPolicy) IsEmulated() bool {
    81  	return false
    82  }
    83  
    84  func (self *SDiskBacupPolicy) GetProjectId() string {
    85  	return ""
    86  }
    87  
    88  func (self *SDiskBacupPolicy) IsActivated() bool {
    89  	if self.ScheduledPolicy.Status == "ON" {
    90  		return true
    91  	}
    92  
    93  	return false
    94  }
    95  
    96  func (self *SDiskBacupPolicy) GetRetentionDays() int {
    97  	return self.ScheduledPolicy.RententionNum
    98  }
    99  
   100  func (self *SDiskBacupPolicy) GetRepeatWeekdays() ([]int, error) {
   101  	return []int{self.ScheduledPolicy.Frequency}, nil
   102  }
   103  
   104  func (self *SDiskBacupPolicy) GetTimePoints() ([]int, error) {
   105  	ret, err := strconv.Atoi(self.ScheduledPolicy.StartTime[0:2])
   106  	return []int{ret}, err
   107  }
   108  
   109  func (self *SRegion) GetDiskBackupPolices() ([]SDiskBacupPolicy, error) {
   110  	params := map[string]string{
   111  		"regionId": self.GetId(),
   112  	}
   113  
   114  	resp, err := self.client.DoGet("/apiproxy/v3/ondemand/queryDiskBackupPolicys", params)
   115  	if err != nil {
   116  		return nil, errors.Wrap(err, "SRegion.GetDiskBackupPolices.DoGet")
   117  	}
   118  
   119  	ret := make([]SDiskBacupPolicy, 0)
   120  	err = resp.Unmarshal(&ret, "returnObj", "backup_policies")
   121  	if err != nil {
   122  		return nil, errors.Wrap(err, "SRegion.GetDiskBackupPolices.Unmarshal")
   123  	}
   124  
   125  	for i := range ret {
   126  		ret[i].region = self
   127  	}
   128  
   129  	return ret, nil
   130  }
   131  
   132  func (self *SRegion) GetDiskBackupPolicy(policyId string) (*SDiskBacupPolicy, error) {
   133  	polices, err := self.GetDiskBackupPolices()
   134  	if err != nil {
   135  		return nil, errors.Wrap(err, "SRegion.GetDiskBackupPolicy.GetDiskBackupPolices")
   136  	}
   137  
   138  	for i := range polices {
   139  		policy := polices[i]
   140  		if policy.BackupPolicyID == policyId {
   141  			return &policy, nil
   142  		}
   143  	}
   144  
   145  	return nil, errors.Wrap(cloudprovider.ErrNotFound, "SRegion.GetDiskBackupPolicy")
   146  }
   147  
   148  func (self *SRegion) CreateDiskBackupPolicy(name, startTime, frequency, rententionNum, firstBackup, status string) error {
   149  	policyParams := jsonutils.NewDict()
   150  	policyParams.Set("policyName", jsonutils.NewString(name))
   151  	scheduleParams := jsonutils.NewDict()
   152  	scheduleParams.Set("startTime", jsonutils.NewString(startTime))
   153  	scheduleParams.Set("frequency", jsonutils.NewString(frequency))
   154  	scheduleParams.Set("rententionNum", jsonutils.NewString(rententionNum))
   155  	scheduleParams.Set("firstBackup", jsonutils.NewString(firstBackup))
   156  	scheduleParams.Set("status", jsonutils.NewString(status))
   157  	policyParams.Set("scheduledPolicy", scheduleParams)
   158  
   159  	params := map[string]jsonutils.JSONObject{
   160  		"regionId": jsonutils.NewString(self.GetId()),
   161  		"jsonStr":  policyParams,
   162  	}
   163  
   164  	_, err := self.client.DoPost("/apiproxy/v3/ondemand/createDiskBackupPolicy", params)
   165  	if err != nil {
   166  		return errors.Wrap(err, "SRegion.CreateDiskBackupPolicy.DoPost")
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  func (self *SRegion) BindingDiskBackupPolicy(policyId, resourceId, resourceType string) error {
   173  	policyParams := jsonutils.NewDict()
   174  	policyParams.Set("policyId", jsonutils.NewString(policyId))
   175  	resourcesParams := jsonutils.NewArray()
   176  	resourcesParam := jsonutils.NewDict()
   177  	resourcesParam.Set("resourceId", jsonutils.NewString(resourceId))
   178  	resourcesParam.Set("resourceType", jsonutils.NewString(resourceType))
   179  	resourcesParams.Add(resourcesParam)
   180  	policyParams.Set("resources", resourcesParams)
   181  
   182  	params := map[string]jsonutils.JSONObject{
   183  		"regionId": jsonutils.NewString(self.GetId()),
   184  		"jsonStr":  policyParams,
   185  	}
   186  
   187  	_, err := self.client.DoPost("/apiproxy/v3/ondemand/bindResourceToPolicy", params)
   188  	if err != nil {
   189  		return errors.Wrap(err, "SRegion.BindingDiskBackupPolicy.DoPost")
   190  	}
   191  
   192  	return nil
   193  }
   194  
   195  func (self *SRegion) UnBindDiskBackupPolicy(policyId, resourceId string) error {
   196  	resourcesParams := jsonutils.NewArray()
   197  	resourcesParam := jsonutils.NewDict()
   198  	resourcesParam.Set("resource_id", jsonutils.NewString(resourceId))
   199  	resourcesParams.Add(resourcesParam)
   200  
   201  	params := map[string]jsonutils.JSONObject{
   202  		"regionId": jsonutils.NewString(self.GetId()),
   203  		"jsonStr":  resourcesParams,
   204  		"policyId": jsonutils.NewString(policyId),
   205  	}
   206  
   207  	_, err := self.client.DoPost("/apiproxy/v3/ondemand/unBindResourceToPolicy", params)
   208  	if err != nil {
   209  		return errors.Wrap(err, "SRegion.UnBindDiskBackupPolicy.DoPost")
   210  	}
   211  
   212  	return nil
   213  }