dns-orchestrator-provider 0.1.2

DNS provider abstraction library for multiple cloud platforms (Cloudflare, Aliyun, DNSPod, Huaweicloud)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! `DNSPod` wrong mapping

use crate::error::ProviderError;
use crate::traits::{ErrorContext, ProviderErrorMapper, RawApiError};

use super::DnspodProvider;

/// `DNSPod` Error code mapping
/// Reference: <https://cloud.tencent.com/document/api/1427/56192>
impl ProviderErrorMapper for DnspodProvider {
    fn provider_name(&self) -> &'static str {
        "dnspod"
    }

    fn map_error(&self, raw: RawApiError, context: ErrorContext) -> ProviderError {
        match raw.code.as_deref() {
            // ============ Authentication error ============
            Some(
                "AuthFailure"
                | "AuthFailure.InvalidAuthorization"
                | "AuthFailure.InvalidSecretId"
                | "AuthFailure.MFAFailure"
                | "AuthFailure.SecretIdNotFound"
                | "AuthFailure.SignatureExpire"
                | "AuthFailure.SignatureFailure"
                | "AuthFailure.TokenFailure"
                | "AuthFailure.UnauthorizedOperation"
                | "InvalidParameter.InvalidSecretId"
                | "InvalidParameter.InvalidSignature"
                | "InvalidParameter.PermissionDenied"
                | "InvalidParameter.LoginTokenIdError"
                | "InvalidParameter.LoginTokenNotExists"
                | "InvalidParameter.LoginTokenValidateFailed",
            ) => ProviderError::InvalidCredentials {
                provider: self.provider_name().to_string(),
                raw_message: Some(raw.message.clone()),
            },

            // ============ Quota limit (resources run out, no retry) ============
            Some(
                "LimitExceeded"
                | "LimitExceeded.AAAACountLimit"
                | "LimitExceeded.AtNsRecordLimit"
                | "LimitExceeded.CustomLineLimited"
                | "LimitExceeded.DomainAliasCountExceeded"
                | "LimitExceeded.DomainAliasNumberLimit"
                | "LimitExceeded.FailedLoginLimitExceeded"
                | "LimitExceeded.GroupNumberLimit"
                | "LimitExceeded.HiddenUrlExceeded"
                | "LimitExceeded.NsCountLimit"
                | "LimitExceeded.OffsetExceeded"
                | "LimitExceeded.SrvCountLimit"
                | "LimitExceeded.SubdomainLevelLimit"
                | "LimitExceeded.SubdomainRollLimit"
                | "LimitExceeded.SubdomainWcardLimit"
                | "LimitExceeded.UrlCountLimit"
                | "RequestLimitExceeded.GlobalRegionUinLimitExceeded"
                | "RequestLimitExceeded.IPLimitExceeded"
                | "RequestLimitExceeded.UinLimitExceeded"
                | "RequestLimitExceeded.BatchTaskLimit"
                | "RequestLimitExceeded.CreateDomainLimit",
            ) => ProviderError::QuotaExceeded {
                provider: self.provider_name().to_string(),
                raw_message: Some(raw.message),
            },

            // ============ Frequency limit (temporary limit, can be retried) ============
            Some(
                "RequestLimitExceeded"
                | "RequestLimitExceeded.RequestLimitExceeded"
                | "FailedOperation.FrequencyLimit"
                | "InvalidParameter.OperationIsTooFrequent",
            ) => ProviderError::RateLimited {
                provider: self.provider_name().to_string(),
                retry_after: None,
                raw_message: Some(raw.message),
            },

            // ============ Record already exists ============
            Some("InvalidParameter.DomainRecordExist") => ProviderError::RecordExists {
                provider: self.provider_name().to_string(),
                record_name: context
                    .record_name
                    .unwrap_or_else(|| "<unknown>".to_string()),
                raw_message: Some(raw.message),
            },

            // ============ The domain name does not exist ============
            Some("ResourceNotFound.NoDataOfDomain" | "InvalidParameterValue.DomainNotExists") => {
                ProviderError::DomainNotFound {
                    provider: self.provider_name().to_string(),
                    domain: context.domain.unwrap_or_else(|| "<unknown>".to_string()),
                    raw_message: Some(raw.message),
                }
            }

            // ============ Domain name is locked/disabled ============
            Some(
                "FailedOperation.DomainIsLocked"
                | "FailedOperation.DomainIsSpam"
                | "FailedOperation.AccountIsLocked"
                | "InvalidParameter.UserAlreadyLocked"
                | "InvalidParameter.DomainIsNotlocked"
                | "InvalidParameter.DomainNotAllowedLock",
            ) => ProviderError::DomainLocked {
                provider: self.provider_name().to_string(),
                domain: context.domain.unwrap_or_else(|| "<unknown>".to_string()),
                raw_message: Some(raw.message),
            },

            // ============ Permission/Operation Denied ============
            Some(
                "OperationDenied"
                | "OperationDenied.AccessDenied"
                | "OperationDenied.DomainOwnerAllowedOnly"
                | "OperationDenied.NoPermissionToOperateDomain"
                | "OperationDenied.NotAdmin"
                | "OperationDenied.NotAgent"
                | "OperationDenied.NotGrantedByOwner"
                | "OperationDenied.NotManagedUser"
                | "OperationDenied.NotOrderOwner"
                | "OperationDenied.NotResourceOwner"
                | "OperationDenied.AgentDenied"
                | "OperationDenied.AgentSubordinateDenied"
                | "UnauthorizedOperation"
                | "FailedOperation.NotDomainOwner"
                | "FailedOperation.NotResourceOwner"
                | "FailedOperation.NotBatchTaskOwner"
                | "InvalidParameter.NoAuthorityToSrcDomain"
                | "InvalidParameter.NoAuthorityToTheGroup",
            ) => ProviderError::PermissionDenied {
                provider: self.provider_name().to_string(),
                raw_message: Some(raw.message),
            },

            // ============ Invalid parameter - line ============
            Some("InvalidParameter.RecordLineInvalid" | "InvalidParameter.LineNotExist") => {
                ProviderError::InvalidParameter {
                    provider: self.provider_name().to_string(),
                    param: "line".to_string(),
                    detail: raw.message,
                }
            }

            // ============ Invalid parameter - record type ============
            Some("InvalidParameter.RecordTypeInvalid") => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "type".to_string(),
                detail: raw.message,
            },

            // ============ Invalid parameter - record value ============
            Some(
                "InvalidParameter.RecordValueInvalid" | "InvalidParameter.RecordValueLengthInvalid",
            ) => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "value".to_string(),
                detail: raw.message,
            },

            // ============ Invalid parameter - subdomain ============
            Some("InvalidParameter.SubdomainInvalid") => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "subdomain".to_string(),
                detail: raw.message,
            },

            // ============ Invalid parameter - TTL ============
            Some("LimitExceeded.RecordTtlLimit") => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "ttl".to_string(),
                detail: raw.message,
            },

            // ============ Invalid parameter - MX priority ============
            Some("InvalidParameter.MxInvalid") => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "mx".to_string(),
                detail: raw.message,
            },

            // ============ Invalid parameter - domain name ============
            Some(
                "InvalidParameter.DomainIdInvalid"
                | "InvalidParameter.DomainInvalid"
                | "InvalidParameter.DomainTooLong"
                | "InvalidParameter.DomainTypeInvalid",
            ) => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "domain".to_string(),
                detail: raw.message,
            },

            // ============ Invalid parameter - record ID ============
            Some("InvalidParameter.RecordIdInvalid") => ProviderError::InvalidParameter {
                provider: self.provider_name().to_string(),
                param: "record_id".to_string(),
                detail: raw.message,
            },

            // ============ Other errors fallback ============
            _ => self.unknown_error(raw),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::RawApiError;

    fn provider() -> DnspodProvider {
        DnspodProvider::new(String::new(), String::new())
    }

    fn default_ctx() -> ErrorContext {
        ErrorContext::default()
    }

    fn ctx_with_record_name(name: &str) -> ErrorContext {
        ErrorContext {
            record_name: Some(name.to_string()),
            ..Default::default()
        }
    }

    fn ctx_with_domain(domain: &str) -> ErrorContext {
        ErrorContext {
            domain: Some(domain.to_string()),
            ..Default::default()
        }
    }

    // ---- Authentication error ----

    #[test]
    fn auth_failure_maps_to_invalid_credentials() {
        let p = provider();
        for code in [
            "AuthFailure",
            "AuthFailure.InvalidSecretId",
            "InvalidParameter.LoginTokenNotExists",
        ] {
            let raw = RawApiError::with_code(code, "auth failed");
            let err = p.map_error(raw, default_ctx());
            assert!(
                matches!(err, ProviderError::InvalidCredentials { .. }),
                "expected InvalidCredentials for code '{code}', got {err:?}"
            );
        }
    }

    // ---- Quota Limitation ----

    #[test]
    fn quota_codes_map_to_quota_exceeded() {
        let p = provider();
        for code in [
            "LimitExceeded",
            "LimitExceeded.AAAACountLimit",
            "RequestLimitExceeded.IPLimitExceeded",
        ] {
            let raw = RawApiError::with_code(code, "quota hit");
            let err = p.map_error(raw, default_ctx());
            assert!(
                matches!(err, ProviderError::QuotaExceeded { .. }),
                "expected QuotaExceeded for code '{code}', got {err:?}"
            );
        }
    }

    // ---- Frequency current limit ----

    #[test]
    fn rate_limit_codes_map_to_rate_limited() {
        let p = provider();
        for code in [
            "RequestLimitExceeded",
            "FailedOperation.FrequencyLimit",
            "InvalidParameter.OperationIsTooFrequent",
        ] {
            let raw = RawApiError::with_code(code, "slow down");
            let err = p.map_error(raw, default_ctx());
            assert!(
                matches!(
                    err,
                    ProviderError::RateLimited {
                        retry_after: None,
                        ..
                    }
                ),
                "expected RateLimited for code '{code}', got {err:?}"
            );
        }
    }

    // ---- Record already exists ----

    #[test]
    fn record_exist_maps_to_record_exists() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.DomainRecordExist", "dup");
        let err = p.map_error(raw, ctx_with_record_name("www"));
        assert!(
            matches!(err, ProviderError::RecordExists { ref record_name, .. } if record_name == "www"),
            "expected RecordExists, got {err:?}"
        );
    }

    // ---- The domain name does not exist ----

    #[test]
    fn domain_not_found_codes() {
        let p = provider();
        for code in [
            "ResourceNotFound.NoDataOfDomain",
            "InvalidParameterValue.DomainNotExists",
        ] {
            let raw = RawApiError::with_code(code, "no domain");
            let err = p.map_error(raw, ctx_with_domain("example.com"));
            assert!(
                matches!(err, ProviderError::DomainNotFound { ref domain, .. } if domain == "example.com"),
                "expected DomainNotFound for code '{code}', got {err:?}"
            );
        }
    }

    // ---- Domain name is locked ----

    #[test]
    fn domain_locked_codes() {
        let p = provider();
        for code in [
            "FailedOperation.DomainIsLocked",
            "FailedOperation.DomainIsSpam",
        ] {
            let raw = RawApiError::with_code(code, "locked");
            let err = p.map_error(raw, ctx_with_domain("example.com"));
            assert!(
                matches!(err, ProviderError::DomainLocked { ref domain, .. } if domain == "example.com"),
                "expected DomainLocked for code '{code}', got {err:?}"
            );
        }
    }

    // ---- Permission denied ----

    #[test]
    fn permission_denied_codes() {
        let p = provider();
        for code in [
            "OperationDenied",
            "UnauthorizedOperation",
            "FailedOperation.NotDomainOwner",
        ] {
            let raw = RawApiError::with_code(code, "denied");
            let err = p.map_error(raw, default_ctx());
            assert!(
                matches!(err, ProviderError::PermissionDenied { .. }),
                "expected PermissionDenied for code '{code}', got {err:?}"
            );
        }
    }

    // ---- Invalid parameter - line ----

    #[test]
    fn invalid_param_line() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.RecordLineInvalid", "bad line");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "line"),
            "expected InvalidParameter(line), got {err:?}"
        );
    }

    // ---- Invalid parameter - record type ----

    #[test]
    fn invalid_param_type() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.RecordTypeInvalid", "bad type");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "type"),
            "expected InvalidParameter(type), got {err:?}"
        );
    }

    // ---- Invalid parameter - record value ----

    #[test]
    fn invalid_param_value() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.RecordValueInvalid", "bad value");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "value"),
            "expected InvalidParameter(value), got {err:?}"
        );
    }

    // ---- Invalid parameter - subdomain name ----

    #[test]
    fn invalid_param_subdomain() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.SubdomainInvalid", "bad sub");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "subdomain"),
            "expected InvalidParameter(subdomain), got {err:?}"
        );
    }

    // ---- Invalid parameter - TTL ----

    #[test]
    fn invalid_param_ttl() {
        let p = provider();
        let raw = RawApiError::with_code("LimitExceeded.RecordTtlLimit", "ttl too high");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "ttl"),
            "expected InvalidParameter(ttl), got {err:?}"
        );
    }

    // ---- Invalid parameter - MX ----

    #[test]
    fn invalid_param_mx() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.MxInvalid", "bad mx");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "mx"),
            "expected InvalidParameter(mx), got {err:?}"
        );
    }

    // ---- Invalid parameter - domain name ----

    #[test]
    fn invalid_param_domain() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.DomainIdInvalid", "bad domain id");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "domain"),
            "expected InvalidParameter(domain), got {err:?}"
        );
    }

    // ---- Invalid parameter - Record ID ----

    #[test]
    fn invalid_param_record_id() {
        let p = provider();
        let raw = RawApiError::with_code("InvalidParameter.RecordIdInvalid", "bad record id");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::InvalidParameter { ref param, .. } if param == "record_id"),
            "expected InvalidParameter(record_id), got {err:?}"
        );
    }

    // ---- Fallback: Unknown error code ----

    #[test]
    fn unknown_code_maps_to_unknown() {
        let p = provider();
        let raw = RawApiError::with_code("SomeNewError.NeverSeenBefore", "surprise");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::Unknown { ref raw_code, .. } if raw_code.as_deref() == Some("SomeNewError.NeverSeenBefore")),
            "expected Unknown with raw_code, got {err:?}"
        );
    }

    // ---- Fallback: No error code ----

    #[test]
    fn no_code_maps_to_unknown() {
        let p = provider();
        let raw = RawApiError::new("something went wrong");
        let err = p.map_error(raw, default_ctx());
        assert!(
            matches!(err, ProviderError::Unknown { ref raw_code, .. } if raw_code.is_none()),
            "expected Unknown with no raw_code, got {err:?}"
        );
    }
}