Skip to content

Commit

Permalink
(Azure) Ensure 'azure_image_name' conforms to CaptureNamePrefix regex…
Browse files Browse the repository at this point in the history
… in packer (#177)
  • Loading branch information
robumberger authored and Matt Duftler committed Feb 10, 2017
1 parent d5e73f2 commit 83a24bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.netflix.spinnaker.rosco.providers.util.ImageNameFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

import java.time.Clock

@Component
public class AzureBakeHandler extends CloudProviderBakeHandler{

Expand Down Expand Up @@ -92,10 +94,26 @@ public class AzureBakeHandler extends CloudProviderBakeHandler{
azure_location: region,
azure_image_publisher: selectedImage?.baseImage?.publisher,
azure_image_offer: selectedImage?.baseImage?.offer,
azure_image_sku: selectedImage?.baseImage?.sku,
azure_image_name: "$bakeRequest.build_number-$bakeRequest.base_name"
azure_image_sku: selectedImage?.baseImage?.sku
]

if (bakeRequest.build_number && bakeRequest.base_name) {
parameterMap.azure_image_name = "$bakeRequest.build_number-$bakeRequest.base_name"
} else if (imageName) {
parameterMap.azure_image_name = imageName
} else {
parameterMap.azure_image_name = Clock.systemUTC.millis().toString()
}

// Ensure 'azure_image_name' conforms to CaptureNamePrefix regex in packer.
// https://github.com/mitchellh/packer/blob/master/builder/azure/arm/config.go#L45
def azureImageName = parameterMap.azure_image_name
azureImageName = azureImageName.replaceAll(/[^A-Za-z0-9_\-\.]/, "")
azureImageName = azureImageName.length() <= 23 ? azureImageName : azureImageName.substring(0, 23)
azureImageName = azureImageName.replaceAll(/[\-\.]+$/, "")

parameterMap.azure_image_name = azureImageName

if (appVersionStr) {
parameterMap.appversion = appVersionStr
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,34 @@ class AzureBakeHandlerSpec extends Specification implements TestDefaults{
1 * imageNameFactoryMock.buildPackagesParameter(NUPKG_PACKAGE_TYPE, osPackages) >> NUPKG_PACKAGES_NAME
1 * packerCommandFactoryMock.buildPackerCommand("", parameterMap, null, "$configDir/azure-windows-2012-r2.json")
}

void 'Create proper azure_image_name'() {
setup:
def azureBakeHandler = new AzureBakeHandler(azureBakeryDefaults: azureBakeryDefaults)

when:
def bakeRequest = new BakeRequest(user: "[email protected]",
package_name: NUPKG_PACKAGES_NAME,
base_os: "windows-2012-r2",
cloud_provider_type: BakeRequest.CloudProviderType.azure,
build_number: buildNumber,
base_name: baseName)
def azureVirtualizationSettings = azureBakeHandler.findVirtualizationSettings(REGION, bakeRequest)
def parameterMap = azureBakeHandler.buildParameterMap(REGION, azureVirtualizationSettings, imageName, bakeRequest, "SomeVersion")

then:
parameterMap.azure_image_name == expectedAzureImageName

where:
buildNumber | baseName | imageName || expectedAzureImageName
null | null | "namethatexceeds.23char.acters" || "namethatexceeds.23char"
BUILD_NUMBER | BUILD_NAME | IMAGE_NAME || IMAGE_NAME
BUILD_NUMBER | BUILD_NAME | null || IMAGE_NAME
BUILD_NUMBER | null | IMAGE_NAME || IMAGE_NAME
null | BUILD_NAME | IMAGE_NAME || IMAGE_NAME
null | null | IMAGE_NAME || IMAGE_NAME
null | null | "test-with!>#characters.morethan.23characters" || "test-withcharacters.mor"
null | null | "test-with!>#characters..-." || "test-withcharacters"
BUILD_NUMBER | BUILD_NAME | "this-is-a--test-withnamethatexceeds.23characters" || IMAGE_NAME
}
}

0 comments on commit 83a24bf

Please sign in to comment.