aboutsummaryrefslogtreecommitdiffstats
path: root/crates/configuration/src/state/routing/mutate_routing.rs
blob: 105cf181a570bddabf4750e45cc041ae76a1a3fc (plain)
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
use opentelemetry_semantic_conventions::attribute;
use tonic::{Request, Response, Status, async_trait};
use tracing::{Instrument, error, info_span, instrument, trace};
use tracing_opentelemetry::OpenTelemetrySpanExt;
use uuid::Uuid;
use warden_core::configuration::{
    ReloadEvent,
    routing::{
        DeleteConfigurationRequest, RoutingConfiguration, UpdateRoutingRequest,
        mutate_routing_server::MutateRouting,
    },
};

use crate::state::{AppHandle, cache_key::CacheKey, invalidate_cache, publish_reload};

#[allow(dead_code)]
struct RoutingRow {
    id: Uuid,
    configuration: sqlx::types::Json<RoutingConfiguration>,
}

#[async_trait]
impl MutateRouting for AppHandle {
    #[instrument(skip(self, request))]
    async fn create_routing_configuration(
        &self,
        request: Request<RoutingConfiguration>,
    ) -> Result<Response<RoutingConfiguration>, Status> {
        trace!("creating routing configuration");

        let request = request.into_inner();
        let span = info_span!("create.configuration.routing");
        span.set_attribute(attribute::DB_SYSTEM_NAME, "postgres");
        span.set_attribute(attribute::DB_OPERATION_NAME, "insert");
        span.set_attribute(attribute::DB_COLLECTION_NAME, "routing");
        span.set_attribute("otel.kind", "client");

        sqlx::query!(
            "insert into routing (id, configuration) values ($1, $2)",
            Uuid::now_v7(),
            sqlx::types::Json(&request) as _
        )
        .execute(&self.services.postgres)
        .instrument(span)
        .await
        .map_err(|e| {
            error!("{e}");
            tonic::Status::internal("database error")
        })?;
        trace!("configuration created");

        Ok(tonic::Response::new(request))
    }

    async fn update_routing_configuration(
        &self,
        request: Request<UpdateRoutingRequest>,
    ) -> Result<Response<RoutingConfiguration>, Status> {
        let conf = self
            .app_config
            .nats
            .subject
            .split(".")
            .next()
            .expect("checked on startup");

        let request = request.into_inner();
        let id = Uuid::parse_str(&request.id)
            .map_err(|_e| tonic::Status::invalid_argument("id is not a uuid"))?;

        let config = request.configuration.expect("configuration to be provided");

        let span = info_span!("update.configuration.routing");
        span.set_attribute(attribute::DB_SYSTEM_NAME, "postgres");
        span.set_attribute(attribute::DB_OPERATION_NAME, "update");
        span.set_attribute(attribute::DB_COLLECTION_NAME, "routing");
        span.set_attribute("otel.kind", "client");

        trace!("updating configuration");

        let updated = sqlx::query_as!(
            RoutingRow,
            r#"
                update routing
                set configuration = $1
                where id = $2
                returning id, configuration as "configuration: sqlx::types::Json<RoutingConfiguration>"
            "#,
            sqlx::types::Json(&config) as _,
            id
        )
        .fetch_one(&self.services.postgres)
        .instrument(span)
        .await
        .map_err(|e| {
            error!("{e}");
            tonic::Status::internal("database is not ready")
        })?;
        trace!("configuration updated");

        let (_del_result, _publish_result) = tokio::try_join!(
            invalidate_cache(self, CacheKey::Routing(&id)),
            publish_reload(self, conf, ReloadEvent::Routing)
        )?;

        let res = updated.configuration.0;

        Ok(Response::new(res))
    }

    async fn delete_routing_configuration(
        &self,
        request: Request<DeleteConfigurationRequest>,
    ) -> Result<Response<RoutingConfiguration>, Status> {
        let conf = self
            .app_config
            .nats
            .subject
            .split(".")
            .next()
            .expect("checked on startup");

        let request = request.into_inner();
        let id = Uuid::parse_str(&request.id)
            .map_err(|_e| tonic::Status::invalid_argument("id is not a uuid"))?;

        let span = info_span!("delete.configuration.routing");
        span.set_attribute(attribute::DB_SYSTEM_NAME, "postgres");
        span.set_attribute(attribute::DB_OPERATION_NAME, "delete");
        span.set_attribute(attribute::DB_COLLECTION_NAME, "routing");
        span.set_attribute("otel.kind", "client");
        trace!("deleting configuration");

        let updated = sqlx::query_as!(
            RoutingRow,
            r#"
                delete from routing
                where id = $1
                returning id, configuration as "configuration: sqlx::types::Json<RoutingConfiguration>"
            "#,
            id
        )
        .fetch_one(&self.services.postgres)
        .instrument(span)
        .await
        .map_err(|e| {
            error!("{e}");
            tonic::Status::internal("database is not ready")
        })?;
        trace!("configuration deleted");

        let (_del_result, _publish_result) = tokio::try_join!(
            invalidate_cache(self, CacheKey::Routing(&id)),
            publish_reload(self, conf, ReloadEvent::Routing)
        )?;

        let res = updated.configuration.0;

        Ok(Response::new(res))
    }
}