Example #1
0
  def _ImportFromFileHelper(self, file_path, file_format, has_optional=True):
    self.Run('compute security-policies import my-policy --file-name "{0}" '
             '--file-format {1}'.format(file_path, file_format))

    # Remove fields that are not included in a Patch request.
    resource = test_resources.MakeSecurityPolicy(self.messages, self.my_policy)
    if has_optional:
      resource = test_resources.MakeSecurityPolicyCloudArmorConfig(
          self.messages, self.my_policy)
    resource.id = None
    resource.name = None
    resource.selfLink = None

    # Remove optional fields if has_optional is False.
    if not has_optional:
      resource.description = None
      for rule in resource.rules:
        rule.description = None

    self.CheckRequests([(self.compute.securityPolicies, 'Patch',
                         self.messages.ComputeSecurityPoliciesPatchRequest(
                             project='my-project',
                             securityPolicy='my-policy',
                             securityPolicyResource=resource))],)
    self.AssertErrContains(
        textwrap.dedent("""\
        Updated [my-policy] with config from [{0}].
        """.format(file_path)))
Example #2
0
    def testWriteToYamlFile(self):
        with open(self.result_file_path, 'w') as yaml_file:
            security_policies_utils.WriteToFile(
                yaml_file,
                test_resources.MakeSecurityPolicyCloudArmorConfig(
                    self.messages, self.my_policy), 'yaml')

        with open(self.result_file_path) as results:
            with open(_YAML_FILE_PATH) as expected:
                self.assertEqual(expected.readlines(), results.readlines())
Example #3
0
    def _ExportToFileHelper(self, expected_file, file_format='json'):
        self.make_requests.side_effect = iter([
            [
                test_resources.MakeSecurityPolicyCloudArmorConfig(
                    self.messages, self.my_policy)
            ],
        ])
        self.Run('compute security-policies export my-policy'
                 ' --file-name "{0}" --file-format {1}'.format(
                     self.result_file_path, file_format))

        with open(self.result_file_path) as result:
            with open(expected_file) as expected:
                self.assertEqual(expected.readlines(), result.readlines())
Example #4
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()