Skip to main content

oci_sdk/
identity.rs

1use chrono::{DateTime, Utc};
2use reqwest::{header::HeaderMap, Response};
3
4use crate::{base_client::oci_signer, config::AuthConfig};
5
6pub struct Identity {
7    config: AuthConfig,
8    service_endpoint: String,
9}
10
11impl Identity {
12    ///Creates a new `Identity` which is the client necessary to interact with this type of object on OCI.
13    ///
14    ///## Example 1
15    ///```no_run
16    ///use oci_sdk::{
17    ///    config::AuthConfig,
18    ///    identity::{Identity},
19    ///};
20    ///
21    ///let auth_config = AuthConfig::from_file(None, None);
22    ///let identity = Identity::new(auth_config, None);
23    ///```
24    ///
25    /// ## Example 2
26    ///
27    ///```rust
28    ///use oci_sdk::{
29    ///    config::AuthConfig,
30    ///    identity::{Identity},
31    ///};
32    ///
33    ///let auth_config = AuthConfig::from_file(Some("tests/assets/oci_config".to_string()), Some("DEFAULT".to_string()));
34    ///let identity = Identity::new(auth_config, None);
35    ///```
36    ///Returns the Nosql client.
37    pub fn new(config: AuthConfig, service_endpoint: Option<String>) -> Identity {
38        let se = service_endpoint.unwrap_or(format!(
39            "https://identity.{}.oci.oraclecloud.com",
40            config.region
41        ));
42        return Identity {
43            config,
44            service_endpoint: se,
45        };
46    }
47
48    pub async fn get_current_user(
49        &self,
50    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
51        let client = reqwest::Client::new();
52
53        let mut headers = HeaderMap::new();
54
55        let now: DateTime<Utc> = Utc::now();
56        headers.insert(
57            "date",
58            now.to_rfc2822().replace("+0000", "GMT").parse().unwrap(),
59        );
60
61        let path = format!("/20160918/users/{}", self.config.user);
62
63        oci_signer(
64            &self.config,
65            &mut headers,
66            String::from("get"),
67            &path,
68            &self.service_endpoint,
69        );
70
71        let response = client
72            .get(format!("{}{}", self.service_endpoint, path))
73            .headers(headers)
74            .send()
75            .await?;
76
77        return Ok(response);
78    }
79
80    pub async fn get_user(
81        &self,
82        user_ocid: String,
83    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
84        let client = reqwest::Client::new();
85
86        let mut headers = HeaderMap::new();
87
88        let now: DateTime<Utc> = Utc::now();
89        headers.insert(
90            "date",
91            now.to_rfc2822().replace("+0000", "GMT").parse().unwrap(),
92        );
93
94        let path = format!("/20160918/users/{}", user_ocid);
95
96        oci_signer(
97            &self.config,
98            &mut headers,
99            String::from("get"),
100            &path,
101            &self.service_endpoint,
102        );
103
104        let response = client
105            .get(format!("{}{}", self.service_endpoint, path))
106            .headers(headers)
107            .send()
108            .await?;
109
110        return Ok(response);
111    }
112
113    pub async fn list_users(
114        &self,
115        compartment_id: String,
116    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
117        let client = reqwest::Client::new();
118
119        let mut headers = HeaderMap::new();
120
121        let now: DateTime<Utc> = Utc::now();
122        headers.insert(
123            "date",
124            now.to_rfc2822().replace("+0000", "GMT").parse().unwrap(),
125        );
126
127        let path = format!("/20160918/users?compartmentId={}", compartment_id);
128
129        oci_signer(
130            &self.config,
131            &mut headers,
132            String::from("get"),
133            &path,
134            &self.service_endpoint,
135        );
136
137        let response = client
138            .get(format!("{}{}", self.service_endpoint, path))
139            // .query(&[("compartmentId", compartment_id)])
140            .headers(headers)
141            .send()
142            .await?;
143
144        return Ok(response);
145    }
146}