github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_google_project_migrate.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  func resourceGoogleProjectMigrateState(v int, s *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
    11  	if s.Empty() {
    12  		log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
    13  		return s, nil
    14  	}
    15  
    16  	switch v {
    17  	case 0:
    18  		log.Println("[INFO] Found Google Project State v0; migrating to v1")
    19  		s, err := migrateGoogleProjectStateV0toV1(s, meta.(*Config))
    20  		if err != nil {
    21  			return s, err
    22  		}
    23  		return s, nil
    24  	default:
    25  		return s, fmt.Errorf("Unexpected schema version: %d", v)
    26  	}
    27  }
    28  
    29  // This migration adjusts google_project resources to include several additional attributes
    30  // required to support project creation/deletion that was added in V1.
    31  func migrateGoogleProjectStateV0toV1(s *terraform.InstanceState, config *Config) (*terraform.InstanceState, error) {
    32  	log.Printf("[DEBUG] Attributes before migration: %#v", s.Attributes)
    33  
    34  	s.Attributes["skip_delete"] = "true"
    35  	s.Attributes["project_id"] = s.ID
    36  
    37  	if s.Attributes["policy_data"] != "" {
    38  		p, err := getProjectIamPolicy(s.ID, config)
    39  		if err != nil {
    40  			return s, fmt.Errorf("Could not retrieve project's IAM policy while attempting to migrate state from V0 to V1: %v", err)
    41  		}
    42  		s.Attributes["policy_etag"] = p.Etag
    43  	}
    44  
    45  	log.Printf("[DEBUG] Attributes after migration: %#v", s.Attributes)
    46  	return s, nil
    47  }