ops_auto_backup.go raw

   1  // Copyright 2022-2025 The sacloud/iaas-api-go Authors
   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 fake
  16  
  17  import (
  18  	"context"
  19  
  20  	"github.com/sacloud/iaas-api-go"
  21  	"github.com/sacloud/iaas-api-go/types"
  22  )
  23  
  24  // Find is fake implementation
  25  func (o *AutoBackupOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.AutoBackupFindResult, error) {
  26  	results, _ := find(o.key, zone, conditions)
  27  	var values []*iaas.AutoBackup
  28  	for _, res := range results {
  29  		dest := &iaas.AutoBackup{}
  30  		copySameNameField(res, dest)
  31  		values = append(values, dest)
  32  	}
  33  	return &iaas.AutoBackupFindResult{
  34  		Total:       len(results),
  35  		Count:       len(results),
  36  		From:        0,
  37  		AutoBackups: values,
  38  	}, nil
  39  }
  40  
  41  // Create is fake implementation
  42  func (o *AutoBackupOp) Create(ctx context.Context, zone string, param *iaas.AutoBackupCreateRequest) (*iaas.AutoBackup, error) {
  43  	result := &iaas.AutoBackup{}
  44  	copySameNameField(param, result)
  45  	fill(result, fillID, fillCreatedAt)
  46  
  47  	result.Availability = types.Availabilities.Available
  48  	result.SettingsHash = "settingshash"
  49  	result.AccountID = accountID
  50  	result.ZoneID = zoneIDs[zone]
  51  	result.ZoneName = zone
  52  
  53  	putAutoBackup(zone, result)
  54  	return result, nil
  55  }
  56  
  57  // Read is fake implementation
  58  func (o *AutoBackupOp) Read(ctx context.Context, zone string, id types.ID) (*iaas.AutoBackup, error) {
  59  	value := getAutoBackupByID(zone, id)
  60  	if value == nil {
  61  		return nil, newErrorNotFound(o.key, id)
  62  	}
  63  	dest := &iaas.AutoBackup{}
  64  	copySameNameField(value, dest)
  65  	return dest, nil
  66  }
  67  
  68  // Update is fake implementation
  69  func (o *AutoBackupOp) Update(ctx context.Context, zone string, id types.ID, param *iaas.AutoBackupUpdateRequest) (*iaas.AutoBackup, error) {
  70  	value, err := o.Read(ctx, zone, id)
  71  	if err != nil {
  72  		return nil, err
  73  	}
  74  	copySameNameField(param, value)
  75  	fill(value, fillModifiedAt)
  76  
  77  	putAutoBackup(zone, value)
  78  	return value, nil
  79  }
  80  
  81  // UpdateSettings is fake implementation
  82  func (o *AutoBackupOp) UpdateSettings(ctx context.Context, zone string, id types.ID, param *iaas.AutoBackupUpdateSettingsRequest) (*iaas.AutoBackup, error) {
  83  	value, err := o.Read(ctx, zone, id)
  84  	if err != nil {
  85  		return nil, err
  86  	}
  87  	copySameNameField(param, value)
  88  	fill(value, fillModifiedAt)
  89  
  90  	putAutoBackup(zone, value)
  91  	return value, nil
  92  }
  93  
  94  // Delete is fake implementation
  95  func (o *AutoBackupOp) Delete(ctx context.Context, zone string, id types.ID) error {
  96  	_, err := o.Read(ctx, zone, id)
  97  	if err != nil {
  98  		return err
  99  	}
 100  
 101  	ds().Delete(o.key, zone, id)
 102  	return nil
 103  }
 104