/* 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 utils import ( "fmt" "regexp" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/samber/lo" ) var ( instanceIDRegex = regexp.MustCompile(`aws:///(?P.*)/(?P.*)`) ) // ParseInstanceID parses the provider ID stored on the node to get the instance ID // associated with a node func ParseInstanceID(providerID string) (string, error) { matches := instanceIDRegex.FindStringSubmatch(providerID) if matches == nil { return "", fmt.Errorf("parsing instance id %s", providerID) } for i, name := range instanceIDRegex.SubexpNames() { if name == "InstanceID" { return matches[i], nil } } return "", fmt.Errorf("parsing instance id %s", providerID) } // MergeTags takes a variadic list of maps and merges them together into a list of // EC2 tags to be passed into EC2 API calls func MergeTags(tags ...map[string]string) []*ec2.Tag { return lo.MapToSlice(lo.Assign(tags...), func(k, v string) *ec2.Tag { return &ec2.Tag{Key: aws.String(k), Value: aws.String(v)} }) }