wadm_client/
topics.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
use wadm_types::api::{DEFAULT_WADM_TOPIC_PREFIX, WADM_STATUS_API_PREFIX};

/// A generator that uses various config options to generate the proper topic names for the wadm API
pub struct TopicGenerator {
    topic_prefix: String,
    model_prefix: String,
}

impl TopicGenerator {
    /// Creates a new topic generator with a lattice ID and an optional API prefix
    pub fn new(lattice: &str, prefix: Option<&str>) -> TopicGenerator {
        let topic_prefix = format!(
            "{}.{}",
            prefix.unwrap_or(DEFAULT_WADM_TOPIC_PREFIX),
            lattice
        );
        let model_prefix = format!("{}.model", topic_prefix);
        TopicGenerator {
            topic_prefix,
            model_prefix,
        }
    }

    /// Returns the full prefix for the topic, including the API prefix and the lattice ID
    pub fn prefix(&self) -> &str {
        &self.topic_prefix
    }

    /// Returns the full prefix for model operations (currently the only operations supported in the
    /// API)
    pub fn model_prefix(&self) -> &str {
        &self.model_prefix
    }

    /// Returns the full topic for a model put operation
    pub fn model_put_topic(&self) -> String {
        format!("{}.put", self.model_prefix())
    }

    /// Returns the full topic for a model get operation
    pub fn model_get_topic(&self, model_name: &str) -> String {
        format!("{}.get.{model_name}", self.model_prefix())
    }

    /// Returns the full topic for a model delete operation
    pub fn model_delete_topic(&self, model_name: &str) -> String {
        format!("{}.del.{model_name}", self.model_prefix())
    }

    /// Returns the full topic for a model list operation
    pub fn model_list_topic(&self) -> String {
        format!("{}.list", self.model_prefix())
    }

    /// Returns the full topic for listing the versions of a model
    pub fn model_versions_topic(&self, model_name: &str) -> String {
        format!("{}.versions.{model_name}", self.model_prefix())
    }

    /// Returns the full topic for a model deploy operation
    pub fn model_deploy_topic(&self, model_name: &str) -> String {
        format!("{}.deploy.{model_name}", self.model_prefix())
    }

    /// Returns the full topic for a model undeploy operation
    pub fn model_undeploy_topic(&self, model_name: &str) -> String {
        format!("{}.undeploy.{model_name}", self.model_prefix())
    }

    /// Returns the full topic for getting a model status
    pub fn model_status_topic(&self, model_name: &str) -> String {
        format!("{}.status.{model_name}", self.model_prefix())
    }

    /// Returns the full topic for WADM status subscriptions
    pub fn wadm_status_topic(&self, app_name: &str) -> String {
        // Extract just the lattice name from topic_prefix
        let lattice = self.topic_prefix.split('.').last().unwrap_or("default");
        format!("{}.{}.{}", WADM_STATUS_API_PREFIX, lattice, app_name)
    }
}