rust-etcd icon indicating copy to clipboard operation
rust-etcd copied to clipboard

There is no isLeader attribute in the result of member list

Open yulidai opened this issue 6 years ago • 2 comments

i need the members' is_leader attribute, can you add it to the result of members::list(&client)?

yulidai avatar Dec 21 '18 09:12 yulidai

@jimmycuadra Or just add members::leader(&client) for getting leader, url is /v2/members/leader

yulidai avatar Dec 22 '18 07:12 yulidai

I solved it by adding this function into member.rs :)

/// Leader of members
///
/// # Parameters
///
/// * client: A `Client` to use to make the API call.
pub fn leader<C>(
    client: &Client<C>,
) -> impl Future<Item = Response<Member>, Error = Vec<Error>> + Send
    where
        C: Clone + Connect,
{
    let http_client = client.http_client().clone();

    first_ok(client.endpoints().to_vec(), move |member| {
        let url = build_url(member, "/leader");
        let uri = Uri::from_str(url.as_str())
            .map_err(Error::from)
            .into_future();

        println!("uri: {:?}", uri);
        let http_client = http_client.clone();

        let response = uri.and_then(move |uri| http_client.get(uri).map_err(Error::from));

        response.and_then(|response| {
            let status = response.status();
            let cluster_info = ClusterInfo::from(response.headers());
            let body = response.into_body().concat2().map_err(Error::from);
            body.and_then(move |ref body| {
                if status == StatusCode::OK {
                    match serde_json::from_slice::<Member>(body) {
                        Ok(data) => {
                            /*
                            for member in data.members {
                                println!("");
                            }
                            */
                            println!("data: {:?}", data);

                            Ok(Response {
                                data: data,
                                cluster_info,
                            })
                        },
                        Err(error) => Err(Error::Serialization(error)),
                    }
                } else {
                    match serde_json::from_slice::<ApiError>(body) {
                        Ok(error) => Err(Error::Api(error)),
                        Err(error) => Err(Error::Serialization(error)),
                    }
                }
            })
        })
    })
}

yulidai avatar Dec 22 '18 09:12 yulidai