ops_container_registry.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 *ContainerRegistryOp) Find(ctx context.Context, conditions *iaas.FindCondition) (*iaas.ContainerRegistryFindResult, error) {
  26  	results, _ := find(o.key, iaas.APIDefaultZone, conditions)
  27  	var values []*iaas.ContainerRegistry
  28  	for _, res := range results {
  29  		dest := &iaas.ContainerRegistry{}
  30  		copySameNameField(res, dest)
  31  		values = append(values, dest)
  32  	}
  33  	return &iaas.ContainerRegistryFindResult{
  34  		Total:               len(results),
  35  		Count:               len(results),
  36  		From:                0,
  37  		ContainerRegistries: values,
  38  	}, nil
  39  }
  40  
  41  // Create is fake implementation
  42  func (o *ContainerRegistryOp) Create(ctx context.Context, param *iaas.ContainerRegistryCreateRequest) (*iaas.ContainerRegistry, error) {
  43  	result := &iaas.ContainerRegistry{}
  44  	copySameNameField(param, result)
  45  	fill(result, fillID, fillCreatedAt)
  46  
  47  	result.FQDN = result.SubDomainLabel + ".sakuracr.jp"
  48  	result.Availability = types.Availabilities.Available
  49  	putContainerRegistry(iaas.APIDefaultZone, result)
  50  	return result, nil
  51  }
  52  
  53  // Read is fake implementation
  54  func (o *ContainerRegistryOp) Read(ctx context.Context, id types.ID) (*iaas.ContainerRegistry, error) {
  55  	value := getContainerRegistryByID(iaas.APIDefaultZone, id)
  56  	if value == nil {
  57  		return nil, newErrorNotFound(o.key, id)
  58  	}
  59  	dest := &iaas.ContainerRegistry{}
  60  	copySameNameField(value, dest)
  61  	return dest, nil
  62  }
  63  
  64  // Update is fake implementation
  65  func (o *ContainerRegistryOp) Update(ctx context.Context, id types.ID, param *iaas.ContainerRegistryUpdateRequest) (*iaas.ContainerRegistry, error) {
  66  	value, err := o.Read(ctx, id)
  67  	if err != nil {
  68  		return nil, err
  69  	}
  70  	copySameNameField(param, value)
  71  	fill(value, fillModifiedAt)
  72  	putContainerRegistry(iaas.APIDefaultZone, value)
  73  	return value, nil
  74  }
  75  
  76  // UpdateSettings is fake implementation
  77  func (o *ContainerRegistryOp) UpdateSettings(ctx context.Context, id types.ID, param *iaas.ContainerRegistryUpdateSettingsRequest) (*iaas.ContainerRegistry, error) {
  78  	value, err := o.Read(ctx, id)
  79  	if err != nil {
  80  		return nil, err
  81  	}
  82  	copySameNameField(param, value)
  83  	fill(value, fillModifiedAt)
  84  	putContainerRegistry(iaas.APIDefaultZone, value)
  85  	return value, nil
  86  }
  87  
  88  // Delete is fake implementation
  89  func (o *ContainerRegistryOp) Delete(ctx context.Context, id types.ID) error {
  90  	_, err := o.Read(ctx, id)
  91  	if err != nil {
  92  		return err
  93  	}
  94  	ds().Delete(o.key, iaas.APIDefaultZone, id)
  95  	return nil
  96  }
  97  
  98  // ListUsers is fake implementation
  99  func (o *ContainerRegistryOp) ListUsers(ctx context.Context, id types.ID) (*iaas.ContainerRegistryUsers, error) {
 100  	_, err := o.Read(ctx, id)
 101  	if err != nil {
 102  		return nil, err
 103  	}
 104  
 105  	v := ds().Get(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id)
 106  	if v != nil {
 107  		users := v.([]*iaas.ContainerRegistryUser)
 108  		return &iaas.ContainerRegistryUsers{
 109  			Users: users,
 110  		}, nil
 111  	}
 112  
 113  	return nil, err
 114  }
 115  
 116  // AddUser is fake implementation
 117  func (o *ContainerRegistryOp) AddUser(ctx context.Context, id types.ID, param *iaas.ContainerRegistryUserCreateRequest) error {
 118  	_, err := o.Read(ctx, id)
 119  	if err != nil {
 120  		return err
 121  	}
 122  
 123  	var users []*iaas.ContainerRegistryUser
 124  	v := ds().Get(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id)
 125  	if v != nil {
 126  		users = v.([]*iaas.ContainerRegistryUser)
 127  	}
 128  	users = append(users, &iaas.ContainerRegistryUser{
 129  		UserName:   param.UserName,
 130  		Permission: param.Permission,
 131  	})
 132  
 133  	ds().Put(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id, users)
 134  	return nil
 135  }
 136  
 137  // UpdateUser is fake implementation
 138  func (o *ContainerRegistryOp) UpdateUser(ctx context.Context, id types.ID, username string, param *iaas.ContainerRegistryUserUpdateRequest) error {
 139  	_, err := o.Read(ctx, id)
 140  	if err != nil {
 141  		return err
 142  	}
 143  
 144  	v := ds().Get(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id)
 145  	if v == nil {
 146  		return newErrorNotFound(ResourceContainerRegistry+"Users", id)
 147  	}
 148  	users := v.([]*iaas.ContainerRegistryUser)
 149  	for _, u := range users {
 150  		if u.UserName == username {
 151  			u.Permission = param.Permission
 152  		}
 153  	}
 154  	ds().Put(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id, users)
 155  	return nil
 156  }
 157  
 158  // DeleteUser is fake implementation
 159  func (o *ContainerRegistryOp) DeleteUser(ctx context.Context, id types.ID, username string) error {
 160  	_, err := o.Read(ctx, id)
 161  	if err != nil {
 162  		return err
 163  	}
 164  
 165  	v := ds().Get(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id)
 166  	if v == nil {
 167  		return newErrorNotFound(ResourceContainerRegistry+"Users", id)
 168  	}
 169  	users := v.([]*iaas.ContainerRegistryUser)
 170  	var newUsers []*iaas.ContainerRegistryUser
 171  	for _, u := range users {
 172  		if u.UserName != username {
 173  			newUsers = append(newUsers, u)
 174  		}
 175  	}
 176  
 177  	ds().Put(ResourceContainerRegistry+"Users", iaas.APIDefaultZone, id, newUsers)
 178  	return nil
 179  }
 180