ops_esme.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 "math/rand"
21 "time"
22
23 "github.com/sacloud/iaas-api-go"
24 "github.com/sacloud/iaas-api-go/types"
25 )
26
27 // Find is fake implementation
28 func (o *ESMEOp) Find(ctx context.Context, conditions *iaas.FindCondition) (*iaas.ESMEFindResult, error) {
29 results, _ := find(o.key, iaas.APIDefaultZone, conditions)
30 var values []*iaas.ESME
31 for _, res := range results {
32 dest := &iaas.ESME{}
33 copySameNameField(res, dest)
34 values = append(values, dest)
35 }
36 return &iaas.ESMEFindResult{
37 Total: len(results),
38 Count: len(results),
39 From: 0,
40 ESME: values,
41 }, nil
42 }
43
44 // Create is fake implementation
45 func (o *ESMEOp) Create(ctx context.Context, param *iaas.ESMECreateRequest) (*iaas.ESME, error) {
46 result := &iaas.ESME{}
47 copySameNameField(param, result)
48 fill(result, fillID, fillCreatedAt)
49 result.Availability = types.Availabilities.Available
50
51 putESME(iaas.APIDefaultZone, result)
52 return result, nil
53 }
54
55 // Read is fake implementation
56 func (o *ESMEOp) Read(ctx context.Context, id types.ID) (*iaas.ESME, error) {
57 value := getESMEByID(iaas.APIDefaultZone, id)
58 if value == nil {
59 return nil, newErrorNotFound(o.key, id)
60 }
61 dest := &iaas.ESME{}
62 copySameNameField(value, dest)
63 return dest, nil
64 }
65
66 // Update is fake implementation
67 func (o *ESMEOp) Update(ctx context.Context, id types.ID, param *iaas.ESMEUpdateRequest) (*iaas.ESME, error) {
68 value, err := o.Read(ctx, id)
69 if err != nil {
70 return nil, err
71 }
72 copySameNameField(param, value)
73 fill(value, fillModifiedAt)
74 putESME(iaas.APIDefaultZone, value)
75
76 return value, nil
77 }
78
79 // Delete is fake implementation
80 func (o *ESMEOp) Delete(ctx context.Context, id types.ID) error {
81 _, err := o.Read(ctx, id)
82 if err != nil {
83 return err
84 }
85
86 ds().Delete(o.key, iaas.APIDefaultZone, id)
87 return nil
88 }
89
90 // randomName testutilパッケージからのコピー(循環参照を防ぐため) TODO パッケージ構造の見直し
91 func (o *ESMEOp) randomName(strlen int) string {
92 charSetNumber := "012346789"
93 result := make([]byte, strlen)
94 for i := 0; i < strlen; i++ {
95 result[i] = charSetNumber[rand.Intn(len(charSetNumber))] //nolint:gosec
96 }
97 return string(result)
98 }
99
100 func (o *ESMEOp) generateMessageID() string {
101 return fmt.Sprintf("%s-%s-%s-%s-%s",
102 o.randomName(8),
103 o.randomName(4),
104 o.randomName(4),
105 o.randomName(4),
106 o.randomName(12),
107 )
108 }
109
110 func (o *ESMEOp) SendMessageWithGeneratedOTP(ctx context.Context, id types.ID, param *iaas.ESMESendMessageWithGeneratedOTPRequest) (*iaas.ESMESendMessageResult, error) {
111 _, err := o.Read(ctx, id)
112 if err != nil {
113 return nil, err
114 }
115
116 result := &iaas.ESMESendMessageResult{
117 MessageID: o.generateMessageID(),
118 Status: "Accepted", // Note: 現在のfakeドライバでは"Delivered"に変更する処理は未実装
119 OTP: o.randomName(6),
120 }
121
122 logs, err := o.Logs(ctx, id)
123 if err != nil {
124 return nil, err
125 }
126 logs = append(logs, &iaas.ESMELogs{
127 MessageID: result.MessageID,
128 Status: result.Status,
129 OTP: result.OTP,
130 Destination: param.Destination,
131 SentAt: time.Now(),
132 RetryCount: 0,
133 })
134 ds().Put(o.key+"Logs", iaas.APIDefaultZone, id, logs)
135
136 return result, nil
137 }
138
139 func (o *ESMEOp) SendMessageWithInputtedOTP(ctx context.Context, id types.ID, param *iaas.ESMESendMessageWithInputtedOTPRequest) (*iaas.ESMESendMessageResult, error) {
140 _, err := o.Read(ctx, id)
141 if err != nil {
142 return nil, err
143 }
144
145 result := &iaas.ESMESendMessageResult{
146 MessageID: o.generateMessageID(),
147 Status: "Accepted", // Note: 現在のfakeドライバでは"Delivered"に変更する処理は未実装
148 OTP: param.OTP,
149 }
150
151 logs, err := o.Logs(ctx, id)
152 if err != nil {
153 return nil, err
154 }
155 logs = append(logs, &iaas.ESMELogs{
156 MessageID: result.MessageID,
157 Status: result.Status,
158 OTP: result.OTP,
159 Destination: param.Destination,
160 SentAt: time.Now(),
161 RetryCount: 0,
162 })
163 ds().Put(o.key+"Logs", iaas.APIDefaultZone, id, logs)
164
165 return result, nil
166 }
167
168 func (o *ESMEOp) Logs(ctx context.Context, id types.ID) ([]*iaas.ESMELogs, error) {
169 _, err := o.Read(ctx, id)
170 if err != nil {
171 return nil, err
172 }
173
174 v := ds().Get(o.key+"Logs", iaas.APIDefaultZone, id)
175 if v == nil {
176 return nil, nil
177 }
178 return v.([]*iaas.ESMELogs), nil
179 }
180