github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/tempname.go (about)

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/packer/common/random"
     8  )
     9  
    10  type TempName struct {
    11  	AdminPassword       string
    12  	CertificatePassword string
    13  	ComputeName         string
    14  	DeploymentName      string
    15  	KeyVaultName        string
    16  	ResourceGroupName   string
    17  	OSDiskName          string
    18  	NicName             string
    19  	SubnetName          string
    20  	PublicIPAddressName string
    21  	VirtualNetworkName  string
    22  }
    23  
    24  func NewTempName() *TempName {
    25  	tempName := &TempName{}
    26  
    27  	suffix := random.AlphaNumLower(10)
    28  	tempName.ComputeName = fmt.Sprintf("pkrvm%s", suffix)
    29  	tempName.DeploymentName = fmt.Sprintf("pkrdp%s", suffix)
    30  	tempName.KeyVaultName = fmt.Sprintf("pkrkv%s", suffix)
    31  	tempName.OSDiskName = fmt.Sprintf("pkros%s", suffix)
    32  	tempName.NicName = fmt.Sprintf("pkrni%s", suffix)
    33  	tempName.PublicIPAddressName = fmt.Sprintf("pkrip%s", suffix)
    34  	tempName.SubnetName = fmt.Sprintf("pkrsn%s", suffix)
    35  	tempName.VirtualNetworkName = fmt.Sprintf("pkrvn%s", suffix)
    36  	tempName.ResourceGroupName = fmt.Sprintf("packer-Resource-Group-%s", suffix)
    37  
    38  	tempName.AdminPassword = generatePassword()
    39  	tempName.CertificatePassword = random.AlphaNum(32)
    40  
    41  	return tempName
    42  }
    43  
    44  // generate a password that is acceptable to Azure
    45  // Three of the four items must be met.
    46  //  1. Contains an uppercase character
    47  //  2. Contains a lowercase character
    48  //  3. Contains a numeric digit
    49  //  4. Contains a special character
    50  func generatePassword() string {
    51  	var s string
    52  	for i := 0; i < 100; i++ {
    53  		s := random.AlphaNum(32)
    54  		if !strings.ContainsAny(s, random.PossibleNumbers) {
    55  			continue
    56  		}
    57  
    58  		if !strings.ContainsAny(s, random.PossibleLowerCase) {
    59  			continue
    60  		}
    61  
    62  		if !strings.ContainsAny(s, random.PossibleUpperCase) {
    63  			continue
    64  		}
    65  
    66  		return s
    67  	}
    68  
    69  	// if an acceptable password cannot be generated in 100 tries, give up
    70  	return s
    71  }