komodo_client 2.1.1

Client for the Komodo build and deployment system
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use bson::{Document, doc};
use derive_builder::Builder;
use derive_default_builder::DefaultBuilder;
use partial_derive2::Partial;
use serde::{Deserialize, Serialize};
use strum::Display;
use typeshare::typeshare;

use crate::deserializers::{
  file_contents_deserializer, option_file_contents_deserializer,
  option_string_list_deserializer, string_list_deserializer,
};

use super::{
  I64, ResourceTarget,
  resource::{Resource, ResourceListItem, ResourceQuery},
};

#[typeshare]
pub type ResourceSyncListItem =
  ResourceListItem<ResourceSyncListItemInfo>;

#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ResourceSyncListItemInfo {
  /// Unix timestamp of last sync, or 0
  pub last_sync_ts: I64,
  /// Whether sync is `files_on_host` mode.
  pub files_on_host: bool,
  /// Whether sync has file contents defined.
  pub file_contents: bool,
  /// Whether sync has `managed` mode enabled.
  pub managed: bool,
  /// Resource paths to the files.
  pub resource_path: Vec<String>,
  /// Linked repo, if one is attached.
  pub linked_repo: String,
  /// The git provider domain.
  pub git_provider: String,
  /// The Github repo used as the source of the sync resources
  pub repo: String,
  /// The branch of the repo
  pub branch: String,
  /// Full link to the repo.
  pub repo_link: String,
  /// Short commit hash of last sync, or empty string
  pub last_sync_hash: Option<String>,
  /// Commit message of last sync, or empty string
  pub last_sync_message: Option<String>,
  /// State of the sync. Reflects whether most recent sync successful.
  pub state: ResourceSyncState,
}

#[typeshare]
#[derive(
  Debug,
  Clone,
  Copy,
  Default,
  PartialEq,
  Eq,
  PartialOrd,
  Ord,
  Serialize,
  Deserialize,
  Display,
)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum ResourceSyncState {
  /// Currently syncing
  Syncing,
  /// Updates pending
  Pending,
  /// Last sync successful (or never synced). No Changes pending
  Ok,
  /// Last sync failed
  Failed,
  /// Other case
  #[default]
  Unknown,
}

#[cfg(feature = "utoipa")]
#[derive(utoipa::ToSchema)]
#[schema(as = ResourceSync)]
pub struct ResourceSyncSchema(
  #[schema(inline)] pub Resource<ResourceSyncConfig, ResourceSyncInfo>,
);

#[typeshare]
pub type ResourceSync =
  Resource<ResourceSyncConfig, ResourceSyncInfo>;

#[typeshare]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ResourceSyncInfo {
  /// Unix timestamp of last applied sync
  #[serde(default)]
  pub last_sync_ts: I64,
  /// Short commit hash of last applied sync
  pub last_sync_hash: Option<String>,
  /// Commit message of last applied sync
  pub last_sync_message: Option<String>,

  /// The list of pending updates to resources
  #[serde(default)]
  pub resource_updates: Vec<ResourceDiff>,
  /// The list of pending updates to variables
  #[serde(default)]
  pub variable_updates: Vec<DiffData>,
  /// The list of pending updates to user groups
  #[serde(default)]
  pub user_group_updates: Vec<DiffData>,
  /// The list of pending deploys to resources.
  #[serde(default)]
  pub pending_deploys: Vec<SyncDeployTarget>,
  /// If there is an error, it will be stored here
  pub pending_error: Option<String>,
  /// If there is an getting pending deploys, it will be stored here
  pub pending_deploy_error: Option<String>,
  /// The commit hash which produced these pending updates.
  pub pending_hash: Option<String>,
  /// The commit message which produced these pending updates.
  pub pending_message: Option<String>,

  /// The current sync files
  #[serde(default)]
  pub remote_contents: Vec<SyncFileContents>,
  /// Any read errors in files by path
  #[serde(default)]
  pub remote_errors: Vec<SyncFileContents>,
}

#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ResourceDiff {
  /// The resource target.
  /// The target id will be empty if "Create" ResourceDiffType.
  pub target: ResourceTarget,
  /// The data associated with the diff.
  pub data: DiffData,
}

#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[serde(tag = "type", content = "data")]
pub enum DiffData {
  /// Resource will be created
  Create {
    /// The name of resource to create
    #[serde(default)]
    name: String,
    /// The proposed resource to create in TOML
    proposed: String,
  },
  Update {
    /// The proposed TOML
    proposed: String,
    /// The current TOML
    current: String,
  },
  Delete {
    /// The current TOML of the resource to delete
    current: String,
  },
}

#[typeshare]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct SyncDeployTarget {
  pub target: ResourceTarget,
  pub reason: String,
  pub after: Vec<ResourceTarget>,
}

#[typeshare(serialized_as = "Partial<ResourceSyncConfig>")]
pub type _PartialResourceSyncConfig = PartialResourceSyncConfig;

/// The sync configuration.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, Builder, Partial)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[partial_derive(Debug, Clone, Default, Serialize, Deserialize)]
#[diff_derive(Debug, Clone, Default, Serialize, Deserialize)]
#[partial(skip_serializing_none, from, diff)]
pub struct ResourceSyncConfig {
  /// Choose a Komodo Repo (Resource) to source the sync files.
  #[serde(default)]
  #[builder(default)]
  pub linked_repo: String,

  /// The git provider domain. Default: github.com
  #[serde(default = "default_git_provider")]
  #[builder(default = "default_git_provider()")]
  #[partial_default(default_git_provider())]
  pub git_provider: String,

  /// Whether to use https to clone the repo (versus http). Default: true
  ///
  /// Note. Komodo does not currently support cloning repos via ssh.
  #[serde(default = "default_git_https")]
  #[builder(default = "default_git_https()")]
  #[partial_default(default_git_https())]
  pub git_https: bool,

  /// The Github repo used as the source of the build.
  #[serde(default)]
  #[builder(default)]
  pub repo: String,

  /// The branch of the repo.
  #[serde(default = "default_branch")]
  #[builder(default = "default_branch()")]
  #[partial_default(default_branch())]
  pub branch: String,

  /// Optionally set a specific commit hash.
  #[serde(default)]
  #[builder(default)]
  pub commit: String,

  /// The git account used to access private repos.
  /// Passing empty string can only clone public repos.
  ///
  /// Note. A token for the account must be available in the core config or the builder server's periphery config
  /// for the configured git provider.
  #[serde(default)]
  #[builder(default)]
  pub git_account: String,

  /// Whether incoming webhooks actually trigger action.
  #[serde(default = "default_webhook_enabled")]
  #[builder(default = "default_webhook_enabled()")]
  #[partial_default(default_webhook_enabled())]
  pub webhook_enabled: bool,

  /// Optionally provide an alternate webhook secret for this sync.
  /// If its an empty string, use the default secret from the config.
  #[serde(default)]
  #[builder(default)]
  pub webhook_secret: String,

  /// Files are available on the Komodo Core host.
  /// Specify the file / folder with [ResourceSyncConfig::resource_path].
  #[serde(default)]
  #[builder(default)]
  pub files_on_host: bool,

  /// The path of the resource file(s) to sync.
  ///  - If Files on Host, this is relative to the configured `sync_directory` in core config.
  ///  - If Git Repo based, this is relative to the root of the repo.
  /// Can be a specific file, or a directory containing multiple files / folders.
  /// See [https://komo.do/docs/sync-resources](https://komo.do/docs/sync-resources) for more information.
  #[serde(default, deserialize_with = "string_list_deserializer")]
  #[partial_attr(serde(
    default,
    deserialize_with = "option_string_list_deserializer"
  ))]
  #[builder(default)]
  pub resource_path: Vec<String>,

  /// Enable "pushes" to the file,
  /// which exports resources matching tags to single file.
  ///  - If using `files_on_host`, it is stored in the file_contents, which must point to a .toml file path (it will be created if it doesn't exist).
  ///  - If using `file_contents`, it is stored in the database.
  /// When using this, "delete" mode is always enabled.
  #[serde(default)]
  #[builder(default)]
  pub managed: bool,

  /// Whether sync should delete resources
  /// not declared in the resource files
  #[serde(default)]
  #[builder(default)]
  pub delete: bool,

  /// Whether sync should include resources.
  /// Default: true
  #[serde(default = "default_include_resources")]
  #[builder(default = "default_include_resources()")]
  #[partial_default(default_include_resources())]
  pub include_resources: bool,

  /// When using `managed` resource sync, will only export resources
  /// matching all of the given tags. If none, will match all resources.
  #[serde(default, deserialize_with = "string_list_deserializer")]
  #[partial_attr(serde(
    default,
    deserialize_with = "option_string_list_deserializer"
  ))]
  #[builder(default)]
  pub match_tags: Vec<String>,

  /// Whether sync should include variables.
  #[serde(default)]
  #[builder(default)]
  pub include_variables: bool,

  /// Whether sync should include user groups.
  #[serde(default)]
  #[builder(default)]
  pub include_user_groups: bool,

  /// Whether sync should send alert when it enters Pending state.
  /// Default: true
  #[serde(default = "default_pending_alert")]
  #[builder(default = "default_pending_alert()")]
  #[partial_default(default_pending_alert())]
  pub pending_alert: bool,

  /// Manage the file contents in the UI.
  #[serde(default, deserialize_with = "file_contents_deserializer")]
  #[partial_attr(serde(
    default,
    deserialize_with = "option_file_contents_deserializer"
  ))]
  #[builder(default)]
  pub file_contents: String,
}

impl ResourceSyncConfig {
  pub fn builder() -> ResourceSyncConfigBuilder {
    ResourceSyncConfigBuilder::default()
  }

  /// Checks for empty file contents, ignoring whitespace / comments.
  pub fn file_contents_empty(&self) -> bool {
    self
      .file_contents
      .split('\n')
      .map(str::trim)
      .filter(|line| !line.is_empty() && !line.starts_with('#'))
      .count()
      == 0
  }
}

fn default_git_provider() -> String {
  String::from("github.com")
}

fn default_git_https() -> bool {
  true
}

fn default_branch() -> String {
  String::from("main")
}

fn default_webhook_enabled() -> bool {
  true
}

fn default_include_resources() -> bool {
  true
}

fn default_pending_alert() -> bool {
  true
}

impl Default for ResourceSyncConfig {
  fn default() -> Self {
    Self {
      linked_repo: Default::default(),
      git_provider: default_git_provider(),
      git_https: default_git_https(),
      repo: Default::default(),
      branch: default_branch(),
      commit: Default::default(),
      git_account: Default::default(),
      resource_path: Default::default(),
      files_on_host: Default::default(),
      file_contents: Default::default(),
      managed: Default::default(),
      include_resources: default_include_resources(),
      match_tags: Default::default(),
      include_variables: Default::default(),
      include_user_groups: Default::default(),
      delete: Default::default(),
      webhook_enabled: default_webhook_enabled(),
      webhook_secret: Default::default(),
      pending_alert: default_pending_alert(),
    }
  }
}

#[cfg(feature = "utoipa")]
impl utoipa::PartialSchema for PartialResourceSyncConfig {
  fn schema()
  -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
    utoipa::schema!(#[inline] std::collections::HashMap<String, serde_json::Value>).into()
  }
}

#[cfg(feature = "utoipa")]
impl utoipa::ToSchema for PartialResourceSyncConfig {}

#[typeshare]
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct SyncFileContents {
  /// The base resource path.
  #[serde(default)]
  pub resource_path: String,
  /// The path of the file / error path relative to the resource path.
  pub path: String,
  /// The contents of the file
  pub contents: String,
}

#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ResourceSyncActionState {
  /// Whether sync currently syncing
  pub syncing: bool,
}

#[typeshare]
pub type ResourceSyncQuery =
  ResourceQuery<ResourceSyncQuerySpecifics>;

#[typeshare]
#[derive(
  Serialize, Deserialize, Debug, Clone, Default, DefaultBuilder,
)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct ResourceSyncQuerySpecifics {
  /// Filter syncs by their repo.
  pub repos: Vec<String>,
}

impl super::resource::AddFilters for ResourceSyncQuerySpecifics {
  fn add_filters(&self, filters: &mut Document) {
    if !self.repos.is_empty() {
      filters.insert("config.repo", doc! { "$in": &self.repos });
    }
  }
}