def SetInitialOwner(unused_ref, args, request):
  """Set the initial owner.

  Defaults to 'empty' for dynamic groups and to 'with-initial-owner' for
  other group types.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  """
  if args.IsSpecified('with_initial_owner'):
    return request

  version = GetApiVersion(args)
  messages = ci_client.GetMessages(version)
  create_message = messages.CloudidentityGroupsCreateRequest
  config_enum = create_message.InitialGroupConfigValueValuesEnum

  if ((args.IsSpecified('group_type') and 'dynamic' in args.group_type)
      or (args.IsSpecified('labels') and 'dynamic' in args.labels)):
    request.initialGroupConfig = config_enum.EMPTY
  else:
    request.initialGroupConfig = config_enum.WITH_INITIAL_OWNER

  return request
Ejemplo n.º 2
0
def ReformatLabels(labels):
  """Reformat label list to encoded labels message.

  Reformatting labels will be done within following two steps,
  1. Filter label strings in a label list.
  2. Convert the filtered label list to OrderedDict.
  3. Encode the OrderedDict format of labels to group.labels message.

  Args:
    labels: list of label strings.
    e.g. ["cloudidentity.googleapis.com/security=",
          "cloudidentity.googleapis.com/discussion_forum"]
  Returns:
    Encoded labels message.

  Raises:
    InvalidArgumentException: If invalid labels string is input.
  """

  # Filter label strings in a label list.
  filtered_labels = FilterLabels(labels)

  # Convert the filtered label list to OrderedDict.
  labels_dict = collections.OrderedDict()
  for label in filtered_labels:
    if '=' in label:
      split_label = label.split('=')
      labels_dict[split_label[0]] = split_label[1]
    else:
      labels_dict[label] = ''

  # Encode the OrderedDict format of labels to group.labels message.
  messages = ci_client.GetMessages()
  return encoding.DictToMessage(labels_dict, messages.Group.LabelsValue)
Ejemplo n.º 3
0
def UpdateRolesParamsToUpdate(arg_dict):
    """Update roles params to update to modifyMembershipRolesRequest.updateRolesParams.

  Args:
    arg_dict: ArgDicts, RolesParams to update.
    (e.g. OrderedDict([(u'OWNER', u'expiration=4d')]))
  Returns:
    List of updateRolesParams to update.
  """

    roles_params = []
    messages = ci_client.GetMessages()
    arg_name = '--update-roles-params'
    for role, params in arg_dict.items():
        # Tokenize params to name and value
        # Example params: expiration=6m
        # ==> name: expiration, value: 6m
        # TODO(b/142829363): Implement to support multiple params if necessary.
        param_name, param_value = TokenizeParams(params, arg_name)

        # Instantiate MembershipRole object.
        expiry_detail = ReformatExpiryDetail(param_value)
        membership_role = messages.MembershipRole(name=role,
                                                  expiryDetail=expiry_detail)

        # Create 'update_mask' string
        update_mask = GetUpdateMask(param_name, arg_name)

        update_membership_roles_params = messages.ModifyMembershipRolesRequestUpdateMembershipRolesParams(
            fieldMask=update_mask, membershipRole=membership_role)

        roles_params.append(update_membership_roles_params)

    return roles_params
Ejemplo n.º 4
0
def SetDynamicUserQuery(unused_ref, args, request):
    """Add DynamicGroupUserQuery to DynamicGroupQueries object list.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated dynamic group queries.
  """

    queries = []

    if args.IsSpecified('dynamic_user_query'):
        dg_user_query = args.dynamic_user_query
        version = GetApiVersion(args)
        messages = ci_client.GetMessages(version)
        resource_type = messages.DynamicGroupQuery.ResourceTypeValueValuesEnum
        new_dynamic_group_query = messages.DynamicGroupQuery(
            resourceType=resource_type.USER, query=dg_user_query)
        queries.append(new_dynamic_group_query)
        request.group.dynamicGroupMetadata = messages.DynamicGroupMetadata(
            queries=queries)

    return request
Ejemplo n.º 5
0
def UpdateRoles(unused_ref, args, request):
    """Update 'MembershipRoles' to request.modifyMembershipRolesRequest.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.

  """

    # Following logic is used only when 'add-roles' parameter is used.
    if hasattr(args, 'add_roles') and args.IsSpecified('add_roles'):
        # Convert a comma separated string to a list of strings.
        role_list = args.add_roles.split(',')

        # Convert a list of strings to a list of MembershipRole objects.
        version = groups_hooks.GetApiVersion(args)
        roles = []
        messages = ci_client.GetMessages(version)
        for role in role_list:
            membership_role = messages.MembershipRole(name=role)
            roles.append(membership_role)

        request.modifyMembershipRolesRequest = messages.ModifyMembershipRolesRequest(
            addRoles=roles)

    return request
def SetLabelsCreate(unused_ref, args, request):
  """Set Labels to request.group.labels for the create command.

  Labels will be used from args.labels if supplied, otherwise labels
  will be looked up based on the args.group_type argument. If neither is
  supplied, labels will be set based on the 'discussion' group type.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  """
  if args.IsSpecified('labels'):
    labels = args.labels
  elif args.IsKnownAndSpecified('group_type'):
    labels = ' '.join(GROUP_TYPE_MAP[args.group_type])
  else:
    labels = ' '.join(GROUP_TYPE_MAP['discussion'])

  if hasattr(request.group, 'labels'):
    request.group.labels = ReformatLabels(args, labels)
  else:
    version = GetApiVersion(args)
    messages = ci_client.GetMessages(version)
    request.group = messages.Group(labels=ReformatLabels(args, labels))

  return request
Ejemplo n.º 7
0
def ReformatLabels(labels_dict):
    """Reformat labels string to labels map.

  Args:
    labels_dict: labels map.
  Returns:
    Encoded label message.
  """

    messages = ci_client.GetMessages()
    return encoding.DictToMessage(labels_dict, messages.Group.LabelsValue)
Ejemplo n.º 8
0
def ReformatExpiryDetail(expiration):
    """Reformat expiration string to ExpiryDetail object.

  Args:
    expiration: expiration string.
  Returns:
    ExpiryDetail object that contains the expiration data.
  """

    messages = ci_client.GetMessages()
    expiration_ts = expiration
    return messages.ExpiryDetail(expireTime=expiration_ts)
Ejemplo n.º 9
0
def ReformatUpdateRolesParams(args, update_roles_params):
    """Reformat update_roles_params string.

  Reformatting update_roles_params will be done by following steps,
  1. Split the comma separated string to a list of strings.
  2. Convert the splitted string to UpdateMembershipRolesParams message.

  Args:
    args: The argparse namespace.
    update_roles_params: A comma separated string.

  Returns:
    A list of reformatted 'UpdateMembershipRolesParams'.

  Raises:
    InvalidArgumentException: If invalid update_roles_params string is input.
  """

    # Split a comma separated 'update_roles_params' string.
    update_roles_params_list = update_roles_params.split(',')

    version = groups_hooks.GetApiVersion(args)
    messages = ci_client.GetMessages(version)
    roles_params = []
    arg_name = '--update-roles-params'
    for update_roles_param in update_roles_params_list:
        role, param_key, param_value = TokenizeUpdateRolesParams(
            update_roles_param, arg_name)

        # Membership expiry is supported only on a MEMBER role.
        if param_key == 'expiration' and role != 'MEMBER':
            error_msg = (
                'Membership Expiry is not supported on a specified role: {}.'
            ).format(role)
            raise exceptions.InvalidArgumentException(arg_name, error_msg)

        # Instantiate MembershipRole object.
        expiry_detail = ReformatExpiryDetail(version, param_value,
                                             'modify-membership-roles')
        membership_role = messages.MembershipRole(name=role,
                                                  expiryDetail=expiry_detail)

        update_mask = GetUpdateMask(param_key, arg_name)

        update_membership_roles_params = messages.UpdateMembershipRolesParams(
            fieldMask=update_mask, membershipRole=membership_role)

        roles_params.append(update_membership_roles_params)

    return roles_params
Ejemplo n.º 10
0
def ReformatExpiryDetail(expiration):
  """Reformat expiration string to ExpiryDetail object.

  Args:
    expiration: expiration string.

  Returns:
    ExpiryDetail object that contains the expiration data.

  """

  messages = ci_client.GetMessages()
  duration = 'P' + expiration
  expiration_ts = FormatDateTime(duration)
  return messages.MembershipRoleExpiryDetail(expireTime=expiration_ts)
Ejemplo n.º 11
0
def UpdateRolesToAdd(arg_list):
  """Update roles to add to modifyMembershipRolesRequest.addRoles.

  Args:
    arg_list: list of roles to add in string.
  Returns:
    List of MembershipRoles to add.
  """

  roles = []
  messages = ci_client.GetMessages()
  for arg in arg_list:
    membership_role = messages.MembershipRole(name=arg)
    roles.append(membership_role)

  return roles
Ejemplo n.º 12
0
def SetEntityKey(unused_ref, args, request):
    """Set EntityKey to request.group.groupKey.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.
  Returns:
    The updated request.
  """

    if hasattr(args, 'email'):
        messages = ci_client.GetMessages()
        request.group.groupKey = messages.EntityKey(id=args.email)

    return request
Ejemplo n.º 13
0
def ReformatMembershipRoles(roles_list):
    """Reformat roles string to MembershipRoles object list.

  Args:
    roles_list: list of roles in a string format.
  Returns:
    List of MembershipRoles object.
  """

    messages = ci_client.GetMessages()
    roles = []
    for role in roles_list:
        new_membership_role = messages.MembershipRole(name=role)
        roles.append(new_membership_role)

    return roles
Ejemplo n.º 14
0
def SetMembership(unused_ref, unused_args, request):
  """Set Membership in request.

  Args:
    unused_ref: unused.
    unused_args: unused.
    request: The request to modify.

  Returns:
    The updated request.

  """

  messages = ci_client.GetMessages()
  request.membership = messages.Membership()

  return request
Ejemplo n.º 15
0
def SetMembership(unused_ref, args, request):
    """Set Membership in request.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.

  """

    version = groups_hooks.GetApiVersion(args)
    messages = ci_client.GetMessages(version)
    request.membership = messages.Membership()

    return request
Ejemplo n.º 16
0
def SetEntityKey(unused_ref, args, request):
    """Set EntityKey in group resource.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.

  """

    messages = ci_client.GetMessages()
    if hasattr(args, 'member_email') and args.IsSpecified('member_email'):
        entity_key = messages.EntityKey(id=args.member_email)
        request.membership.preferredMemberKey = entity_key

    return request
Ejemplo n.º 17
0
def AddExpiryDetailInMembershipRoles(version, request, expiration):
    """Add an expiration in request.membership.roles.

  Args:
    version: version
    request: The request to modify
    expiration: expiration date to set

  Returns:
    The updated roles.

  Raises:
    InvalidArgumentException: If 'expiration' is specified without MEMBER role.

  """

    # When setting 'expiration', a single role should be input.
    if len(request.membership.roles) > 1:
        raise exceptions.InvalidArgumentException(
            'roles',
            'When setting "expiration", a single role should be input.')

    messages = ci_client.GetMessages(version)
    roles = []
    has_member_role = False
    for role in request.membership.roles:
        if hasattr(role, 'name') and role.name == 'MEMBER':
            has_member_role = True
            roles.append(
                messages.MembershipRole(name='MEMBER',
                                        expiryDetail=ReformatExpiryDetail(
                                            version, expiration)))
        else:
            roles.append(role)

    # Checking whether the 'expiration' is specified with a MEMBER role.
    if not has_member_role:
        raise exceptions.InvalidArgumentException(
            'expiration',
            'Expiration date can be set with a MEMBER role only.')

    return roles
Ejemplo n.º 18
0
def AddDynamicGroupUserQuery(arg_list):
    """Add DynamicGroupUserQuery to DynamicGroupQueries object list.

  Args:
    arg_list: dynamicGroupQuery whose resource type is USER.
  Returns:
    The updated dynamic group queries.
  """

    queries = []

    if arg_list:
        dg_user_query = ','.join(arg_list)
        messages = ci_client.GetMessages()
        resource_type = messages.DynamicGroupQuery.ResourceTypeValueValuesEnum
        new_dynamic_group_query = messages.DynamicGroupQuery(
            resourceType=resource_type.USER, query=dg_user_query)
        queries.append(new_dynamic_group_query)

    return queries
Ejemplo n.º 19
0
def ReformatExpiryDetail(version, expiration):
    """Reformat expiration string to ExpiryDetail object.

  Args:
    version: Release track information
    expiration: expiration string.

  Returns:
    ExpiryDetail object that contains the expiration data.

  """

    messages = ci_client.GetMessages(version)
    duration = 'P' + expiration
    expiration_ts = FormatDateTime(duration)

    if version == 'v1alpha1':
        return messages.MembershipRoleExpiryDetail(expireTime=expiration_ts)
    else:
        return messages.ExpiryDetail(expireTime=expiration_ts)
def SetLabels(unused_ref, args, request):
  """Set Labels to request.group.labels.

  Args:
    unused_ref: unused.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  """

  if args.IsSpecified('labels'):
    if hasattr(request.group, 'labels'):
      request.group.labels = ReformatLabels(args, args.labels)
    else:
      version = GetApiVersion(args)
      messages = ci_client.GetMessages(version)
      request.group = messages.Group(labels=ReformatLabels(args, args.labels))

  return request
Ejemplo n.º 21
0
def ReformatExpiryDetail(version, expiration, command):
    """Reformat expiration string to ExpiryDetail object.

  Args:
    version: Release track information
    expiration: expiration string.
    command: gcloud command name.

  Returns:
    ExpiryDetail object that contains the expiration data.

  """

    messages = ci_client.GetMessages(version)
    duration = 'P' + expiration
    expiration_ts = FormatDateTime(duration)

    if version == 'v1alpha1' and command == 'modify-membership-roles':
        return messages.MembershipRoleExpiryDetail(expireTime=expiration_ts)

    return messages.ExpiryDetail(expireTime=expiration_ts)
Ejemplo n.º 22
0
def SetParent(unused_ref, args, request):
    """Set obfuscated customer id to request.group.parent or request.parent.

  Args:
    unused_ref: A string representing the operation reference. Unused and may
      be None.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.
  """

    version = GetApiVersion(args)
    messages = ci_client.GetMessages(version)

    if not hasattr(request, 'group'):
        request.group = messages.Group()

    request.group.parent = GetCustomerId(args)

    return request
Ejemplo n.º 23
0
def ReformatMembershipRoles(roles_list):
  """Reformat roles string to MembershipRoles object list.

  Args:
    roles_list: list of roles in a string format.

  Returns:
    List of MembershipRoles object.

  """

  messages = ci_client.GetMessages()
  roles = []
  if not roles_list:
    # If no MembershipRole is provided, 'MEMBER' is used as a default value.
    roles.append(messages.MembershipRole(name='MEMBER'))
    return roles

  for role in roles_list:
    new_membership_role = messages.MembershipRole(name=role)
    roles.append(new_membership_role)

  return roles
Ejemplo n.º 24
0
def SetUpdateRolesParams(unused_ref, args, request):
    """Update 'MembershipRoles' to request.modifyMembershipRolesRequest.

  Args:
    unused_ref: A string representing the operation reference. Unused and may
      be None.
    args: The argparse namespace.
    request: The request to modify.

  Returns:
    The updated request.

  """

    if hasattr(
            args,
            'update_roles_params') and args.IsSpecified('update_roles_params'):
        version = groups_hooks.GetApiVersion(args)
        messages = ci_client.GetMessages(version)
        request.modifyMembershipRolesRequest = messages.ModifyMembershipRolesRequest(
            updateRolesParams=ReformatUpdateRolesParams(
                args, args.update_roles_params))

    return request