|
4 | 4 |
|
5 | 5 | use super::*; |
6 | 6 |
|
| 7 | +fn byoip_cidr_xml(c: &ByoipCidr) -> String { |
| 8 | + format!( |
| 9 | + "{}{}{}", |
| 10 | + ec2_elem("cidr", &c.cidr), |
| 11 | + ec2_elem("description", &c.description), |
| 12 | + ec2_elem("state", &c.state), |
| 13 | + ) |
| 14 | +} |
| 15 | + |
| 16 | +/// Update the persisted BYOIP CIDR's state (inserting it when unknown), then |
| 17 | +/// render it. Shared by advertise/withdraw so each op round-trips. |
| 18 | +fn set_byoip_state(svc: &Ec2Service, req: &AwsRequest, cidr: &str, state: &str) -> ByoipCidr { |
| 19 | + let mut accounts = svc.state.write(); |
| 20 | + let account = accounts.get_or_create(&req.account_id); |
| 21 | + let entry = account |
| 22 | + .byoip_cidrs |
| 23 | + .entry(cidr.to_string()) |
| 24 | + .or_insert_with(|| ByoipCidr { |
| 25 | + cidr: cidr.to_string(), |
| 26 | + description: String::new(), |
| 27 | + state: state.to_string(), |
| 28 | + }); |
| 29 | + entry.state = state.to_string(); |
| 30 | + entry.clone() |
| 31 | +} |
| 32 | + |
7 | 33 | pub(crate) fn advertise_byoip_cidr( |
8 | | - _svc: &Ec2Service, |
| 34 | + svc: &Ec2Service, |
9 | 35 | req: &AwsRequest, |
10 | 36 | ) -> Result<AwsResponse, AwsServiceError> { |
11 | | - require(&req.query_params, "Cidr")?; |
| 37 | + let cidr = require(&req.query_params, "Cidr")?; |
| 38 | + let c = set_byoip_state(svc, req, &cidr, "advertised"); |
12 | 39 | Ok(Ec2Service::respond( |
13 | 40 | "AdvertiseByoipCidr", |
14 | 41 | &req.request_id, |
15 | | - "", |
| 42 | + &format!("<byoipCidr>{}</byoipCidr>", byoip_cidr_xml(&c)), |
16 | 43 | )) |
17 | 44 | } |
18 | 45 |
|
19 | 46 | pub(crate) fn deprovision_byoip_cidr( |
20 | | - _svc: &Ec2Service, |
| 47 | + svc: &Ec2Service, |
21 | 48 | req: &AwsRequest, |
22 | 49 | ) -> Result<AwsResponse, AwsServiceError> { |
23 | | - require(&req.query_params, "Cidr")?; |
| 50 | + let cidr = require(&req.query_params, "Cidr")?; |
| 51 | + // Deprovision removes the CIDR from the pool; report the transitional state. |
| 52 | + let mut c = set_byoip_state(svc, req, &cidr, "pending-deprovision"); |
| 53 | + { |
| 54 | + let mut accounts = svc.state.write(); |
| 55 | + accounts |
| 56 | + .get_or_create(&req.account_id) |
| 57 | + .byoip_cidrs |
| 58 | + .remove(&cidr); |
| 59 | + } |
| 60 | + c.state = "pending-deprovision".to_string(); |
24 | 61 | Ok(Ec2Service::respond( |
25 | 62 | "DeprovisionByoipCidr", |
26 | 63 | &req.request_id, |
27 | | - "", |
| 64 | + &format!("<byoipCidr>{}</byoipCidr>", byoip_cidr_xml(&c)), |
28 | 65 | )) |
29 | 66 | } |
30 | 67 |
|
31 | 68 | pub(crate) fn describe_byoip_cidrs( |
32 | | - _svc: &Ec2Service, |
| 69 | + svc: &Ec2Service, |
33 | 70 | req: &AwsRequest, |
34 | 71 | ) -> Result<AwsResponse, AwsServiceError> { |
35 | 72 | require(&req.query_params, "MaxResults")?; |
36 | 73 | validate_max_results(&req.query_params, 1, 100)?; |
| 74 | + let accounts = svc.state.read(); |
| 75 | + let empty = Ec2State::new(&req.account_id, &req.region); |
| 76 | + let state = accounts.get(&req.account_id).unwrap_or(&empty); |
| 77 | + let items: Vec<String> = state.byoip_cidrs.values().map(byoip_cidr_xml).collect(); |
37 | 78 | Ok(Ec2Service::respond( |
38 | 79 | "DescribeByoipCidrs", |
39 | 80 | &req.request_id, |
40 | | - "", |
| 81 | + &ec2_list("byoipCidrSet", &items), |
41 | 82 | )) |
42 | 83 | } |
43 | 84 |
|
44 | 85 | pub(crate) fn provision_byoip_cidr( |
45 | | - _svc: &Ec2Service, |
| 86 | + svc: &Ec2Service, |
46 | 87 | req: &AwsRequest, |
47 | 88 | ) -> Result<AwsResponse, AwsServiceError> { |
48 | | - require(&req.query_params, "Cidr")?; |
| 89 | + let cidr = require(&req.query_params, "Cidr")?; |
| 90 | + let c = ByoipCidr { |
| 91 | + cidr: cidr.clone(), |
| 92 | + description: req |
| 93 | + .query_params |
| 94 | + .get("Description") |
| 95 | + .cloned() |
| 96 | + .unwrap_or_default(), |
| 97 | + state: "provisioned".to_string(), |
| 98 | + }; |
| 99 | + { |
| 100 | + let mut accounts = svc.state.write(); |
| 101 | + accounts |
| 102 | + .get_or_create(&req.account_id) |
| 103 | + .byoip_cidrs |
| 104 | + .insert(cidr, c.clone()); |
| 105 | + } |
49 | 106 | Ok(Ec2Service::respond( |
50 | 107 | "ProvisionByoipCidr", |
51 | 108 | &req.request_id, |
52 | | - "", |
| 109 | + &format!("<byoipCidr>{}</byoipCidr>", byoip_cidr_xml(&c)), |
53 | 110 | )) |
54 | 111 | } |
55 | 112 |
|
56 | 113 | pub(crate) fn withdraw_byoip_cidr( |
57 | | - _svc: &Ec2Service, |
| 114 | + svc: &Ec2Service, |
58 | 115 | req: &AwsRequest, |
59 | 116 | ) -> Result<AwsResponse, AwsServiceError> { |
60 | | - require(&req.query_params, "Cidr")?; |
| 117 | + let cidr = require(&req.query_params, "Cidr")?; |
| 118 | + let c = set_byoip_state(svc, req, &cidr, "provisioned"); |
61 | 119 | Ok(Ec2Service::respond( |
62 | 120 | "WithdrawByoipCidr", |
63 | 121 | &req.request_id, |
64 | | - "", |
| 122 | + &format!("<byoipCidr>{}</byoipCidr>", byoip_cidr_xml(&c)), |
65 | 123 | )) |
66 | 124 | } |
0 commit comments