github.com/sacloud/iaas-api-go@v1.12.0/helper/query/nfs_plan.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 "encoding/json" 20 "errors" 21 "fmt" 22 23 "github.com/sacloud/iaas-api-go" 24 "github.com/sacloud/iaas-api-go/search" 25 "github.com/sacloud/iaas-api-go/search/keys" 26 "github.com/sacloud/iaas-api-go/types" 27 ) 28 29 // NFSPlanInfo NFSプランIDに対応するプラン情報 30 type NFSPlanInfo struct { 31 NFSPlanID types.ID 32 Size types.ENFSSize 33 DiskPlanID types.ID 34 } 35 36 // GetNFSPlanInfo NFSプランIDから対応するプラン情報を取得 37 func GetNFSPlanInfo(ctx context.Context, finder NoteFinder, nfsPlanID types.ID) (*NFSPlanInfo, error) { 38 plans, err := findNFSPlans(ctx, finder) 39 if err != nil { 40 return nil, err 41 } 42 info := plans.findByPlanID(nfsPlanID) 43 if info == nil { 44 return nil, fmt.Errorf("nfs plan [id:%d] not found", nfsPlanID) 45 } 46 return info, nil 47 } 48 49 // FindNFSPlanID ディスクプランとサイズからNFSのプランIDを取得 50 func FindNFSPlanID(ctx context.Context, finder NoteFinder, diskPlanID types.ID, size types.ENFSSize) (types.ID, error) { 51 plans, err := findNFSPlans(ctx, finder) 52 if err != nil { 53 return types.ID(0), err 54 } 55 return plans.findPlanID(diskPlanID, size), nil 56 } 57 58 type nfsPlansEnvelope struct { 59 Plans *nfsPlans `json:"plans"` 60 } 61 62 type nfsPlans struct { 63 HDD []nfsPlanValue 64 SSD []nfsPlanValue 65 } 66 67 func (p *nfsPlans) findPlanID(diskPlanID types.ID, size types.ENFSSize) types.ID { 68 var plans []nfsPlanValue 69 switch diskPlanID { 70 case types.NFSPlans.HDD: 71 plans = p.HDD 72 case types.NFSPlans.SSD: 73 plans = p.SSD 74 default: 75 return types.ID(0) 76 } 77 78 for _, plan := range plans { 79 if plan.Availability.IsAvailable() && plan.Size == int(size) { 80 return plan.PlanID 81 } 82 } 83 84 return types.ID(0) 85 } 86 87 func (p *nfsPlans) findByPlanID(planID types.ID) *NFSPlanInfo { 88 for _, p := range p.HDD { 89 if p.PlanID == planID { 90 return &NFSPlanInfo{ 91 NFSPlanID: planID, 92 DiskPlanID: types.NFSPlans.HDD, 93 Size: types.ENFSSize(p.Size), 94 } 95 } 96 } 97 for _, p := range p.SSD { 98 if p.PlanID == planID { 99 return &NFSPlanInfo{ 100 NFSPlanID: planID, 101 DiskPlanID: types.NFSPlans.SSD, 102 Size: types.ENFSSize(p.Size), 103 } 104 } 105 } 106 return nil 107 } 108 109 type nfsPlanValue struct { 110 Size int `json:"size"` 111 Availability types.EAvailability `json:"availability"` 112 PlanID types.ID `json:"planId"` 113 } 114 115 func findNFSPlans(ctx context.Context, finder NoteFinder) (*nfsPlans, error) { 116 // find note 117 searched, err := finder.Find(ctx, &iaas.FindCondition{ 118 Filter: search.Filter{ 119 search.Key(keys.Name): "sys-nfs", 120 search.Key("Class"): "json", 121 }, 122 }) 123 if err != nil { 124 return nil, err 125 } 126 if searched.Count == 0 || len(searched.Notes) == 0 { 127 return nil, errors.New("note[sys-nfs] not found") 128 } 129 note := searched.Notes[0] 130 131 // parse note's content 132 var pe nfsPlansEnvelope 133 if err := json.Unmarshal([]byte(note.Content), &pe); err != nil { 134 return nil, err 135 } 136 return pe.Plans, nil 137 }