Contact Us 1-800-596-4880

Accessing Policy Metadata

PDK exposes a Metadata injectable that provides metadata about the policy, the Flex instance, the API instance, and the Anypoint Organization. The Metadata struct is as follows:

pub struct Metadata {
    pub flex_metadata: FlexMetadata,
    pub policy_metadata: PolicyMetadata,
    pub api_metadata: ApiMetadata,
    pub platform_metadata: PlatformMetadata,
}

The Metadata struct references the following structures:

  • FlexMetadata:

    pub struct FlexMetadata {
        pub flex_name: String,
        pub flex_version: String,
    }
  • PolicyMetadata:

    pub struct PolicyMetadata {
        pub policy_name: String,
        pub policy_namespace: String,
    }
  • ApiMetadata:

    pub struct ApiMetadata {
        pub id: Option<String>,
        pub name: Option<String>,
        pub version: Option<String>,
        pub slas: Option<Vec<ApiSla>>, // API SLA tiers in the platform
    }
    
    // Each ApiSla contains the limits for each SLA.
    pub struct ApiSla {
        pub id: String,
        pub tiers: Vec<Tier>, // Limits within an API SLA tier in the platform
    }
  • PlatformMetadata:

    pub struct PlatformMetadata {
        pub organization_id: String,
        pub environment_id: String,
        pub root_organization_id: String,
    }

Inject Metadata into the #[entrypoint]

To inject metadata into the #[entrypoint] configuration function, see the following code snippet:

#[entrypoint]
async fn configure(launcher: Launcher, metadata: Metadata) -> Result<()> {
    logger::info!("Flex instance name is: {}", metadata.flex_metadata.flex_name.to_string());

    launcher
        .launch(on_request(filter))
        .await?;
    Ok(())
}

Inject Metadata into the Wrapped Functions

You cannot directly inject metadata into the wrapped functions. To use metadata in the wrapped functions, first inject the data into the #[entrypoint] function and then the wrapped function:

#[entrypoint]
async fn configure(launcher: Launcher, metadata: Metadata) -> Result<()> {
    launcher
        .launch(on_request(|r| request_filter(r, &metadata)))
        .await?;
    Ok(())
}

async fn request_filter(_: RequestState, metadata: &Metadata) -> Flow<()> {
    let mut vec = Vec::new();
    let headers = &mut vec;
    headers.push((
        "flex_name".to_string(),
        metadata.flex_metadata.flex_name.to_string(),
    ));
    headers.push((
        "policy_name".to_string(),
        metadata.policy_metadata.policy_name.to_string(),
    ));

    Flow::Break(Response::new(201).with_headers(vec))
}