brane_api/
schema.rs

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
//  SCHEMA.rs
//    by Lut99
//
//  Created:
//    17 Oct 2022, 15:17:39
//  Last edited:
//    08 Oct 2024, 15:20:52
//  Auto updated?
//    Yes
//
//  Description:
//!   Defines things that we need when accessing the API with GraphQL.
//

use std::path::PathBuf;
use std::str::FromStr;

use chrono::{DateTime, TimeZone, Utc};
use juniper::{EmptySubscription, FieldResult, GraphQLObject, RootNode, graphql_object};
use log::{debug, info};
use scylla::IntoTypedRows;
use specifications::version::Version;

use crate::packages::PackageUdt;
use crate::spec::Context;

pub type Schema = RootNode<'static, Query, Mutations, EmptySubscription<Context>>;
impl juniper::Context for Context {}

#[derive(Clone, Debug, GraphQLObject)]
pub struct Package {
    pub created: DateTime<Utc>,
    pub description: Option<String>,
    pub detached: bool,
    pub digest: String,
    pub owners: Vec<String>,
    pub id: String,
    pub kind: String,
    pub name: String,
    pub version: String,
    pub functions_as_json: Option<String>,
    pub types_as_json: Option<String>,
}

impl From<PackageUdt> for Package {
    fn from(row: PackageUdt) -> Self {
        let created = Utc.timestamp_millis_opt(row.created).unwrap();

        Package {
            created,
            description: Some(row.description),
            detached: row.detached,
            digest: row.digest,
            owners: row.owners,
            id: row.id.to_string(),
            kind: row.kind,
            name: row.name,
            version: row.version,
            functions_as_json: Some(row.functions_as_json),
            types_as_json: Some(row.types_as_json),
        }
    }
}

pub struct Query;

#[graphql_object(context = Context)]
impl Query {
    #[allow(non_snake_case)]
    async fn apiVersion() -> &'static str {
        info!("Handling GRAPHQL on '/graphql' (i.e., get API version)");
        env!("CARGO_PKG_VERSION")
    }

    async fn packages(name: Option<String>, version: Option<String>, term: Option<String>, context: &Context) -> FieldResult<Vec<Package>> {
        info!("Handling GRAPHQL on '/graphql' (i.e., get packages list)");
        let scylla = context.scylla.clone();

        let like = format!("%{}%", term.unwrap_or_default());
        let query = "SELECT package FROM brane.packages WHERE name LIKE ? ALLOW FILTERING";

        debug!("Querying Scylla database...");
        let mut packages: Vec<Package> = vec![];
        if let Some(rows) = scylla.query(query, &(like,)).await?.rows {
            // Search for all matches of this package
            for row in rows.into_typed::<(PackageUdt,)>() {
                let (package,) = row?;

                if let Some(name) = &name {
                    if name != &package.name {
                        continue;
                    }
                }

                packages.push(package.into());
            }

            // Now find the target version if relevant
            if let Some(version) = version {
                let target_version: Version = Version::from_str(&version)?;
                let mut package: Option<Package> = None;
                let mut version: Option<Version> = None;
                if target_version.is_latest() {
                    for p in packages {
                        // Find the one with the highest version

                        // Parse it as a version
                        let pversion: Version = match Version::from_str(&p.version) {
                            Ok(version) => version,
                            Err(_) => {
                                continue;
                            },
                        };

                        // Compare
                        if package.is_none() || &pversion > version.as_ref().unwrap() {
                            package = Some(p);
                            version = Some(pversion);
                        }
                    }
                } else {
                    for p in packages {
                        // Find the first matching one

                        // Parse it as a version
                        let pversion: Version = match Version::from_str(&p.version) {
                            Ok(version) => version,
                            Err(_) => {
                                continue;
                            },
                        };

                        // Compare
                        if target_version == pversion {
                            package = Some(p);
                        }
                    }
                }

                // Overwrite the list
                packages = if let Some(package) = package { vec![package] } else { vec![] };
            }
        }

        debug!("Returning {} packages", packages.len());
        Ok(packages)
    }
}

pub struct Mutations;

#[graphql_object(context = Context)]
impl Mutations {
    async fn login(_username: String, _password: String, _context: &Context) -> FieldResult<String> {
        info!("Handling GRAPHQL on '/graphql' (i.e., login)");
        todo!();
    }

    async fn unpublish_package(name: String, version: String, context: &Context) -> FieldResult<&str> {
        info!("Handling GRAPHQL on '/graphql' (i.e., unpublish package)");
        let scylla = context.scylla.clone();

        // Get the image file first, tho
        debug!("Querying file path from Scylla database...");
        let query = "SELECT file FROM brane.packages WHERE name = ? AND version = ?";
        let file = scylla.query(query, &(&name, &version)).await?;
        if let Some(rows) = file.rows {
            if rows.is_empty() {
                return Ok("OK!");
            }
            let file: PathBuf = PathBuf::from(rows[0].columns[0].as_ref().unwrap().as_text().unwrap());

            // Delete the thing from the database
            debug!("Deleting package from Scylla database...");
            let query = "DELETE FROM brane.packages WHERE name = ? AND version = ?";
            scylla.query(query, &(&name, &version)).await?;

            // Delete the file
            debug!("Deleting container file '{}'...", file.display());
            tokio::fs::remove_file(&file).await?;
        }

        Ok("OK!")
    }
}