github.com/sacloud/iaas-api-go@v1.12.0/fake/ops_nfs.go (about) 1 // Copyright 2022-2023 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 "time" 21 22 "github.com/sacloud/iaas-api-go" 23 "github.com/sacloud/iaas-api-go/types" 24 ) 25 26 // Find is fake implementation 27 func (o *NFSOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.NFSFindResult, error) { 28 results, _ := find(o.key, zone, conditions) 29 var values []*iaas.NFS 30 for _, res := range results { 31 dest := &iaas.NFS{} 32 copySameNameField(res, dest) 33 values = append(values, dest) 34 } 35 return &iaas.NFSFindResult{ 36 Total: len(results), 37 Count: len(results), 38 From: 0, 39 NFS: values, 40 }, nil 41 } 42 43 // Create is fake implementation 44 func (o *NFSOp) Create(ctx context.Context, zone string, param *iaas.NFSCreateRequest) (*iaas.NFS, error) { 45 result := &iaas.NFS{} 46 copySameNameField(param, result) 47 fill(result, fillID, fillCreatedAt) 48 49 result.Class = "nfs" 50 result.Availability = types.Availabilities.Migrating 51 result.ZoneID = zoneIDs[zone] 52 53 putNFS(zone, result) 54 55 id := result.ID 56 startPowerOn(o.key, zone, func() (interface{}, error) { 57 return o.Read(context.Background(), zone, id) 58 }) 59 return result, nil 60 } 61 62 // Read is fake implementation 63 func (o *NFSOp) Read(ctx context.Context, zone string, id types.ID) (*iaas.NFS, error) { 64 value := getNFSByID(zone, id) 65 if value == nil { 66 return nil, newErrorNotFound(o.key, id) 67 } 68 dest := &iaas.NFS{} 69 copySameNameField(value, dest) 70 return dest, nil 71 } 72 73 // Update is fake implementation 74 func (o *NFSOp) Update(ctx context.Context, zone string, id types.ID, param *iaas.NFSUpdateRequest) (*iaas.NFS, error) { 75 value, err := o.Read(ctx, zone, id) 76 if err != nil { 77 return nil, err 78 } 79 80 copySameNameField(param, value) 81 fill(value, fillModifiedAt) 82 83 putNFS(zone, value) 84 return value, nil 85 } 86 87 // Delete is fake implementation 88 func (o *NFSOp) Delete(ctx context.Context, zone string, id types.ID) error { 89 value, err := o.Read(ctx, zone, id) 90 if err != nil { 91 return err 92 } 93 if value.InstanceStatus.IsUp() { 94 return newErrorConflict(o.key, id, fmt.Sprintf("NFS[%s] is still running", id)) 95 } 96 97 ds().Delete(o.key, zone, id) 98 return nil 99 } 100 101 // Boot is fake implementation 102 func (o *NFSOp) Boot(ctx context.Context, zone string, id types.ID) error { 103 value, err := o.Read(ctx, zone, id) 104 if err != nil { 105 return err 106 } 107 if value.InstanceStatus.IsUp() { 108 return newErrorConflict(o.key, id, "Boot is failed") 109 } 110 111 startPowerOn(o.key, zone, func() (interface{}, error) { 112 return o.Read(context.Background(), zone, id) 113 }) 114 115 return err 116 } 117 118 // Shutdown is fake implementation 119 func (o *NFSOp) Shutdown(ctx context.Context, zone string, id types.ID, shutdownOption *iaas.ShutdownOption) error { 120 value, err := o.Read(ctx, zone, id) 121 if err != nil { 122 return err 123 } 124 if !value.InstanceStatus.IsUp() { 125 return newErrorConflict(o.key, id, "Shutdown is failed") 126 } 127 128 startPowerOff(o.key, zone, func() (interface{}, error) { 129 return o.Read(context.Background(), zone, id) 130 }) 131 132 return err 133 } 134 135 // Reset is fake implementation 136 func (o *NFSOp) Reset(ctx context.Context, zone string, id types.ID) error { 137 value, err := o.Read(ctx, zone, id) 138 if err != nil { 139 return err 140 } 141 if !value.InstanceStatus.IsUp() { 142 return newErrorConflict(o.key, id, "Reset is failed") 143 } 144 145 startPowerOn(o.key, zone, func() (interface{}, error) { 146 return o.Read(context.Background(), zone, id) 147 }) 148 149 return nil 150 } 151 152 // MonitorFreeDiskSize is fake implementation 153 func (o *NFSOp) MonitorFreeDiskSize(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.FreeDiskSizeActivity, error) { 154 _, err := o.Read(ctx, zone, id) 155 if err != nil { 156 return nil, err 157 } 158 159 now := time.Now().Truncate(time.Second) 160 m := now.Minute() % 5 161 if m != 0 { 162 now.Add(time.Duration(m) * time.Minute) 163 } 164 165 res := &iaas.FreeDiskSizeActivity{} 166 for i := 0; i < 5; i++ { 167 res.Values = append(res.Values, &iaas.MonitorFreeDiskSizeValue{ 168 Time: now.Add(time.Duration(i*-5) * time.Minute), 169 FreeDiskSize: float64(random(1000)), 170 }) 171 } 172 173 return res, nil 174 } 175 176 // MonitorInterface is fake implementation 177 func (o *NFSOp) MonitorInterface(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.InterfaceActivity, error) { 178 _, err := o.Read(ctx, zone, id) 179 if err != nil { 180 return nil, err 181 } 182 183 now := time.Now().Truncate(time.Second) 184 m := now.Minute() % 5 185 if m != 0 { 186 now.Add(time.Duration(m) * time.Minute) 187 } 188 189 res := &iaas.InterfaceActivity{} 190 for i := 0; i < 5; i++ { 191 res.Values = append(res.Values, &iaas.MonitorInterfaceValue{ 192 Time: now.Add(time.Duration(i*-5) * time.Minute), 193 Send: float64(random(1000)), 194 Receive: float64(random(1000)), 195 }) 196 } 197 198 return res, nil 199 } 200 201 // MonitorCPU is fake implementation 202 func (o *NFSOp) MonitorCPU(ctx context.Context, zone string, id types.ID, condition *iaas.MonitorCondition) (*iaas.CPUTimeActivity, error) { 203 _, err := o.Read(ctx, zone, id) 204 if err != nil { 205 return nil, err 206 } 207 208 now := time.Now().Truncate(time.Second) 209 m := now.Minute() % 5 210 if m != 0 { 211 now.Add(time.Duration(m) * time.Minute) 212 } 213 214 res := &iaas.CPUTimeActivity{} 215 for i := 0; i < 5; i++ { 216 res.Values = append(res.Values, &iaas.MonitorCPUTimeValue{ 217 Time: now.Add(time.Duration(i*-5) * time.Minute), 218 CPUTime: float64(random(1000)), 219 }) 220 } 221 222 return res, nil 223 }