示例#1
0
    def testUpdateOpsAgentsPolicyWithUpdatingAgents(self):
        metrics = agent_policy.OpsAgentPolicy.AgentRule.Type.METRICS
        installed = agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED
        expected_ops_agents_policy = agent_policy.OpsAgentPolicy(
            assignment=AGENT_ASSIGNMENT,
            agent_rules=[
                agent_policy.OpsAgentPolicy.AgentRule(agent_type=metrics,
                                                      version='6.*.*',
                                                      package_state=installed,
                                                      enable_autoupgrade=False)
            ],
            description=OPS_AGENT_DESCRIPTION,
            etag=ETAG,
            name=NAME,
            update_time=None,
            create_time=CREATE_TIME)

        actual_ops_agents_policy = agent_policy.UpdateOpsAgentsPolicy(
            ops_agents_policy=OPS_AGENT_POLICY,
            description=OPS_AGENT_DESCRIPTION,
            etag=None,
            agent_rules=[{
                'type': 'metrics',
                'version': '6.*.*',
                'package-state': 'installed',
                'enable-autoupgrade': False,
            }],
            os_types=None,
            group_labels=None,
            zones=None,
            instances=None)
        self.assertEqual(repr(expected_ops_agents_policy),
                         repr(actual_ops_agents_policy))
示例#2
0
    def testUpdateOpsAgentsPolicyWithClearMatcher(self):
        expected_ops_agents_policy = agent_policy.OpsAgentPolicy(
            assignment=agent_policy.OpsAgentPolicy.Assignment(
                group_labels=[],
                zones=['us-central1-c'],
                instances=[],
                os_types=[AGENT_OS_TYPE]),
            agent_rules=OPS_AGENT_POLICY_AGENT_RULES,
            description=OPS_AGENT_DESCRIPTION,
            etag=ETAG,
            name=NAME,
            update_time=None,
            create_time=CREATE_TIME)

        actual_ops_agents_policy = agent_policy.UpdateOpsAgentsPolicy(
            ops_agents_policy=OPS_AGENT_POLICY,
            description=OPS_AGENT_DESCRIPTION,
            etag=None,
            agent_rules=None,
            os_types=None,
            group_labels=[],
            zones=['us-central1-c'],
            instances=[])
        self.assertEqual(repr(expected_ops_agents_policy),
                         repr(actual_ops_agents_policy))
示例#3
0
def ConvertGuestPolicyToOpsAgentPolicy(guest_policy):
    description, agents = _ExtractDescriptionAndAgents(
        guest_policy.description)
    ops_agent_policy = agent_policy.OpsAgentPolicy(
        _CreateAssignment(guest_policy.assignment), _CreateAgents(agents),
        description, guest_policy.etag, guest_policy.name,
        guest_policy.updateTime, guest_policy.createTime)
    return ops_agent_policy
示例#4
0
def ConvertGuestPolicyToOpsAgentPolicy(guest_policy):
  """Converts OS Config guest policy to Ops Agent policy."""
  description, agent_rules = _ExtractDescriptionAndAgentRules(
      guest_policy.description)
  return agent_policy.OpsAgentPolicy(
      assignment=_CreateAssignment(guest_policy.assignment),
      agent_rules=_CreateAgentRules(agent_rules),
      description=description,
      etag=guest_policy.etag,
      name=guest_policy.name,
      update_time=guest_policy.updateTime,
      create_time=guest_policy.createTime)
示例#5
0
def _CreateOpsAgentPolicy(distro, agent_rules):
    return agent_policy.OpsAgentPolicy(
        assignment=agent_policy.OpsAgentPolicy.Assignment(
            group_labels=GROUP_LABELS,
            zones=ZONES,
            instances=_GUEST_POLICY_TEMPLATES[distro].instances,
            os_types=_GUEST_POLICY_TEMPLATES[distro].os_types),
        agent_rules=agent_rules,
        description=OPS_AGENT_RULE_DESCRIPTION,
        etag=ETAG,
        name=None,
        update_time=None,
        create_time=None)
示例#6
0
 def SetUp(self):
   self.messages = osconfig_api_utils.GetClientMessages(
       None, api_version_override='v1beta')
   self.expected_ops_agents_policy = agent_policy.OpsAgentPolicy(
       assignment=AGENT_RULE_ASSIGNMENT,
       agent_rules=OPS_AGENT_POLICY_AGENT_RULES,
       description=OPS_AGENT_DESCRIPTION,
       etag=ETAG,
       name=POLICY_NAME,
       update_time=UPDATE_TIME,
       create_time=CREATE_TIME)
   self.guest_policy = self._LoadMessage(
       GUEST_POLICY_YAML, self.messages.GuestPolicy)
示例#7
0
 def testCreateOpsAgentPolicy(self):
     expected_ops_agents_policy = agent_policy.OpsAgentPolicy(
         assignment=AGENT_ASSIGNMENT,
         agent_rules=OPS_AGENT_POLICY_AGENT_RULES,
         description=OPS_AGENT_DESCRIPTION,
         etag=None,
         name=None,
         update_time=None,
         create_time=None)
     # Make sure this doesn't raise an exception.
     actual_ops_agents_policy = agent_policy.CreateOpsAgentPolicy(
         OPS_AGENT_DESCRIPTION, AGENT_RULES, GROUP_LABELS, OS_TYPES, ZONES,
         INSTANCES)
     self.assertMultiLineEqual(repr(expected_ops_agents_policy),
                               repr(actual_ops_agents_policy))
示例#8
0
  def Run(self, args):
    """See base class."""
    release_track = self.ReleaseTrack()
    client = osconfig_api_utils.GetClientInstance(
        release_track, api_version_override='v1beta')
    messages = osconfig_api_utils.GetClientMessages(
        release_track, api_version_override='v1beta')

    project = properties.VALUES.core.project.GetOrFail()
    request = messages.OsconfigProjectsGuestPoliciesListRequest(
        pageSize=args.page_size,
        parent=osconfig_command_utils.GetProjectUriPath(project),
    )
    service = client.projects_guestPolicies

    for guest_policy in list_pager.YieldFromList(
        service,
        request,
        limit=args.limit,
        predicate=guest_policy_validator.IsOpsAgentPolicy,
        batch_size=osconfig_command_utils.GetListBatchSize(args),
        field='guestPolicies',
        batch_size_attribute='pageSize',
    ):
      try:
        yield converter.ConvertGuestPolicyToOpsAgentPolicy(guest_policy)
      except exceptions.BadArgumentException:
        log.warning(
            'Encountered a malformed policy. The Ops Agents policy [%s] may '
            'have been modified directly by the OS Config guest policy API / '
            'gcloud commands. If so, please delete and re-create with the Ops '
            'Agents policy gcloud commands. If not, this may be an internal '
            'error.',
            guest_policy.name,
        )
        yield agent_policy.OpsAgentPolicy(
            assignment=None,
            agent_rules=None,
            description='<MALFORMED>',
            etag=None,
            name=guest_policy.name,
            update_time=guest_policy.updateTime,
            create_time=guest_policy.createTime
        )
示例#9
0
    def testUpdateOpsAgentsPolicyWithNoneMatcher(self):
        expected_ops_agents_policy = agent_policy.OpsAgentPolicy(
            assignment=AGENT_ASSIGNMENT,
            agent_rules=OPS_AGENT_POLICY_AGENT_RULES,
            description=OPS_AGENT_DESCRIPTION,
            etag=ETAG,
            name=NAME,
            update_time=None,
            create_time=CREATE_TIME)

        actual_ops_agents_policy = agent_policy.UpdateOpsAgentsPolicy(
            ops_agents_policy=OPS_AGENT_POLICY,
            description=None,
            etag=None,
            agent_rules=None,
            os_types=None,
            group_labels=None,
            zones=None,
            instances=None)
        self.assertEqual(repr(expected_ops_agents_policy),
                         repr(actual_ops_agents_policy))
示例#10
0
    def testUpdateOpsAgentsPolicyWithUpdatingEtag(self):
        new_etag = '38dd4394-1550-48fd-8d70-c0e31142fadd'
        self.assertNotEqual(new_etag, ETAG)

        expected_ops_agents_policy = agent_policy.OpsAgentPolicy(
            assignment=AGENT_ASSIGNMENT,
            agent_rules=OPS_AGENT_POLICY_AGENT_RULES,
            description=OPS_AGENT_DESCRIPTION,
            etag=new_etag,
            name=NAME,
            update_time=None,
            create_time=CREATE_TIME)

        actual_ops_agents_policy = agent_policy.UpdateOpsAgentsPolicy(
            ops_agents_policy=OPS_AGENT_POLICY,
            description=None,
            etag=new_etag,
            agent_rules=None,
            os_types=None,
            group_labels=None,
            zones=None,
            instances=None)
        self.assertEqual(repr(expected_ops_agents_policy),
                         repr(actual_ops_agents_policy))
示例#11
0
    agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, True)
LOGGING_AGENT_RULE = agent_policy.OpsAgentPolicy.AgentRule(
    agent_policy.OpsAgentPolicy.AgentRule.Type.LOGGING, '1.6.35',
    agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, True)
OPS_AGENT_POLICY_AGENT_RULES = [LOGGING_AGENT_RULE, METRICS_AGENT_RULE]
AGENT_OS_TYPE = agent_policy.OpsAgentPolicy.Assignment.OsType(
    agent_policy.OpsAgentPolicy.Assignment.OsType.OsShortName.CENTOS, '7')
AGENT_ASSIGNMENT = agent_policy.OpsAgentPolicy.Assignment(
    group_labels=GROUP_LABELS,
    zones=ZONES,
    instances=INSTANCES,
    os_types=[AGENT_OS_TYPE])
OPS_AGENT_POLICY = agent_policy.OpsAgentPolicy(
    assignment=AGENT_ASSIGNMENT,
    agent_rules=OPS_AGENT_POLICY_AGENT_RULES,
    description=OPS_AGENT_DESCRIPTION,
    etag=ETAG,
    name=NAME,
    update_time=UPDATE_TIME,
    create_time=CREATE_TIME)


class OpsAgentPolicyTest(test_case.TestCase):
    def SetUp(self):
        self.maxDiff = None  # pylint: disable=invalid-name

    def testEqual(self):
        self.assertEqual(OPS_AGENT_POLICY, OPS_AGENT_POLICY)

    def testRepr(self):
        self.assertMultiLineEqual(POLICY_JSON, repr(OPS_AGENT_POLICY))
        self.assertMultiLineEqual(POLICY_JSON, six.text_type(OPS_AGENT_POLICY))
示例#12
0
    agent_policy.OpsAgentPolicy.AgentRule.Type.LOGGING, '1.*.*',
    agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, True)
GOOD_METRICS_AGENT_RULE = agent_policy.OpsAgentPolicy.AgentRule(
    agent_policy.OpsAgentPolicy.AgentRule.Type.METRICS, 'latest',
    agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, False)
GOOD_OPS_AGENT_RULE = agent_policy.OpsAgentPolicy.AgentRule(
    agent_policy.OpsAgentPolicy.AgentRule.Type.OPS_AGENT, '1.*.*',
    agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, True)
GOOD_OS_TYPE = agent_policy.OpsAgentPolicy.Assignment.OsType(
    agent_policy.OpsAgentPolicy.Assignment.OsType.OsShortName.CENTOS, '8')
GOOD_ASSIGNMENT = agent_policy.OpsAgentPolicy.Assignment(
    group_labels=[], zones=[], instances=[], os_types=[GOOD_OS_TYPE])
GOOD_POLICY = agent_policy.OpsAgentPolicy(
    assignment=GOOD_ASSIGNMENT,
    agent_rules=[GOOD_LOGGING_AGENT_RULE, GOOD_METRICS_AGENT_RULE],
    description=GOOD_DESCRIPTION,
    etag=None,
    name=None,
    update_time=None,
    create_time=None)

BAD_LOGGING_AGENT_RULE_INVALID_VERSION = agent_policy.OpsAgentPolicy.AgentRule(
    agent_policy.OpsAgentPolicy.AgentRule.Type.LOGGING, '1',
    agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, False)
BAD_METRICS_AGENT_RULE_VERSION_AUTOUPGRADE_CONFLICT = (
    agent_policy.OpsAgentPolicy.AgentRule(
        agent_policy.OpsAgentPolicy.AgentRule.Type.METRICS, '5.3.1',
        agent_policy.OpsAgentPolicy.AgentRule.PackageState.INSTALLED, True))
BAD_OS_TYPE_INVALID_VERSION = agent_policy.OpsAgentPolicy.Assignment.OsType(
    agent_policy.OpsAgentPolicy.Assignment.OsType.OsShortName.CENTOS, '6')