ops_gslb.go raw
1 // Copyright 2022-2025 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 *GSLBOp) Find(ctx context.Context, conditions *iaas.FindCondition) (*iaas.GSLBFindResult, error) {
27 results, _ := find(o.key, iaas.APIDefaultZone, conditions)
28 var values []*iaas.GSLB
29 for _, res := range results {
30 dest := &iaas.GSLB{}
31 copySameNameField(res, dest)
32 values = append(values, dest)
33 }
34 return &iaas.GSLBFindResult{
35 Total: len(results),
36 Count: len(results),
37 From: 0,
38 GSLBs: values,
39 }, nil
40 }
41
42 // Create is fake implementation
43 func (o *GSLBOp) Create(ctx context.Context, param *iaas.GSLBCreateRequest) (*iaas.GSLB, error) {
44 result := &iaas.GSLB{}
45 copySameNameField(param, result)
46 fill(result, fillID, fillCreatedAt, fillAvailability)
47
48 result.FQDN = fmt.Sprintf("site-%d.gslb7.example.ne.jp", result.ID)
49 result.SettingsHash = "settingshash"
50
51 putGSLB(iaas.APIDefaultZone, result)
52 return result, nil
53 }
54
55 // Read is fake implementation
56 func (o *GSLBOp) Read(ctx context.Context, id types.ID) (*iaas.GSLB, error) {
57 value := getGSLBByID(iaas.APIDefaultZone, id)
58 if value == nil {
59 return nil, newErrorNotFound(o.key, id)
60 }
61
62 dest := &iaas.GSLB{}
63 copySameNameField(value, dest)
64 return dest, nil
65 }
66
67 // Update is fake implementation
68 func (o *GSLBOp) Update(ctx context.Context, id types.ID, param *iaas.GSLBUpdateRequest) (*iaas.GSLB, error) {
69 value, err := o.Read(ctx, id)
70 if err != nil {
71 return nil, err
72 }
73 if param.DelayLoop == 0 {
74 param.DelayLoop = 10 // default value
75 }
76 copySameNameField(param, value)
77 fill(value, fillModifiedAt)
78
79 putGSLB(iaas.APIDefaultZone, value)
80 return value, nil
81 }
82
83 // UpdateSettings is fake implementation
84 func (o *GSLBOp) UpdateSettings(ctx context.Context, id types.ID, param *iaas.GSLBUpdateSettingsRequest) (*iaas.GSLB, error) {
85 value, err := o.Read(ctx, id)
86 if err != nil {
87 return nil, err
88 }
89 copySameNameField(param, value)
90 fill(value, fillModifiedAt)
91
92 putGSLB(iaas.APIDefaultZone, value)
93 return value, nil
94 }
95
96 // Delete is fake implementation
97 func (o *GSLBOp) Delete(ctx context.Context, id types.ID) error {
98 _, err := o.Read(ctx, id)
99 if err != nil {
100 return err
101 }
102 ds().Delete(o.key, iaas.APIDefaultZone, id)
103 return nil
104 }
105