github.com/sacloud/iaas-api-go@v1.12.0/helper/query/previous_id.go (about) 1 // Copyright 2016-2022 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 query 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/sacloud/iaas-api-go" 22 "github.com/sacloud/iaas-api-go/helper/plans" 23 "github.com/sacloud/iaas-api-go/search" 24 "github.com/sacloud/iaas-api-go/types" 25 ) 26 27 func findByPreviousIDCondition(id types.ID) *iaas.FindCondition { 28 return &iaas.FindCondition{ 29 Filter: search.Filter{ 30 search.Key("Tags.Name"): search.TagsAndEqual(fmt.Sprintf("%s=%s", plans.PreviousIDTagName, id)), 31 }, 32 } 33 } 34 35 // ReadServer 指定のIDでサーバを検索、IDで見つからなかった場合は@previous-idタグで検索し見つかったサーバリソースを返す 36 // 37 // 対象が見つからなかった場合はiaas.NoResultsErrorを返す 38 func ReadServer(ctx context.Context, caller iaas.APICaller, zone string, id types.ID) (*iaas.Server, error) { 39 serverOp := iaas.NewServerOp(caller) 40 41 server, err := serverOp.Read(ctx, zone, id) 42 if err != nil { 43 if !iaas.IsNotFoundError(err) { 44 return nil, err 45 } 46 47 found, err := serverOp.Find(ctx, zone, findByPreviousIDCondition(id)) 48 if err != nil { 49 return nil, err 50 } 51 if len(found.Servers) == 0 { 52 return nil, iaas.NewNoResultsError() 53 } 54 55 // 複数ヒットした場合でも先頭だけ返す 56 server = found.Servers[0] 57 } 58 59 return server, nil 60 } 61 62 // ReadRouter 指定のIDでルータを検索、IDで見つからなかった場合は@previous-idタグで検索し見つかったリソースを返す 63 // 64 // 対象が見つからなかった場合はiaas.NoResultsErrorを返す 65 func ReadRouter(ctx context.Context, caller iaas.APICaller, zone string, id types.ID) (*iaas.Internet, error) { 66 routerOp := iaas.NewInternetOp(caller) 67 68 router, err := routerOp.Read(ctx, zone, id) 69 if err != nil { 70 if !iaas.IsNotFoundError(err) { 71 return nil, err 72 } 73 74 found, err := routerOp.Find(ctx, zone, findByPreviousIDCondition(id)) 75 if err != nil { 76 return nil, err 77 } 78 if len(found.Internet) == 0 { 79 return nil, iaas.NewNoResultsError() 80 } 81 82 // 複数ヒットした場合でも先頭だけ返す 83 router = found.Internet[0] 84 } 85 86 return router, nil 87 } 88 89 // ReadProxyLB 指定のIDでELBを検索、IDで見つからなかった場合は@previous-idタグで検索し見つかったリソースを返す 90 // 91 // 対象が見つからなかった場合はiaas.NoResultsErrorを返す 92 func ReadProxyLB(ctx context.Context, caller iaas.APICaller, id types.ID) (*iaas.ProxyLB, error) { 93 elbOp := iaas.NewProxyLBOp(caller) 94 95 elb, err := elbOp.Read(ctx, id) 96 if err != nil { 97 if !iaas.IsNotFoundError(err) { 98 return nil, err 99 } 100 101 found, err := elbOp.Find(ctx, findByPreviousIDCondition(id)) 102 if err != nil { 103 return nil, err 104 } 105 if len(found.ProxyLBs) == 0 { 106 return nil, iaas.NewNoResultsError() 107 } 108 109 // 複数ヒットした場合でも先頭だけ返す 110 elb = found.ProxyLBs[0] 111 } 112 113 return elb, nil 114 }