Esempio n. 1
0
    def testGetMessageFromFile_InvalidFile_ThrowsError(self, file_contents):
        filepath = self.Touch(self.temp_path, contents=file_contents)

        with self.AssertRaisesExceptionMatches(
                exceptions.InvalidInputError,
                'Unable to parse file [{}]'.format(filepath)):
            utils.GetMessageFromFile(
                filepath,
                self.org_policy_messages.GoogleCloudOrgpolicyV2alpha1Policy)
Esempio n. 2
0
    def Run(self, args):
        """Creates or updates a policy from a JSON or YAML file.

    This first converts the contents of the specified file into a policy object.
    It then fetches the current policy using GetPolicy. If it does not exist,
    the policy is created using CreatePolicy. If it does, the retrieved policy
    is checked to see if it needs to be updated. If so, the policy is updated
    using UpdatePolicy.

    Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the Args method.

    Returns:
      The created or updated policy.
    """
        policy_service = org_policy_service.PolicyService()
        org_policy_messages = org_policy_service.OrgPolicyMessages()

        input_policy = utils.GetMessageFromFile(
            args.policy_file,
            org_policy_messages.GoogleCloudOrgpolicyV2alpha1Policy)

        if not input_policy.name:
            raise exceptions.InvalidInputError(
                'Name field not present in the organization policy.')

        get_request = org_policy_messages.OrgpolicyPoliciesGetRequest(
            name=input_policy.name)
        try:
            policy = policy_service.Get(get_request)
        except api_exceptions.HttpNotFoundError:
            constraint = org_policy_utils.GetConstraintFromPolicyName(
                input_policy.name)
            parent = org_policy_utils.GetResourceFromPolicyName(
                input_policy.name)

            create_request = org_policy_messages.OrgpolicyPoliciesCreateRequest(
                constraint=constraint,
                parent=parent,
                googleCloudOrgpolicyV2alpha1Policy=input_policy)
            create_response = policy_service.Create(create_request)
            log.CreatedResource(input_policy.name, 'policy')
            return create_response

        if policy == input_policy:
            return policy

        update_request = org_policy_messages.OrgpolicyPoliciesPatchRequest(
            name=input_policy.name,
            forceUnconditionalWrite=False,
            googleCloudOrgpolicyV2alpha1Policy=input_policy)
        update_response = policy_service.Patch(update_request)
        log.UpdatedResource(input_policy.name, 'policy')
        return update_response
Esempio n. 3
0
    def testGetMessageFromFile_JsonFile_ReturnsMessage(self):
        policy = self.Policy(
            rule_data=[{
                'condition': self.CONDITION_EXPRESSION_A
            }])
        json_str = encoding.MessageToJson(policy)
        filename = self.Touch(self.temp_path, contents=json_str)

        message = utils.GetMessageFromFile(
            filename,
            self.org_policy_messages.GoogleCloudOrgpolicyV2alpha1Policy)

        self.assertEqual(policy, message)
Esempio n. 4
0
    def Run(self, args):
        """Creates or updates a policy from a JSON or YAML file.

    This first converts the contents of the specified file into a policy object.
    It then fetches the current policy using GetPolicy. If it does not exist,
    the policy is created using CreatePolicy. If it does, the retrieved policy
    is checked to see if it needs to be updated. If so, the policy is updated
    using UpdatePolicy.

    Args:
      args: argparse.Namespace, An object that contains the values for the
        arguments specified in the Args method.

    Returns:
      The created or updated policy.
    """
        org_policy_api = org_policy_service.OrgPolicyApi(self.ReleaseTrack())
        input_policy = utils.GetMessageFromFile(args.policy_file,
                                                self.ReleaseTrack())
        if not input_policy.name:
            raise exceptions.InvalidInputError(
                'Name field not present in the organization policy.')

        try:
            policy = org_policy_api.GetPolicy(input_policy.name)
        except api_exceptions.HttpNotFoundError:
            create_response = org_policy_api.CreatePolicy(input_policy)
            log.CreatedResource(input_policy.name, 'policy')
            return create_response

        if policy == input_policy:
            return policy

        update_response = org_policy_api.UpdatePolicy(input_policy)
        log.UpdatedResource(input_policy.name, 'policy')
        return update_response