From aa076cac8bca1316b12ae3304d3abc269cb9bc42 Mon Sep 17 00:00:00 2001 From: Benjamin Sommerfeld Date: Tue, 15 Oct 2024 15:48:11 +0200 Subject: [PATCH] refactor: split models into separate files --- src/auth.rs | 2 +- src/clap_app.rs | 7 +- src/domains.rs | 5 +- src/environments.rs | 3 +- src/execution.rs | 2 +- src/logs.rs | 2 +- src/models.rs | 449 -------------------------------------- src/models/auth.rs | 19 ++ src/models/basic.rs | 29 +++ src/models/config.rs | 38 ++++ src/models/domain.rs | 54 +++++ src/models/environment.rs | 18 ++ src/models/execution.rs | 33 +++ src/models/log.rs | 72 ++++++ src/models/mod.rs | 10 + src/models/pipeline.rs | 29 +++ src/models/program.rs | 28 +++ src/models/variables.rs | 136 ++++++++++++ src/pipelines.rs | 3 +- src/programs.rs | 2 +- src/variables.rs | 15 +- 21 files changed, 486 insertions(+), 470 deletions(-) delete mode 100644 src/models.rs create mode 100644 src/models/auth.rs create mode 100644 src/models/basic.rs create mode 100644 src/models/config.rs create mode 100644 src/models/domain.rs create mode 100644 src/models/environment.rs create mode 100644 src/models/execution.rs create mode 100644 src/models/log.rs create mode 100644 src/models/mod.rs create mode 100644 src/models/pipeline.rs create mode 100644 src/models/program.rs create mode 100644 src/models/variables.rs diff --git a/src/auth.rs b/src/auth.rs index 647ba2d..93d3d00 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -1,6 +1,6 @@ use crate::client::CloudManagerClient; use crate::config::{AuthStrategy, Scope}; -use crate::models::{BearerResponse, JwtClaims}; +use crate::models::auth::{BearerResponse, JwtClaims}; use crate::IMS_ENDPOINT; use chrono::{Duration, Utc}; use jsonwebtoken::{encode, Algorithm, EncodingKey, Header}; diff --git a/src/clap_app.rs b/src/clap_app.rs index edda328..64510d6 100644 --- a/src/clap_app.rs +++ b/src/clap_app.rs @@ -12,9 +12,10 @@ use crate::client::CloudManagerClient; use crate::config::CloudManagerConfig; use crate::encryption::{decrypt, encrypt}; use crate::logs::{download_log, tail_log}; -use crate::models::{ - Domain, EnvironmentVariableServiceType, LogType, PipelineVariableServiceType, ServiceType, -}; +use crate::models::domain::Domain; +use crate::models::log::{LogType, ServiceType}; +use crate::models::variables::{EnvironmentVariableServiceType, PipelineVariableServiceType}; + use crate::variables::{ get_env_vars, get_pipeline_vars, set_env_vars_from_file, set_pipeline_vars_from_file, }; diff --git a/src/domains.rs b/src/domains.rs index 264c090..278b190 100644 --- a/src/domains.rs +++ b/src/domains.rs @@ -1,6 +1,7 @@ use crate::client::{AdobeConnector, CloudManagerClient}; use crate::errors::throw_adobe_api_error; -use crate::models::{CreateDomainResponse, DomainList, DomainResponse, MinimumDomain, YamlConfig}; +use crate::models::config::YamlConfig; +use crate::models::domain::{CreateDomainResponse, DomainList, DomainResponse, MinimumDomain}; use crate::HOST_NAME; extern crate uuid; use colored::Colorize; @@ -57,7 +58,7 @@ pub async fn create_domains( process::exit(1); }); let mut ret_value = 0; - let programs: Vec = input.programs; + let programs: Vec = input.programs; for d in &programs { println!("☁ Program: {}", d.id,); if let Some(environments_vec) = &d.environments { diff --git a/src/environments.rs b/src/environments.rs index acd69c4..3e666e0 100644 --- a/src/environments.rs +++ b/src/environments.rs @@ -1,6 +1,7 @@ use crate::client::{AdobeConnector, CloudManagerClient}; use crate::errors::throw_adobe_api_error; -use crate::models::{Environment, EnvironmentsList, EnvironmentsResponse}; +use crate::models::environment::{Environment, EnvironmentsList}; +use crate::models::variables::EnvironmentsResponse; use crate::HOST_NAME; use reqwest::{Error, Method}; use std::process; diff --git a/src/execution.rs b/src/execution.rs index d847331..267f194 100644 --- a/src/execution.rs +++ b/src/execution.rs @@ -1,6 +1,6 @@ use crate::client::{AdobeConnector, CloudManagerClient}; use crate::errors::throw_adobe_api_error; -use crate::models::{ExecutionList, ExecutionResponse}; +use crate::models::execution::{ExecutionList, ExecutionResponse}; use crate::HOST_NAME; use reqwest::{Error, Method}; use std::process; diff --git a/src/logs.rs b/src/logs.rs index 6cb2e0b..d539ccd 100644 --- a/src/logs.rs +++ b/src/logs.rs @@ -10,7 +10,7 @@ use reqwest::{Error, Method, StatusCode}; use crate::client::{AdobeConnector, CloudManagerClient}; use crate::errors::throw_adobe_api_error; -use crate::models::{LogTailResponse, LogType, ServiceType}; +use crate::models::log::{LogTailResponse, LogType, ServiceType}; use crate::HOST_NAME; /// Downloads the specified log. diff --git a/src/models.rs b/src/models.rs deleted file mode 100644 index 26b406a..0000000 --- a/src/models.rs +++ /dev/null @@ -1,449 +0,0 @@ -use chrono::NaiveDate; -use serde::{Deserialize, Serialize}; -use std::fmt; -use strum_macros::{EnumString, IntoStaticStr}; - -// Common models used across multiple modules -// ------------------------------------------------------------------------------------------------- - -/// Model for all programs that will be read from the configuration YAML -#[derive(Debug, Deserialize, Serialize)] -pub struct YamlConfig { - pub programs: Vec, -} - -/// Model for a program's ID and all its environments that will be read from the configuration YAML -#[derive(Debug, Deserialize, Serialize)] -pub struct ProgramsConfig { - pub id: u32, - pub environments: Option>, - pub pipelines: Option>, -} - -/// Model for an environment's ID and all its variables that will be read from the configuration YAML -#[derive(Debug, Deserialize, Serialize)] -pub struct DomainConfig { - pub domainname: String, - pub certificate_id: i64, -} - -/// Model for an environment's ID and all its variables that will be read from the configuration YAML -#[derive(Debug, Deserialize, Serialize)] -pub struct EnvironmentsConfig { - pub id: u32, - pub variables: Vec, - pub domains: Option>, -} - -/// Model for a pipeline's ID and all its variables that will be read from the configuration YAML -#[derive(Debug, Deserialize, Serialize)] -pub struct PipelinesConfig { - pub id: u32, - pub variables: Vec, -} - -/// Model for common cloud manager variables - -/// Possible types that a variable can have -#[derive(Clone, Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub enum VariableType { - String, - SecretString, -} - -/// Model for all information about a Cloud Manager environment variable -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct EnvironmentVariable { - pub name: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub value: Option, - #[serde(rename(deserialize = "type", serialize = "type"))] - pub variable_type: VariableType, - #[serde( - default = "EnvironmentVariableServiceType::default", - skip_serializing_if = "environment_variable_skip_serializing" - )] - pub service: EnvironmentVariableServiceType, -} - -/// Possible service types that an environment variable can have -#[derive(Clone, Debug, Deserialize, Serialize, IntoStaticStr, EnumString, PartialEq, Eq)] -#[strum(serialize_all = "lowercase")] -#[serde(rename_all = "lowercase")] -pub enum EnvironmentVariableServiceType { - All, - Author, - Publish, - Preview, - #[serde(other)] - Invalid, -} - -impl fmt::Display for EnvironmentVariableServiceType { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!( - formatter, - "{}", - format!("{}", serde_json::to_string(self).unwrap().to_string()) - ) - } -} -fn environment_variable_skip_serializing(t: &EnvironmentVariableServiceType) -> bool { - *t == EnvironmentVariableServiceType::All -} - -impl EnvironmentVariableServiceType { - fn default() -> Self { - EnvironmentVariableServiceType::All - } -} - -/// Model for all information about a Cloud Manager pipeline variable -/// Model for all information about a Cloud Manager environment variable -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct PipelineVariable { - pub name: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub value: Option, - #[serde(rename(deserialize = "type", serialize = "type"))] - pub variable_type: VariableType, - #[serde(default = "PipelineVariableServiceType::default")] - pub service: PipelineVariableServiceType, -} - -/// Possible service types that an pipeline variable can have -#[derive(Clone, Debug, Deserialize, Serialize, IntoStaticStr, EnumString, PartialEq, Eq)] -#[strum(serialize_all = "camelCase")] -#[serde(rename_all = "camelCase")] -pub enum PipelineVariableServiceType { - Build, - UiTest, - FunctionalTest, - #[serde(other)] - Invalid, -} - -impl fmt::Display for PipelineVariableServiceType { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!( - formatter, - "{}", - format!("{}", serde_json::to_string(self).unwrap().to_string()) - ) - } -} - -impl PipelineVariableServiceType { - fn default() -> Self { - PipelineVariableServiceType::Build - } -} - -/// Model for the necessary JWT claims to retrieve an Adobe access token -#[derive(Deserialize, Serialize)] -pub struct JwtClaims { - pub exp: usize, - pub iss: String, - pub sub: String, - pub aud: String, - #[serde(rename(serialize = "https://ims-na1.adobelogin.com/s/ent_cloudmgr_sdk"))] - pub scope_ent_cloudmgr_sdk: bool, - #[serde(rename(serialize = "https://ims-na1.adobelogin.com/s/ent_aem_cloud_api"))] - pub scope_ent_aem_cloud_api: bool, -} - -/// Helper struct that is used to serialize the access token retrieved from Adobe -#[derive(Debug, Deserialize)] -pub struct BearerResponse { - pub access_token: String, -} - -// Models for representing Cloud Manager programs -// ------------------------------------------------------------------------------------------------- - -/// Struct that holds the response when requesting /api/programs -#[derive(Deserialize, Serialize)] -pub struct ProgramsResponse { - #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] - pub programs_list: ProgramsList, -} - -/// Model for a list of programs -#[derive(Debug, Deserialize, Serialize)] -pub struct ProgramsList { - programs: Vec, -} - -/// Model for a program and its relevant metadata -#[derive(Debug, Deserialize, Serialize)] -pub struct Program { - id: String, - name: String, - #[serde(rename(deserialize = "tenantId", serialize = "tenantId"))] - tenant_id: String, - enabled: bool, - status: String, -} - -// Models for representing Cloud Manager environments and descendant objects -// ------------------------------------------------------------------------------------------------- - -/// Struct that holds the response when requesting /api/program/{id}/environments -#[derive(Deserialize, Serialize)] -pub struct EnvironmentsResponse { - #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] - pub environments_list: EnvironmentsList, -} - -/// Model for a list of environments -#[derive(Debug, Deserialize, Serialize)] -pub struct EnvironmentsList { - environments: Vec, -} - -/// Model for an environment and its relevant metadata -#[derive(Debug, Deserialize, Serialize)] -pub struct Environment { - pub name: String, - #[serde(rename(deserialize = "type", serialize = "type"))] - env_type: String, - pub status: String, - id: String, - #[serde(rename(deserialize = "programId", serialize = "programId"))] - program_id: String, -} - -/// Struct to serialize the response of requesting /api/program/{id}/environment/{id}/variables -#[derive(Debug, Deserialize, Serialize)] -pub struct EnvironmentVariablesResponse { - #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] - pub variables_list: EnvironmentVariablesList, -} - -/// Struct to serialize the response of requesting /api/program/{id}/environment/{id}/variables -#[derive(Debug, Deserialize, Serialize)] -pub struct PipelineVariablesResponse { - #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] - pub variables_list: PipelineVariablesList, -} - -/// Struct that holds a list of variables -#[derive(Debug, Deserialize, Serialize)] -pub struct EnvironmentVariablesList { - pub variables: Vec, -} - -/// Struct that holds a list of variables -#[derive(Debug, Deserialize, Serialize)] -pub struct PipelineVariablesList { - pub variables: Vec, -} - -// Models for representing Cloud Manager pipelines and descendant objects -// ------------------------------------------------------------------------------------------------- - -/// Struct that holds the response when requesting /api/programs -#[derive(Deserialize, Serialize)] -pub struct PipelinesResponse { - #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] - pub pipelines_list: PipelinesList, -} - -/// Model for a list of pipelines -#[derive(Debug, Deserialize, Serialize)] -pub struct PipelinesList { - pipelines: Vec, -} - -/// Model for a pipeline and its relevant metadata -#[derive(Debug, Deserialize, Serialize)] -pub struct Pipeline { - pub name: String, - pub status: String, - id: String, - #[serde(rename(deserialize = "programId", serialize = "programId"))] - program_id: String, -} - -// ------------------------------------------------------------------------------------------------- - -/// Possible types that a service can have -#[derive(Clone, Deserialize, Serialize, IntoStaticStr, EnumString)] -#[strum(serialize_all = "lowercase")] -#[serde(rename_all = "lowercase")] -pub enum ServiceType { - Author, - Publish, - Dispatcher, - #[strum(serialize = "preview_dispatcher")] - #[serde(rename(deserialize = "preview_dispatcher", serialize = "preview_dispatcher"))] - PreviewDispatcher, -} - -// Models for representing Cloud Manager logs -/// Possible types that a log can have -#[derive(Clone, Deserialize, Serialize, IntoStaticStr, EnumString)] -#[strum(serialize_all = "lowercase")] -#[serde(rename_all = "lowercase")] -pub enum LogType { - AemAccess, - AemDispatcher, - AemError, - AemRequest, - Cdn, - HttpdAccess, - HttpdError, -} - -/// Struct that holds the response when requesting /api/program/{id}/environment/{id}/logs -#[derive(Deserialize, Serialize)] -pub struct LogsResponse { - days: u32, - name: Vec, - service: Vec, - #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] - pub embedded: LogsEmbedment, -} - -/// Helper struct that is used because of the JSON structure that LogsResponse has -#[derive(Deserialize, Serialize)] -pub struct LogsEmbedment { - pub downloads: Vec, -} - -/// Struct that represents an available logfile -#[derive(Deserialize, Serialize)] -pub struct Log { - name: LogType, - service: ServiceType, - date: NaiveDate, -} - -/// Model for a list of programs -#[derive(Debug, Deserialize, Serialize)] -pub struct ExecutionList { - #[serde(rename = "executions")] - list: Vec, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ExecutionResponse { - #[serde(rename = "_embedded")] - pub execution_list: ExecutionList, - #[serde(rename = "_totalNumberOfItems")] - pub total_number_of_items: i64, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Execution { - pub id: String, - pub program_id: String, - pub pipeline_id: String, - trigger: String, - user: String, - pub status: String, - created_at: Option, - updated_at: Option, - pipeline_type: String, - pipeline_execution_mode: String, - finished_at: Option, -} - -/// Model for a list of programs -#[derive(Debug, Deserialize, Serialize)] -pub struct DomainList { - #[serde(rename = "domainNames")] - pub list: Vec, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct DomainResponse { - #[serde(rename = "_embedded")] - pub domain_list: DomainList, - #[serde(rename = "_totalNumberOfItems")] - pub total_number_of_items: i64, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Domain { - pub id: Option, - pub name: String, - pub status: Option, - pub dns_txt_record: String, - pub environment_id: i64, - pub environment_name: Option, - pub tier: Option, - pub certificate_id: i64, - pub certificate_name: Option, - pub certificate_expire_at: Option, - pub created_at: Option, - pub updated_at: Option, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct MinimumDomain { - pub name: String, - pub dns_txt_record: String, - pub environment_id: i64, - pub certificate_id: i64, - pub dns_zone: String, -} - -#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CreateDomainResponse { - #[serde(rename = "type")] - pub type_field: String, - pub status: i64, - pub title: String, - pub errors: Option>, -} - -#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Error { - pub code: String, - pub message: String, - pub field: String, -} - -// Tail Log - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct LogTailResponse { - #[serde(rename = "_embedded")] - pub embedded: LogTailList, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct LogTailList { - pub downloads: Vec, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Download { - #[serde(rename = "_links")] - pub links: Links, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Links { - #[serde(rename = "http://ns.adobe.com/adobecloud/rel/logs/tail")] - pub http_ns_adobe_com_adobecloud_rel_logs_tail: Option, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct HttpNsAdobeComAdobecloudRelLogsTail { - pub href: String, -} diff --git a/src/models/auth.rs b/src/models/auth.rs new file mode 100644 index 0000000..c6995f6 --- /dev/null +++ b/src/models/auth.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; +/// Model for the necessary JWT claims to retrieve an Adobe access token +#[derive(Deserialize, Serialize)] +pub struct JwtClaims { + pub exp: usize, + pub iss: String, + pub sub: String, + pub aud: String, + #[serde(rename(serialize = "https://ims-na1.adobelogin.com/s/ent_cloudmgr_sdk"))] + pub scope_ent_cloudmgr_sdk: bool, + #[serde(rename(serialize = "https://ims-na1.adobelogin.com/s/ent_aem_cloud_api"))] + pub scope_ent_aem_cloud_api: bool, +} + +/// Helper struct that is used to serialize the access token retrieved from Adobe +#[derive(Debug, Deserialize)] +pub struct BearerResponse { + pub access_token: String, +} diff --git a/src/models/basic.rs b/src/models/basic.rs new file mode 100644 index 0000000..bc58247 --- /dev/null +++ b/src/models/basic.rs @@ -0,0 +1,29 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Error { + pub code: String, + pub message: String, + pub field: String, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Download { + #[serde(rename = "_links")] + pub links: Links, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Links { + #[serde(rename = "http://ns.adobe.com/adobecloud/rel/logs/tail")] + pub http_ns_adobe_com_adobecloud_rel_logs_tail: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct HttpNsAdobeComAdobecloudRelLogsTail { + pub href: String, +} diff --git a/src/models/config.rs b/src/models/config.rs new file mode 100644 index 0000000..8eb0e9d --- /dev/null +++ b/src/models/config.rs @@ -0,0 +1,38 @@ +use serde::{Deserialize, Serialize}; + +use super::variables::{EnvironmentVariable, PipelineVariable}; +/// Model for all programs that will be read from the configuration YAML +#[derive(Debug, Deserialize, Serialize)] +pub struct YamlConfig { + pub programs: Vec, +} + +/// Model for a program's ID and all its environments that will be read from the configuration YAML +#[derive(Debug, Deserialize, Serialize)] +pub struct ProgramsConfig { + pub id: u32, + pub environments: Option>, + pub pipelines: Option>, +} + +/// Model for an environment's ID and all its variables that will be read from the configuration YAML +#[derive(Debug, Deserialize, Serialize)] +pub struct DomainConfig { + pub domainname: String, + pub certificate_id: i64, +} + +/// Model for an environment's ID and all its variables that will be read from the configuration YAML +#[derive(Debug, Deserialize, Serialize)] +pub struct EnvironmentsConfig { + pub id: u32, + pub variables: Vec, + pub domains: Option>, +} + +/// Model for a pipeline's ID and all its variables that will be read from the configuration YAML +#[derive(Debug, Deserialize, Serialize)] +pub struct PipelinesConfig { + pub id: u32, + pub variables: Vec, +} diff --git a/src/models/domain.rs b/src/models/domain.rs new file mode 100644 index 0000000..5916fcb --- /dev/null +++ b/src/models/domain.rs @@ -0,0 +1,54 @@ +use serde::{Deserialize, Serialize}; + +/// Model for a list of programs +#[derive(Debug, Deserialize, Serialize)] +pub struct DomainList { + #[serde(rename = "domainNames")] + pub list: Vec, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DomainResponse { + #[serde(rename = "_embedded")] + pub domain_list: DomainList, + #[serde(rename = "_totalNumberOfItems")] + pub total_number_of_items: i64, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Domain { + pub id: Option, + pub name: String, + pub status: Option, + pub dns_txt_record: String, + pub environment_id: i64, + pub environment_name: Option, + pub tier: Option, + pub certificate_id: i64, + pub certificate_name: Option, + pub certificate_expire_at: Option, + pub created_at: Option, + pub updated_at: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct MinimumDomain { + pub name: String, + pub dns_txt_record: String, + pub environment_id: i64, + pub certificate_id: i64, + pub dns_zone: String, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CreateDomainResponse { + #[serde(rename = "type")] + pub type_field: String, + pub status: i64, + pub title: String, + pub errors: Option>, +} diff --git a/src/models/environment.rs b/src/models/environment.rs new file mode 100644 index 0000000..8796a39 --- /dev/null +++ b/src/models/environment.rs @@ -0,0 +1,18 @@ +use serde::{Deserialize, Serialize}; +/// Model for a list of environments +#[derive(Debug, Deserialize, Serialize)] +pub struct EnvironmentsList { + environments: Vec, +} + +/// Model for an environment and its relevant metadata +#[derive(Debug, Deserialize, Serialize)] +pub struct Environment { + pub name: String, + #[serde(rename(deserialize = "type", serialize = "type"))] + env_type: String, + pub status: String, + id: String, + #[serde(rename(deserialize = "programId", serialize = "programId"))] + program_id: String, +} diff --git a/src/models/execution.rs b/src/models/execution.rs new file mode 100644 index 0000000..4b08313 --- /dev/null +++ b/src/models/execution.rs @@ -0,0 +1,33 @@ +use serde::{Deserialize, Serialize}; + +/// Model for a list of programs +#[derive(Debug, Deserialize, Serialize)] +pub struct ExecutionList { + #[serde(rename = "executions")] + list: Vec, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ExecutionResponse { + #[serde(rename = "_embedded")] + pub execution_list: ExecutionList, + #[serde(rename = "_totalNumberOfItems")] + pub total_number_of_items: i64, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Execution { + pub id: String, + pub program_id: String, + pub pipeline_id: String, + trigger: String, + user: String, + pub status: String, + created_at: Option, + updated_at: Option, + pipeline_type: String, + pipeline_execution_mode: String, + finished_at: Option, +} diff --git a/src/models/log.rs b/src/models/log.rs new file mode 100644 index 0000000..34fb1f2 --- /dev/null +++ b/src/models/log.rs @@ -0,0 +1,72 @@ +use chrono::NaiveDate; +use serde::{Deserialize, Serialize}; + +use strum_macros::{EnumString, IntoStaticStr}; + +use super::basic::Download; +/// Possible types that a service can have +#[derive(Clone, Deserialize, Serialize, IntoStaticStr, EnumString)] +#[strum(serialize_all = "lowercase")] +#[serde(rename_all = "lowercase")] +pub enum ServiceType { + Author, + Publish, + Dispatcher, + #[strum(serialize = "preview_dispatcher")] + #[serde(rename(deserialize = "preview_dispatcher", serialize = "preview_dispatcher"))] + PreviewDispatcher, +} + +// Models for representing Cloud Manager logs +/// Possible types that a log can have +#[derive(Clone, Deserialize, Serialize, IntoStaticStr, EnumString)] +#[strum(serialize_all = "lowercase")] +#[serde(rename_all = "lowercase")] +pub enum LogType { + AemAccess, + AemDispatcher, + AemError, + AemRequest, + Cdn, + HttpdAccess, + HttpdError, +} + +/// Struct that holds the response when requesting /api/program/{id}/environment/{id}/logs +#[derive(Deserialize, Serialize)] +pub struct LogsResponse { + days: u32, + name: Vec, + service: Vec, + #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] + pub embedded: LogsEmbedment, +} + +/// Helper struct that is used because of the JSON structure that LogsResponse has +#[derive(Deserialize, Serialize)] +pub struct LogsEmbedment { + pub downloads: Vec, +} + +/// Struct that represents an available logfile +#[derive(Deserialize, Serialize)] +pub struct Log { + name: LogType, + service: ServiceType, + date: NaiveDate, +} + +// Tail Log + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct LogTailResponse { + #[serde(rename = "_embedded")] + pub embedded: LogTailList, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct LogTailList { + pub downloads: Vec, +} diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..e6ca4ee --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1,10 @@ +pub mod auth; +pub mod basic; +pub mod config; +pub mod domain; +pub mod environment; +pub mod execution; +pub mod log; +pub mod pipeline; +pub mod program; +pub mod variables; diff --git a/src/models/pipeline.rs b/src/models/pipeline.rs new file mode 100644 index 0000000..e5ff198 --- /dev/null +++ b/src/models/pipeline.rs @@ -0,0 +1,29 @@ +// Models for representing Cloud Manager pipelines and descendant objects +// ------------------------------------------------------------------------------------------------- + +use serde::{Deserialize, Serialize}; + +/// Struct that holds the response when requesting /api/programs +#[derive(Deserialize, Serialize)] +pub struct PipelinesResponse { + #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] + pub pipelines_list: PipelinesList, +} + +/// Model for a list of pipelines +#[derive(Debug, Deserialize, Serialize)] +pub struct PipelinesList { + pipelines: Vec, +} + +/// Model for a pipeline and its relevant metadata +#[derive(Debug, Deserialize, Serialize)] +pub struct Pipeline { + pub name: String, + pub status: String, + id: String, + #[serde(rename(deserialize = "programId", serialize = "programId"))] + program_id: String, +} + +// ------------------------------------------------------------------------------------------------- diff --git a/src/models/program.rs b/src/models/program.rs new file mode 100644 index 0000000..cb8f232 --- /dev/null +++ b/src/models/program.rs @@ -0,0 +1,28 @@ +use serde::{Deserialize, Serialize}; + +// Models for representing Cloud Manager programs +// ------------------------------------------------------------------------------------------------- + +/// Struct that holds the response when requesting /api/programs +#[derive(Deserialize, Serialize)] +pub struct ProgramsResponse { + #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] + pub programs_list: ProgramsList, +} + +/// Model for a list of programs +#[derive(Debug, Deserialize, Serialize)] +pub struct ProgramsList { + programs: Vec, +} + +/// Model for a program and its relevant metadata +#[derive(Debug, Deserialize, Serialize)] +pub struct Program { + id: String, + name: String, + #[serde(rename(deserialize = "tenantId", serialize = "tenantId"))] + tenant_id: String, + enabled: bool, + status: String, +} diff --git a/src/models/variables.rs b/src/models/variables.rs new file mode 100644 index 0000000..64133ca --- /dev/null +++ b/src/models/variables.rs @@ -0,0 +1,136 @@ +use serde::{Deserialize, Serialize}; +use std::fmt; +use strum_macros::{EnumString, IntoStaticStr}; + +use super::environment::EnvironmentsList; + +/// Model for common cloud manager variables + +/// Possible types that a variable can have +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub enum VariableType { + String, + SecretString, +} + +/// Model for all information about a Cloud Manager environment variable +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct EnvironmentVariable { + pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub value: Option, + #[serde(rename(deserialize = "type", serialize = "type"))] + pub variable_type: VariableType, + #[serde( + default = "EnvironmentVariableServiceType::default", + skip_serializing_if = "environment_variable_skip_serializing" + )] + pub service: EnvironmentVariableServiceType, +} + +/// Possible service types that an environment variable can have +#[derive(Clone, Debug, Deserialize, Serialize, IntoStaticStr, EnumString, PartialEq, Eq)] +#[strum(serialize_all = "lowercase")] +#[serde(rename_all = "lowercase")] +pub enum EnvironmentVariableServiceType { + All, + Author, + Publish, + Preview, + #[serde(other)] + Invalid, +} + +impl fmt::Display for EnvironmentVariableServiceType { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!( + formatter, + "{}", + format!("{}", serde_json::to_string(self).unwrap().to_string()) + ) + } +} +fn environment_variable_skip_serializing(t: &EnvironmentVariableServiceType) -> bool { + *t == EnvironmentVariableServiceType::All +} + +impl EnvironmentVariableServiceType { + fn default() -> Self { + EnvironmentVariableServiceType::All + } +} + +/// Model for all information about a Cloud Manager pipeline variable +/// Model for all information about a Cloud Manager environment variable +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct PipelineVariable { + pub name: String, + #[serde(skip_serializing_if = "Option::is_none")] + pub value: Option, + #[serde(rename(deserialize = "type", serialize = "type"))] + pub variable_type: VariableType, + #[serde(default = "PipelineVariableServiceType::default")] + pub service: PipelineVariableServiceType, +} + +/// Possible service types that an pipeline variable can have +#[derive(Clone, Debug, Deserialize, Serialize, IntoStaticStr, EnumString, PartialEq, Eq)] +#[strum(serialize_all = "camelCase")] +#[serde(rename_all = "camelCase")] +pub enum PipelineVariableServiceType { + Build, + UiTest, + FunctionalTest, + #[serde(other)] + Invalid, +} + +impl fmt::Display for PipelineVariableServiceType { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!( + formatter, + "{}", + format!("{}", serde_json::to_string(self).unwrap().to_string()) + ) + } +} + +impl PipelineVariableServiceType { + fn default() -> Self { + PipelineVariableServiceType::Build + } +} + +/// Struct that holds the response when requesting /api/program/{id}/environments +#[derive(Deserialize, Serialize)] +pub struct EnvironmentsResponse { + #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] + pub environments_list: EnvironmentsList, +} + +/// Struct to serialize the response of requesting /api/program/{id}/environment/{id}/variables +#[derive(Debug, Deserialize, Serialize)] +pub struct EnvironmentVariablesResponse { + #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] + pub variables_list: EnvironmentVariablesList, +} + +/// Struct to serialize the response of requesting /api/program/{id}/environment/{id}/variables +#[derive(Debug, Deserialize, Serialize)] +pub struct PipelineVariablesResponse { + #[serde(rename(deserialize = "_embedded", serialize = "_embedded"))] + pub variables_list: PipelineVariablesList, +} + +/// Struct that holds a list of variables +#[derive(Debug, Deserialize, Serialize)] +pub struct EnvironmentVariablesList { + pub variables: Vec, +} + +/// Struct that holds a list of variables +#[derive(Debug, Deserialize, Serialize)] +pub struct PipelineVariablesList { + pub variables: Vec, +} diff --git a/src/pipelines.rs b/src/pipelines.rs index 7b15779..3d107d4 100644 --- a/src/pipelines.rs +++ b/src/pipelines.rs @@ -1,6 +1,7 @@ use crate::client::{AdobeConnector, CloudManagerClient}; use crate::errors::throw_adobe_api_error; -use crate::models::{Execution, Pipeline, PipelinesList, PipelinesResponse}; +use crate::models::execution::Execution; +use crate::models::pipeline::{Pipeline, PipelinesList, PipelinesResponse}; use crate::HOST_NAME; use reqwest::{Error, Method, StatusCode}; use std::process; diff --git a/src/programs.rs b/src/programs.rs index 08ce46c..787d0e2 100644 --- a/src/programs.rs +++ b/src/programs.rs @@ -1,6 +1,6 @@ use crate::client::{AdobeConnector, CloudManagerClient}; use crate::errors::throw_adobe_api_error; -use crate::models::{ProgramsList, ProgramsResponse}; +use crate::models::program::{ProgramsList, ProgramsResponse}; use crate::HOST_NAME; use reqwest::{Error, Method}; use std::process; diff --git a/src/variables.rs b/src/variables.rs index 13df0d5..5c86411 100644 --- a/src/variables.rs +++ b/src/variables.rs @@ -2,10 +2,11 @@ use crate::client::{AdobeConnector, CloudManagerClient}; use crate::encryption::decrypt; use crate::environments::get_environment; use crate::errors::throw_adobe_api_error; -use crate::models::{ +use crate::models::config::YamlConfig; +use crate::models::variables::{ EnvironmentVariable, EnvironmentVariableServiceType, EnvironmentVariablesList, EnvironmentVariablesResponse, PipelineVariable, PipelineVariableServiceType, - PipelineVariablesList, PipelineVariablesResponse, VariableType, YamlConfig, + PipelineVariablesList, PipelineVariablesResponse, VariableType, }; use crate::pipelines::get_pipeline; use crate::HOST_NAME; @@ -230,10 +231,7 @@ pub async fn set_env_vars_from_file( } if dry_run { - println!( - "{:>8} --dry-run detected. Not performing any actions.", - "⚠️", - ); + println!("{:>8} --dry-run detected. Not performing any actions.", "⚠️",); } else { match set_env_vars(client, p.id, e.id, &vars_final).await { Ok(status) => match status { @@ -474,10 +472,7 @@ pub async fn set_pipeline_vars_from_file( } if dry_run { - println!( - "{:>8} --dry-run detected. Not performing any actions.", - "⚠️", - ); + println!("{:>8} --dry-run detected. Not performing any actions.", "⚠️",); } else { match set_pipeline_vars(client, p.id, l.id, &vars_final).await { Ok(status) => match status {