github.com/sacloud/libsacloud/v2@v2.32.3/helper/service/nfs/builder.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 nfs 16 17 import ( 18 "context" 19 "errors" 20 21 "github.com/sacloud/libsacloud/v2/helper/query" 22 "github.com/sacloud/libsacloud/v2/helper/wait" 23 24 "github.com/sacloud/libsacloud/v2/pkg/util" 25 "github.com/sacloud/libsacloud/v2/sacloud" 26 "github.com/sacloud/libsacloud/v2/sacloud/types" 27 ) 28 29 type Builder struct { 30 ID types.ID 31 Zone string 32 33 Name string 34 Description string 35 Tags types.Tags 36 IconID types.ID 37 SwitchID types.ID 38 39 Plan types.ID 40 Size types.ENFSSize 41 42 IPAddresses []string 43 NetworkMaskLen int 44 DefaultRoute string 45 46 Caller sacloud.APICaller 47 NoWait bool 48 } 49 50 func (b *Builder) Build(ctx context.Context) (*sacloud.NFS, error) { 51 if b.ID.IsEmpty() { 52 return b.create(ctx) 53 } 54 return b.update(ctx) 55 } 56 57 func (b *Builder) findPlanID(ctx context.Context) (types.ID, error) { 58 return query.FindNFSPlanID(ctx, sacloud.NewNoteOp(b.Caller), b.Plan, b.Size) 59 } 60 61 func (b *Builder) create(ctx context.Context) (*sacloud.NFS, error) { 62 client := sacloud.NewNFSOp(b.Caller) 63 planID, err := b.findPlanID(ctx) 64 if err != nil { 65 return nil, err 66 } 67 created, err := client.Create(ctx, b.Zone, &sacloud.NFSCreateRequest{ 68 SwitchID: b.SwitchID, 69 PlanID: planID, 70 IPAddresses: b.IPAddresses, 71 NetworkMaskLen: b.NetworkMaskLen, 72 DefaultRoute: b.DefaultRoute, 73 Name: b.Name, 74 Description: b.Description, 75 Tags: b.Tags, 76 IconID: b.IconID, 77 }) 78 if err != nil { 79 return nil, err 80 } 81 if b.NoWait { 82 return created, nil 83 } 84 85 return wait.UntilNFSIsUp(ctx, client, b.Zone, created.ID) 86 } 87 88 func (b *Builder) validateForUpdate(ctx context.Context, current *sacloud.NFS) error { 89 planID, err := b.findPlanID(ctx) 90 if err != nil { 91 return err 92 } 93 if current.SwitchID != b.SwitchID { 94 return errors.New("SwitchID cannot be changed") 95 } 96 if current.PlanID != planID { 97 return errors.New("Plan/Size cannot be changed") 98 } 99 if !util.DeepEqual(current.IPAddresses, b.IPAddresses) { 100 return errors.New("IPAddresses cannot be changed") 101 } 102 if current.NetworkMaskLen != b.NetworkMaskLen { 103 return errors.New("NetworkMaskLen cannot be changed") 104 } 105 if current.DefaultRoute != b.DefaultRoute { 106 return errors.New("DefaultRoute cannot be changed") 107 } 108 return nil 109 } 110 111 func (b *Builder) update(ctx context.Context) (*sacloud.NFS, error) { 112 client := sacloud.NewNFSOp(b.Caller) 113 current, err := client.Read(ctx, b.Zone, b.ID) 114 if err != nil { 115 return nil, err 116 } 117 if err := b.validateForUpdate(ctx, current); err != nil { 118 return nil, err 119 } 120 121 return client.Update(ctx, b.Zone, b.ID, &sacloud.NFSUpdateRequest{ 122 Name: b.Name, 123 Description: b.Description, 124 Tags: b.Tags, 125 IconID: b.IconID, 126 }) 127 }