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
use anyhow::Result;
use std::collections::HashMap;

use crate::http_client::NacosHTTPClient;
use crate::service::structs::*;

#[derive(Clone, Debug)]
pub struct NacosService {
    client: NacosHTTPClient,
}

impl NacosService {
    pub fn new(client: &NacosHTTPClient) -> Self {
        Self {
            client: client.clone(),
        }
    }

    pub async fn register_instance(&self, args: &RegisterInstance) -> Result<()> {
        self.client.simple_post("/v1/ns/instance", args).await
    }

    pub async fn deregister_instance(&self, args: &DeregistryInstance) -> Result<()> {
        self.client.simple_delete("/v1/ns/instance", args).await
    }

    pub async fn modify_instance(&self, args: &ModifyInstance) -> Result<()> {
        self.client.simple_put("/v1/ns/instance", args).await
    }

    // TODO: FIXME String
    pub async fn query_instances(&self, args: &QueryInstance) -> Result<String> {
        self.client.get_string("/v1/ns/instance/list", args).await
    }

    // TODO: FIXME string
    pub async fn query_instance_detail(&self, args: &QueryInstanceDetail) -> Result<String> {
        self.client.get_string("/v1/ns/instance", args).await
    }

    pub async fn send_instance_beat(&self, args: &SendInstanceBeat) -> Result<()> {
        self.client.simple_put("/v1/ns/instance/beat", args).await
    }

    pub async fn create_service(&self, args: &CreateService) -> Result<()> {
        self.client.simple_post("/v1/ns/service", args).await
    }

    pub async fn update_service(&self, args: &UpdateService) -> Result<()> {
        self.client.simple_put("/v1/ns/instance", args).await
    }

    pub async fn delete_service(&self, args: &DeleteService) -> Result<()> {
        self.client.simple_delete("/v1/ns/instance", args).await
    }

    pub async fn query_service(&self, args: &QueryService) -> Result<String> {
        self.client.get_string("/v1/ns/service", args).await
    }

    pub async fn query_service_list(&self, args: &QueryServiceList) -> Result<String> {
        self.client.get_string("/v1/ns/service/list", args).await
    }

    pub async fn query_system_switch(&self) -> Result<String> {
        let params = HashMap::<String, String>::new();
        self.client
            .get_string("/v1/ns/operator/switches", &params)
            .await
    }

    pub async fn update_system_switch(&self, args: &UpdateSystemSwitch) -> Result<()> {
        self.client
            .simple_put("/v1/ns/operator/switches", args)
            .await
    }

    pub async fn query_system_metrics(&self) -> Result<String> {
        let params = HashMap::<String, String>::new();
        self.client
            .get_string("/v1/ns/operator/metrics", &params)
            .await
    }

    pub async fn query_server_list(&self, healthy: bool) -> Result<String> {
        self.client
            .get_string("/v1/ns/operator/servers", &[("healthy", healthy)])
            .await
    }

    pub async fn get_leader(&self) -> Result<String> {
        self.client
            .get_string("/v1/ns/raft/leader", &HashMap::<String, String>::new())
            .await
    }

    pub async fn update_instance_health_status(
        &self,
        args: &UpdateInstanceHealthStatus,
    ) -> Result<()> {
        self.client.simple_put("/v1/ns/health/instance", args).await
    }
}