ops_enhanced_db.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  	"fmt"
  20  
  21  	"github.com/sacloud/iaas-api-go"
  22  	"github.com/sacloud/iaas-api-go/types"
  23  )
  24  
  25  // Find is fake implementation
  26  func (o *EnhancedDBOp) Find(ctx context.Context, conditions *iaas.FindCondition) (*iaas.EnhancedDBFindResult, error) {
  27  	results, _ := find(o.key, iaas.APIDefaultZone, conditions)
  28  	var values []*iaas.EnhancedDB
  29  	for _, res := range results {
  30  		dest := &iaas.EnhancedDB{}
  31  		copySameNameField(res, dest)
  32  		values = append(values, dest)
  33  	}
  34  	return &iaas.EnhancedDBFindResult{
  35  		Total:       len(results),
  36  		Count:       len(results),
  37  		From:        0,
  38  		EnhancedDBs: values,
  39  	}, nil
  40  }
  41  
  42  // Create is fake implementation
  43  func (o *EnhancedDBOp) Create(ctx context.Context, param *iaas.EnhancedDBCreateRequest) (*iaas.EnhancedDB, error) {
  44  	result := &iaas.EnhancedDB{}
  45  	copySameNameField(param, result)
  46  	fill(result, fillID, fillCreatedAt)
  47  
  48  	if result.DatabaseType == "" {
  49  		result.DatabaseType = "tidb"
  50  	}
  51  	if result.Region == "" {
  52  		result.Region = "is1"
  53  	}
  54  	result.Port = 3306
  55  	result.HostName = fmt.Sprintf("%s.%s-%s.db.sakurausercontent.com", result.DatabaseName, result.DatabaseType, result.Region)
  56  	result.Availability = types.Availabilities.Available
  57  
  58  	putEnhancedDB(iaas.APIDefaultZone, result)
  59  	return result, nil
  60  }
  61  
  62  // Read is fake implementation
  63  func (o *EnhancedDBOp) Read(ctx context.Context, id types.ID) (*iaas.EnhancedDB, error) {
  64  	value := getEnhancedDBByID(iaas.APIDefaultZone, id)
  65  	if value == nil {
  66  		return nil, newErrorNotFound(o.key, id)
  67  	}
  68  	dest := &iaas.EnhancedDB{}
  69  	copySameNameField(value, dest)
  70  	return dest, nil
  71  }
  72  
  73  // Update is fake implementation
  74  func (o *EnhancedDBOp) Update(ctx context.Context, id types.ID, param *iaas.EnhancedDBUpdateRequest) (*iaas.EnhancedDB, error) {
  75  	value, err := o.Read(ctx, id)
  76  	if err != nil {
  77  		return nil, err
  78  	}
  79  	copySameNameField(param, value)
  80  	fill(value, fillModifiedAt)
  81  
  82  	putEnhancedDB(iaas.APIDefaultZone, value)
  83  	return value, nil
  84  }
  85  
  86  // Delete is fake implementation
  87  func (o *EnhancedDBOp) Delete(ctx context.Context, id types.ID) error {
  88  	_, err := o.Read(ctx, id)
  89  	if err != nil {
  90  		return err
  91  	}
  92  
  93  	ds().Delete(o.key, iaas.APIDefaultZone, id)
  94  	return nil
  95  }
  96  
  97  // SetPassword is fake implementation
  98  func (o *EnhancedDBOp) SetPassword(ctx context.Context, id types.ID, param *iaas.EnhancedDBSetPasswordRequest) error {
  99  	_, err := o.Read(ctx, id)
 100  	return err
 101  }
 102  
 103  func (o *EnhancedDBOp) GetConfig(ctx context.Context, id types.ID) (*iaas.EnhancedDBConfig, error) {
 104  	_, err := o.Read(ctx, id)
 105  	if err != nil {
 106  		return nil, err
 107  	}
 108  
 109  	return &iaas.EnhancedDBConfig{MaxConnections: 50}, nil
 110  }
 111  
 112  func (o *EnhancedDBOp) SetConfig(ctx context.Context, id types.ID, param *iaas.EnhancedDBSetConfigRequest) error {
 113  	_, err := o.Read(ctx, id)
 114  	return err
 115  }
 116