Exemplo n.º 1
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'
Exemplo n.º 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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 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)
Exemplo n.º 6
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')
Exemplo n.º 7
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')