1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::sync::Arc;

use crate::http::bigquery_client::BigqueryClient;
use crate::http::error::Error;
use crate::http::row_access_policy;
use crate::http::row_access_policy::list::{
    ListRowAccessPoliciesRequest, ListRowAccessPoliciesResponse, RowAccessPolicyOverview,
};
use crate::http::table::get_iam_policy::GetIamPolicyRequest;
use crate::http::table::test_iam_permissions::{TestIamPermissionsRequest, TestIamPermissionsResponse};
use crate::http::types::Policy;

#[derive(Debug, Clone)]
pub struct BigqueryRowAccessPolicyClient {
    inner: Arc<BigqueryClient>,
}

impl BigqueryRowAccessPolicyClient {
    pub fn new(inner: Arc<BigqueryClient>) -> Self {
        Self { inner }
    }

    /// https://cloud.google.com/bigquery/docs/reference/rest/v2/rowAccessPolicies/getIamPolicy
    #[cfg_attr(feature = "trace", tracing::instrument(skip_all))]
    pub async fn get_iam_policy(
        &self,
        project_id: &str,
        dataset_id: &str,
        table_id: &str,
        policy_id: &str,
        req: &GetIamPolicyRequest,
    ) -> Result<Policy, Error> {
        let builder = row_access_policy::get_iam_policy::build(
            self.inner.endpoint(),
            self.inner.http(),
            project_id,
            dataset_id,
            table_id,
            policy_id,
            req,
        );
        self.inner.send(builder).await
    }

    /// https://cloud.google.com/bigquery/docs/reference/rest/v2/rowAccessPolicies/testIamPermissions
    #[cfg_attr(feature = "trace", tracing::instrument(skip_all))]
    pub async fn test_iam_permissions(
        &self,
        project_id: &str,
        dataset_id: &str,
        table_id: &str,
        policy_id: &str,
        req: &TestIamPermissionsRequest,
    ) -> Result<TestIamPermissionsResponse, Error> {
        let builder = row_access_policy::test_iam_permissions::build(
            self.inner.endpoint(),
            self.inner.http(),
            project_id,
            dataset_id,
            table_id,
            policy_id,
            req,
        );
        self.inner.send(builder).await
    }

    /// https://cloud.google.com/bigquery/docs/reference/rest/v2/rowAccessPolicies/list
    #[cfg_attr(feature = "trace", tracing::instrument(skip_all))]
    pub async fn list(
        &self,
        project_id: &str,
        dataset_id: &str,
        table_id: &str,
        req: &ListRowAccessPoliciesRequest,
    ) -> Result<Vec<RowAccessPolicyOverview>, Error> {
        let mut page_token: Option<String> = None;
        let mut policies = vec![];
        loop {
            let builder = row_access_policy::list::build(
                self.inner.endpoint(),
                self.inner.http(),
                project_id,
                dataset_id,
                table_id,
                req,
                page_token,
            );
            let response: ListRowAccessPoliciesResponse = self.inner.send(builder).await?;
            if let Some(data) = response.row_access_policies {
                policies.extend(data);
            }
            if response.next_page_token.is_none() {
                break;
            }
            page_token = response.next_page_token;
        }
        Ok(policies)
    }
}

#[cfg(test)]
mod test {
    use std::sync::Arc;

    use serial_test::serial;

    use crate::http::bigquery_client::test::create_client;
    use crate::http::bigquery_row_access_policy_client::BigqueryRowAccessPolicyClient;
    use crate::http::row_access_policy::list::ListRowAccessPoliciesRequest;
    use crate::http::table::get_iam_policy::GetIamPolicyRequest;
    use crate::http::table::TableReference;

    #[ctor::ctor]
    fn init() {
        let _ = tracing_subscriber::fmt::try_init();
    }

    #[tokio::test]
    #[serial]
    pub async fn test_policy() {
        /*
        CREATE ROW ACCESS POLICY test_policy ON `rust_test_job.rust_test_load_result_iam` GRANT TO ('allAuthenticatedUsers') FILTER USING (string_field_1='test');
        CREATE ROW ACCESS POLICY test_policy2 ON `rust_test_job.rust_test_load_result_iam` GRANT TO ('allAuthenticatedUsers') FILTER USING (string_field_1='testhoge');
         */
        let (client, project) = create_client().await;
        let client = BigqueryRowAccessPolicyClient::new(Arc::new(client));

        let table1 = TableReference {
            dataset_id: "rust_test_job".to_string(),
            project_id: project.to_string(),
            table_id: "rust_test_load_result_iam".to_string(),
        };

        // iam
        let policies = client
            .list(
                &table1.project_id,
                &table1.dataset_id,
                &table1.table_id,
                &ListRowAccessPoliciesRequest { page_size: Some(1) },
            )
            .await
            .unwrap();
        assert_eq!(policies.len(), 2);
        assert_eq!(policies[0].filter_predicate, "string_field_1 = 'test'");
        assert_eq!(policies[1].filter_predicate, "string_field_1 = 'testhoge'");
        for p in policies {
            let r = p.row_access_policy_reference;
            let p = client
                .get_iam_policy(
                    &r.project_id,
                    &r.dataset_id,
                    &r.table_id,
                    &r.policy_id,
                    &GetIamPolicyRequest { options: None },
                )
                .await
                .unwrap();
            assert_eq!(p.bindings[0].role, "roles/bigquery.filteredDataViewer");
        }
    }
}