1# -*- coding: utf-8 -*- # 2# Copyright 2019 Google LLC. All Rights Reserved. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16# pylint: disable=line-too-long 17"""Implements command to create an ops agents policy.""" 18 19from __future__ import absolute_import 20from __future__ import division 21from __future__ import unicode_literals 22 23from googlecloudsdk.api_lib.compute.instances.ops_agents import ops_agents_policy as agent_policy 24from googlecloudsdk.api_lib.compute.instances.ops_agents.converters import guest_policy_to_ops_agents_policy_converter as to_ops_agents 25from googlecloudsdk.api_lib.compute.instances.ops_agents.converters import ops_agents_policy_to_guest_policy_converter as to_guest_policy 26from googlecloudsdk.api_lib.compute.instances.ops_agents.validators import ops_agents_policy_validator as validator 27from googlecloudsdk.api_lib.compute.os_config import utils as osconfig_api_utils 28from googlecloudsdk.calliope import base 29from googlecloudsdk.command_lib.compute.instances.ops_agents.policies import parser_utils 30from googlecloudsdk.command_lib.compute.os_config import utils as osconfig_command_utils 31from googlecloudsdk.core import properties 32 33 34@base.ReleaseTracks(base.ReleaseTrack.BETA, base.ReleaseTrack.ALPHA) 35class Create(base.Command): 36 """Create a Google Cloud's operations suite agents (Ops Agents) policy. 37 38 *{command}* creates a policy that facilitates agent management across 39 Compute Engine instances based on user specified instance filters. This policy 40 installs, specifies versioning, enables autoupgrade, and removes Ops Agents. 41 42 The command returns the content of the created policy or an error indicating 43 why the creation fails. The created policy takes effect asynchronously. It 44 can take 10-15 minutes for the VMs to enforce the newly created policy. 45 """ 46 47 detailed_help = { 48 'DESCRIPTION': 49 '{description}', 50 'EXAMPLES': 51 """\ 52 To create a policy named ``ops-agents-test-policy'' that targets a 53 single CentOS 7 VM instance named 54 ``zones/us-central1-a/instances/test-instance'' for testing or 55 development and installs both Logging and Monitoring Agents on that 56 VM instance, run: 57 58 $ {command} ops-agents-test-policy --agent-rules="type=logging;type=metrics" --description="A test policy." --os-types=short-name=centos,version=7 --instances=zones/us-central1-a/instances/test-instance 59 60 To create a policy named ``ops-agents-prod-policy'' that targets all 61 CentOS 7 VMs in zone ``us-central1-a'' with either 62 ``env=prod,product=myapp'' or ``env=staging,product=myapp'' labels 63 and makes sure the logging agent and metrics agent versions are pinned 64 to specific major versions for staging and production, run: 65 66 $ {command} ops-agents-prod-policy --agent-rules="type=logging,version=1.*.*;type=metrics,version=6.*.*" --description="A prod policy." --os-types=short-name=centos,version=7 --zones=us-central1-a --group-labels="env=prod,product=myapp;env=staging,product=myapp" 67 68 To create a policy named ``ops-agents-prod-policy'' that targets all 69 CentOS 7 VMs in zone ``us-central1-a'' with either 70 ``env=prod,product=myapp'' or ``env=staging,product=myapp'' labels 71 and makes sure the ops-agent version is pinned 72 to specific major versions for staging and production, run: 73 74 $ {command} ops-agents-prod-policy --agent-rules="type=ops-agent,version=1.*.*" --description="A prod policy." --os-types=short-name=centos,version=7 --zones=us-central1-a --group-labels="env=prod,product=myapp;env=staging,product=myapp" 75 """, 76 } 77 78 @staticmethod 79 def Args(parser): 80 """See base class.""" 81 parser_utils.AddSharedArgs(parser) 82 parser_utils.AddMutationArgs(parser) 83 parser_utils.AddCreateArgs(parser) 84 85 def Run(self, args): 86 """See base class.""" 87 88 release_track = self.ReleaseTrack() 89 client = osconfig_api_utils.GetClientInstance( 90 release_track, api_version_override='v1beta') 91 messages = osconfig_api_utils.GetClientMessages( 92 release_track, api_version_override='v1beta') 93 ops_agents_policy = agent_policy.CreateOpsAgentPolicy( 94 args.description, args.agent_rules, args.group_labels, args.os_types, 95 args.zones, args.instances) 96 validator.ValidateOpsAgentsPolicy(ops_agents_policy) 97 guest_policy = to_guest_policy.ConvertOpsAgentPolicyToGuestPolicy( 98 messages, ops_agents_policy) 99 project = properties.VALUES.core.project.GetOrFail() 100 parent_path = osconfig_command_utils.GetProjectUriPath(project) 101 request = messages.OsconfigProjectsGuestPoliciesCreateRequest( 102 guestPolicy=guest_policy, 103 guestPolicyId=args.POLICY_ID, 104 parent=parent_path, 105 ) 106 service = client.projects_guestPolicies 107 complete_guest_policy = service.Create(request) 108 ops_agents_policy = to_ops_agents.ConvertGuestPolicyToOpsAgentPolicy( 109 complete_guest_policy) 110 return ops_agents_policy 111