Add keypair tag and ID support to cloudmock

This commit is contained in:
Peter Rifel 2020-07-08 22:43:58 -05:00
parent 268398cdac
commit 690f2e3605
No known key found for this signature in database
GPG Key ID: 30DB43602027D941
3 changed files with 22 additions and 7 deletions

View File

@ -91,7 +91,7 @@ func (m *MockEC2) All() map[string]interface{} {
all[id] = o
}
for id, o := range m.KeyPairs {
all["sshkey-"+id] = o
all[id] = o
}
for id, o := range m.Vpcs {
all[id] = o

View File

@ -50,18 +50,25 @@ func (m *MockEC2) ImportKeyPair(request *ec2.ImportKeyPairInput) (*ec2.ImportKey
return nil, err
}
n := len(m.KeyPairs) + 1
id := fmt.Sprintf("key-%d", n)
kp := &ec2.KeyPairInfo{
KeyFingerprint: aws.String(fp),
KeyName: request.KeyName,
KeyPairId: aws.String(id),
}
if m.KeyPairs == nil {
m.KeyPairs = make(map[string]*ec2.KeyPairInfo)
}
m.KeyPairs[aws.StringValue(request.KeyName)] = kp
m.KeyPairs[id] = kp
response := &ec2.ImportKeyPairOutput{
KeyFingerprint: kp.KeyFingerprint,
KeyName: kp.KeyName,
}
m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeKeyPair)...)
return response, nil
}
func (m *MockEC2) CreateKeyPairRequest(*ec2.CreateKeyPairInput) (*request.Request, *ec2.CreateKeyPairOutput) {
@ -122,6 +129,7 @@ func (m *MockEC2) DescribeKeyPairs(request *ec2.DescribeKeyPairsInput) (*ec2.Des
}
copy := *keypair
copy.Tags = m.getTags(ec2.ResourceTypeKeyPair, *copy.KeyPairId)
keypairs = append(keypairs, &copy)
}
@ -138,12 +146,17 @@ func (m *MockEC2) DeleteKeyPair(request *ec2.DeleteKeyPairInput) (*ec2.DeleteKey
klog.Infof("DeleteKeyPair: %v", request)
id := aws.StringValue(request.KeyName)
o := m.KeyPairs[id]
if o == nil {
return nil, fmt.Errorf("KeyPairs %q not found", id)
keyID := aws.StringValue(request.KeyPairId)
found := false
for id, kp := range m.KeyPairs {
if aws.StringValue(kp.KeyPairId) == keyID {
found = true
delete(m.KeyPairs, id)
}
}
if !found {
return nil, fmt.Errorf("KeyPairs %q not found", keyID)
}
delete(m.KeyPairs, id)
return &ec2.DeleteKeyPairOutput{}, nil
}

View File

@ -71,6 +71,8 @@ func (m *MockEC2) addTags(resourceId string, tags ...*ec2.Tag) {
resourceType = ec2.ResourceTypeElasticIp
} else if strings.HasPrefix(resourceId, "lt-") {
resourceType = ec2.ResourceTypeLaunchTemplate
} else if strings.HasPrefix(resourceId, "key-") {
resourceType = ec2.ResourceTypeKeyPair
} else {
klog.Fatalf("Unknown resource-type in create tags: %v", resourceId)
}