forked from kubernetes-sigs/cluster-api-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 43
/
awsclient.go
130 lines (110 loc) · 3.62 KB
/
awsclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package machine
import (
"fmt"
"github.com/openshift/cluster-api-actuator-pkg/pkg/types"
awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/client"
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)
// AwsClientWrapper implements CloudProviderClient for aws e2e framework
type AwsClientWrapper struct {
client awsclient.Client
}
var _ types.CloudProviderClient = &AwsClientWrapper{}
// NewAwsClientWrapper returns aws client implementaton of CloudProviderClient
// used for testing in CI environmet
func NewAwsClientWrapper(client awsclient.Client) *AwsClientWrapper {
return &AwsClientWrapper{client: client}
}
// GetRunningInstances gets running instances (of a given cloud provider) managed by the machine object
func (client *AwsClientWrapper) GetRunningInstances(machine *clusterv1alpha1.Machine) ([]interface{}, error) {
runningInstances, err := getRunningInstances(machine, client.client)
if err != nil {
return nil, err
}
var instances []interface{}
for _, instance := range runningInstances {
instances = append(instances, instance)
}
return instances, nil
}
// GetPublicDNSName gets running instance public DNS name
func (client *AwsClientWrapper) GetPublicDNSName(machine *clusterv1alpha1.Machine) (string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return "", err
}
if *instance.PublicDnsName == "" {
return "", fmt.Errorf("machine instance public DNS name not set")
}
return *instance.PublicDnsName, nil
}
// GetPrivateIP gets private IP
func (client *AwsClientWrapper) GetPrivateIP(machine *clusterv1alpha1.Machine) (string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return "", err
}
if *instance.PrivateIpAddress == "" {
return "", fmt.Errorf("machine instance public DNS name not set")
}
return *instance.PrivateIpAddress, nil
}
// GetSecurityGroups gets security groups
func (client *AwsClientWrapper) GetSecurityGroups(machine *clusterv1alpha1.Machine) ([]string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return nil, err
}
var groups []string
for _, groupIdentifier := range instance.SecurityGroups {
if *groupIdentifier.GroupName != "" {
groups = append(groups, *groupIdentifier.GroupName)
}
}
return groups, nil
}
// GetIAMRole gets IAM role
func (client *AwsClientWrapper) GetIAMRole(machine *clusterv1alpha1.Machine) (string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return "", err
}
if instance.IamInstanceProfile == nil {
return "", err
}
return *instance.IamInstanceProfile.Id, nil
}
// GetTags gets tags
func (client *AwsClientWrapper) GetTags(machine *clusterv1alpha1.Machine) (map[string]string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return nil, err
}
tags := make(map[string]string, len(instance.Tags))
for _, tag := range instance.Tags {
tags[*tag.Key] = *tag.Value
}
return tags, nil
}
// GetSubnet gets subnet
func (client *AwsClientWrapper) GetSubnet(machine *clusterv1alpha1.Machine) (string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return "", err
}
if instance.SubnetId == nil {
return "", err
}
return *instance.SubnetId, nil
}
// GetAvailabilityZone gets availability zone
func (client *AwsClientWrapper) GetAvailabilityZone(machine *clusterv1alpha1.Machine) (string, error) {
instance, err := getRunningInstance(machine, client.client)
if err != nil {
return "", err
}
if instance.Placement == nil {
return "", err
}
return *instance.Placement.AvailabilityZone, nil
}