Skip to content

Commit

Permalink
Factor out common credentials-handling logic into helper class. (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Duftler committed May 16, 2017
1 parent 6b5d465 commit a23af0b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.netflix.kayenta.storage.StorageServiceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -49,19 +48,9 @@ public class CanaryConfigController {
@RequestMapping(method = RequestMethod.GET)
public CanaryConfig loadCanaryConfig(@RequestParam(required = false) final String accountName,
@RequestParam String canaryConfigUUID) {
AccountCredentials credentials;

if (StringUtils.hasLength(accountName)) {
credentials = accountCredentialsRepository
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
} else {
credentials = accountCredentialsRepository
.getOne(AccountCredentials.Type.OBJECT_STORE)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account of type " + AccountCredentials.Type.OBJECT_STORE + "."));
}

String resolvedAccountName = credentials.getName();
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);

if (storageService.isPresent()) {
Expand All @@ -75,19 +64,9 @@ public CanaryConfig loadCanaryConfig(@RequestParam(required = false) final Strin
@RequestMapping(consumes = "application/context+json", method = RequestMethod.POST)
public String storeCanaryConfig(@RequestParam(required = false) final String accountName,
@RequestBody CanaryConfig canaryConfig) throws IOException {
AccountCredentials credentials;

if (StringUtils.hasLength(accountName)) {
credentials = accountCredentialsRepository
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
} else {
credentials = accountCredentialsRepository
.getOne(AccountCredentials.Type.OBJECT_STORE)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account of type " + AccountCredentials.Type.OBJECT_STORE + "."));
}

String resolvedAccountName = credentials.getName();
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.OBJECT_STORE,
accountCredentialsRepository);
Optional<StorageService> storageService = storageServiceRepository.getOne(resolvedAccountName);
String canaryConfigId = UUID.randomUUID() + "";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017 Google, 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.kayenta.controllers;

import com.netflix.kayenta.security.AccountCredentials;
import com.netflix.kayenta.security.AccountCredentialsRepository;
import org.springframework.util.StringUtils;

public class CredentialsHelper {

static String resolveAccountByNameOrType(String accountName, AccountCredentials.Type accountType, AccountCredentialsRepository accountCredentialsRepository) {
AccountCredentials credentials;

if (StringUtils.hasLength(accountName)) {
credentials = accountCredentialsRepository
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
} else {
credentials = accountCredentialsRepository
.getOne(accountType)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account of type " + accountType + "."));
}

return credentials.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -59,19 +58,9 @@ public List<MetricSet> queryMetrics(@RequestParam(required = false) final String
@ApiParam(defaultValue = "myapp-v010-") @RequestParam String instanceNamePrefix,
@ApiParam(defaultValue = "2017-03-24T15:13:00Z") @RequestParam String intervalStartTime,
@ApiParam(defaultValue = "2017-03-24T15:27:00Z") @RequestParam String intervalEndTime) throws IOException {
AccountCredentials credentials;

if (StringUtils.hasLength(accountName)) {
credentials = accountCredentialsRepository
.getOne(accountName)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + "."));
} else {
credentials = accountCredentialsRepository
.getOne(AccountCredentials.Type.METRICS_STORE)
.orElseThrow(() -> new IllegalArgumentException("Unable to resolve account of type " + AccountCredentials.Type.METRICS_STORE + "."));
}

String resolvedAccountName = credentials.getName();
String resolvedAccountName = CredentialsHelper.resolveAccountByNameOrType(accountName,
AccountCredentials.Type.METRICS_STORE,
accountCredentialsRepository);
Optional<MetricsService> metricsService = metricsServiceRepository.getOne(resolvedAccountName);
List<MetricSet> metricSetList;

Expand Down

0 comments on commit a23af0b

Please sign in to comment.