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
use ntex::rt;
use ntex::ws;
use ntex::io::Base;
use ntex::ws::WsConnection;

use nanocl_utils::io_error::FromIo;
use nanocl_utils::http_client_error::HttpClientError;

use nanocl_stubs::generic::GenericNspQuery;
use nanocl_stubs::vm::{Vm, VmSummary, VmInspect};
use nanocl_stubs::vm_config::{VmConfigPartial, VmConfigUpdate};

use crate::NanocldClient;

impl NanocldClient {
  pub async fn create_vm(
    &self,
    vm: &VmConfigPartial,
    namespace: Option<String>,
  ) -> Result<Vm, HttpClientError> {
    let res = self
      .send_post(
        format!("/{}/vms", self.version),
        Some(vm),
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Self::res_json(res).await
  }

  pub async fn list_vm(
    &self,
    namespace: Option<String>,
  ) -> Result<Vec<VmSummary>, HttpClientError> {
    let res = self
      .send_get(
        format!("/{}/vms", self.version),
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Self::res_json(res).await
  }

  pub async fn delete_vm(
    &self,
    name: &str,
    namespace: Option<String>,
  ) -> Result<(), HttpClientError> {
    self
      .send_delete(
        format!("/{}/vms/{}", self.version, name),
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Ok(())
  }

  pub async fn inspect_vm(
    &self,
    name: &str,
    namespace: Option<String>,
  ) -> Result<VmInspect, HttpClientError> {
    let res = self
      .send_get(
        format!("/{}/vms/{}/inspect", self.version, name),
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Self::res_json(res).await
  }

  pub async fn start_vm(
    &self,
    name: &str,
    namespace: Option<String>,
  ) -> Result<(), HttpClientError> {
    self
      .send_post(
        format!("/{}/vms/{}/start", self.version, name),
        None::<String>,
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Ok(())
  }

  pub async fn stop_vm(
    &self,
    name: &str,
    namespace: Option<String>,
  ) -> Result<(), HttpClientError> {
    self
      .send_post(
        format!("/{}/vms/{}/stop", self.version, name),
        None::<String>,
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Ok(())
  }

  pub async fn patch_vm(
    &self,
    name: &str,
    vm: &VmConfigUpdate,
    namespace: Option<String>,
  ) -> Result<(), HttpClientError> {
    self
      .send_patch(
        format!("/{}/vms/{}", self.version, name),
        Some(vm),
        Some(&GenericNspQuery { namespace }),
      )
      .await?;

    Ok(())
  }

  pub async fn attach_vm(
    &self,
    name: &str,
    namespace: Option<String>,
  ) -> Result<WsConnection<Base>, HttpClientError> {
    let qs = if let Some(namespace) = namespace {
      format!("?namespace={}", namespace)
    } else {
      "".to_string()
    };
    let url = format!("{}/{}/vms/{name}/attach{qs}", self.url, &self.version);
    // open websockets connection over http transport
    let con = match &self.unix_socket {
      Some(path) => ws::WsClient::build(&url)
        .connector(ntex::service::fn_service(|_| async move {
          Ok::<_, _>(rt::unix_connect(&path).await?)
        }))
        .finish()
        .map_err(|err| err.map_err_context(|| path))?
        .connect()
        .await
        .map_err(|err| err.map_err_context(|| path))?,
      None => ws::WsClient::build(&url)
        .finish()
        .map_err(|err| err.map_err_context(|| &self.url))?
        .connect()
        .await
        .map_err(|err| err.map_err_context(|| &self.url))?,
    };
    Ok(con)
  }
}