cx_sdk/client/
ip_access.rs

1// Copyright 2025 Coralogix Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cx_api::proto::com::coralogixapis::aaa::v1::{
16    CreateCompanyIpAccessSettingsRequest,
17    CreateCompanyIpAccessSettingsResponse,
18    DeleteCompanyIpAccessSettingsRequest,
19    DeleteCompanyIpAccessSettingsResponse,
20    GetCompanyIpAccessSettingsRequest,
21    GetCompanyIpAccessSettingsResponse,
22    ReplaceCompanyIpAccessSettingsRequest,
23    ReplaceCompanyIpAccessSettingsResponse,
24    ip_access_service_client::IpAccessServiceClient,
25};
26
27use std::str::FromStr;
28
29use crate::{
30    CoralogixRegion,
31    auth::AuthContext,
32    error::{
33        Result,
34        SdkApiError,
35        SdkError,
36    },
37    metadata::CallProperties,
38    util::make_request_with_metadata,
39};
40
41use tokio::sync::Mutex;
42use tonic::{
43    metadata::MetadataMap,
44    transport::{
45        Channel,
46        ClientTlsConfig,
47        Endpoint,
48    },
49};
50
51pub use crate::com::coralogixapis::aaa::v1::{
52    CompanyIpAccessSettings,
53    CoralogixCustomerSupportAccess,
54    IpAccess,
55};
56
57const IP_ACCESS_FEATURE_GROUP_ID: &str = "aaa";
58
59/// IP Access API client.
60/// Read more at <https://coralogix.com/docs/user-guides/account-management/account-settings/ip-access-control/>
61pub struct IpAccessClient {
62    metadata_map: MetadataMap,
63    service_client: Mutex<IpAccessServiceClient<Channel>>,
64}
65
66impl IpAccessClient {
67    /// Creates a new client for the IP Access API.
68    ///
69    /// # Arguments
70    /// * `auth_context` - The [`AuthContext`] to use for authentication (team-level).
71    /// * `region` - The [`CoralogixRegion`] to connect to.
72    pub fn new(auth_context: AuthContext, region: CoralogixRegion) -> Result<Self> {
73        let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
74            .tls_config(ClientTlsConfig::new().with_native_roots())?
75            .connect_lazy();
76
77        // Use team-level API key semantics (same as ApiKeys client).
78        let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
79
80        Ok(Self {
81            metadata_map: request_metadata.to_metadata_map(),
82            service_client: Mutex::new(IpAccessServiceClient::new(channel)),
83        })
84    }
85
86    /// Creates company IP access settings.
87    ///
88    /// # Arguments
89    /// * `ip_access` - A vector of [`IpAccess`] settings to apply.
90    /// * `support_access` - Optional [`CoralogixCustomerSupportAccess`] setting to enable or disable access for Coralogix customer support.
91    pub async fn create(
92        &self,
93        ip_access: Vec<IpAccess>,
94        support_access: CoralogixCustomerSupportAccess,
95    ) -> Result<CreateCompanyIpAccessSettingsResponse> {
96        let req = CreateCompanyIpAccessSettingsRequest {
97            ip_access,
98            enable_coralogix_customer_support_access: support_access.into(),
99        };
100
101        let request = make_request_with_metadata(req, &self.metadata_map);
102
103        self.service_client
104            .lock()
105            .await
106            .create_company_ip_access_settings(request)
107            .await
108            .map(|r| r.into_inner())
109            .map_err(|status| {
110                SdkError::ApiError(SdkApiError {
111                    status,
112                    endpoint:
113                        "/com.coralogixapis.aaa.v1.IpAccessService/CreateCompanyIpAccessSettings"
114                            .into(),
115                    feature_group: IP_ACCESS_FEATURE_GROUP_ID.into(),
116                })
117            })
118    }
119
120    /// Replaces company IP access settings.
121    ///
122    /// # Arguments
123    /// * `id` - The optional ID of the settings to replace.
124    /// * `ip_access` - A vector of [`IpAccess`] settings to apply.
125    /// * `support_access` - Optional [`CoralogixCustomerSupportAccess`] setting to enable or disable access for Coralogix customer support.
126    pub async fn replace(
127        &self,
128        id: Option<String>,
129        ip_access: Vec<IpAccess>,
130        support_access: CoralogixCustomerSupportAccess,
131    ) -> Result<ReplaceCompanyIpAccessSettingsResponse> {
132        let req = ReplaceCompanyIpAccessSettingsRequest {
133            id,
134            ip_access,
135            enable_coralogix_customer_support_access: support_access.into(),
136        };
137
138        let request = make_request_with_metadata(req, &self.metadata_map);
139
140        self.service_client
141            .lock()
142            .await
143            .replace_company_ip_access_settings(request)
144            .await
145            .map(|r| r.into_inner())
146            .map_err(|status| {
147                SdkError::ApiError(SdkApiError {
148                    status,
149                    endpoint:
150                        "/com.coralogixapis.aaa.v1.IpAccessService/ReplaceCompanyIpAccessSettings"
151                            .into(),
152                    feature_group: IP_ACCESS_FEATURE_GROUP_ID.into(),
153                })
154            })
155    }
156
157    /// Retrieves company IP access settings.
158    ///
159    /// # Arguments
160    /// * `id` - The optional ID of the settings to retrieve.
161    pub async fn get(&self, id: Option<String>) -> Result<GetCompanyIpAccessSettingsResponse> {
162        let req = GetCompanyIpAccessSettingsRequest { id };
163
164        let request = make_request_with_metadata(req, &self.metadata_map);
165
166        self.service_client
167            .lock()
168            .await
169            .get_company_ip_access_settings(request)
170            .await
171            .map(|r| r.into_inner())
172            .map_err(|status| {
173                SdkError::ApiError(SdkApiError {
174                    status,
175                    endpoint:
176                        "/com.coralogixapis.aaa.v1.IpAccessService/GetCompanyIpAccessSettings"
177                            .into(),
178                    feature_group: IP_ACCESS_FEATURE_GROUP_ID.into(),
179                })
180            })
181    }
182
183    /// Deletes company IP access settings.
184    ///
185    /// # Arguments
186    /// * `id` - The optional ID of the settings to delete.
187    pub async fn delete(
188        &self,
189        id: Option<String>,
190    ) -> Result<DeleteCompanyIpAccessSettingsResponse> {
191        let req = DeleteCompanyIpAccessSettingsRequest { id };
192
193        let request = make_request_with_metadata(req, &self.metadata_map);
194
195        self.service_client
196            .lock()
197            .await
198            .delete_company_ip_access_settings(request)
199            .await
200            .map(|r| r.into_inner())
201            .map_err(|status| {
202                SdkError::ApiError(SdkApiError {
203                    status,
204                    endpoint:
205                        "/com.coralogixapis.aaa.v1.IpAccessService/DeleteCompanyIpAccessSettings"
206                            .into(),
207                    feature_group: IP_ACCESS_FEATURE_GROUP_ID.into(),
208                })
209            })
210    }
211}