ops_ipv6_addr.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  type ipv6Addr struct {
  25  	ID types.ID
  26  	*iaas.IPv6Addr
  27  }
  28  
  29  // Find is fake implementation
  30  func (o *IPv6AddrOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.IPv6AddrFindResult, error) {
  31  	results, _ := find(o.key, zone, conditions)
  32  	var values []*iaas.IPv6Addr
  33  	for _, res := range results {
  34  		dest := &iaas.IPv6Addr{}
  35  		copySameNameField(res, dest)
  36  		values = append(values, dest)
  37  	}
  38  	return &iaas.IPv6AddrFindResult{
  39  		Total:     len(results),
  40  		Count:     len(results),
  41  		From:      0,
  42  		IPv6Addrs: values,
  43  	}, nil
  44  }
  45  
  46  // Create is fake implementation
  47  func (o *IPv6AddrOp) Create(ctx context.Context, zone string, param *iaas.IPv6AddrCreateRequest) (*iaas.IPv6Addr, error) {
  48  	result := &iaas.IPv6Addr{}
  49  	copySameNameField(param, result)
  50  
  51  	ds().Put(ResourceIPv6Addr, zone, pool().generateID(), &ipv6Addr{IPv6Addr: result})
  52  	return result, nil
  53  }
  54  
  55  // Read is fake implementation
  56  func (o *IPv6AddrOp) Read(ctx context.Context, zone string, ipv6addr string) (*iaas.IPv6Addr, error) {
  57  	var value *iaas.IPv6Addr
  58  
  59  	results := ds().List(o.key, zone)
  60  	for _, res := range results {
  61  		v := res.(*ipv6Addr)
  62  		if v.IPv6Addr.IPv6Addr == ipv6addr {
  63  			value = v.IPv6Addr
  64  			break
  65  		}
  66  	}
  67  
  68  	if value == nil {
  69  		return nil, newErrorNotFound(o.key, ipv6addr)
  70  	}
  71  	return value, nil
  72  }
  73  
  74  // Update is fake implementation
  75  func (o *IPv6AddrOp) Update(ctx context.Context, zone string, ipv6addr string, param *iaas.IPv6AddrUpdateRequest) (*iaas.IPv6Addr, error) {
  76  	found := false
  77  	results := ds().List(o.key, zone)
  78  	var value *iaas.IPv6Addr
  79  	for _, res := range results {
  80  		v := res.(*ipv6Addr)
  81  		if v.IPv6Addr.IPv6Addr == ipv6addr {
  82  			copySameNameField(param, v.IPv6Addr)
  83  			found = true
  84  			ds().Put(o.key, zone, v.ID, v)
  85  			value = v.IPv6Addr
  86  		}
  87  	}
  88  
  89  	if !found {
  90  		return nil, newErrorNotFound(o.key, ipv6addr)
  91  	}
  92  
  93  	return value, nil
  94  }
  95  
  96  // Delete is fake implementation
  97  func (o *IPv6AddrOp) Delete(ctx context.Context, zone string, ipv6addr string) error {
  98  	found := false
  99  	results := ds().List(o.key, zone)
 100  	for _, res := range results {
 101  		v := res.(*ipv6Addr)
 102  		if v.IPv6Addr.IPv6Addr == ipv6addr {
 103  			found = true
 104  			ds().Delete(o.key, zone, v.ID)
 105  		}
 106  	}
 107  
 108  	if !found {
 109  		return newErrorNotFound(o.key, ipv6addr)
 110  	}
 111  
 112  	return nil
 113  }
 114