コード例 #1
0
ファイル: import.py プロジェクト: bopopescu/nodeserver
    def Run(self, args):
        if not os.path.exists(args.file_name):
            raise exceptions.BadFileException('No such file [{0}]'.format(
                args.file_name))
        if os.path.isdir(args.file_name):
            raise exceptions.BadFileException('[{0}] is a directory'.format(
                args.file_name))

        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        ref = self.SECURITY_POLICY_ARG.ResolveAsResource(
            args, holder.resources)

        # Get the imported security policy config.
        try:
            with open(args.file_name) as import_file:
                if args.file_format == 'yaml':
                    imported = security_policies_utils.SecurityPolicyFromFile(
                        import_file, holder.client.messages, 'yaml')
                else:
                    imported = security_policies_utils.SecurityPolicyFromFile(
                        import_file, holder.client.messages, 'json')
        except Exception as exp:
            msg = (
                u'Unable to read security policy config from specified file [{0}] '
                u'because [{1}]'.format(args.file_name, exp.message))
            raise exceptions.BadFileException(msg)

        # Send the change to the service.
        security_policy = client.SecurityPolicy(ref,
                                                compute_client=holder.client)
        security_policy.Patch(security_policy=imported)

        msg = u'Updated [{0}] with config from [{1}].'.format(
            ref.Name(), args.file_name)
        log.status.Print(msg)
コード例 #2
0
 def _GetTemplateFromFile(self, args, messages):
     if not os.path.exists(args.file_name):
         raise exceptions.BadFileException('No such file [{0}]'.format(
             args.file_name))
     if os.path.isdir(args.file_name):
         raise exceptions.BadFileException('[{0}] is a directory'.format(
             args.file_name))
     try:
         with files.FileReader(args.file_name) as import_file:
             if args.file_format == 'json':
                 return security_policies_utils.SecurityPolicyFromFile(
                     import_file, messages, 'json')
             return security_policies_utils.SecurityPolicyFromFile(
                 import_file, messages, 'yaml')
     except Exception as exp:
         exp_msg = getattr(exp, 'message', six.text_type(exp))
         msg = ('Unable to read security policy config from specified file '
                '[{0}] because [{1}]'.format(args.file_name, exp_msg))
         raise exceptions.BadFileException(msg)
コード例 #3
0
  def testSecurityPolicyFromYamlFile(self):
    yaml_file = open(_YAML_FILE_PATH)
    security_policy = security_policies_utils.SecurityPolicyFromFile(
        yaml_file, self.messages, 'yaml')

    # Add fields that are not required for an actual import, but are required
    # for the sake of this test.
    test_policy = test_resources.MakeSecurityPolicyCloudArmorConfig(
        self.messages, self.my_policy)
    security_policy.id = test_policy.id
    security_policy.name = test_policy.name
    security_policy.selfLink = test_policy.selfLink

    self.assertEqual(test_policy, security_policy)
    yaml_file.close()
コード例 #4
0
    def testSecurityPolicyFromFile(self, file_path, file_format):
        template = open(file_path)
        actual = security_policies_utils.SecurityPolicyFromFile(
            template, self.messages, file_format)

        expected = test_resources.MakeSecurityPolicy(
            self.messages, self.CreateSecurityPolicy())

        # Add fields that are not required for an actual import, but are required
        # for the sake of this test.
        actual.name = expected.name
        actual.id = expected.id
        actual.selfLink = expected.selfLink

        self.assertEqual(expected, actual)
        template.close()