Skip to content

Commit

Permalink
fix(clusters): fix list lost op (#1110)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Sep 3, 2024
1 parent ef010d0 commit 8ae85d2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 45 deletions.
37 changes: 13 additions & 24 deletions svc/pkg/cluster/src/ops/server/lost_list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::{
collections::HashSet,
convert::{TryFrom, TryInto},
net::IpAddr,
};
use std::{collections::HashSet, convert::TryInto, net::IpAddr};

use chirp_workflow::prelude::*;
use linode::util::client;
Expand Down Expand Up @@ -39,33 +35,25 @@ pub struct Output {
pub async fn cluster_server_lost_list(ctx: &OperationCtx, input: &Input) -> GlobalResult<Output> {
let linode_token = util::env::read_secret(&["linode", "token"]).await?;

let dc_rows = sql_fetch_all!(
[ctx, (i64, Option<String>)]
let accounts = sql_fetch_all!(
[ctx, (sqlx::types::Json<Provider>, String)]
"
SELECT provider, provider_api_token
SELECT provider2, provider_api_token
FROM db_cluster.datacenters
WHERE
provider_api_token IS NOT NULL AND
($1 IS NULL OR cluster_id = ANY($1))
",
&input.filter.cluster_ids,
)
.await?;

let accounts = dc_rows
.iter()
.map(|(provider, provider_api_token)| {
let provider = Provider::try_from(*provider)?;
// Default token if none is set
let provider_api_token = match &provider {
Provider::Linode => provider_api_token
.clone()
.unwrap_or_else(|| linode_token.clone()),
};

Ok((provider, provider_api_token))
})
.collect::<GlobalResult<HashSet<_>>>()?;
.await?
.into_iter()
.map(|(provider, provider_api_token)| (provider.0, provider_api_token))
.chain(std::iter::once((
Provider::Linode,
util::env::read_secret(&["linode", "token"]).await?,
)))
.collect::<HashSet<_>>();

// Filter by namespace
let filter = json!({
Expand Down Expand Up @@ -141,6 +129,7 @@ async fn run_for_linode_account(
s.datacenter_id,
s.pool_type,
s.pool_type2,
s.provider_server_id,
s.vlan_ip,
s.public_ip,
s.cloud_destroy_ts
Expand Down
39 changes: 18 additions & 21 deletions svc/pkg/cluster/src/ops/server/prune_with_filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, convert::TryFrom};
use std::collections::HashSet;

use chirp_workflow::prelude::*;
use linode::util::{api, client};
Expand Down Expand Up @@ -31,35 +31,27 @@ pub async fn cluster_server_prune_with_filter(
let dc_ids = servers_res
.servers
.iter()
.map(|x| x.server_id)
.map(|x| x.datacenter_id)
.collect::<Vec<_>>();
let dc_rows = sql_fetch_all!(
[ctx, (i64, Option<String>)]
let accounts = sql_fetch_all!(
[ctx, (sqlx::types::Json<Provider>, String)]
"
SELECT provider, provider_api_token
SELECT provider2, provider_api_token
FROM db_cluster.datacenters
WHERE
provider_api_token IS NOT NULL AND
datacenter_id = ANY($1)
",
dc_ids,
)
.await?;

let accounts = dc_rows
.iter()
.map(|(provider, provider_api_token)| {
let provider = Provider::try_from(*provider)?;
// Default token if none is set
let provider_api_token = match &provider {
Provider::Linode => provider_api_token
.clone()
.unwrap_or_else(|| linode_token.clone()),
};

Ok((provider, provider_api_token))
})
.collect::<GlobalResult<HashSet<_>>>()?;
.await?
.into_iter()
.map(|(provider, provider_api_token)| (provider.0, provider_api_token))
.chain(std::iter::once((
Provider::Linode,
util::env::read_secret(&["linode", "token"]).await?,
)))
.collect::<HashSet<_>>();

// Filter by namespace
let filter = json!({
Expand Down Expand Up @@ -93,8 +85,13 @@ async fn run_for_linode_account(
let client =
client::Client::new_with_headers(Some(api_token.to_string()), headers.clone()).await?;

tracing::info!("pruning {} servers", servers.len());

for server in servers {
let linode_id = unwrap_ref!(server.provider_server_id).parse()?;

tracing::info!("pruning {} (linode_id {})", server.server_id, linode_id);

let firewalls = api::list_linode_firewalls(&client, linode_id).await?;

for firewall in firewalls {
Expand Down

0 comments on commit 8ae85d2

Please sign in to comment.