# Copyright 2017-2018 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from c7n.utils import type_schema
from c7n_gcp.actions import MethodAction
from c7n_gcp.provider import resources
from c7n_gcp.query import QueryResourceManager, TypeInfo
from c7n.filters.offhours import OffHour, OnHour
[docs]@resources.register('instance')
class Instance(QueryResourceManager):
[docs] class resource_type(TypeInfo):
service = 'compute'
version = 'v1'
component = 'instances'
enum_spec = ('aggregatedList', 'items.*.instances[]', None)
scope = 'project'
id = 'name'
[docs] @staticmethod
def get(client, resource_info):
# The api docs for compute instance get are wrong,
# they spell instance as resourceId
return client.execute_command(
'get', {'project': resource_info['project_id'],
'zone': resource_info['zone'],
'instance': resource_info[
'resourceName'].rsplit('/', 1)[-1]})
[docs]@Instance.filter_registry.register('offhour')
class InstanceOffHour(OffHour):
[docs] def get_tag_value(self, instance):
return instance.get('labels', {}).get(self.tag_key)
[docs]@Instance.filter_registry.register('onhour')
class InstanceOnHour(OnHour):
[docs] def get_tag_value(self, instance):
return instance.get('labels', {}).get(self.tag_key)
[docs]class InstanceAction(MethodAction):
[docs] def get_resource_params(self, model, resource):
project, zone, instance = self.path_param_re.match(
resource['selfLink']).groups()
return {'project': project, 'zone': zone, 'instance': instance}
[docs]@Instance.action_registry.register('start')
class Start(InstanceAction):
schema = type_schema('start')
method_spec = {'op': 'start'}
path_param_re = re.compile(
'.*?/projects/(.*?)/zones/(.*?)/instances/(.*)')
attr_filter = ('status', ('TERMINATED',))
[docs]@Instance.action_registry.register('stop')
class Stop(InstanceAction):
schema = type_schema('stop')
method_spec = {'op': 'stop'}
path_param_re = re.compile(
'.*?/projects/(.*?)/zones/(.*?)/instances/(.*)')
attr_filter = ('status', ('RUNNING',))
[docs]@Instance.action_registry.register('delete')
class Delete(InstanceAction):
schema = type_schema('delete')
method_spec = {'op': 'delete'}
path_param_re = re.compile(
'.*?/projects/(.*?)/zones/(.*?)/instances/(.*)')
[docs]@resources.register('image')
class Image(QueryResourceManager):
[docs] class resource_type(TypeInfo):
service = 'compute'
version = 'v1'
component = 'images'
id = 'name'
[docs] @staticmethod
def get(client, resource_info):
return client.execute_command(
'get', {'project': resource_info['project_id'],
'resourceId': resource_info['image_id']})
[docs]@resources.register('disk')
class Disk(QueryResourceManager):
[docs] class resource_type(TypeInfo):
service = 'compute'
version = 'v1'
component = 'disks'
scope = 'zone'
enum_spec = ('aggregatedList', 'items.*.disks[]', None)
id = 'name'
[docs] @staticmethod
def get(client, resource_info):
return client.execute_command(
'get', {'project': resource_info['project_id'],
'zone': resource_info['zone'],
'resourceId': resource_info['disk_id']})
[docs]@Disk.action_registry.register('snapshot')
class DiskSnapshot(MethodAction):
schema = type_schema('snapshot')
method_spec = {'op': 'createSnapshot'}
path_param_re = re.compile(
'.*?/projects/(.*?)/zones/(.*?)/instances/(.*)')
attr_filter = ('status', ('RUNNING',))
[docs]@resources.register('snapshot')
class Snapshot(QueryResourceManager):
[docs] class resource_type(TypeInfo):
service = 'compute'
version = 'v1'
component = 'snapshots'
enum_spec = ('list', 'items[]', None)
id = 'name'
[docs] @staticmethod
def get(client, resource_info):
return client.execute_command(
'get', {'project': resource_info['project_id'],
'snapshot_id': resource_info['snapshot_id']})
[docs]@Snapshot.action_registry.register('delete')
class DeleteSnapshot(MethodAction):
schema = type_schema('delete')
method_spec = {'op': 'delete'}
attr_filter = ('status', ('READY', 'UPLOADING'))
path_param_re = re.compile('.*?/projects/(.*?)/global/snapshots/(.*)')
[docs] def get_resource_params(self, m, r):
project, snapshot_id = self.path_param_re.match(r['selfLink']).groups()
# Docs are wrong :-(
# https://cloud.google.com/compute/docs/reference/rest/v1/snapshots/delete
return {'project': project, 'snapshot': snapshot_id}