<commit_before>/*
 * Copyright 2014 Netflix, Inc.
 *
 * 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.
 */

package com.netflix.spinnaker.oort.controllers.aws

import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.cats.cache.CacheData
import com.netflix.spinnaker.oort.data.aws.Keys
import groovy.transform.InheritConstructors
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController

import static com.netflix.spinnaker.oort.data.aws.Keys.Namespace.IMAGES
import static com.netflix.spinnaker.oort.data.aws.Keys.Namespace.NAMED_IMAGES

@RestController
@RequestMapping("/aws/images")
class AmazonNamedImageLookupController {
  private static final int MIN_NAME_FILTER = 2;
  private static final String EXCEPTION_REASON = 'Minimum of ' + MIN_NAME_FILTER + ' characters required to filter namedImages'

  private final Cache cacheView

  @Autowired
  AmazonNamedImageLookupController(Cache cacheView) {
    this.cacheView = cacheView
  }

  @RequestMapping(value = '/{account}/{region}/{imageId}', method = RequestMethod.GET)
  List<NamedImage> getByAmiId(@PathVariable String account, @PathVariable String region, @PathVariable String imageId) {
    CacheData cd = cacheView.get(IMAGES.ns, Keys.getImageKey(imageId, account, region))
    if (cd == null) {
      throw new ImageNotFoundException("${imageId} not found in ${account}/${region}")
    }
    Collection<String> namedImageKeys = cd.relationships[NAMED_IMAGES.ns]

    if (!namedImageKeys) {
      throw new ImageNotFoundException("Name not found on image ${imageId} in ${account}/${region}")
    }

    Collection<CacheData> namedImages = cacheView.getAll(NAMED_IMAGES.ns, namedImageKeys)
    render(namedImages, null, region)
  }

  @RequestMapping(value = '/find', method = RequestMethod.GET)
  List<NamedImage> list(LookupOptions lookupOptions) {
    if (lookupOptions.imageName == null || lookupOptions.imageName.length() < MIN_NAME_FILTER) {
      throw new InsufficientLookupOptionsException(EXCEPTION_REASON)
    }

    Collection<String> identifiers = cacheView.getIdentifiers(NAMED_IMAGES.ns)
    Collection<String> filtered = identifiers.findAll {
      def parts = Keys.parse(it)
      if (lookupOptions.imageName) {
        if (parts.imageName.indexOf(lookupOptions.imageName) == -1) {
          return false
        }
      }

      if (lookupOptions.account) {
        if (parts.account != lookupOptions.account) {
          return false
        }
      }

      true
    }

    Collection<CacheData> namedImages = cacheView.getAll(NAMED_IMAGES.ns, filtered)
    render(namedImages, lookupOptions.imageName)<commit_msg>filter on region for image lookup<commit_after>/*
 * Copyright 2014 Netflix, Inc.
 *
 * 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.
 */

package com.netflix.spinnaker.oort.controllers.aws

import com.netflix.spinnaker.cats.cache.Cache
import com.netflix.spinnaker.cats.cache.CacheData
import com.netflix.spinnaker.oort.data.aws.Keys
import groovy.transform.InheritConstructors
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController

import static com.netflix.spinnaker.oort.data.aws.Keys.Namespace.IMAGES
import static com.netflix.spinnaker.oort.data.aws.Keys.Namespace.NAMED_IMAGES

@RestController
@RequestMapping("/aws/images")
class AmazonNamedImageLookupController {
  private static final int MIN_NAME_FILTER = 2;
  private static final String EXCEPTION_REASON = 'Minimum of ' + MIN_NAME_FILTER + ' characters required to filter namedImages'

  private final Cache cacheView

  @Autowired
  AmazonNamedImageLookupController(Cache cacheView) {
    this.cacheView = cacheView
  }

  @RequestMapping(value = '/{account}/{region}/{imageId}', method = RequestMethod.GET)
  List<NamedImage> getByAmiId(@PathVariable String account, @PathVariable String region, @PathVariable String imageId) {
    CacheData cd = cacheView.get(IMAGES.ns, Keys.getImageKey(imageId, account, region))
    if (cd == null) {
      throw new ImageNotFoundException("${imageId} not found in ${account}/${region}")
    }
    Collection<String> namedImageKeys = cd.relationships[NAMED_IMAGES.ns]

    if (!namedImageKeys) {
      throw new ImageNotFoundException("Name not found on image ${imageId} in ${account}/${region}")
    }

    Collection<CacheData> namedImages = cacheView.getAll(NAMED_IMAGES.ns, namedImageKeys)
    render(namedImages, null, region)
  }

  @RequestMapping(value = '/find', method = RequestMethod.GET)
  List<NamedImage> list(LookupOptions lookupOptions) {
    if (lookupOptions.imageName == null || lookupOptions.imageName.length() < MIN_NAME_FILTER) {
      throw new InsufficientLookupOptionsException(EXCEPTION_REASON)
    }

    Collection<String> identifiers = cacheView.getIdentifiers(NAMED_IMAGES.ns)
    Collection<String> filtered = identifiers.findAll {
      def parts = Keys.parse(it)
      if (lookupOptions.imageName) {
        if (parts.imageName.indexOf(lookupOptions.imageName) == -1) {
          return false
        }
      }

      if (lookupOptions.account) {
        if (parts.account != lookupOptions.account) {
          return false
        }
      }

      true
    }

    Collection<CacheData> namedImages = cacheView.getAll(NAMED_IMAGES.ns, filtered)

    if (lookupOptions.region) {
      namedImages = namedImages.findAll { namedImage ->
        return namedImage.relationships[IMAGES.ns].any { imageKey ->
          Keys.parse(imageKey).region == lookupOptions.region
        }
      }
    }

    render(namedImages, lookupOptions.imageName)
{'fraction_non_alphanumeric': 0.09557497572478846, 'fraction_numerical': 0.0027743098904147593, 'mean_word_length': 4.2322206095791, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5665661', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1999, 'n_words': 498}