github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/builder/azure/arm/step_get_certificate.go (about)

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