Skip to content

Commit e9820f4

Browse files
committed
Added access binding samples
1 parent d0deecd commit e9820f4

39 files changed

Lines changed: 1944 additions & 3 deletions

File tree

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ nox -s lint blacken
7070
export GA_TEST_WEB_DATA_STREAM_ID=
7171
export GA_TEST_WEB_DATA_SECRET_ID=
7272
export GA_TEST_CONVERSION_EVENT_ID=
73+
export GA_TEST_ACCOUNT_ACCESS_BINDING_ID=
74+
export GA_TEST_PROPERTY_ACCESS_BINDING_ID=
7375
```
7476

7577
4. Execute tests from the `google-analytics-admin` or `google-analytics-data`
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which creates a access binding for
18+
the account using a batch call.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.accessBindings/batchCreate
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_access_bindings_batch_create]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
from google.analytics.admin_v1alpha.types import (
26+
AccessBinding,
27+
BatchCreateAccessBindingsRequest,
28+
CreateAccessBindingRequest,
29+
)
30+
31+
32+
def run_sample():
33+
"""Runs the sample."""
34+
35+
# !!! ATTENTION !!!
36+
# Running this sample may change/delete your Google Analytics account
37+
# configuration. Make sure to not use the Google Analytics account ID from
38+
# your production environment below.
39+
40+
# TODO(developer): Replace this variable with your Google Analytics
41+
# account ID (e.g. "123456") before running the sample.
42+
account_id = "YOUR-GA-ACCOUNT-ID"
43+
44+
# TODO(developer): Replace this variable with an email address of the user to
45+
# link. This user will be given access to your account after running the
46+
# sample.
47+
email_address = "TEST-EMAIL-ADDRESS"
48+
49+
batch_create_account_access_binding(account_id, email_address)
50+
51+
52+
def batch_create_account_access_binding(
53+
account_id: str, email_address: str, transport: str = None
54+
):
55+
"""
56+
Creates a access binding for the account using a batch call.
57+
58+
Args:
59+
account_id(str): The Google Analytics Account ID.
60+
email_address(str): Email address of the access binding user.
61+
transport(str): The transport to use. For example, "grpc"
62+
or "rest". If set to None, a transport is chosen automatically.
63+
"""
64+
client = AnalyticsAdminServiceClient(transport=transport)
65+
response = client.batch_create_access_bindings(
66+
BatchCreateAccessBindingsRequest(
67+
parent=f"accounts/{account_id}",
68+
requests=[
69+
CreateAccessBindingRequest(
70+
access_binding=AccessBinding(
71+
user=email_address,
72+
roles=["predefinedRoles/read"],
73+
)
74+
)
75+
],
76+
)
77+
)
78+
79+
print("Result:")
80+
for access_binding in response.access_bindings:
81+
print(access_binding)
82+
print()
83+
84+
85+
# [END analyticsadmin_accounts_access_bindings_batch_create]
86+
87+
if __name__ == "__main__":
88+
run_sample()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
import accounts_access_bindings_batch_create
18+
19+
FAKE_ACCOUNT_ID = "1"
20+
FAKE_EMAIL_ADDRESS = "FAKE_EMAIL_ADDRESS"
21+
22+
23+
def test_accounts_access_bindings_batch_create():
24+
transports = ["grpc", "rest"]
25+
for transport in transports:
26+
# This test ensures that the call is valid and reaches the server, even
27+
# though the operation does not succeed due to permission error.
28+
with pytest.raises(Exception, match="The caller does not have permission"):
29+
accounts_access_bindings_batch_create.batch_create_account_access_binding(
30+
FAKE_ACCOUNT_ID, FAKE_EMAIL_ADDRESS, transport=transport
31+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which deletes the access binding
18+
using a batch call.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.accessBindings/batchDelete
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_access_bindings_batch_delete]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
from google.analytics.admin_v1alpha.types import (
26+
BatchDeleteAccessBindingsRequest,
27+
DeleteAccessBindingRequest,
28+
)
29+
30+
31+
def run_sample():
32+
"""Runs the sample."""
33+
34+
# !!! ATTENTION !!!
35+
# Running this sample may change/delete your Google Analytics account
36+
# configuration. Make sure to not use the Google Analytics property ID from
37+
# your production environment below.
38+
39+
# TODO(developer): Replace this variable with your Google Analytics
40+
# account ID (e.g. "123456") before running the sample.
41+
account_id = "YOUR-GA-ACCOUNT-ID"
42+
43+
# TODO(developer): Replace this variable with your Google Analytics
44+
# account access binding ID (e.g. "123456") before running the sample.
45+
account_access_binding_id = "YOUR-ACCOUNT-ACCESS-BINDING-ID"
46+
47+
batch_delete_account_access_binding(account_id, account_access_binding_id)
48+
49+
50+
def batch_delete_account_access_binding(
51+
account_id: str, account_access_binding_id: str, transport: str = None
52+
):
53+
"""
54+
Deletes the access binding using a batch call.
55+
56+
Args:
57+
account_id(str): The Google Analytics Account ID.
58+
account_access_binding_id(str): Google Analytics account access binding ID.
59+
transport(str): The transport to use. For example, "grpc"
60+
or "rest". If set to None, a transport is chosen automatically.
61+
"""
62+
client = AnalyticsAdminServiceClient(transport=transport)
63+
client.batch_delete_access_bindings(
64+
BatchDeleteAccessBindingsRequest(
65+
parent=f"accounts/{account_id}",
66+
requests=[
67+
DeleteAccessBindingRequest(
68+
name=f"accounts/{account_id}/accessBindings/{account_access_binding_id}"
69+
)
70+
],
71+
)
72+
)
73+
print("Access binding deleted")
74+
75+
76+
# [END analyticsadmin_accounts_access_bindings_batch_delete]
77+
78+
79+
if __name__ == "__main__":
80+
run_sample()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
17+
import accounts_access_bindings_batch_delete
18+
19+
FAKE_ACCOUNT_ID = "1"
20+
FAKE_ACCOUNT_ACCESS_BINDING_ID = "1"
21+
22+
23+
def test_accounts_access_bindings_batch_delete():
24+
transports = ["grpc", "rest"]
25+
for transport in transports:
26+
# This test ensures that the call is valid and reaches the server, even
27+
# though the operation does not succeed due to permission error.
28+
with pytest.raises(Exception, match="The caller does not have permission"):
29+
accounts_access_bindings_batch_delete.batch_delete_account_access_binding(
30+
FAKE_ACCOUNT_ID, FAKE_ACCOUNT_ACCESS_BINDING_ID, transport=transport
31+
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 Google LLC All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Analytics Admin API sample application which prints the details for
18+
the account access binding using a batch call.
19+
20+
See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.accessBindings/batchGet
21+
for more information.
22+
"""
23+
# [START analyticsadmin_accounts_access_bindings_batch_get]
24+
from google.analytics.admin import AnalyticsAdminServiceClient
25+
from google.analytics.admin_v1alpha.types import BatchGetAccessBindingsRequest
26+
27+
28+
def run_sample():
29+
"""Runs the sample."""
30+
# TODO(developer): Replace this variable with your Google Analytics
31+
# account ID (e.g. "123456") before running the sample.
32+
account_id = "YOUR-GA-ACCOUNT-ID"
33+
34+
# TODO(developer): Replace this variable with your Google Analytics
35+
# account access binding ID (e.g. "123456") before running the sample.
36+
account_access_binding_id = "YOUR-ACCOUNT-ACCESS-BINDING-ID"
37+
38+
batch_get_account_access_binding(account_id, account_access_binding_id)
39+
40+
41+
def batch_get_account_access_binding(
42+
account_id: str, account_access_binding_id: str, transport: str = None
43+
):
44+
"""
45+
Retrieves details for the account access binding using a batch call.
46+
47+
Args:
48+
account_id(str): The Google Analytics Account ID.
49+
account_access_binding_id(str): Google Analytics account access binding ID.
50+
transport(str): The transport to use. For example, "grpc"
51+
or "rest". If set to None, a transport is chosen automatically.
52+
"""
53+
client = AnalyticsAdminServiceClient(transport=transport)
54+
response = client.batch_get_access_bindings(
55+
BatchGetAccessBindingsRequest(
56+
parent=f"accounts/{account_id}",
57+
names=[f"accounts/{account_id}/accessBindings/{account_access_binding_id}"],
58+
)
59+
)
60+
61+
print("Result:")
62+
for access_binding in response.access_bindings:
63+
print(access_binding)
64+
print()
65+
66+
67+
# [END analyticsadmin_accounts_access_bindings_batch_get]
68+
69+
70+
if __name__ == "__main__":
71+
run_sample()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2021 Google LLC All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import accounts_access_bindings_batch_get
18+
19+
TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID")
20+
TEST_ACCOUNT_ACCESS_BINDING_ID = os.getenv("GA_TEST_ACCOUNT_ACCESS_BINDING_ID")
21+
22+
23+
def test_accounts_access_bindings_batch_get(capsys):
24+
transports = ["grpc", "rest"]
25+
for transport in transports:
26+
accounts_access_bindings_batch_get.batch_get_account_access_binding(
27+
TEST_ACCOUNT_ID, TEST_ACCOUNT_ACCESS_BINDING_ID, transport=transport
28+
)
29+
out, _ = capsys.readouterr()
30+
assert "Result" in out

0 commit comments

Comments
 (0)