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