packer-cn/builder/azure/arm/template_factory.go

100 lines
3.1 KiB
Go
Raw Normal View History

2016-05-21 02:01:16 -04:00
package arm
import (
"encoding/json"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/mitchellh/packer/builder/azure/common/constants"
"github.com/mitchellh/packer/builder/azure/common/template"
)
type templateFactoryFunc func(*Config) (*resources.Deployment, error)
func GetKeyVaultDeployment(config *Config) (*resources.Deployment, error) {
params := &template.TemplateParameters{
KeyVaultName: &template.TemplateParameter{Value: config.tmpKeyVaultName},
KeyVaultSecretValue: &template.TemplateParameter{Value: config.winrmCertificate},
ObjectId: &template.TemplateParameter{Value: config.ObjectID},
TenantId: &template.TemplateParameter{Value: config.TenantID},
}
2016-07-29 17:17:33 -04:00
builder, _ := template.NewTemplateBuilder(template.KeyVault)
builder.SetTags(&config.AzureTags)
doc, _ := builder.ToJSON()
return createDeploymentParameters(*doc, params)
2016-05-21 02:01:16 -04:00
}
func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) {
params := &template.TemplateParameters{
AdminUsername: &template.TemplateParameter{Value: config.UserName},
AdminPassword: &template.TemplateParameter{Value: config.Password},
DnsNameForPublicIP: &template.TemplateParameter{Value: config.tmpComputeName},
OSDiskName: &template.TemplateParameter{Value: config.tmpOSDiskName},
StorageAccountBlobEndpoint: &template.TemplateParameter{Value: config.storageAccountBlobEndpoint},
VMSize: &template.TemplateParameter{Value: config.VMSize},
VMName: &template.TemplateParameter{Value: config.tmpComputeName},
}
2016-07-29 17:17:33 -04:00
builder, _ := template.NewTemplateBuilder(template.BasicTemplate)
2016-05-21 02:01:16 -04:00
osType := compute.Linux
switch config.OSType {
case constants.Target_Linux:
builder.BuildLinux(config.sshAuthorizedKey)
case constants.Target_Windows:
osType = compute.Windows
builder.BuildWindows(config.tmpKeyVaultName, config.tmpWinRMCertificateUrl)
}
if config.ImageUrl != "" {
builder.SetImageUrl(config.ImageUrl, osType)
} else {
builder.SetMarketPlaceImage(config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion)
}
if config.OSDiskSizeGB > 0 {
builder.SetOSDiskSizeGB(config.OSDiskSizeGB)
}
if config.VirtualNetworkName != "" {
builder.SetVirtualNetwork(
config.VirtualNetworkResourceGroupName,
config.VirtualNetworkName,
config.VirtualNetworkSubnetName)
}
2016-07-29 17:17:33 -04:00
builder.SetTags(&config.AzureTags)
2016-05-21 02:01:16 -04:00
doc, _ := builder.ToJSON()
return createDeploymentParameters(*doc, params)
}
func createDeploymentParameters(doc string, parameters *template.TemplateParameters) (*resources.Deployment, error) {
var template map[string]interface{}
err := json.Unmarshal(([]byte)(doc), &template)
if err != nil {
return nil, err
}
bs, err := json.Marshal(*parameters)
if err != nil {
return nil, err
}
var templateParameters map[string]interface{}
err = json.Unmarshal(bs, &templateParameters)
if err != nil {
return nil, err
}
return &resources.Deployment{
Properties: &resources.DeploymentProperties{
Mode: resources.Incremental,
Template: &template,
Parameters: &templateParameters,
},
}, nil
}