Skip to main content

rabbitmq_http_client/blocking_api/
feature_flags.rs

1// Copyright (C) 2023-2025 RabbitMQ Core Team (teamrabbitmq@gmail.com)
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// http://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 crate::{
16    path,
17    responses::{FeatureFlag, FeatureFlagList, FeatureFlagStability, FeatureFlagState},
18};
19
20use super::client::{Client, Result};
21use std::fmt::Display;
22
23impl<E, U, P> Client<E, U, P>
24where
25    E: Display,
26    U: Display,
27    P: Display,
28{
29    /// Lists all feature flags and their current states.
30    /// See [Feature Flags Guide](https://www.rabbitmq.com/docs/feature-flags) to learn more.
31    ///
32    /// Requires the `administrator` user tag. Does not modify state.
33    pub fn list_feature_flags(&self) -> Result<FeatureFlagList> {
34        let response = self.http_get("feature-flags", None, None)?;
35        let response = response.json()?;
36        Ok(response)
37    }
38
39    /// Enables a specific feature flag by name.
40    /// This function is idempotent: enabling an already enabled feature flag
41    /// will succeed.
42    /// See [Feature Flags Guide](https://www.rabbitmq.com/docs/feature-flags) to learn more.
43    ///
44    /// Requires the `administrator` user tag.
45    pub fn enable_feature_flag(&self, name: &str) -> Result<()> {
46        let body = serde_json::json!({
47            "name": name
48        });
49        let _response = self.http_put(path!("feature-flags", name, "enable"), &body, None, None)?;
50        Ok(())
51    }
52
53    /// Enables all stable feature flags in the cluster.
54    /// This function is idempotent: enabling an already enabled feature flag
55    /// will succeed.
56    /// See [Feature Flags Guide](https://www.rabbitmq.com/docs/feature-flags) to learn more.
57    ///
58    /// Requires the `administrator` user tag.
59    pub fn enable_all_stable_feature_flags(&self) -> Result<()> {
60        // PUT /api/feature-flags/{name}/enable does not support the special 'all' value like 'rabbitmqctl enable_feature_flag' does.
61        // Thus we do what management UI does: discover the stable disabled flags and enable
62        // them one by one.
63        let discovered_flags = self.list_feature_flags()?;
64        let flags_to_enable: Vec<&FeatureFlag> = discovered_flags
65            .0
66            .iter()
67            .filter(|&ff| {
68                ff.state == FeatureFlagState::Disabled
69                    && ff.stability == FeatureFlagStability::Stable
70            })
71            .collect();
72
73        for ff in flags_to_enable {
74            self.enable_feature_flag(&ff.name)?;
75        }
76
77        Ok(())
78    }
79}