Skip to content

Commit

Permalink
feat(provider/oraclebmcs): Resize server group operation (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonlord authored and Matt Duftler committed Apr 24, 2017
1 parent f830f63 commit 3609ef0
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.netflix.spinnaker.clouddriver.oraclebmcs.security.OracleBMCSNamedAccountCredentials
import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport

/**
* Created by slord on 11/01/2017.
*/
class OracleBMCSAtomicOperationConverterHelper {

static <T> T convertDescription(Map input,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.converter

import com.netflix.spinnaker.clouddriver.oraclebmcs.OracleBMCSOperation
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description.ResizeOracleBMCSServerGroupDescription
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.op.ResizeOracleBMCSServerGroupAtomicOperation
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations
import com.netflix.spinnaker.clouddriver.security.AbstractAtomicOperationsCredentialsSupport
import org.springframework.stereotype.Component

@OracleBMCSOperation(AtomicOperations.RESIZE_SERVER_GROUP)
@Component("resizeOracleBMCSServerGroupDescription")
class ResizeOracleBMCSServerGroupAtomicOperationConverter extends AbstractAtomicOperationsCredentialsSupport {

@Override
AtomicOperation convertOperation(Map input) {
new ResizeOracleBMCSServerGroupAtomicOperation(convertDescription(input))
}

@Override
ResizeOracleBMCSServerGroupDescription convertDescription(Map input) {
OracleBMCSAtomicOperationConverterHelper.convertDescription(input, this, ResizeOracleBMCSServerGroupDescription)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description

import com.netflix.spinnaker.clouddriver.model.ServerGroup
import groovy.transform.ToString

@ToString(includeNames = true)
class ResizeOracleBMCSServerGroupDescription extends AbstractOracleBMCSCredentialsDescription {

String accountName
String region
String serverGroupName
ServerGroup.Capacity capacity = new ServerGroup.Capacity()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.op

import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description.ResizeOracleBMCSServerGroupDescription
import com.netflix.spinnaker.clouddriver.oraclebmcs.service.servergroup.OracleBMCSServerGroupService
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperation
import org.springframework.beans.factory.annotation.Autowired

class ResizeOracleBMCSServerGroupAtomicOperation implements AtomicOperation<Void> {

private final ResizeOracleBMCSServerGroupDescription description

private static final String BASE_PHASE = "RESIZE_SERVER_GROUP"

private static Task getTask() {
TaskRepository.threadLocalTask.get()
}

@Autowired
OracleBMCSServerGroupService oracleBMCSServerGroupService

ResizeOracleBMCSServerGroupAtomicOperation(ResizeOracleBMCSServerGroupDescription description) {
this.description = description
}

@Override
Void operate(List priorOutputs) {
task.updateStatus BASE_PHASE, "Resizing server group: " + description.serverGroupName
oracleBMCSServerGroupService.resizeServerGroup(task, description.credentials, description.serverGroupName, description.capacity.desired)
task.updateStatus BASE_PHASE, "Completed server group resize"
return null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.validator

import com.netflix.spinnaker.clouddriver.deploy.DescriptionValidator
import com.netflix.spinnaker.clouddriver.oraclebmcs.OracleBMCSOperation
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description.ResizeOracleBMCSServerGroupDescription
import com.netflix.spinnaker.clouddriver.orchestration.AtomicOperations
import org.springframework.stereotype.Component
import org.springframework.validation.Errors

@OracleBMCSOperation(AtomicOperations.RESIZE_SERVER_GROUP)
@Component("resizeOracleBMCSServerGroupDescriptionValidator")
class ResizeOracleBMCSServerGroupDescriptionValidator extends DescriptionValidator<ResizeOracleBMCSServerGroupDescription> {

@Override
void validate(List priorDescriptions, ResizeOracleBMCSServerGroupDescription description, Errors errors) {
def helper = new StandardOracleBMCSAttributeValidator("resizeServerGroupDescription", errors)

helper.validateNotEmptyString(description.serverGroupName, "serverGroupName")
helper.validateNotEmptyString(description.region, "region")
helper.validateNotEmptyString(description.accountName, "accountName")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.validator

import org.springframework.validation.Errors

class StandardOracleBMCSAttributeValidator {

String context
Errors errors

StandardOracleBMCSAttributeValidator(String context, Errors errors) {
this.context = context
this.errors = errors
}

def validateNotEmptyString(String value, String attribute) {
if (!value) {
errors.rejectValue(attribute, "${context}.${attribute}.empty")
return false
}
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ package com.netflix.spinnaker.clouddriver.oraclebmcs.service.servergroup

import com.netflix.spinnaker.clouddriver.oraclebmcs.security.OracleBMCSNamedAccountCredentials

/**
* Created by slord on 12/04/2017.
*/
class OracleBMCSPersistenceContext {

OracleBMCSNamedAccountCredentials creds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.oraclebmcs.model.OracleBMCSServerGroup
import com.netflix.spinnaker.clouddriver.oraclebmcs.security.OracleBMCSNamedAccountCredentials

/**
* Created by slord on 16/01/2017.
*/
interface OracleBMCSServerGroupService {

public List<OracleBMCSServerGroup> listAllServerGroups(OracleBMCSNamedAccountCredentials creds)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.converter

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description.ResizeOracleBMCSServerGroupDescription
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.op.ResizeOracleBMCSServerGroupAtomicOperation
import com.netflix.spinnaker.clouddriver.oraclebmcs.security.OracleBMCSNamedAccountCredentials
import com.netflix.spinnaker.clouddriver.security.AccountCredentialsProvider
import spock.lang.Shared
import spock.lang.Specification

class ResizeOracleBMCSServerGroupAtomicOperationConverterUnitSpec extends Specification {

private static final SERVER_GROUP_NAME = "spinnaker-test-v000"
private static final TARGET_SIZE = 5
private static final REGION = "us-central1"
private static final ZONE = "us-central1-b"
private static final ACCOUNT_NAME = "auto"

@Shared
ObjectMapper mapper = new ObjectMapper()

void "resizeOracleBMCSServerGroupDescription type returns ResizeOracleBMCSServerGroupDescription and ResizeOracleBMCSServerGroupAtomicOperation"() {
setup:
def input = [serverGroupName: SERVER_GROUP_NAME,
targetSize : TARGET_SIZE,
region : REGION,
zone : ZONE,
accountName : ACCOUNT_NAME]

ResizeOracleBMCSServerGroupAtomicOperationConverter converter =
new ResizeOracleBMCSServerGroupAtomicOperationConverter(objectMapper: mapper)

def accountCredentialsProvider = Mock(AccountCredentialsProvider)
def mockCredentials = Mock(OracleBMCSNamedAccountCredentials)
accountCredentialsProvider.getCredentials(_) >> mockCredentials
converter.accountCredentialsProvider = accountCredentialsProvider

when:
def description = converter.convertDescription(input)

then:
description instanceof ResizeOracleBMCSServerGroupDescription

when:
def operation = converter.convertOperation(input)

then:
operation instanceof ResizeOracleBMCSServerGroupAtomicOperation

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.op

import com.netflix.spinnaker.clouddriver.data.task.Task
import com.netflix.spinnaker.clouddriver.data.task.TaskRepository
import com.netflix.spinnaker.clouddriver.model.ServerGroup
import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description.ResizeOracleBMCSServerGroupDescription
import com.netflix.spinnaker.clouddriver.oraclebmcs.service.servergroup.OracleBMCSServerGroupService
import spock.lang.Specification

class ResizeOracleBMCSServerGroupAtomicOperationSpec extends Specification {

def "Resize server group"() {
setup:
def resizeDesc = new ResizeOracleBMCSServerGroupDescription()
resizeDesc.serverGroupName = "sg1"
resizeDesc.capacity = new ServerGroup.Capacity(desired: 3)

TaskRepository.threadLocalTask.set(Mock(Task))
def sgService = Mock(OracleBMCSServerGroupService)
ResizeOracleBMCSServerGroupAtomicOperation op = new ResizeOracleBMCSServerGroupAtomicOperation(resizeDesc)
op.oracleBMCSServerGroupService = sgService

when:
op.operate(null)

then:
1 * sgService.resizeServerGroup(_, _, "sg1", 3)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.validator

import com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.description.ResizeOracleBMCSServerGroupDescription
import org.springframework.validation.Errors
import spock.lang.Shared
import spock.lang.Specification

class ResizeOracleBMCSServerGroupDescriptionValidatorSpec extends Specification {

@Shared ResizeOracleBMCSServerGroupDescriptionValidator validator

void setupSpec() {
validator = new ResizeOracleBMCSServerGroupDescriptionValidator()
}

void "invalid description fails validation"() {
setup:
def description = new ResizeOracleBMCSServerGroupDescription()
def errors = Mock(Errors)

when:
validator.validate([], description, errors)

then:
1 * errors.rejectValue("serverGroupName", "resizeServerGroupDescription.serverGroupName.empty")
1 * errors.rejectValue("region", "resizeServerGroupDescription.region.empty")
1 * errors.rejectValue("accountName", "resizeServerGroupDescription.accountName.empty")
}

void "pass validation with proper description inputs"() {
setup:
def description = new ResizeOracleBMCSServerGroupDescription(
serverGroupName: "spinnaker-test-v000",
region: "us-phoenix-1",
accountName: "DEFAULT"
)

def errors = Mock(Errors)

when:
validator.validate([], description, errors)

then:
0 * errors._
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2017 Oracle America, Inc.
*
* The contents of this file are subject to the Apache License Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* If a copy of the Apache License Version 2.0 was not distributed with this file,
* You can obtain one at https://www.apache.org/licenses/LICENSE-2.0.html
*/
package com.netflix.spinnaker.clouddriver.oraclebmcs.deploy.validator

import org.springframework.validation.Errors
import spock.lang.Specification

class StandardOracleBMCSAttributeValidatorSpec extends Specification {

void "validateNotEmptyString ok"() {
setup:
def errors = Mock(Errors)
def validator = new StandardOracleBMCSAttributeValidator("context", errors)

when:
validator.validateNotEmptyString("DEFAULT", "accountName")
then:
0 * errors._

when:
validator.validateNotEmptyString("", "accountName")
then:
1 * errors._

when:
validator.validateNotEmptyString(null, "accountName")
then:
1 * errors._
}
}

0 comments on commit 3609ef0

Please sign in to comment.