ops_icon.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 *IconOp) Find(ctx context.Context, conditions *iaas.FindCondition) (*iaas.IconFindResult, error) {
  27  	results, _ := find(o.key, iaas.APIDefaultZone, conditions)
  28  	var values []*iaas.Icon
  29  	for _, res := range results {
  30  		dest := &iaas.Icon{}
  31  		copySameNameField(res, dest)
  32  		values = append(values, dest)
  33  	}
  34  	return &iaas.IconFindResult{
  35  		Total: len(results),
  36  		Count: len(results),
  37  		From:  0,
  38  		Icons: values,
  39  	}, nil
  40  }
  41  
  42  // Create is fake implementation
  43  func (o *IconOp) Create(ctx context.Context, param *iaas.IconCreateRequest) (*iaas.Icon, error) {
  44  	result := &iaas.Icon{}
  45  	copySameNameField(param, result)
  46  	fill(result, fillID, fillCreatedAt, fillModifiedAt)
  47  
  48  	result.Availability = types.Availabilities.Available
  49  	result.Scope = types.Scopes.User
  50  	result.URL = fmt.Sprintf("https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/icon/%d.png", result.ID)
  51  
  52  	putIcon(iaas.APIDefaultZone, result)
  53  	return result, nil
  54  }
  55  
  56  // Read is fake implementation
  57  func (o *IconOp) Read(ctx context.Context, id types.ID) (*iaas.Icon, error) {
  58  	value := getIconByID(iaas.APIDefaultZone, id)
  59  	if value == nil {
  60  		return nil, newErrorNotFound(o.key, id)
  61  	}
  62  	dest := &iaas.Icon{}
  63  	copySameNameField(value, dest)
  64  	return dest, nil
  65  }
  66  
  67  // Update is fake implementation
  68  func (o *IconOp) Update(ctx context.Context, id types.ID, param *iaas.IconUpdateRequest) (*iaas.Icon, error) {
  69  	value, err := o.Read(ctx, id)
  70  	if err != nil {
  71  		return nil, err
  72  	}
  73  	copySameNameField(param, value)
  74  	fill(value, fillModifiedAt)
  75  
  76  	putIcon(iaas.APIDefaultZone, value)
  77  	return value, nil
  78  }
  79  
  80  // Delete is fake implementation
  81  func (o *IconOp) Delete(ctx context.Context, id types.ID) error {
  82  	_, err := o.Read(ctx, id)
  83  	if err != nil {
  84  		return err
  85  	}
  86  
  87  	ds().Delete(o.key, iaas.APIDefaultZone, id)
  88  	return nil
  89  }
  90