1# -*- coding: utf-8 -*- #
2# Copyright 2019 Google LLC. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Useful commands for interacting with the Cloud Identity Groups API."""
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import unicode_literals
19
20from apitools.base.py import encoding
21
22from googlecloudsdk.api_lib.util import apis
23
24API_NAME = 'cloudidentity'
25
26
27def GetClient(version):
28  """Import and return the appropriate Cloud Identity Groups client.
29
30  Args:
31    version: str, the version of the API desired
32
33  Returns:
34    Cloud Identity Groups client for the appropriate release track
35  """
36  return apis.GetClientInstance(API_NAME, version)
37
38
39def GetMessages(version):
40  """Import and return the appropriate Cloud Identity Groups messages module.
41
42  Args:
43    version: str, the version of the API desired
44
45  Returns:
46    Cloud Identity Groups messages for the appropriate release track
47  """
48  return apis.GetMessagesModule(API_NAME, version)
49
50
51def GetGroup(version, group):
52  """Get a Cloud Identity Group.
53
54  Args:
55    version: Release track information.
56    group: Name of group as returned by LookupGroupName()
57      (i.e. 'groups/{group_id}').
58  Returns:
59    Group resource object.
60  """
61  client = GetClient(version)
62  messages = GetMessages(version)
63  return client.groups.Get(
64      messages.CloudidentityGroupsGetRequest(name=group))
65
66
67def LookupGroupName(version, email):
68  """Lookup Group Name for a specified group key id.
69
70  Args:
71    version: Release track information
72    email: str, group email
73
74  Returns:
75    LookupGroupNameResponse: Response message for LookupGroupName operation
76    which is containing a resource name of the group in the format:
77    'name: groups/{group_id}'
78  """
79
80  client = GetClient(version)
81  messages = GetMessages(version)
82
83  encoding.AddCustomJsonFieldMapping(
84      messages.CloudidentityGroupsLookupRequest,
85      'groupKey_id', 'groupKey.id')
86  return client.groups.Lookup(
87      messages.CloudidentityGroupsLookupRequest(groupKey_id=email))
88
89
90def LookupMembershipName(version, group_id, member_email):
91  """Lookup membership name for a specific pair of member key id and group email.
92
93  Args:
94    version: Release track information
95    group_id: str, group id (e.g. groups/03qco8b4452k99t)
96    member_email: str, member email
97  Returns:
98    LookupMembershipNameResponse: Response message for LookupMembershipName
99    operation which is containing a resource name of the membership in the
100    format:
101    'name: members/{member_id}'
102  """
103
104  client = GetClient(version)
105  messages = GetMessages(version)
106
107  encoding.AddCustomJsonFieldMapping(
108      messages.CloudidentityGroupsMembershipsLookupRequest,
109      'memberKey_id', 'memberKey.id')
110  return client.groups_memberships.Lookup(
111      messages.CloudidentityGroupsMembershipsLookupRequest(
112          memberKey_id=member_email, parent=group_id))
113