github.com/sacloud/libsacloud/v2@v2.32.3/helper/query/types_test.go (about) 1 // Copyright 2016-2022 The Libsacloud 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 query 16 17 import ( 18 "context" 19 "net/http" 20 21 "github.com/sacloud/libsacloud/v2/sacloud" 22 "github.com/sacloud/libsacloud/v2/sacloud/types" 23 ) 24 25 type dummyArchiveFinder struct { 26 archive *sacloud.ArchiveFindResult 27 err error 28 } 29 30 func (f *dummyArchiveFinder) Find(ctx context.Context, zone string, conditions *sacloud.FindCondition) (*sacloud.ArchiveFindResult, error) { 31 return f.archive, f.err 32 } 33 34 type dummyNoteFinder struct { 35 notes []*sacloud.Note 36 err error 37 } 38 39 func (f *dummyNoteFinder) Find(ctx context.Context, conditions *sacloud.FindCondition) (*sacloud.NoteFindResult, error) { 40 if f.err != nil { 41 return nil, f.err 42 } 43 44 return &sacloud.NoteFindResult{ 45 Total: len(f.notes), 46 Count: len(f.notes), 47 Notes: f.notes, 48 }, nil 49 } 50 51 type dummyServerPlanFinder struct { 52 plans []*sacloud.ServerPlan 53 err error 54 } 55 56 func (f *dummyServerPlanFinder) Find(ctx context.Context, zone string, conditions *sacloud.FindCondition) (*sacloud.ServerPlanFindResult, error) { 57 if f.err != nil { 58 return nil, f.err 59 } 60 61 return &sacloud.ServerPlanFindResult{ 62 Total: len(f.plans), 63 Count: len(f.plans), 64 ServerPlans: f.plans, 65 }, nil 66 } 67 68 type dummyServerReader struct { 69 servers []*sacloud.Server 70 err error 71 } 72 73 func (r *dummyServerReader) Read(ctx context.Context, zone string, id types.ID) (*sacloud.Server, error) { 74 if r.err != nil { 75 return nil, r.err 76 } 77 for _, s := range r.servers { 78 if s.ID == id { 79 return s, nil 80 } 81 } 82 return nil, sacloud.NewAPIError(http.MethodGet, nil, http.StatusNotFound, nil) 83 } 84 85 type dummyArchiveReader struct { 86 archives []*sacloud.Archive 87 err error 88 } 89 90 func (r *dummyArchiveReader) Read(ctx context.Context, zone string, id types.ID) (*sacloud.Archive, error) { 91 if r.err != nil { 92 return nil, r.err 93 } 94 for _, a := range r.archives { 95 if a.ID == id { 96 return a, nil 97 } 98 } 99 return nil, sacloud.NewAPIError(http.MethodGet, nil, http.StatusNotFound, nil) 100 } 101 102 type dummyDiskReader struct { 103 disks []*sacloud.Disk 104 err error 105 } 106 107 func (r *dummyDiskReader) Read(ctx context.Context, zone string, id types.ID) (*sacloud.Disk, error) { 108 if r.err != nil { 109 return nil, r.err 110 } 111 for _, d := range r.disks { 112 if d.ID == id { 113 return d, nil 114 } 115 } 116 return nil, sacloud.NewAPIError(http.MethodGet, nil, http.StatusNotFound, nil) 117 }