From f15210a689d3a22bb76d97b9c22aaad1e7e44f5e Mon Sep 17 00:00:00 2001 From: ikuleshov Date: Thu, 27 May 2021 16:50:05 -0700 Subject: [PATCH] docs: add Admin API samples for account management methods (#65) * docs: add Admin API samples for account management methods * update copyright and remove redundant run_sample method * update noxfile template and set enforce_type_hints=False * add type annotations * docs: add Admin API samples for account user link management methods * fix the copyright string, avoid importing functions from other samples --- .../samples/accounts_user_links_audit.py | 59 ++++++++++++++ .../samples/accounts_user_links_audit_test.py | 26 ++++++ .../accounts_user_links_batch_create.py | 77 ++++++++++++++++++ .../accounts_user_links_batch_create_test.py | 32 ++++++++ .../accounts_user_links_batch_delete.py | 68 ++++++++++++++++ .../accounts_user_links_batch_delete_test.py | 30 +++++++ .../samples/accounts_user_links_batch_get.py | 61 ++++++++++++++ .../accounts_user_links_batch_get_test.py | 28 +++++++ .../accounts_user_links_batch_update.py | 79 +++++++++++++++++++ .../accounts_user_links_batch_update_test.py | 30 +++++++ .../samples/accounts_user_links_create.py | 69 ++++++++++++++++ .../accounts_user_links_create_test.py | 32 ++++++++ .../samples/accounts_user_links_delete.py | 62 +++++++++++++++ .../accounts_user_links_delete_test.py | 30 +++++++ .../samples/accounts_user_links_get.py | 63 +++++++++++++++ .../samples/accounts_user_links_get_test.py | 26 ++++++ .../samples/accounts_user_links_list.py | 50 ++++++++++++ .../samples/accounts_user_links_list_test.py | 25 ++++++ .../samples/accounts_user_links_update.py | 68 ++++++++++++++++ .../accounts_user_links_update_test.py | 30 +++++++ 20 files changed, 945 insertions(+) create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_audit.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_audit_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_create.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_create_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_delete.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_delete_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_get.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_get_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_update.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_batch_update_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_create.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_create_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_delete.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_delete_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_get.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_get_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_list.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_list_test.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_update.py create mode 100644 packages/google-analytics-admin/samples/accounts_user_links_update_test.py diff --git a/packages/google-analytics-admin/samples/accounts_user_links_audit.py b/packages/google-analytics-admin/samples/accounts_user_links_audit.py new file mode 100644 index 000000000000..106906a0a938 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_audit.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which prints all user links on +an account. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/audit +for more information. +""" +# [START analyticsadmin_accounts_user_links_audit] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import AuditUserLinksRequest + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + audit_account_user_links(account_id) + + +def audit_account_user_links(account_id): + """Lists all user links on an account, including implicit ones that come + from effective permissions granted by groups or organization admin roles.""" + client = AnalyticsAdminServiceClient() + + print("Result:") + for user_link in client.audit_user_links( + AuditUserLinksRequest(parent=f"accounts/{account_id}") + ): + print(f"Resource name: {user_link.name}") + print(f"Email address: {user_link.email_address}") + for direct_role in user_link.direct_roles: + print(f"Direct role: {direct_role}") + + for effective_role in user_link.effective_roles: + print(f"Effective role: {effective_role}") + print() + + +# [END analyticsadmin_accounts_user_links_audit] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_audit_test.py b/packages/google-analytics-admin/samples/accounts_user_links_audit_test.py new file mode 100644 index 000000000000..d717c6c80682 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_audit_test.py @@ -0,0 +1,26 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import accounts_user_links_audit + + +TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") + + +def test_accounts_user_links_audit(capsys): + accounts_user_links_audit.audit_account_user_links(TEST_ACCOUNT_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_create.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_create.py new file mode 100644 index 000000000000..56d619e38253 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_create.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a user link for +the account using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/batchCreate +for more information. +""" +# [START analyticsadmin_accounts_user_links_batch_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchCreateUserLinksRequest +from google.analytics.admin_v1alpha.types import CreateUserLinkRequest +from google.analytics.admin_v1alpha.types import UserLink + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics account ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with an email address of the user to + # link. This user will be given access to your account after running the + # sample. + email_address = "TEST-EMAIL-ADDRESS" + + batch_create_account_user_link(account_id, email_address) + + +def batch_create_account_user_link(account_id, email_address): + """Creates a user link for the account using a batch call.""" + client = AnalyticsAdminServiceClient() + response = client.batch_create_user_links( + BatchCreateUserLinksRequest( + parent=f"accounts/{account_id}", + requests=[ + CreateUserLinkRequest( + user_link=UserLink( + email_address=email_address, + direct_roles=["predefinedRoles/read"], + ) + ) + ], + notify_new_users=True, + ) + ) + + print("Result:") + for user_link in response.user_links: + print(user_link) + print() + + +# [END analyticsadmin_accounts_user_links_batch_create] + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_create_test.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_create_test.py new file mode 100644 index 000000000000..5141c174c167 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_create_test.py @@ -0,0 +1,32 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import pytest + +import accounts_user_links_batch_create + + +FAKE_ACCOUNT_ID = "1" +TEST_EMAIL_ADDRESS = os.getenv("GA_TEST_EMAIL_ADDRESS") + + +def test_accounts_user_links_batch_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + accounts_user_links_batch_create.batch_create_account_user_link( + FAKE_ACCOUNT_ID, TEST_EMAIL_ADDRESS + ) diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_delete.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_delete.py new file mode 100644 index 000000000000..102ad9e22366 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_delete.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the user link +using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/batchDelete +for more information. +""" +# [START analyticsadmin_accounts_user_links_batch_delete] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchDeleteUserLinksRequest +from google.analytics.admin_v1alpha.types import DeleteUserLinkRequest + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + batch_delete_account_user_link(account_id, account_user_link_id) + + +def batch_delete_account_user_link(account_id, account_user_link_id): + """Deletes the user link using a batch call.""" + client = AnalyticsAdminServiceClient() + client.batch_delete_user_links( + BatchDeleteUserLinksRequest( + parent=f"accounts/{account_id}", + requests=[ + DeleteUserLinkRequest( + name=f"accounts/{account_id}/userLinks/{account_user_link_id}" + ) + ], + ) + ) + print("User link deleted") + + +# [END analyticsadmin_accounts_user_links_batch_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_delete_test.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_delete_test.py new file mode 100644 index 000000000000..3b9eac242e96 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_delete_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import accounts_user_links_batch_delete + + +FAKE_ACCOUNT_ID = "1" +FAKE_ACCOUNT_USER_LINK_ID = "1" + + +def test_accounts_user_links_batch_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + accounts_user_links_batch_delete.batch_delete_account_user_link( + FAKE_ACCOUNT_ID, FAKE_ACCOUNT_USER_LINK_ID + ) diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_get.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_get.py new file mode 100644 index 000000000000..d5506d3038e7 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_get.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which prints the details for +the account user link using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/batchGet +for more information. +""" +# [START analyticsadmin_accounts_user_links_batch_get] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchGetUserLinksRequest + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + batch_get_account_user_link(account_id, account_user_link_id) + + +def batch_get_account_user_link(account_id, account_user_link_id): + """Retrieves details for the account user link using a batch call.""" + client = AnalyticsAdminServiceClient() + response = client.batch_get_user_links( + BatchGetUserLinksRequest( + parent=f"accounts/{account_id}", + names=[f"accounts/{account_id}/userLinks/{account_user_link_id}"], + ) + ) + + print("Result:") + for user_link in response.user_links: + print(user_link) + print() + + +# [END analyticsadmin_accounts_user_links_batch_get] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_get_test.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_get_test.py new file mode 100644 index 000000000000..29775cb6d58a --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_get_test.py @@ -0,0 +1,28 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import accounts_user_links_batch_get + +TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") +TEST_USER_LINK_ID = os.getenv("GA_TEST_USER_LINK_ID") + + +def test_accounts_user_links_batch_get(capsys): + accounts_user_links_batch_get.batch_get_account_user_link( + TEST_ACCOUNT_ID, TEST_USER_LINK_ID + ) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_update.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_update.py new file mode 100644 index 000000000000..9778a506757f --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_update.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the account +user link using a batch call. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/batchUpdate +for more information. +""" +# [START analyticsadmin_accounts_user_links_batch_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import BatchUpdateUserLinksRequest +from google.analytics.admin_v1alpha.types import UpdateUserLinkRequest +from google.analytics.admin_v1alpha.types import UserLink + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + batch_update_account_user_link(account_id, account_user_link_id) + + +def batch_update_account_user_link(account_id, account_user_link_id): + """Updates the account user link using a batch call.""" + client = AnalyticsAdminServiceClient() + # This call updates the email address and direct roles of the user link. + # The user link to update is specified in the `name` field of the `UserLink` + # instance. + response = client.batch_update_user_links( + BatchUpdateUserLinksRequest( + parent=f"accounts/{account_id}", + requests=[ + UpdateUserLinkRequest( + user_link=UserLink( + name=f"accounts/{account_id}/userLinks/{account_user_link_id}", + direct_roles=["predefinedRoles/collaborate"], + ), + ) + ], + ) + ) + + print("Result:") + for user_link in response.user_links: + print(user_link) + print() + + +# [END analyticsadmin_accounts_user_links_batch_update] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_batch_update_test.py b/packages/google-analytics-admin/samples/accounts_user_links_batch_update_test.py new file mode 100644 index 000000000000..b709c4f87513 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_batch_update_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import accounts_user_links_batch_update + + +FAKE_ACCOUNT_ID = "1" +FAKE_ACCOUNT_USER_LINK_ID = "1" + + +def test_accounts_user_links_batch_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + accounts_user_links_batch_update.batch_update_account_user_link( + FAKE_ACCOUNT_ID, FAKE_ACCOUNT_USER_LINK_ID + ) diff --git a/packages/google-analytics-admin/samples/accounts_user_links_create.py b/packages/google-analytics-admin/samples/accounts_user_links_create.py new file mode 100644 index 000000000000..5fe6ac35646b --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_create.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which creates a user link +for the account. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/create +for more information. +""" +# [START analyticsadmin_accounts_user_links_create] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import CreateUserLinkRequest +from google.analytics.admin_v1alpha.types import UserLink + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics account ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with an email address of the user to + # link. This user will be given access to your account after running the + # sample. + email_address = "TEST-EMAIL-ADDRESS" + + create_account_user_link(account_id, email_address) + + +def create_account_user_link(account_id, email_address): + """Creates a user link for the account.""" + client = AnalyticsAdminServiceClient() + user_link = client.create_user_link( + CreateUserLinkRequest( + parent=f"accounts/{account_id}", + user_link=UserLink( + email_address=email_address, direct_roles=["predefinedRoles/read"] + ), + notify_new_user=True, + ) + ) + + print("Result:") + print(user_link) + + +# [END analyticsadmin_accounts_user_links_create] + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_create_test.py b/packages/google-analytics-admin/samples/accounts_user_links_create_test.py new file mode 100644 index 000000000000..13c721298eb8 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_create_test.py @@ -0,0 +1,32 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import pytest + +import accounts_user_links_create + + +FAKE_ACCOUNT_ID = "1" +TEST_EMAIL_ADDRESS = os.getenv("GA_TEST_EMAIL_ADDRESS") + + +def test_accounts_user_links_create(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + accounts_user_links_create.create_account_user_link( + FAKE_ACCOUNT_ID, TEST_EMAIL_ADDRESS + ) diff --git a/packages/google-analytics-admin/samples/accounts_user_links_delete.py b/packages/google-analytics-admin/samples/accounts_user_links_delete.py new file mode 100644 index 000000000000..1263d962f100 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_delete.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which deletes the user link +for the account. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/delete +for more information. +""" +# [START analyticsadmin_accounts_user_links_delete] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import DeleteUserLinkRequest + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + delete_account_user_link(account_id, account_user_link_id) + + +def delete_account_user_link(account_id, account_user_link_id): + """Deletes the user link for the account.""" + client = AnalyticsAdminServiceClient() + client.delete_user_link( + DeleteUserLinkRequest( + name=f"accounts/{account_id}/userLinks/{account_user_link_id}" + ) + ) + print("User link deleted") + + +# [END analyticsadmin_accounts_user_links_delete] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_delete_test.py b/packages/google-analytics-admin/samples/accounts_user_links_delete_test.py new file mode 100644 index 000000000000..e8bd0a5ded41 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_delete_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import accounts_user_links_delete + + +FAKE_ACCOUNT_ID = "1" +FAKE_ACCOUNT_USER_LINK_ID = "1" + + +def test_accounts_user_links_delete(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + accounts_user_links_delete.delete_account_user_link( + FAKE_ACCOUNT_ID, FAKE_ACCOUNT_USER_LINK_ID + ) diff --git a/packages/google-analytics-admin/samples/accounts_user_links_get.py b/packages/google-analytics-admin/samples/accounts_user_links_get.py new file mode 100644 index 000000000000..a9fc8b3d2816 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_get.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which prints the account user +link details. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/get +for more information. +""" +# [START analyticsadmin_accounts_user_links_get] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + get_account_user_link(account_id, account_user_link_id) + + +def get_account_user_link(account_id, account_user_link_id): + """Retrieves the account user link details.""" + client = AnalyticsAdminServiceClient() + user_link = client.get_user_link( + name=f"accounts/{account_id}/userLinks/{account_user_link_id}" + ) + + print("Result:") + print_user_link(user_link) + + +def print_user_link(user_link): + """Prints the user link details.""" + print(f"Resource name: {user_link.name}") + print(f"Email address: {user_link.email_address}") + for direct_role in user_link.direct_roles: + print(f"Direct role: {direct_role}") + + +# [END analyticsadmin_accounts_user_links_get] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_get_test.py b/packages/google-analytics-admin/samples/accounts_user_links_get_test.py new file mode 100644 index 000000000000..0e717d378059 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_get_test.py @@ -0,0 +1,26 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import accounts_user_links_get + +TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") +TEST_USER_LINK_ID = os.getenv("GA_TEST_USER_LINK_ID") + + +def test_accounts_user_links_get(capsys): + accounts_user_links_get.get_account_user_link(TEST_ACCOUNT_ID, TEST_USER_LINK_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/packages/google-analytics-admin/samples/accounts_user_links_list.py b/packages/google-analytics-admin/samples/accounts_user_links_list.py new file mode 100644 index 000000000000..f42f8ffc5dbe --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_list.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which prints user links +under the specified parent account. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/list +for more information. +""" +# [START analyticsadmin_accounts_user_links_list] +from google.analytics.admin import AnalyticsAdminServiceClient + + +def run_sample(): + """Runs the sample.""" + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + list_account_user_links(account_id) + + +def list_account_user_links(account_id): + """Lists user links under the specified parent account.""" + client = AnalyticsAdminServiceClient() + results = client.list_user_links(parent=f"accounts/{account_id}") + + print("Result:") + for user_link in results: + print(user_link) + print() + + +# [END analyticsadmin_accounts_user_links_list] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_list_test.py b/packages/google-analytics-admin/samples/accounts_user_links_list_test.py new file mode 100644 index 000000000000..6eb0806ea79c --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_list_test.py @@ -0,0 +1,25 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import accounts_user_links_list + +TEST_ACCOUNT_ID = os.getenv("GA_TEST_ACCOUNT_ID") + + +def test_accounts_user_links_list(capsys): + accounts_user_links_list.list_account_user_links(TEST_ACCOUNT_ID) + out, _ = capsys.readouterr() + assert "Result" in out diff --git a/packages/google-analytics-admin/samples/accounts_user_links_update.py b/packages/google-analytics-admin/samples/accounts_user_links_update.py new file mode 100644 index 000000000000..1a9424abc765 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_update.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Analytics Admin API sample application which updates the account +user link. + +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.userLinks/update +for more information. +""" +# [START analyticsadmin_accounts_user_links_update] +from google.analytics.admin import AnalyticsAdminServiceClient +from google.analytics.admin_v1alpha.types import UserLink + + +def run_sample(): + """Runs the sample.""" + + # !!! ATTENTION !!! + # Running this sample may change/delete your Google Analytics account + # configuration. Make sure to not use the Google Analytics property ID from + # your production environment below. + + # TODO(developer): Replace this variable with your Google Analytics + # account ID (e.g. "123456") before running the sample. + account_id = "YOUR-GA-ACCOUNT-ID" + + # TODO(developer): Replace this variable with your Google Analytics + # account user link ID (e.g. "123456") before running the sample. + account_user_link_id = "YOUR-ACCOUNT-USER-LINK-ID" + + update_account_user_link(account_id, account_user_link_id) + + +def update_account_user_link(account_id, account_user_link_id): + """Updates the account user link.""" + client = AnalyticsAdminServiceClient() + # This call updates the email address and direct roles of the user link. + # The user link to update is specified in the `name` field of the `UserLink` + # instance. + user_link = client.update_user_link( + user_link=UserLink( + name=f"accounts/{account_id}/userLinks/{account_user_link_id}", + direct_roles=["predefinedRoles/collaborate"], + ), + ) + + print("Result:") + print(user_link) + + +# [END analyticsadmin_accounts_user_links_update] + + +if __name__ == "__main__": + run_sample() diff --git a/packages/google-analytics-admin/samples/accounts_user_links_update_test.py b/packages/google-analytics-admin/samples/accounts_user_links_update_test.py new file mode 100644 index 000000000000..a72293072290 --- /dev/null +++ b/packages/google-analytics-admin/samples/accounts_user_links_update_test.py @@ -0,0 +1,30 @@ +# Copyright 2021 Google LLC All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +import accounts_user_links_update + + +FAKE_ACCOUNT_ID = "1" +FAKE_ACCOUNT_USER_LINK_ID = "1" + + +def test_accounts_user_links_update(): + # This test ensures that the call is valid and reaches the server, even + # though the operation does not succeed due to permission error. + with pytest.raises(Exception, match="403 The caller does not have permission"): + accounts_user_links_update.update_account_user_link( + FAKE_ACCOUNT_ID, FAKE_ACCOUNT_USER_LINK_ID + )