Exemple #1
0
    def do_command_delete_list(self, api):
        """Delete all the specified instances of each of the api resources."""
        resource_type = api
        results = {}
        collected = self.do_command_collect_api(api)
        for resource_type, data in collected.items():
            elems = set(data.response)
            discovery_doc = GcpAgent.download_discovery_document(api=api)
            results[resource_type] = self.actuator.delete_all_collected(
                resource_type, discovery_doc, elems, bindings=None)

        self.actuator.wait_for_delete_and_maybe_retry(results)
Exemple #2
0
  def do_command_delete_list(self, api):
    """Delete all the specified instances of each of the api resources."""
    resource_type = api
    results = {}
    collected = self.do_command_collect_api(api)
    for resource_type, data in collected.items():
      elems = set(data.response)
      discovery_doc = GcpAgent.download_discovery_document(api=api)
      results[resource_type] = self.actuator.delete_all_collected(
          resource_type, discovery_doc, elems, bindings=None)

    self.actuator.wait_for_delete_and_maybe_retry(results)
Exemple #3
0
  def delete_added(self, api, version, before, after, resource_filter):
    """Delete resources that were added since the baseline.

    Args:
      api: [string] The API name containing the resources.
      version: [string] The API version.
      before: [dict] {resource: [ResourceList]} baseline.
      after: [dict] {resource: [ResourceList]} changed.
      resource_filter: [ResourceFilter] Determines resource names to consider.
    """
    if resource_filter is None:
      return

    discovery_doc = GcpAgent.download_discovery_document(
        api=api, version=version)

    common_resources = set([
        resource
        for resource in set(before.keys()).intersection(set(after.keys()))
        if resource_filter.wanted(resource)])
    for resource in common_resources:
      if before[resource].params != after[resource].params:
        print 'WARNING: ignoring "{0}" because parameters do not match.'.format(
            resource)
        continue

      before_values = set(before[resource].response)
      after_values = set(after[resource].response)
      added = after_values.difference(before_values)
      if not added:
        continue

      delete = (discovery_doc
                .get('resources', {})
                .get(resource, {})
                .get('methods', {})
                .get('delete', None))
      if not delete:
        print '*** Cannot find delete method for "{0}"'.format(resource)
        continue

      scope = self.__explorer.pick_scope(
          delete.get('scopes', []), api, mutable=True)
      agent = self.make_agent(api, version, scope,
                              default_variables=after[resource].params)
      print '{action} from API={api} with scope={scope}'.format(
          action=('Deleting' if self.__options.delete_for_real
                  else 'Simulating Delete'),
          api=api,
          scope=scope if self.__options.credentials_path else '<default>')

      self.__delete_all(agent, resource, added, after[resource].aggregated)
      print '-' * 40 + '\n'
Exemple #4
0
  def collect_apis():
    """Collect names and current versions of all the Google APIs."""
    context = ExecutionContext()
    agent = GcpAgent.make_agent(api='discovery', version='v1')
    apis = agent.list_resource(context, 'apis')

    catalog = {}
    for entry in apis:
      preferred = entry.get('preferred', True)
      if not preferred:
        continue
      catalog[entry['name']] = entry['version']
    return catalog
Exemple #5
0
    def find_listable_resources(self, api, version, resource_filter):
        """Find all the resources within an API that we can list elements of.

    Args:
      api: [string] The API name containing the resources.
      version: [string] The API version.
      resource_filter: [ApiResourceFilter] Determines resource names to match.

    Returns:
      Map of resource name to the method specification for listing it.
    """
        doc = GcpAgent.download_discovery_document(api, version)
        resources = doc['resources']
        return self.__find_listable_resources_helper(None, resources,
                                                     resource_filter)
Exemple #6
0
  def find_listable_resources(self, api, version, resource_filter):
    """Find all the resources within an API that we can list elements of.

    Args:
      api: [string] The API name containing the resources.
      version: [string] The API version.
      resource_filter: [ApiResourceFilter] Determines resource names to match.

    Returns:
      Map of resource name to the method specification for listing it.
    """
    doc = GcpAgent.download_discovery_document(api, version)
    resources = doc['resources']
    return self.__find_listable_resources_helper(
        None, resources, resource_filter)
Exemple #7
0
  def make_agent(self, api, version, default_scope, default_variables=None):
    """Construct an agent to talk to a Google API.

    Args:
      api: [string] The API name containing the resources.
      version: [string] The API version.
      default_scope: [string] The oauth scope to use if options.credentials_path
    """
    credentials = self.__options.credentials_path or None
    default_variables = default_variables or self.__default_variables
    scope_list = [default_scope] if credentials else None
    return GcpAgent.make_agent(
        api=api, version=version,
        scopes=scope_list,
        credentials_path=credentials,
        default_variables=default_variables)
Exemple #8
0
def _gcp_agent_singleton(**kwargs):
    """Manage singleton of agents to particular apis.

    This treates the agent instances as singletons based on their configuration.
    The reason for this is simply to reduce logging noise on the creation.
    This is possible since agents are stateless.

  Args:
    kargs: [kwargs]  Arguments to GcpAgent.make_agent
  """
    # pylint: disable=global-statement
    global __AGENT_CACHE
    key = str(kwargs)
    if key not in __AGENT_CACHE:
        __AGENT_CACHE[key] = GcpAgent.make_agent(**kwargs)

    return __AGENT_CACHE[key]
def _gcp_agent_singleton(**kwargs):
  """Manage singleton of agents to particular apis.

    This treates the agent instances as singletons based on their configuration.
    The reason for this is simply to reduce logging noise on the creation.
    This is possible since agents are stateless.

  Args:
    kargs: [kwargs]  Arguments to GcpAgent.make_agent
  """
  # pylint: disable=global-statement
  global __AGENT_CACHE
  key = str(kwargs)
  if key not in __AGENT_CACHE:
    __AGENT_CACHE[key] = GcpAgent.make_agent(**kwargs)

  return __AGENT_CACHE[key]
Exemple #10
0
    def collect_apis():
        """Collect names and current versions of all the Google APIs."""
        if ApiInvestigator.__api_version_map:
            return ApiInvestigator.__api_version_map

        context = ExecutionContext()
        agent = GcpAgent.make_agent(api='discovery', version='v1')
        apis = agent.list_resource(context, 'apis')

        catalog = {}
        for entry in apis:
            preferred = entry.get('preferred', True)
            if not preferred:
                continue
            catalog[entry['name']] = entry['version']

        ApiInvestigator.__api_version_map = catalog
        return catalog
Exemple #11
0
    def delete_added(self, api, before, after, resource_filter):
        """Delete resources that were added since the baseline.

    Args:
      api: [string] The API name containing the resources.
      before: [dict] {resource: [ResourceList]} baseline.
      after: [dict] {resource: [ResourceList]} changed.
      resource_filter: [ResourceFilter] Determines resource names to consider.
    """
        if resource_filter is None:
            return

        discovery_doc = GcpAgent.download_discovery_document(api=api)

        common_resource_types = set([
            resource_type for resource_type in set(before.keys()).intersection(
                set(after.keys())) if resource_filter.wanted(resource_type)
        ])
        added_resource_types = set([
            resource_type
            for resource_type in set(after.keys()).difference(before.keys())
            if resource_filter.wanted(resource_type)
        ])
        resource_types_to_consider = common_resource_types.union(
            added_resource_types)

        all_results = {}
        for resource_type in resource_types_to_consider:
            added = self.__determine_added_instances(resource_type, before,
                                                     after)
            if not added:
                continue

            type_results = self.delete_all_collected(
                resource_type, discovery_doc, added,
                after[resource_type].params)
            if type_results:
                all_results[resource_type] = type_results

        self.wait_for_delete_and_maybe_retry(all_results)
        print('-' * 40 + '\n')
Exemple #12
0
  def delete_added(self, api, before, after, resource_filter):
    """Delete resources that were added since the baseline.

    Args:
      api: [string] The API name containing the resources.
      before: [dict] {resource: [ResourceList]} baseline.
      after: [dict] {resource: [ResourceList]} changed.
      resource_filter: [ResourceFilter] Determines resource names to consider.
    """
    if resource_filter is None:
      return

    discovery_doc = GcpAgent.download_discovery_document(api=api)

    common_resource_types = set([
        resource_type
        for resource_type in set(before.keys()).intersection(set(after.keys()))
        if resource_filter.wanted(resource_type)])
    added_resource_types = set([
        resource_type
        for resource_type in set(after.keys()).difference(before.keys())
        if resource_filter.wanted(resource_type)])
    resource_types_to_consider = common_resource_types.union(
        added_resource_types)

    all_results = {}
    for resource_type in resource_types_to_consider:
      added = self.__determine_added_instances(resource_type, before, after)
      if not added:
        continue

      type_results = self.delete_all_collected(
          resource_type, discovery_doc, added, after[resource_type].params)
      if type_results:
        all_results[resource_type] = type_results

    self.wait_for_delete_and_maybe_retry(all_results)
    print('-' * 40 + '\n')