v2_service_accounts_CreateServiceAccount/
v2_service-accounts_CreateServiceAccount.rs

1// Create a service account returns "OK" response
2use datadog_api_client::datadog;
3use datadog_api_client::datadogV2::api_service_accounts::ServiceAccountsAPI;
4use datadog_api_client::datadogV2::model::RelationshipToRoleData;
5use datadog_api_client::datadogV2::model::RelationshipToRoles;
6use datadog_api_client::datadogV2::model::RolesType;
7use datadog_api_client::datadogV2::model::ServiceAccountCreateAttributes;
8use datadog_api_client::datadogV2::model::ServiceAccountCreateData;
9use datadog_api_client::datadogV2::model::ServiceAccountCreateRequest;
10use datadog_api_client::datadogV2::model::UserRelationships;
11use datadog_api_client::datadogV2::model::UsersType;
12
13#[tokio::main]
14async fn main() {
15    // there is a valid "role" in the system
16    let role_data_id = std::env::var("ROLE_DATA_ID").unwrap();
17    let body =
18        ServiceAccountCreateRequest::new(
19            ServiceAccountCreateData::new(
20                ServiceAccountCreateAttributes::new(
21                    "Example-Service-Account@datadoghq.com".to_string(),
22                    true,
23                )
24                .name("Test API Client".to_string()),
25                UsersType::USERS,
26            )
27            .relationships(UserRelationships::new().roles(
28                RelationshipToRoles::new().data(vec![RelationshipToRoleData::new()
29                    .id(role_data_id.clone())
30                    .type_(RolesType::ROLES)]),
31            )),
32        );
33    let configuration = datadog::Configuration::new();
34    let api = ServiceAccountsAPI::with_config(configuration);
35    let resp = api.create_service_account(body).await;
36    if let Ok(value) = resp {
37        println!("{:#?}", value);
38    } else {
39        println!("{:#?}", resp.unwrap_err());
40    }
41}