github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_security_group_rule_migrate.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/ec2" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func resourceAwsSecurityGroupRuleMigrateState( 15 v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) { 16 switch v { 17 case 0: 18 log.Println("[INFO] Found AWS Security Group State v0; migrating to v1") 19 return migrateSGRuleStateV0toV1(is) 20 case 1: 21 log.Println("[INFO] Found AWS Security Group State v1; migrating to v2") 22 // migrating to version 2 of the schema is the same as 0->1, since the 23 // method signature has changed now and will use the security group id in 24 // the hash 25 return migrateSGRuleStateV0toV1(is) 26 default: 27 return is, fmt.Errorf("Unexpected schema version: %d", v) 28 } 29 } 30 31 func migrateSGRuleStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) { 32 if is.Empty() { 33 log.Println("[DEBUG] Empty InstanceState; nothing to migrate.") 34 return is, nil 35 } 36 37 perm, err := migrateExpandIPPerm(is.Attributes) 38 39 if err != nil { 40 return nil, fmt.Errorf("[WARN] Error making new IP Permission in Security Group migration") 41 } 42 43 log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes) 44 newID := ipPermissionIDHash(is.Attributes["security_group_id"], is.Attributes["type"], perm) 45 is.Attributes["id"] = newID 46 is.ID = newID 47 log.Printf("[DEBUG] Attributes after migration: %#v, new id: %s", is.Attributes, newID) 48 return is, nil 49 } 50 51 func migrateExpandIPPerm(attrs map[string]string) (*ec2.IpPermission, error) { 52 var perm ec2.IpPermission 53 tp, err := strconv.Atoi(attrs["to_port"]) 54 if err != nil { 55 return nil, fmt.Errorf("Error converting to_port in Security Group migration") 56 } 57 58 fp, err := strconv.Atoi(attrs["from_port"]) 59 if err != nil { 60 return nil, fmt.Errorf("Error converting from_port in Security Group migration") 61 } 62 63 perm.ToPort = aws.Int64(int64(tp)) 64 perm.FromPort = aws.Int64(int64(fp)) 65 perm.IpProtocol = aws.String(attrs["protocol"]) 66 67 groups := make(map[string]bool) 68 if attrs["self"] == "true" { 69 groups[attrs["security_group_id"]] = true 70 } 71 72 if attrs["source_security_group_id"] != "" { 73 groups[attrs["source_security_group_id"]] = true 74 } 75 76 if len(groups) > 0 { 77 perm.UserIdGroupPairs = make([]*ec2.UserIdGroupPair, len(groups)) 78 // build string list of group name/ids 79 var gl []string 80 for k, _ := range groups { 81 gl = append(gl, k) 82 } 83 84 for i, name := range gl { 85 perm.UserIdGroupPairs[i] = &ec2.UserIdGroupPair{ 86 GroupId: aws.String(name), 87 } 88 } 89 } 90 91 var cb []string 92 for k, v := range attrs { 93 if k != "cidr_blocks.#" && strings.HasPrefix(k, "cidr_blocks") { 94 cb = append(cb, v) 95 } 96 } 97 if len(cb) > 0 { 98 perm.IpRanges = make([]*ec2.IpRange, len(cb)) 99 for i, v := range cb { 100 perm.IpRanges[i] = &ec2.IpRange{CidrIp: aws.String(v)} 101 } 102 } 103 104 return &perm, nil 105 }