github.com/kubernetes-incubator/kube-aws@v0.16.4/test/helper/cfn.go (about) 1 package helper 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/service/cloudformation" 7 "github.com/aws/aws-sdk-go/service/ec2" 8 ) 9 10 type DummyCloudformationService struct { 11 ExpectedTags []*cloudformation.Tag 12 StackEvents []*cloudformation.StackEvent 13 StackStatus string 14 } 15 16 // DummyCFInterrogator is used to prevent calls to AWS - always returns empty results. 17 type DummyCFInterrogator struct { 18 ListStacksResourcesResult *cloudformation.ListStackResourcesOutput 19 DescribeStacksResult *cloudformation.DescribeStacksOutput 20 } 21 22 func (cf DummyCFInterrogator) ListStackResources(input *cloudformation.ListStackResourcesInput) (*cloudformation.ListStackResourcesOutput, error) { 23 return cf.ListStacksResourcesResult, nil 24 } 25 26 func (cf DummyCFInterrogator) DescribeStacks(input *cloudformation.DescribeStacksInput) (*cloudformation.DescribeStacksOutput, error) { 27 return cf.DescribeStacksResult, nil 28 } 29 30 type DummyEC2Interrogator struct { 31 DescribeInstancesOutput *ec2.DescribeInstancesOutput 32 } 33 34 func (ec DummyEC2Interrogator) DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) { 35 return ec.DescribeInstancesOutput, nil 36 } 37 38 func (cfSvc *DummyCloudformationService) CreateStack(req *cloudformation.CreateStackInput) (*cloudformation.CreateStackOutput, error) { 39 40 if len(cfSvc.ExpectedTags) != len(req.Tags) { 41 return nil, fmt.Errorf( 42 "expected tag count does not match supplied tag count\nexpected=%v, supplied=%v", 43 cfSvc.ExpectedTags, 44 req.Tags, 45 ) 46 } 47 48 matchCnt := 0 49 for _, eTag := range cfSvc.ExpectedTags { 50 for _, tag := range req.Tags { 51 if *tag.Key == *eTag.Key && *tag.Value == *eTag.Value { 52 matchCnt++ 53 break 54 } 55 } 56 } 57 58 if matchCnt != len(cfSvc.ExpectedTags) { 59 return nil, fmt.Errorf( 60 "not all tags matched\nexpected=%v, observed=%v", 61 cfSvc.ExpectedTags, 62 req.Tags, 63 ) 64 } 65 66 resp := &cloudformation.CreateStackOutput{ 67 StackId: req.StackName, 68 } 69 70 return resp, nil 71 } 72 73 type DummyStackTemplateGetter struct { 74 GetStackTemplateOutput *cloudformation.GetTemplateOutput 75 } 76 77 func (cfn DummyStackTemplateGetter) GetTemplate(input *cloudformation.GetTemplateInput) (*cloudformation.GetTemplateOutput, error) { 78 if cfn.GetStackTemplateOutput == nil { 79 return nil, fmt.Errorf("result is not set") 80 } 81 return cfn.GetStackTemplateOutput, nil 82 }