|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package convertersv2 provides conversion functions for AWS SDK V2 types to CAPA types. |
| 18 | +package convertersv2 |
| 19 | + |
| 20 | +import ( |
| 21 | + "sort" |
| 22 | + |
| 23 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 24 | + autoscalingtypes "github.com/aws/aws-sdk-go-v2/service/autoscaling/types" |
| 25 | + v2ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 26 | + elbv1types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types" |
| 27 | + elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types" |
| 28 | + iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types" |
| 29 | + secretsmanagertypes "github.com/aws/aws-sdk-go-v2/service/secretsmanager/types" |
| 30 | + ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types" |
| 31 | + |
| 32 | + infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 33 | +) |
| 34 | + |
| 35 | +// TagsToMap converts a []v2ec2types.Tag into a infrav1.Tags. |
| 36 | +func TagsToMap(src []v2ec2types.Tag) infrav1.Tags { |
| 37 | + tags := make(infrav1.Tags, len(src)) |
| 38 | + |
| 39 | + for _, t := range src { |
| 40 | + tags[*t.Key] = *t.Value |
| 41 | + } |
| 42 | + |
| 43 | + return tags |
| 44 | +} |
| 45 | + |
| 46 | +// MapPtrToMap converts a [string]*string into a infrav1.Tags. |
| 47 | +func MapPtrToMap(src map[string]*string) infrav1.Tags { |
| 48 | + tags := make(infrav1.Tags, len(src)) |
| 49 | + |
| 50 | + for k, v := range src { |
| 51 | + tags[k] = *v |
| 52 | + } |
| 53 | + |
| 54 | + return tags |
| 55 | +} |
| 56 | + |
| 57 | +// MapToTags converts a infrav1.Tags to a []v2ec2types.Tag. |
| 58 | +func MapToTags(src infrav1.Tags) []v2ec2types.Tag { |
| 59 | + tags := make([]v2ec2types.Tag, 0, len(src)) |
| 60 | + |
| 61 | + for k, v := range src { |
| 62 | + tag := v2ec2types.Tag{ |
| 63 | + Key: aws.String(k), |
| 64 | + Value: aws.String(v), |
| 65 | + } |
| 66 | + |
| 67 | + tags = append(tags, tag) |
| 68 | + } |
| 69 | + |
| 70 | + // Sort so that unit tests can expect a stable order |
| 71 | + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) |
| 72 | + |
| 73 | + return tags |
| 74 | +} |
| 75 | + |
| 76 | +// ELBTagsToMap converts a []elbv1types.Tag into a infrav1.Tags. |
| 77 | +func ELBTagsToMap(src []elbv1types.Tag) infrav1.Tags { |
| 78 | + tags := make(infrav1.Tags, len(src)) |
| 79 | + |
| 80 | + for _, t := range src { |
| 81 | + tags[*t.Key] = *t.Value |
| 82 | + } |
| 83 | + |
| 84 | + return tags |
| 85 | +} |
| 86 | + |
| 87 | +// V2TagsToMap converts a []elbv2types.Tag into a infrav1.Tags. |
| 88 | +func V2TagsToMap(src []elbv2types.Tag) infrav1.Tags { |
| 89 | + tags := make(infrav1.Tags, len(src)) |
| 90 | + |
| 91 | + for _, t := range src { |
| 92 | + tags[*t.Key] = *t.Value |
| 93 | + } |
| 94 | + |
| 95 | + return tags |
| 96 | +} |
| 97 | + |
| 98 | +// MapToELBTags converts a infrav1.Tags to a []elbv1types.Tag. |
| 99 | +func MapToELBTags(src infrav1.Tags) []elbv1types.Tag { |
| 100 | + tags := make([]elbv1types.Tag, 0, len(src)) |
| 101 | + |
| 102 | + for k, v := range src { |
| 103 | + tag := elbv1types.Tag{ |
| 104 | + Key: aws.String(k), |
| 105 | + Value: aws.String(v), |
| 106 | + } |
| 107 | + |
| 108 | + tags = append(tags, tag) |
| 109 | + } |
| 110 | + |
| 111 | + // Sort so that unit tests can expect a stable order |
| 112 | + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) |
| 113 | + |
| 114 | + return tags |
| 115 | +} |
| 116 | + |
| 117 | +// MapToV2Tags converts a infrav1.Tags to a []elbv2types.Tag. |
| 118 | +func MapToV2Tags(src infrav1.Tags) []elbv2types.Tag { |
| 119 | + tags := make([]elbv2types.Tag, 0, len(src)) |
| 120 | + |
| 121 | + for k, v := range src { |
| 122 | + tag := elbv2types.Tag{ |
| 123 | + Key: aws.String(k), |
| 124 | + Value: aws.String(v), |
| 125 | + } |
| 126 | + |
| 127 | + tags = append(tags, tag) |
| 128 | + } |
| 129 | + |
| 130 | + // Sort so that unit tests can expect a stable order |
| 131 | + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) |
| 132 | + |
| 133 | + return tags |
| 134 | +} |
| 135 | + |
| 136 | +// MapToSecretsManagerTags converts a infrav1.Tags to a []secretsmanagertypes.Tag. |
| 137 | +func MapToSecretsManagerTags(src infrav1.Tags) []secretsmanagertypes.Tag { |
| 138 | + tags := make([]secretsmanagertypes.Tag, 0, len(src)) |
| 139 | + |
| 140 | + for k, v := range src { |
| 141 | + tag := secretsmanagertypes.Tag{ |
| 142 | + Key: aws.String(k), |
| 143 | + Value: aws.String(v), |
| 144 | + } |
| 145 | + |
| 146 | + tags = append(tags, tag) |
| 147 | + } |
| 148 | + |
| 149 | + // Sort so that unit tests can expect a stable order |
| 150 | + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) |
| 151 | + |
| 152 | + return tags |
| 153 | +} |
| 154 | + |
| 155 | +// MapToSSMTags converts a infrav1.Tags to a []ssm.Tag. |
| 156 | +func MapToSSMTags(src infrav1.Tags) []ssmtypes.Tag { |
| 157 | + tags := make([]ssmtypes.Tag, 0, len(src)) |
| 158 | + |
| 159 | + for k, v := range src { |
| 160 | + tag := ssmtypes.Tag{ |
| 161 | + Key: aws.String(k), |
| 162 | + Value: aws.String(v), |
| 163 | + } |
| 164 | + |
| 165 | + tags = append(tags, tag) |
| 166 | + } |
| 167 | + |
| 168 | + // Sort so that unit tests can expect a stable order |
| 169 | + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) |
| 170 | + |
| 171 | + return tags |
| 172 | +} |
| 173 | + |
| 174 | +// MapToIAMTags converts a infrav1.Tags to a []iamtypes.Tag. |
| 175 | +func MapToIAMTags(src infrav1.Tags) []iamtypes.Tag { |
| 176 | + tags := make([]iamtypes.Tag, 0, len(src)) |
| 177 | + |
| 178 | + for k, v := range src { |
| 179 | + tag := iamtypes.Tag{ |
| 180 | + Key: aws.String(k), |
| 181 | + Value: aws.String(v), |
| 182 | + } |
| 183 | + |
| 184 | + tags = append(tags, tag) |
| 185 | + } |
| 186 | + |
| 187 | + // Sort so that unit tests can expect a stable order |
| 188 | + sort.Slice(tags, func(i, j int) bool { return *tags[i].Key < *tags[j].Key }) |
| 189 | + |
| 190 | + return tags |
| 191 | +} |
| 192 | + |
| 193 | +// ASGTagsToMap converts a []autoscalingtypes.TagDescription into a infrav1.Tags. |
| 194 | +func ASGTagsToMap(src []autoscalingtypes.TagDescription) infrav1.Tags { |
| 195 | + tags := make(infrav1.Tags, len(src)) |
| 196 | + |
| 197 | + for _, t := range src { |
| 198 | + tags[*t.Key] = *t.Value |
| 199 | + } |
| 200 | + |
| 201 | + return tags |
| 202 | +} |
0 commit comments