github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  	return is, nil
    31  }
    32  
    33  func migrateSGRuleStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
    34  	if is.Empty() {
    35  		log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
    36  		return is, nil
    37  	}
    38  
    39  	perm, err := migrateExpandIPPerm(is.Attributes)
    40  
    41  	if err != nil {
    42  		return nil, fmt.Errorf("[WARN] Error making new IP Permission in Security Group migration")
    43  	}
    44  
    45  	log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
    46  	newID := ipPermissionIDHash(is.Attributes["security_group_id"], is.Attributes["type"], perm)
    47  	is.Attributes["id"] = newID
    48  	is.ID = newID
    49  	log.Printf("[DEBUG] Attributes after migration: %#v, new id: %s", is.Attributes, newID)
    50  	return is, nil
    51  }
    52  
    53  func migrateExpandIPPerm(attrs map[string]string) (*ec2.IpPermission, error) {
    54  	var perm ec2.IpPermission
    55  	tp, err := strconv.Atoi(attrs["to_port"])
    56  	if err != nil {
    57  		return nil, fmt.Errorf("Error converting to_port in Security Group migration")
    58  	}
    59  
    60  	fp, err := strconv.Atoi(attrs["from_port"])
    61  	if err != nil {
    62  		return nil, fmt.Errorf("Error converting from_port in Security Group migration")
    63  	}
    64  
    65  	perm.ToPort = aws.Int64(int64(tp))
    66  	perm.FromPort = aws.Int64(int64(fp))
    67  	perm.IpProtocol = aws.String(attrs["protocol"])
    68  
    69  	groups := make(map[string]bool)
    70  	if attrs["self"] == "true" {
    71  		groups[attrs["security_group_id"]] = true
    72  	}
    73  
    74  	if attrs["source_security_group_id"] != "" {
    75  		groups[attrs["source_security_group_id"]] = true
    76  	}
    77  
    78  	if len(groups) > 0 {
    79  		perm.UserIdGroupPairs = make([]*ec2.UserIdGroupPair, len(groups))
    80  		// build string list of group name/ids
    81  		var gl []string
    82  		for k, _ := range groups {
    83  			gl = append(gl, k)
    84  		}
    85  
    86  		for i, name := range gl {
    87  			perm.UserIdGroupPairs[i] = &ec2.UserIdGroupPair{
    88  				GroupId: aws.String(name),
    89  			}
    90  		}
    91  	}
    92  
    93  	var cb []string
    94  	for k, v := range attrs {
    95  		if k != "cidr_blocks.#" && strings.HasPrefix(k, "cidr_blocks") {
    96  			cb = append(cb, v)
    97  		}
    98  	}
    99  	if len(cb) > 0 {
   100  		perm.IpRanges = make([]*ec2.IpRange, len(cb))
   101  		for i, v := range cb {
   102  			perm.IpRanges[i] = &ec2.IpRange{CidrIp: aws.String(v)}
   103  		}
   104  	}
   105  
   106  	return &perm, nil
   107  }