|
6 | 6 |
|
7 | 7 | use crate::external_api::params; |
8 | 8 | use crate::external_api::shared; |
9 | | -use crate::external_api::views; |
10 | | -use chrono::Utc; |
11 | 9 | use ipnetwork::IpNetwork; |
12 | 10 | use nexus_db_lookup::LookupPath; |
13 | 11 | use nexus_db_lookup::lookup; |
@@ -80,38 +78,12 @@ impl super::Nexus { |
80 | 78 | // https://github.com/oxidecomputer/omicron/issues/8881 |
81 | 79 | let ip_version = pool_params.ip_version.into(); |
82 | 80 |
|
83 | | - let pool = match ( |
84 | | - pool_params.pool_type.clone(), |
85 | | - pool_params.switch_port_uplinks.is_some(), |
86 | | - ) { |
87 | | - (shared::IpPoolType::Unicast, true) => { |
88 | | - return Err(Error::invalid_request( |
89 | | - "switch_port_uplinks are only allowed for multicast IP pools", |
90 | | - )); |
91 | | - } |
92 | | - (shared::IpPoolType::Unicast, false) => { |
93 | | - if pool_params.mvlan.is_some() { |
94 | | - return Err(Error::invalid_request( |
95 | | - "mvlan is only allowed for multicast IP pools", |
96 | | - )); |
97 | | - } |
| 81 | + let pool = match pool_params.pool_type.clone() { |
| 82 | + shared::IpPoolType::Unicast => { |
98 | 83 | IpPool::new(&pool_params.identity, ip_version) |
99 | 84 | } |
100 | | - (shared::IpPoolType::Multicast, _) => { |
101 | | - let switch_port_ids = self |
102 | | - .resolve_switch_port_ids( |
103 | | - opctx, |
104 | | - self.rack_id(), |
105 | | - &pool_params.switch_port_uplinks, |
106 | | - ) |
107 | | - .await?; |
108 | | - |
109 | | - IpPool::new_multicast( |
110 | | - &pool_params.identity, |
111 | | - ip_version, |
112 | | - switch_port_ids, |
113 | | - pool_params.mvlan, |
114 | | - ) |
| 85 | + shared::IpPoolType::Multicast => { |
| 86 | + IpPool::new_multicast(&pool_params.identity, ip_version) |
115 | 87 | } |
116 | 88 | }; |
117 | 89 |
|
@@ -316,21 +288,7 @@ impl super::Nexus { |
316 | 288 | return Err(not_found_from_lookup(pool_lookup)); |
317 | 289 | } |
318 | 290 |
|
319 | | - let switch_port_ids = self |
320 | | - .resolve_switch_port_ids( |
321 | | - opctx, |
322 | | - self.rack_id(), |
323 | | - &updates.switch_port_uplinks, |
324 | | - ) |
325 | | - .await?; |
326 | | - |
327 | | - let updates_db = IpPoolUpdate { |
328 | | - name: updates.identity.name.clone().map(Into::into), |
329 | | - description: updates.identity.description.clone(), |
330 | | - switch_port_uplinks: switch_port_ids, |
331 | | - mvlan: updates.mvlan.map(|vid| u16::from(vid).into()), |
332 | | - time_modified: Utc::now(), |
333 | | - }; |
| 291 | + let updates_db = IpPoolUpdate::from(updates.clone()); |
334 | 292 |
|
335 | 293 | self.db_datastore.ip_pool_update(opctx, &authz_pool, updates_db).await |
336 | 294 | } |
@@ -544,99 +502,4 @@ impl super::Nexus { |
544 | 502 | opctx.authorize(authz::Action::Modify, &authz_pool).await?; |
545 | 503 | self.db_datastore.ip_pool_delete_range(opctx, &authz_pool, range).await |
546 | 504 | } |
547 | | - |
548 | | - async fn resolve_switch_port_ids( |
549 | | - &self, |
550 | | - opctx: &OpContext, |
551 | | - rack_id: Uuid, |
552 | | - uplinks: &Option<Vec<params::SwitchPortUplink>>, |
553 | | - ) -> Result<Option<Vec<Uuid>>, Error> { |
554 | | - match uplinks { |
555 | | - None => Ok(None), |
556 | | - Some(list) => { |
557 | | - let mut ids = Vec::with_capacity(list.len()); |
558 | | - |
559 | | - for uplink in list { |
560 | | - let switch_location = |
561 | | - Name::from(uplink.switch_location.clone()); |
562 | | - let port_name = Name::from(uplink.port_name.clone()); |
563 | | - let id = self |
564 | | - .db_datastore |
565 | | - .switch_port_get_id( |
566 | | - opctx, |
567 | | - rack_id, |
568 | | - switch_location, |
569 | | - port_name, |
570 | | - ) |
571 | | - .await |
572 | | - .map_err(|_| { |
573 | | - Error::invalid_value( |
574 | | - "switch_port_uplinks", |
575 | | - format!("Switch port '{}' not found", uplink), |
576 | | - ) |
577 | | - })?; |
578 | | - ids.push(id); |
579 | | - } |
580 | | - Ok(Some(ids)) |
581 | | - } |
582 | | - } |
583 | | - } |
584 | | - |
585 | | - /// Convert IP pool with proper switch port name resolution in an async |
586 | | - /// context. |
587 | | - pub(crate) async fn ip_pool_to_view( |
588 | | - &self, |
589 | | - opctx: &OpContext, |
590 | | - pool: db::model::IpPool, |
591 | | - ) -> Result<views::IpPool, Error> { |
592 | | - let identity = pool.identity(); |
593 | | - let pool_type = pool.pool_type; |
594 | | - |
595 | | - // Convert switch port UUIDs to "switch.port" format |
596 | | - let switch_port_uplinks = self |
597 | | - .resolve_switch_port_names(opctx, &pool.switch_port_uplinks) |
598 | | - .await?; |
599 | | - |
600 | | - let mvlan = pool.mvlan.map(|vlan| vlan.into()); |
601 | | - |
602 | | - Ok(views::IpPool { |
603 | | - identity, |
604 | | - ip_version: pool.ip_version.into(), |
605 | | - pool_type: pool_type.into(), |
606 | | - switch_port_uplinks, |
607 | | - mvlan, |
608 | | - }) |
609 | | - } |
610 | | - |
611 | | - // Convert switch port UUIDs to "switch.port" format for views |
612 | | - async fn resolve_switch_port_names( |
613 | | - &self, |
614 | | - opctx: &OpContext, |
615 | | - switch_port_ids: &Option<Vec<Uuid>>, |
616 | | - ) -> Result<Option<Vec<String>>, Error> { |
617 | | - match switch_port_ids { |
618 | | - None => Ok(None), |
619 | | - Some(ids) => { |
620 | | - let mut names = Vec::with_capacity(ids.len()); |
621 | | - for &id in ids { |
622 | | - let switch_port = self |
623 | | - .db_datastore |
624 | | - .switch_port_get(opctx, id) |
625 | | - .await |
626 | | - .map_err(|_| { |
627 | | - Error::internal_error(&format!( |
628 | | - "Switch port with ID {} not found", |
629 | | - id |
630 | | - )) |
631 | | - })?; |
632 | | - let name = format!( |
633 | | - "{}.{}", |
634 | | - switch_port.switch_location, switch_port.port_name |
635 | | - ); |
636 | | - names.push(name); |
637 | | - } |
638 | | - Ok(Some(names)) |
639 | | - } |
640 | | - } |
641 | | - } |
642 | 505 | } |
0 commit comments