ops_cdrom.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 *CDROMOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.CDROMFindResult, error) {
  27  	results, _ := find(o.key, zone, conditions)
  28  	var values []*iaas.CDROM
  29  	for _, res := range results {
  30  		dest := &iaas.CDROM{}
  31  		copySameNameField(res, dest)
  32  		values = append(values, dest)
  33  	}
  34  	return &iaas.CDROMFindResult{
  35  		Total:  len(results),
  36  		Count:  len(results),
  37  		From:   0,
  38  		CDROMs: values,
  39  	}, nil
  40  }
  41  
  42  // Create is fake implementation
  43  func (o *CDROMOp) Create(ctx context.Context, zone string, param *iaas.CDROMCreateRequest) (*iaas.CDROM, *iaas.FTPServer, error) {
  44  	result := &iaas.CDROM{}
  45  	copySameNameField(param, result)
  46  	fill(result, fillID, fillCreatedAt, fillAvailability, fillScope)
  47  	result.Availability = types.Availabilities.Uploading
  48  
  49  	putCDROM(zone, result)
  50  	return result, &iaas.FTPServer{
  51  		HostName:  fmt.Sprintf("sac-%s-ftp.example.jp", zone),
  52  		IPAddress: "192.0.2.1",
  53  		User:      fmt.Sprintf("cdrom%d", result.ID),
  54  		Password:  "password-is-not-a-password",
  55  	}, nil
  56  }
  57  
  58  // Read is fake implementation
  59  func (o *CDROMOp) Read(ctx context.Context, zone string, id types.ID) (*iaas.CDROM, error) {
  60  	value := getCDROMByID(zone, id)
  61  	if value == nil {
  62  		return nil, newErrorNotFound(o.key, id)
  63  	}
  64  	dest := &iaas.CDROM{}
  65  	copySameNameField(value, dest)
  66  	return dest, nil
  67  }
  68  
  69  // Update is fake implementation
  70  func (o *CDROMOp) Update(ctx context.Context, zone string, id types.ID, param *iaas.CDROMUpdateRequest) (*iaas.CDROM, error) {
  71  	value, err := o.Read(ctx, zone, id)
  72  	if err != nil {
  73  		return nil, err
  74  	}
  75  	copySameNameField(param, value)
  76  	fill(value, fillModifiedAt)
  77  
  78  	putCDROM(zone, value)
  79  	return value, nil
  80  }
  81  
  82  // Delete is fake implementation
  83  func (o *CDROMOp) Delete(ctx context.Context, zone string, id types.ID) error {
  84  	_, err := o.Read(ctx, zone, id)
  85  	if err != nil {
  86  		return err
  87  	}
  88  	ds().Delete(o.key, zone, id)
  89  	return nil
  90  }
  91  
  92  // OpenFTP is fake implementation
  93  func (o *CDROMOp) OpenFTP(ctx context.Context, zone string, id types.ID, openOption *iaas.OpenFTPRequest) (*iaas.FTPServer, error) {
  94  	value, err := o.Read(ctx, zone, id)
  95  	if err != nil {
  96  		return nil, err
  97  	}
  98  
  99  	value.SetAvailability(types.Availabilities.Uploading)
 100  	putCDROM(zone, value)
 101  
 102  	return &iaas.FTPServer{
 103  		HostName:  fmt.Sprintf("sac-%s-ftp.example.jp", zone),
 104  		IPAddress: "192.0.2.1",
 105  		User:      fmt.Sprintf("cdrom%d", id),
 106  		Password:  "password-is-not-a-password",
 107  	}, nil
 108  }
 109  
 110  // CloseFTP is fake implementation
 111  func (o *CDROMOp) CloseFTP(ctx context.Context, zone string, id types.ID) error {
 112  	value, err := o.Read(ctx, zone, id)
 113  	if err != nil {
 114  		return err
 115  	}
 116  	if !value.Availability.IsUploading() {
 117  		value.SetAvailability(types.Availabilities.Available)
 118  	}
 119  	putCDROM(zone, value)
 120  	return nil
 121  }
 122