Skip to content

Commit

Permalink
handle gateway info metadata struct
Browse files Browse the repository at this point in the history
  • Loading branch information
andymck committed Apr 3, 2023
1 parent 4234f4f commit c648d31
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 164 deletions.
40 changes: 23 additions & 17 deletions iot_config_client/src/gateway_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,42 @@ pub trait GatewayInfoResolver {
#[derive(Debug, Clone)]
pub struct GatewayInfo {
pub address: PublicKeyBinary,
pub location: Option<u64>,
pub elevation: Option<i32>,
pub gain: i32,
pub is_full_hotspot: bool,
pub location: u64,
pub elevation: i32,
pub gain: i32,
pub region: Region,
}

#[derive(Debug, thiserror::Error)]
pub enum GatewayResolverError {
#[error("unsupported region {0}")]
Region(String),
#[error("gateway not asserted: {0}")]
GatewayNotAsserted(PublicKeyBinary),
#[error("gateway not found: {0}")]
GatewayNotFound(PublicKeyBinary),
}

impl TryFrom<GatewayInfoProto> for GatewayInfo {
type Error = GatewayResolverError;
fn try_from(v: GatewayInfoProto) -> Result<Self, GatewayResolverError> {
let region: Region = Region::from_i32(v.region)
.ok_or_else(|| GatewayResolverError::Region(format!("{:?}", v.region)))?;
let location = u64::from_str_radix(&v.location, 16).ok();
let elevation = match location {
Some(_) => Some(v.elevation),
None => None,
};
Ok(Self {
address: v.address.into(),
location,
elevation,
gain: v.gain,
is_full_hotspot: v.is_full_hotspot,
region,
})
//TODO: use the updated proto when available
// which will have a metadata field defined as an Option
// if Some(metadata) then we have an asserted gateway
// if None the gateway is unasserted
// we only return Ok(GatewayInfo) for asserted gateways
match location {
Some(_) => Ok(Self {
address: v.address.into(),
is_full_hotspot: v.is_full_hotspot,
location: location.unwrap(),
elevation: v.elevation,
gain: v.gain,
region: Region::from_i32(v.region).unwrap(),
}),
None => Err(GatewayResolverError::GatewayNotAsserted(v.address.into())),
}
}
}
7 changes: 6 additions & 1 deletion iot_config_client/src/iot_config_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum IotConfigClientError {
RegionParamsResolverError(#[from] region_params_resolver::RegionParamsResolverError),
#[error("gateway not found: {0}")]
GatewayNotFound(PublicKeyBinary),
#[error("gateway not asserted: {0}")]
GatewayNotAsserted(PublicKeyBinary),
#[error("region params not found {0}")]
RegionParamsNotFound(String),
#[error("uri error")]
Expand All @@ -57,7 +59,10 @@ impl GatewayInfoResolver for IotConfigClient {
req.signature = self.keypair.sign(&req.encode_to_vec())?;
let res = self.gateway_client.info(req).await?.into_inner();
match res.info {
Some(gateway_info) => Ok(gateway_info.try_into()?),
Some(gateway_info) => match gateway_info.try_into() {
Ok(gwinfo) => Ok(gwinfo),
Err(_) => Err(IotConfigClientError::GatewayNotAsserted(address.clone())),
},
_ => Err(IotConfigClientError::GatewayNotFound(address.clone())),
}
}
Expand Down
12 changes: 9 additions & 3 deletions iot_verifier/src/gateway_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub struct GatewayCache {
pub enum GatewayCacheError {
#[error("gateway not found: {0}")]
GatewayNotFound(PublicKeyBinary),
#[error("error querying iot config service")]
IotConfigClient(#[from] IotConfigClientError),
#[error("gateway not asserted: {0}")]
GatewayNotAsserted(PublicKeyBinary),
}

impl GatewayCache {
Expand Down Expand Up @@ -73,7 +73,13 @@ impl GatewayCache {
_ = self.insert(res.clone()).await;
Ok(res)
}
_ => Err(GatewayCacheError::GatewayNotFound(address.clone())),
Err(IotConfigClientError::GatewayNotAsserted(_)) => {
Err(GatewayCacheError::GatewayNotAsserted(address.clone()))
}
Err(IotConfigClientError::GatewayNotFound(_)) => {
Err(GatewayCacheError::GatewayNotFound(address.clone()))
}
Err(_) => Err(GatewayCacheError::GatewayNotFound(address.clone())),
}
}
}
Expand Down
Loading

0 comments on commit c648d31

Please sign in to comment.