-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add converters package, use awserrors pkg for all errors
Signed-off-by: Vince Prignano <[email protected]>
- Loading branch information
Showing
16 changed files
with
261 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"instance.go", | ||
"tags.go", | ||
], | ||
importpath = "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/converters", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/apis/awsprovider/v1alpha1:go_default_library", | ||
"//vendor/github.com/aws/aws-sdk-go/aws:go_default_library", | ||
"//vendor/github.com/aws/aws-sdk-go/service/ec2:go_default_library", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright © 2018 The Kubernetes Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package converters | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1" | ||
) | ||
|
||
func SDKToInstance(v *ec2.Instance) *v1alpha1.Instance { | ||
i := &v1alpha1.Instance{ | ||
ID: aws.StringValue(v.InstanceId), | ||
State: v1alpha1.InstanceState(*v.State.Name), | ||
Type: aws.StringValue(v.InstanceType), | ||
SubnetID: aws.StringValue(v.SubnetId), | ||
ImageID: aws.StringValue(v.ImageId), | ||
KeyName: v.KeyName, | ||
PrivateIP: v.PrivateIpAddress, | ||
PublicIP: v.PublicIpAddress, | ||
ENASupport: v.EnaSupport, | ||
EBSOptimized: v.EbsOptimized, | ||
} | ||
|
||
for _, sg := range v.SecurityGroups { | ||
i.SecurityGroupIDs = append(i.SecurityGroupIDs, *sg.GroupId) | ||
} | ||
|
||
// TODO: Handle returned IAM instance profile, since we are currently | ||
// using a string representing the name, but the InstanceProfile returned | ||
// from the sdk only returns ARN and ID. | ||
|
||
if len(v.Tags) > 0 { | ||
i.Tags = TagsToMap(v.Tags) | ||
} | ||
|
||
return i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package converters | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
) | ||
|
||
// TagsToMap converts a []*ec2.Tag into a map[string]string. | ||
func TagsToMap(src []*ec2.Tag) map[string]string { | ||
// Create an array of exactly the length we require to hopefully avoid some | ||
// allocations while looping. | ||
tags := make(map[string]string) | ||
|
||
for _, t := range src { | ||
tags[*t.Key] = *t.Value | ||
} | ||
|
||
return tags | ||
} | ||
|
||
// MapToTags converts a map[string]string to a []*ec2.Tag | ||
func MapToTags(src map[string]string) []*ec2.Tag { | ||
// Create an array of exactly the length we require to hopefully avoid some | ||
// allocations while looping. | ||
tags := make([]*ec2.Tag, 0, len(src)) | ||
|
||
for k, v := range src { | ||
tag := &ec2.Tag{ | ||
Key: aws.String(k), | ||
Value: aws.String(v), | ||
} | ||
|
||
tags = append(tags, tag) | ||
} | ||
|
||
return tags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.