github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/google/resource_container_node_pool.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/helper/schema" 9 "google.golang.org/api/container/v1" 10 "google.golang.org/api/googleapi" 11 ) 12 13 func resourceContainerNodePool() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceContainerNodePoolCreate, 16 Read: resourceContainerNodePoolRead, 17 Delete: resourceContainerNodePoolDelete, 18 Exists: resourceContainerNodePoolExists, 19 20 Schema: map[string]*schema.Schema{ 21 "project": &schema.Schema{ 22 Type: schema.TypeString, 23 Optional: true, 24 ForceNew: true, 25 }, 26 27 "name": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 Computed: true, 31 ConflictsWith: []string{"name_prefix"}, 32 ForceNew: true, 33 }, 34 35 "name_prefix": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 41 "zone": &schema.Schema{ 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 }, 46 47 "cluster": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 ForceNew: true, 51 }, 52 53 "initial_node_count": &schema.Schema{ 54 Type: schema.TypeInt, 55 Required: true, 56 ForceNew: true, 57 }, 58 }, 59 } 60 } 61 62 func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) error { 63 config := meta.(*Config) 64 65 project, err := getProject(d, config) 66 if err != nil { 67 return err 68 } 69 70 zone := d.Get("zone").(string) 71 cluster := d.Get("cluster").(string) 72 nodeCount := d.Get("initial_node_count").(int) 73 74 var name string 75 if v, ok := d.GetOk("name"); ok { 76 name = v.(string) 77 } else if v, ok := d.GetOk("name_prefix"); ok { 78 name = resource.PrefixedUniqueId(v.(string)) 79 } else { 80 name = resource.UniqueId() 81 } 82 83 nodePool := &container.NodePool{ 84 Name: name, 85 InitialNodeCount: int64(nodeCount), 86 } 87 88 req := &container.CreateNodePoolRequest{ 89 NodePool: nodePool, 90 } 91 92 op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Create(project, zone, cluster, req).Do() 93 94 if err != nil { 95 return fmt.Errorf("Error creating NodePool: %s", err) 96 } 97 98 waitErr := containerOperationWait(config, op, project, zone, "creating GKE NodePool", 10, 3) 99 if waitErr != nil { 100 // The resource didn't actually create 101 d.SetId("") 102 return waitErr 103 } 104 105 log.Printf("[INFO] GKE NodePool %s has been created", name) 106 107 d.SetId(name) 108 109 return resourceContainerNodePoolRead(d, meta) 110 } 111 112 func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) error { 113 config := meta.(*Config) 114 115 project, err := getProject(d, config) 116 if err != nil { 117 return err 118 } 119 120 zone := d.Get("zone").(string) 121 name := d.Get("name").(string) 122 cluster := d.Get("cluster").(string) 123 124 nodePool, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Get( 125 project, zone, cluster, name).Do() 126 if err != nil { 127 return fmt.Errorf("Error reading NodePool: %s", err) 128 } 129 130 d.Set("name", nodePool.Name) 131 d.Set("initial_node_count", nodePool.InitialNodeCount) 132 133 return nil 134 } 135 136 func resourceContainerNodePoolDelete(d *schema.ResourceData, meta interface{}) error { 137 config := meta.(*Config) 138 139 project, err := getProject(d, config) 140 if err != nil { 141 return err 142 } 143 144 zone := d.Get("zone").(string) 145 name := d.Get("name").(string) 146 cluster := d.Get("cluster").(string) 147 148 op, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Delete( 149 project, zone, cluster, name).Do() 150 if err != nil { 151 return fmt.Errorf("Error deleting NodePool: %s", err) 152 } 153 154 // Wait until it's deleted 155 waitErr := containerOperationWait(config, op, project, zone, "deleting GKE NodePool", 10, 2) 156 if waitErr != nil { 157 return waitErr 158 } 159 160 log.Printf("[INFO] GKE NodePool %s has been deleted", d.Id()) 161 162 d.SetId("") 163 164 return nil 165 } 166 167 func resourceContainerNodePoolExists(d *schema.ResourceData, meta interface{}) (bool, error) { 168 config := meta.(*Config) 169 170 project, err := getProject(d, config) 171 if err != nil { 172 return false, err 173 } 174 175 zone := d.Get("zone").(string) 176 name := d.Get("name").(string) 177 cluster := d.Get("cluster").(string) 178 179 _, err = config.clientContainer.Projects.Zones.Clusters.NodePools.Get( 180 project, zone, cluster, name).Do() 181 if err != nil { 182 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 183 log.Printf("[WARN] Removing Container NodePool %q because it's gone", name) 184 // The resource doesn't exist anymore 185 return false, err 186 } 187 // There was some other error in reading the resource 188 return true, err 189 } 190 return true, nil 191 }