Beispiel #1
0
  def testNoFlagsOrgPolicySuccess(self):
    list_policy = self.org_policies_messages.ListPolicy(
        allowedValues=['projects/centos-cloud'])
    self.mocked_org_policies_client.projects.GetEffectiveOrgPolicy.Expect(
        request=self.org_policies_messages.
        CloudresourcemanagerProjectsGetEffectiveOrgPolicyRequest(
            projectsId='my-project',
            getEffectiveOrgPolicyRequest=self.
            org_policies_messages.GetEffectiveOrgPolicyRequest(
                constraint=org_policies.FormatConstraint(
                    'compute.trustedImageProjects'))),
        response=self.org_policies_messages.OrgPolicy(listPolicy=list_policy))

    self.Run("""
        compute images list
        """)

    self.list_json.assert_called_once_with(
        requests=self.all_project_requests,
        http=self.mock_http(),
        batch_url=self.batch_url,
        errors=[])
    self.AssertOutputEquals(
        """\
        NAME               PROJECT      FAMILY   DEPRECATED STATUS
        image-1            my-project                       BLOCKED_BY_POLICY
        image-2            my-project                       BLOCKED_BY_POLICY
        image-4            my-project                       BLOCKED_BY_POLICY
        centos-6-v20140408 centos-cloud centos-6            READY
        """,
        normalize_space=True)
    def Run(self, args):
        service = org_policies_base.OrgPoliciesService(args)
        messages = org_policies.OrgPoliciesMessages()

        return service.SetOrgPolicy(
            org_policies_base.SetOrgPolicyRequest(
                args,
                messages.OrgPolicy(
                    constraint=org_policies.FormatConstraint(args.id),
                    booleanPolicy=messages.BooleanPolicy(enforced=True))))
Beispiel #3
0
  def ClearOrgPolicyRequest(args):
    messages = org_policies.OrgPoliciesMessages()
    resource_id = org_policies_base.GetResource(args)
    request = messages.ClearOrgPolicyRequest(
        constraint=org_policies.FormatConstraint(args.id))

    if args.project:
      return messages.CloudresourcemanagerProjectsClearOrgPolicyRequest(
          projectsId=resource_id, clearOrgPolicyRequest=request)
    elif args.organization:
      return messages.CloudresourcemanagerOrganizationsClearOrgPolicyRequest(
          organizationsId=resource_id, clearOrgPolicyRequest=request)
    return None
Beispiel #4
0
def _GetPolicy(project_id):
    """Get effective org policy of given project."""
    messages = org_policies.OrgPoliciesMessages()
    request = messages.CloudresourcemanagerProjectsGetEffectiveOrgPolicyRequest(
        projectsId=project_id,
        getEffectiveOrgPolicyRequest=messages.GetEffectiveOrgPolicyRequest(
            constraint=org_policies.FormatConstraint(
                'compute.trustedImageProjects')))
    client = org_policies.OrgPoliciesClient()
    response = client.projects.GetEffectiveOrgPolicy(request)
    # There are several possible policy types; the only policy type that applies
    # to 'compute.trustedImageProjects' is listPolicy, so we can assume that's
    # what the caller is interested in.
    return response.listPolicy
  def GetEffectiveOrgPolicyRequest(args):
    m = org_policies.OrgPoliciesMessages()
    resource_id = org_policies_base.GetResource(args)
    request = m.GetEffectiveOrgPolicyRequest(
        constraint=org_policies.FormatConstraint(args.id))

    if args.project:
      return m.CloudresourcemanagerProjectsGetEffectiveOrgPolicyRequest(
          projectsId=resource_id, getEffectiveOrgPolicyRequest=request)
    elif args.organization:
      return m.CloudresourcemanagerOrganizationsGetEffectiveOrgPolicyRequest(
          organizationsId=resource_id, getEffectiveOrgPolicyRequest=request)
    elif args.folder:
      return m.CloudresourcemanagerFoldersGetEffectiveOrgPolicyRequest(
          foldersId=resource_id, getEffectiveOrgPolicyRequest=request)
    return None
def GetOrgPolicyRequest(args):
    """Constructs a resource-dependent GetOrgPolicyRequest.

  Args:
    args: Command line arguments.

  Returns:
    Resource-dependent GetOrgPolicyRequest.
  """
    messages = org_policies.OrgPoliciesMessages()
    request = messages.GetOrgPolicyRequest(
        constraint=org_policies.FormatConstraint(args.id))
    resource_id = GetResource(args)
    if args.project:
        return messages.CloudresourcemanagerProjectsGetOrgPolicyRequest(
            projectsId=resource_id, getOrgPolicyRequest=request)
    elif args.organization:
        return messages.CloudresourcemanagerOrganizationsGetOrgPolicyRequest(
            organizationsId=resource_id, getOrgPolicyRequest=request)
    return None
Beispiel #7
0
  def testNoFlagsOrgPolicyFailure(self):

    list_policy = self.org_policies_messages.ListPolicy(
        allowedValues=['THIS RECORD IS MALFORMED'])
    self.mocked_org_policies_client.projects.GetEffectiveOrgPolicy.Expect(
        request=self.org_policies_messages.
        CloudresourcemanagerProjectsGetEffectiveOrgPolicyRequest(
            projectsId='my-project',
            getEffectiveOrgPolicyRequest=self.
            org_policies_messages.GetEffectiveOrgPolicyRequest(
                constraint=org_policies.FormatConstraint(
                    'compute.trustedImageProjects'))),
        response=self.org_policies_messages.OrgPolicy(listPolicy=list_policy))

    self.Run('compute images list --verbosity=info')

    self.list_json.assert_called_once_with(
        requests=self.all_project_requests,
        http=self.mock_http(),
        batch_url=self.batch_url,
        errors=[])
    self.AssertOutputEquals(
        """\
        NAME               PROJECT      FAMILY   DEPRECATED STATUS
        image-1            my-project                       READY
        image-2            my-project                       READY
        image-4            my-project                       READY
        centos-6-v20140408 centos-cloud centos-6            READY
        """,
        normalize_space=True)
    # pylint:disable=line-too-long
    self.AssertErrContains(
        """\
        INFO: could not parse resource [THIS RECORD IS MALFORMED]: It is not in compute.projects collection as it does not match path template projects/(.*)$
        INFO: could not parse resource [THIS RECORD IS MALFORMED]: It is not in compute.projects collection as it does not match path template projects/(.*)$
        INFO: could not parse resource [THIS RECORD IS MALFORMED]: It is not in compute.projects collection as it does not match path template projects/(.*)$
        """,
        normalize_space=True)