github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/arn.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  )
     7  
     8  type ARN struct {
     9  	Arn                string `yaml:"arn,omitempty"`
    10  	ArnFromStackOutput string `yaml:"arnFromStackOutput,omitempty"`
    11  	ArnFromFn          string `yaml:"arnFromFn,omitempty"`
    12  }
    13  
    14  // HasArn returns true when the id of a resource i.e. either `arn` or `arnFromStackOutput` is specified
    15  func (i ARN) HasArn() bool {
    16  	return i.Arn != "" || i.ArnFromStackOutput != ""
    17  }
    18  
    19  func (i ARN) Validate() error {
    20  	if i.ArnFromFn != "" {
    21  		var jsonHolder map[string]interface{}
    22  		if err := json.Unmarshal([]byte(i.ArnFromFn), &jsonHolder); err != nil {
    23  			return fmt.Errorf("arnFromFn must be a valid json expression but was not: %s", i.ArnFromFn)
    24  		}
    25  	}
    26  	return nil
    27  }
    28  
    29  func (i ARN) OrGetAttArn(logicalNameProvider func() (string, error)) (string, error) {
    30  	return i.OrExpr(func() (string, error) {
    31  		logicalName, err := logicalNameProvider()
    32  		if err != nil {
    33  			// So that kube-aws can print a more useful error message with
    34  			// the line number for the stack-template.json when there's an error
    35  			return "", fmt.Errorf("failed to get arn: %v", err)
    36  		}
    37  		return fmt.Sprintf(`{ "Fn::GetAtt": [ %q, "Arn" ] }`, logicalName), nil
    38  	})
    39  }
    40  
    41  func (i ARN) OrRef(logicalNameProvider func() (string, error)) (string, error) {
    42  	return i.OrExpr(func() (string, error) {
    43  		logicalName, err := logicalNameProvider()
    44  		if err != nil {
    45  			// So that kube-aws can print a more useful error message with
    46  			// the line number for the stack-template.json when there's an error
    47  			return "", fmt.Errorf("failed to get arn: %v", err)
    48  		}
    49  		return fmt.Sprintf(`{ "Ref": %q }`, logicalName), nil
    50  	})
    51  }
    52  
    53  func (i ARN) OrExpr(exprProvider func() (string, error)) (string, error) {
    54  	if i.ArnFromStackOutput != "" {
    55  		return fmt.Sprintf(`{ "Fn::ImportValue" : %q }`, i.ArnFromStackOutput), nil
    56  	} else if i.Arn != "" {
    57  		return fmt.Sprintf(`"%s"`, i.Arn), nil
    58  	} else if i.ArnFromFn != "" {
    59  		return i.ArnFromFn, nil
    60  	} else {
    61  		expr, err := exprProvider()
    62  		if err != nil {
    63  			// So that kube-aws can print a more useful error message with
    64  			// the line number for the stack-template.json when there's an error
    65  			return "", fmt.Errorf("failed to get arn: %v", err)
    66  		}
    67  		return expr, nil
    68  	}
    69  }