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

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  type StepGetCertificate struct {
    13  	client *AzureClient
    14  	get    func(keyVaultName string, secretName string) (string, error)
    15  	say    func(message string)
    16  	error  func(e error)
    17  	pause  func()
    18  }
    19  
    20  func NewStepGetCertificate(client *AzureClient, ui packer.Ui) *StepGetCertificate {
    21  	var step = &StepGetCertificate{
    22  		client: client,
    23  		say:    func(message string) { ui.Say(message) },
    24  		error:  func(e error) { ui.Error(e.Error()) },
    25  		pause:  func() { time.Sleep(30 * time.Second) },
    26  	}
    27  
    28  	step.get = step.getCertificateUrl
    29  	return step
    30  }
    31  
    32  func (s *StepGetCertificate) getCertificateUrl(keyVaultName string, secretName string) (string, error) {
    33  	secret, err := s.client.GetSecret(keyVaultName, secretName)
    34  	if err != nil {
    35  		s.say(s.client.LastError.Error())
    36  		return "", err
    37  	}
    38  
    39  	return *secret.ID, err
    40  }
    41  
    42  func (s *StepGetCertificate) Run(state multistep.StateBag) multistep.StepAction {
    43  	s.say("Getting the certificate's URL ...")
    44  
    45  	var keyVaultName = state.Get(constants.ArmKeyVaultName).(string)
    46  
    47  	s.say(fmt.Sprintf(" -> Key Vault Name        : '%s'", keyVaultName))
    48  	s.say(fmt.Sprintf(" -> Key Vault Secret Name : '%s'", DefaultSecretName))
    49  
    50  	var err error
    51  	var url string
    52  	for i := 0; i < 5; i++ {
    53  		url, err = s.get(keyVaultName, DefaultSecretName)
    54  		if err == nil {
    55  			break
    56  		}
    57  
    58  		s.say(fmt.Sprintf(" ...failed to get certificate URL, retry(%d)", i))
    59  		s.pause()
    60  	}
    61  
    62  	if err != nil {
    63  		state.Put(constants.Error, err)
    64  		s.error(err)
    65  
    66  		return multistep.ActionHalt
    67  	}
    68  
    69  	s.say(fmt.Sprintf(" -> Certificate URL       : '%s'", url))
    70  	state.Put(constants.ArmCertificateUrl, url)
    71  
    72  	return multistep.ActionContinue
    73  }
    74  
    75  func (*StepGetCertificate) Cleanup(multistep.StateBag) {
    76  }