Beispiel #1
0
def Update(project_ref,
           name=None,
           parent=None,
           update_labels=None,
           remove_labels=None):
    """Update project information."""
    client = projects_util.GetClient()
    messages = projects_util.GetMessages()

    try:
        project = client.projects.Get(
            client.MESSAGES_MODULE.CloudresourcemanagerProjectsGetRequest(
                projectId=project_ref.projectId))
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)

    if name:
        project.name = name

    if parent:
        project.parent = parent

    project.labels = labels_util.UpdateLabels(project.labels,
                                              messages.Project.LabelsValue,
                                              update_labels=update_labels,
                                              remove_labels=remove_labels)
    try:
        return client.projects.Update(project)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #2
0
def Update(project_ref,
           name=None,
           organization=None,
           update_labels=None,
           remove_labels=None):
    """Update project information."""
    client = projects_util.GetClient()
    messages = projects_util.GetMessages()

    try:
        project = client.projects.Get(project_ref.Request())
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)

    if name:
        project.name = name
    if organization:
        if project.parent is not None:
            raise errors.ProjectMoveError(project, organization)
        project.parent = messages.ResourceId(id=organization,
                                             type='organization')
    project.labels = labels_util.UpdateLabels(project.labels,
                                              messages.Project.LabelsValue,
                                              update_labels=update_labels,
                                              remove_labels=remove_labels)
    try:
        return client.projects.Update(project)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #3
0
def Update(project_ref,
           name=None,
           parent=None,
           labels_diff=None):
  """Update project information."""
  client = projects_util.GetClient()
  messages = projects_util.GetMessages()

  try:
    project = client.projects.Get(
        client.MESSAGES_MODULE.CloudresourcemanagerProjectsGetRequest(
            projectId=project_ref.projectId))
  except exceptions.HttpError as error:
    raise projects_util.ConvertHttpError(error)

  if name:
    project.name = name

  if parent:
    project.parent = parent

  if labels_diff:
    labels_update = labels_diff.Apply(messages.Project.LabelsValue,
                                      project.labels)
    if labels_update.needs_update:
      project.labels = labels_update.labels

  try:
    return client.projects.Update(project)
  except exceptions.HttpError as error:
    raise projects_util.ConvertHttpError(error)
Beispiel #4
0
def RemoveIamPolicyBinding(project_ref, member, role):
    try:
        policy = GetIamPolicy(project_ref)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
    iam_util.RemoveBindingFromIamPolicy(policy, member, role)
    try:
        return SetIamPolicy(project_ref, policy)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #5
0
def AddIamPolicyBinding(project_ref, member, role):
    messages = projects_util.GetMessages()

    try:
        policy = GetIamPolicy(project_ref)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
    iam_util.AddBindingToIamPolicy(messages, policy, member, role)
    try:
        return SetIamPolicy(project_ref, policy)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #6
0
def Get(project_ref):
    """Get project information."""
    client = projects_util.GetClient()
    try:
        return client.projects.Get(project_ref.Request())
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #7
0
def SetIamPolicyFromFile(project_ref, policy_file):
    """Read projects IAM policy from a file, and set it."""
    messages = projects_util.GetMessages()
    policy = iam_util.ParsePolicyFile(policy_file, messages.Policy)
    try:
        return SetIamPolicy(project_ref, policy)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #8
0
def Get(project_ref):
    """Get project information."""
    client = projects_util.GetClient()
    try:
        return client.projects.Get(
            client.MESSAGES_MODULE.CloudresourcemanagerProjectsGetRequest(
                projectId=project_ref.projectId))
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #9
0
def SetIamPolicy(project_ref, policy):
    """Set IAM policy, for a given project."""
    client = projects_util.GetClient()
    messages = projects_util.GetMessages()
    policy_request = messages.CloudresourcemanagerProjectsSetIamPolicyRequest(
        resource=project_ref.Name(),
        setIamPolicyRequest=messages.SetIamPolicyRequest(policy=policy))
    try:
        return client.projects.SetIamPolicy(policy_request)
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
Beispiel #10
0
def Undelete(project_ref):
    """Undelete a project that has been deleted."""
    client = projects_util.GetClient()
    messages = projects_util.GetMessages()

    try:
        client.projects.Undelete(
            messages.CloudresourcemanagerProjectsUndeleteRequest(
                projectId=project_ref.Name()))
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
    return projects_util.DeletedResource(project_ref.Name())
Beispiel #11
0
def Delete(project_ref):
    """Delete an existing project."""
    client = projects_util.GetClient()
    messages = projects_util.GetMessages()

    try:
        client.projects.Delete(
            messages.CloudresourcemanagerProjectsDeleteRequest(
                projectId=project_ref.Name()))
    except exceptions.HttpError as error:
        raise projects_util.ConvertHttpError(error)
    return projects_util.DeletedResource(project_ref.Name())
Beispiel #12
0
def SetIamPolicyFromFile(project_ref, policy_file):
  """Read projects IAM policy from a file, and set it."""
  messages = projects_util.GetMessages()
  policy = iam_util.ParsePolicyFile(policy_file, messages.Policy)
  update_mask = iam_util.ConstructUpdateMaskFromPolicy(policy_file)

  # To preserve the existing set-iam-policy behavior of always overwriting
  # bindings and etag, add bindings and etag to update_mask.
  if 'bindings' not in update_mask:
    update_mask += ',bindings'
  if 'etag' not in update_mask:
    update_mask += ',etag'

  try:
    return SetIamPolicy(project_ref, policy, update_mask)
  except exceptions.HttpError as error:
    raise projects_util.ConvertHttpError(error)
Beispiel #13
0
def SetIamPolicy(project_ref, policy, update_mask=None):
  """Set IAM policy, for a given project."""
  client = projects_util.GetClient()
  messages = projects_util.GetMessages()

  set_iam_policy_request = messages.SetIamPolicyRequest(policy=policy)
  # Only include update_mask if provided, otherwise, leave the field unset.
  if update_mask is not None:
    set_iam_policy_request.updateMask = update_mask

  policy_request = messages.CloudresourcemanagerProjectsSetIamPolicyRequest(
      resource=project_ref.Name(),
      setIamPolicyRequest=set_iam_policy_request)
  try:
    return client.projects.SetIamPolicy(policy_request)
  except exceptions.HttpError as error:
    raise projects_util.ConvertHttpError(error)