github.com/mitchellh/packer@v1.3.2/builder/azure/arm/step_get_certificate.go (about)

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