|
| 1 | +/* |
| 2 | +Copyright 2019 Cortex Labs, Inc. |
| 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 aws |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/aws/aws-sdk-go/service/autoscaling" |
| 21 | + "github.com/cortexlabs/cortex/pkg/lib/errors" |
| 22 | +) |
| 23 | + |
| 24 | +// if specified, all tags must be present |
| 25 | +func (c *Client) AutoscalingGroups(tags map[string]string) ([]*autoscaling.Group, error) { |
| 26 | + var asgs []*autoscaling.Group |
| 27 | + |
| 28 | + err := c.autoscaling.DescribeAutoScalingGroupsPages(nil, |
| 29 | + func(page *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool { |
| 30 | + for _, asg := range page.AutoScalingGroups { |
| 31 | + asgTags := make(map[string]string, len(asg.Tags)) |
| 32 | + for _, asgTag := range asg.Tags { |
| 33 | + if asgTag.Key != nil && asgTag.Value != nil { |
| 34 | + asgTags[*asgTag.Key] = *asgTag.Value |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + missingTag := false |
| 39 | + for key, value := range tags { |
| 40 | + if asgTags[key] != value { |
| 41 | + missingTag = true |
| 42 | + break |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if missingTag { |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + asgs = append(asgs, asg) |
| 51 | + } |
| 52 | + |
| 53 | + return true |
| 54 | + }) |
| 55 | + |
| 56 | + if err != nil { |
| 57 | + return nil, errors.WithStack(err) |
| 58 | + } |
| 59 | + |
| 60 | + return asgs, nil |
| 61 | +} |
0 commit comments