github.com/sacloud/iaas-api-go@v1.12.0/fake/ops_switch.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 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 *SwitchOp) Find(ctx context.Context, zone string, conditions *iaas.FindCondition) (*iaas.SwitchFindResult, error) { 27 results, _ := find(o.key, zone, conditions) 28 var values []*iaas.Switch 29 for _, res := range results { 30 dest := &iaas.Switch{} 31 copySameNameField(res, dest) 32 values = append(values, dest) 33 } 34 return &iaas.SwitchFindResult{ 35 Total: len(results), 36 Count: len(results), 37 From: 0, 38 Switches: values, 39 }, nil 40 } 41 42 // Create is fake implementation 43 func (o *SwitchOp) Create(ctx context.Context, zone string, param *iaas.SwitchCreateRequest) (*iaas.Switch, error) { 44 result := &iaas.Switch{} 45 copySameNameField(param, result) 46 fill(result, fillID, fillCreatedAt, fillAvailability, fillScope) 47 result.Scope = types.Scopes.User 48 putSwitch(zone, result) 49 return result, nil 50 } 51 52 // Read is fake implementation 53 func (o *SwitchOp) Read(ctx context.Context, zone string, id types.ID) (*iaas.Switch, error) { 54 value := getSwitchByID(zone, id) 55 if value == nil { 56 return nil, newErrorNotFound(o.key, id) 57 } 58 dest := &iaas.Switch{} 59 copySameNameField(value, dest) 60 return dest, nil 61 } 62 63 // Update is fake implementation 64 func (o *SwitchOp) Update(ctx context.Context, zone string, id types.ID, param *iaas.SwitchUpdateRequest) (*iaas.Switch, error) { 65 value, err := o.Read(ctx, zone, id) 66 if err != nil { 67 return nil, err 68 } 69 70 copySameNameField(param, value) 71 fill(value, fillModifiedAt) 72 73 putSwitch(zone, value) 74 return value, nil 75 } 76 77 // Delete is fake implementation 78 func (o *SwitchOp) Delete(ctx context.Context, zone string, id types.ID) error { 79 _, err := o.Read(ctx, zone, id) 80 if err != nil { 81 return err 82 } 83 ds().Delete(o.key, zone, id) 84 return nil 85 } 86 87 // ConnectToBridge is fake implementation 88 func (o *SwitchOp) ConnectToBridge(ctx context.Context, zone string, id types.ID, bridgeID types.ID) error { 89 value, err := o.Read(ctx, zone, id) 90 if err != nil { 91 return err 92 } 93 94 bridgeOp := NewBridgeOp() 95 bridge, err := bridgeOp.Read(ctx, zone, bridgeID) 96 if err != nil { 97 return fmt.Errorf("ConnectToBridge is failed: %s", err) 98 } 99 100 if bridge.SwitchInZone != nil { 101 return newErrorConflict(o.key, id, fmt.Sprintf("Bridge[%d] already connected to switch", bridgeID)) 102 } 103 104 value.BridgeID = bridgeID 105 106 switchInZone := &iaas.BridgeSwitchInfo{} 107 copySameNameField(value, switchInZone) 108 bridge.SwitchInZone = switchInZone 109 110 // bridge.BridgeInfo = append(bridge.BridgeInfo, &iaas.BridgeInfo{ 111 // ID: value.ID, 112 // Name: value.Name, 113 // ZoneID: zoneIDs[zone], 114 // }) 115 116 putBridge(zone, bridge) 117 putSwitch(zone, value) 118 return nil 119 } 120 121 // DisconnectFromBridge is fake implementation 122 func (o *SwitchOp) DisconnectFromBridge(ctx context.Context, zone string, id types.ID) error { 123 value, err := o.Read(ctx, zone, id) 124 if err != nil { 125 return err 126 } 127 128 if value.BridgeID.IsEmpty() { 129 return newErrorConflict(o.key, id, fmt.Sprintf("Switch[%d] already disconnected from switch", id)) 130 } 131 132 bridgeOp := NewBridgeOp() 133 bridge, err := bridgeOp.Read(ctx, zone, value.BridgeID) 134 if err != nil { 135 return fmt.Errorf("DisconnectFromBridge is failed: %s", err) 136 } 137 138 // var bridgeInfo []*iaas.BridgeInfo 139 // for _, i := range bridge.BridgeInfo { 140 // if i.ID != value.ID { 141 // bridgeInfo = append(bridgeInfo, i) 142 // } 143 // } 144 145 value.BridgeID = types.ID(0) 146 bridge.SwitchInZone = nil 147 // fakeドライバーではBridgeInfoに非対応 148 // bridge.BridgeInfo = bridgeInfo 149 150 putBridge(zone, bridge) 151 putSwitch(zone, value) 152 return nil 153 } 154 155 // GetServers is fake implementation 156 func (o *SwitchOp) GetServers(ctx context.Context, zone string, id types.ID) (*iaas.SwitchGetServersResult, error) { 157 value, err := o.Read(ctx, zone, id) 158 if err != nil { 159 return nil, err 160 } 161 res := &iaas.SwitchGetServersResult{} 162 if value.ServerCount == 0 { 163 return res, nil 164 } 165 166 searched, err := NewServerOp().Find(ctx, zone, nil) 167 if err != nil { 168 return nil, err 169 } 170 for _, server := range searched.Servers { 171 for _, nic := range server.Interfaces { 172 if nic.SwitchID == id { 173 res.Servers = append(res.Servers, server) 174 res.Count++ 175 res.Total++ 176 break 177 } 178 } 179 } 180 return res, nil 181 }