github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/template_factory.go (about)

     1  package arm
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/arm/compute"
     7  	"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
     8  
     9  	"fmt"
    10  	"github.com/hashicorp/packer/builder/azure/common/constants"
    11  	"github.com/hashicorp/packer/builder/azure/common/template"
    12  )
    13  
    14  type templateFactoryFunc func(*Config) (*resources.Deployment, error)
    15  
    16  func GetKeyVaultDeployment(config *Config) (*resources.Deployment, error) {
    17  	params := &template.TemplateParameters{
    18  		KeyVaultName:        &template.TemplateParameter{Value: config.tmpKeyVaultName},
    19  		KeyVaultSecretValue: &template.TemplateParameter{Value: config.winrmCertificate},
    20  		ObjectId:            &template.TemplateParameter{Value: config.ObjectID},
    21  		TenantId:            &template.TemplateParameter{Value: config.TenantID},
    22  	}
    23  
    24  	builder, _ := template.NewTemplateBuilder(template.KeyVault)
    25  	builder.SetTags(&config.AzureTags)
    26  
    27  	doc, _ := builder.ToJSON()
    28  	return createDeploymentParameters(*doc, params)
    29  }
    30  
    31  func GetVirtualMachineDeployment(config *Config) (*resources.Deployment, error) {
    32  	params := &template.TemplateParameters{
    33  		AdminUsername:              &template.TemplateParameter{Value: config.UserName},
    34  		AdminPassword:              &template.TemplateParameter{Value: config.Password},
    35  		DnsNameForPublicIP:         &template.TemplateParameter{Value: config.tmpComputeName},
    36  		OSDiskName:                 &template.TemplateParameter{Value: config.tmpOSDiskName},
    37  		StorageAccountBlobEndpoint: &template.TemplateParameter{Value: config.storageAccountBlobEndpoint},
    38  		VMSize: &template.TemplateParameter{Value: config.VMSize},
    39  		VMName: &template.TemplateParameter{Value: config.tmpComputeName},
    40  	}
    41  
    42  	builder, _ := template.NewTemplateBuilder(template.BasicTemplate)
    43  	osType := compute.Linux
    44  
    45  	switch config.OSType {
    46  	case constants.Target_Linux:
    47  		builder.BuildLinux(config.sshAuthorizedKey)
    48  	case constants.Target_Windows:
    49  		osType = compute.Windows
    50  		builder.BuildWindows(config.tmpKeyVaultName, config.tmpWinRMCertificateUrl)
    51  	}
    52  
    53  	if config.ImageUrl != "" {
    54  		builder.SetImageUrl(config.ImageUrl, osType)
    55  	} else if config.CustomManagedImageName != "" {
    56  		builder.SetManagedDiskUrl(config.customManagedImageID, config.managedImageStorageAccountType)
    57  	} else if config.ManagedImageName != "" && config.ImagePublisher != "" {
    58  		imageID := fmt.Sprintf("/subscriptions/%s/providers/Microsoft.Compute/locations/%s/publishers/%s/ArtifactTypes/vmimage/offers/%s/skus/%s/versions/%s",
    59  			config.SubscriptionID,
    60  			config.Location,
    61  			config.ImagePublisher,
    62  			config.ImageOffer,
    63  			config.ImageSku,
    64  			config.ImageVersion)
    65  
    66  		builder.SetManagedMarketplaceImage(config.Location, config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion, imageID, config.managedImageStorageAccountType)
    67  	} else {
    68  		builder.SetMarketPlaceImage(config.ImagePublisher, config.ImageOffer, config.ImageSku, config.ImageVersion)
    69  	}
    70  
    71  	if config.OSDiskSizeGB > 0 {
    72  		builder.SetOSDiskSizeGB(config.OSDiskSizeGB)
    73  	}
    74  
    75  	if config.customData != "" {
    76  		builder.SetCustomData(config.customData)
    77  	}
    78  
    79  	if config.VirtualNetworkName != "" && DefaultPrivateVirtualNetworkWithPublicIp != config.PrivateVirtualNetworkWithPublicIp {
    80  		builder.SetPrivateVirtualNetworWithPublicIp(
    81  			config.VirtualNetworkResourceGroupName,
    82  			config.VirtualNetworkName,
    83  			config.VirtualNetworkSubnetName)
    84  	} else if config.VirtualNetworkName != "" {
    85  		builder.SetVirtualNetwork(
    86  			config.VirtualNetworkResourceGroupName,
    87  			config.VirtualNetworkName,
    88  			config.VirtualNetworkSubnetName)
    89  	}
    90  
    91  	builder.SetTags(&config.AzureTags)
    92  	doc, _ := builder.ToJSON()
    93  	return createDeploymentParameters(*doc, params)
    94  }
    95  
    96  func createDeploymentParameters(doc string, parameters *template.TemplateParameters) (*resources.Deployment, error) {
    97  	var template map[string]interface{}
    98  	err := json.Unmarshal(([]byte)(doc), &template)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	bs, err := json.Marshal(*parameters)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	var templateParameters map[string]interface{}
   109  	err = json.Unmarshal(bs, &templateParameters)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	return &resources.Deployment{
   115  		Properties: &resources.DeploymentProperties{
   116  			Mode:       resources.Incremental,
   117  			Template:   &template,
   118  			Parameters: &templateParameters,
   119  		},
   120  	}, nil
   121  }