Skip to content

Commit 1ec247e

Browse files
committed
Use new autoscaler / instance group manager APIs.
1 parent c6f0bf4 commit 1ec247e

11 files changed

Lines changed: 136 additions & 285 deletions

builtin/providers/google/config.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import (
1414
"golang.org/x/oauth2"
1515
"golang.org/x/oauth2/google"
1616
"golang.org/x/oauth2/jwt"
17-
"google.golang.org/api/autoscaler/v1beta2"
1817
"google.golang.org/api/compute/v1"
1918
"google.golang.org/api/container/v1"
2019
"google.golang.org/api/dns/v1"
21-
"google.golang.org/api/replicapool/v1beta2"
2220
"google.golang.org/api/storage/v1"
2321
)
2422

@@ -29,11 +27,9 @@ type Config struct {
2927
Project string
3028
Region string
3129

32-
clientAutoscaler *autoscaler.Service
3330
clientCompute *compute.Service
3431
clientContainer *container.Service
3532
clientDns *dns.Service
36-
clientReplicaPool *replicapool.Service
3733
clientStorage *storage.Service
3834
}
3935

@@ -132,20 +128,6 @@ func (c *Config) loadAndValidate() error {
132128
}
133129
c.clientDns.UserAgent = userAgent
134130

135-
log.Printf("[INFO] Instantiating Google Replica Pool client...")
136-
c.clientReplicaPool, err = replicapool.New(client)
137-
if err != nil {
138-
return err
139-
}
140-
c.clientReplicaPool.UserAgent = userAgent
141-
142-
log.Printf("[INFO] Instantiating Google Autoscaler client...")
143-
c.clientAutoscaler, err = autoscaler.New(client)
144-
if err != nil {
145-
return err
146-
}
147-
c.clientAutoscaler.UserAgent = userAgent
148-
149131
log.Printf("[INFO] Instantiating Google Storage Client...")
150132
c.clientStorage, err = storage.New(client)
151133
if err != nil {

builtin/providers/google/operation.go

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"bytes"
55
"fmt"
66

7-
"google.golang.org/api/autoscaler/v1beta2"
87
"google.golang.org/api/compute/v1"
9-
"google.golang.org/api/replicapool/v1beta2"
108
"github.com/hashicorp/terraform/helper/resource"
119
)
1210

@@ -26,8 +24,8 @@ type OperationWaiter struct {
2624
Op *compute.Operation
2725
Project string
2826
Region string
29-
Zone string
3027
Type OperationWaitType
28+
Zone string
3129
}
3230

3331
func (w *OperationWaiter) RefreshFunc() resource.StateRefreshFunc {
@@ -80,95 +78,3 @@ func (e OperationError) Error() string {
8078
return buf.String()
8179
}
8280

83-
// Replicapool Operations
84-
type ReplicaPoolOperationWaiter struct {
85-
Service *replicapool.Service
86-
Op *replicapool.Operation
87-
Project string
88-
Region string
89-
Zone string
90-
}
91-
92-
func (w *ReplicaPoolOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
93-
return func() (interface{}, string, error) {
94-
var op *replicapool.Operation
95-
var err error
96-
97-
op, err = w.Service.ZoneOperations.Get(
98-
w.Project, w.Zone, w.Op.Name).Do()
99-
100-
if err != nil {
101-
return nil, "", err
102-
}
103-
104-
return op, op.Status, nil
105-
}
106-
}
107-
108-
func (w *ReplicaPoolOperationWaiter) Conf() *resource.StateChangeConf {
109-
return &resource.StateChangeConf{
110-
Pending: []string{"PENDING", "RUNNING"},
111-
Target: "DONE",
112-
Refresh: w.RefreshFunc(),
113-
}
114-
}
115-
116-
// ReplicaPoolOperationError wraps replicapool.OperationError and implements the
117-
// error interface so it can be returned.
118-
type ReplicaPoolOperationError replicapool.OperationError
119-
120-
func (e ReplicaPoolOperationError) Error() string {
121-
var buf bytes.Buffer
122-
123-
for _, err := range e.Errors {
124-
buf.WriteString(err.Message + "\n")
125-
}
126-
127-
return buf.String()
128-
}
129-
130-
// Autoscaler Operations
131-
type AutoscalerOperationWaiter struct {
132-
Service *autoscaler.Service
133-
Op *autoscaler.Operation
134-
Project string
135-
Zone string
136-
}
137-
138-
func (w *AutoscalerOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
139-
return func() (interface{}, string, error) {
140-
var op *autoscaler.Operation
141-
var err error
142-
143-
op, err = w.Service.ZoneOperations.Get(
144-
w.Project, w.Zone, w.Op.Name).Do()
145-
146-
if err != nil {
147-
return nil, "", err
148-
}
149-
150-
return op, op.Status, nil
151-
}
152-
}
153-
154-
func (w *AutoscalerOperationWaiter) Conf() *resource.StateChangeConf {
155-
return &resource.StateChangeConf{
156-
Pending: []string{"PENDING", "RUNNING"},
157-
Target: "DONE",
158-
Refresh: w.RefreshFunc(),
159-
}
160-
}
161-
162-
// AutoscalerOperationError wraps autoscaler.OperationError and implements the
163-
// error interface so it can be returned.
164-
type AutoscalerOperationError autoscaler.OperationError
165-
166-
func (e AutoscalerOperationError) Error() string {
167-
var buf bytes.Buffer
168-
169-
for _, err := range e.Errors {
170-
buf.WriteString(err.Message + "\n")
171-
}
172-
173-
return buf.String()
174-
}

builtin/providers/google/provider.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func Provider() terraform.ResourceProvider {
2929
},
3030

3131
ResourcesMap: map[string]*schema.Resource{
32-
"google_autoscaler": resourceAutoscaler(),
32+
"google_compute_autoscaler": resourceComputeAutoscaler(),
3333
"google_compute_address": resourceComputeAddress(),
3434
"google_compute_disk": resourceComputeDisk(),
3535
"google_compute_firewall": resourceComputeFirewall(),
@@ -43,7 +43,7 @@ func Provider() terraform.ResourceProvider {
4343
"google_container_cluster": resourceContainerCluster(),
4444
"google_dns_managed_zone": resourceDnsManagedZone(),
4545
"google_dns_record_set": resourceDnsRecordSet(),
46-
"google_replicapool_instance_group_manager": resourceReplicaPoolInstanceGroupManager(),
46+
"google_compute_instance_group_manager": resourceComputeInstanceGroupManager(),
4747
"google_storage_bucket": resourceStorageBucket(),
4848
},
4949

builtin/providers/google/resource_autoscaler.go renamed to builtin/providers/google/resource_compute_autoscaler.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import (
66
"time"
77

88
"google.golang.org/api/googleapi"
9-
"google.golang.org/api/autoscaler/v1beta2"
9+
"google.golang.org/api/compute/v1"
1010
"github.com/hashicorp/terraform/helper/schema"
1111
)
1212

13-
func resourceAutoscaler() *schema.Resource {
13+
func resourceComputeAutoscaler() *schema.Resource {
1414
return &schema.Resource{
15-
Create: resourceAutoscalerCreate,
16-
Read: resourceAutoscalerRead,
17-
Update: resourceAutoscalerUpdate,
18-
Delete: resourceAutoscalerDelete,
15+
Create: resourceComputeAutoscalerCreate,
16+
Read: resourceComputeAutoscalerRead,
17+
Update: resourceComputeAutoscalerUpdate,
18+
Delete: resourceComputeAutoscalerDelete,
1919

2020
Schema: map[string]*schema.Schema{
2121
"name": &schema.Schema{
@@ -120,10 +120,10 @@ func resourceAutoscaler() *schema.Resource {
120120
}
121121
}
122122

123-
func buildAutoscaler(d *schema.ResourceData) (*autoscaler.Autoscaler, error) {
123+
func buildAutoscaler(d *schema.ResourceData) (*compute.Autoscaler, error) {
124124

125125
// Build the parameter
126-
scaler := &autoscaler.Autoscaler{
126+
scaler := &compute.Autoscaler{
127127
Name: d.Get("name").(string),
128128
Target: d.Get("target").(string),
129129
}
@@ -140,7 +140,7 @@ func buildAutoscaler(d *schema.ResourceData) (*autoscaler.Autoscaler, error) {
140140

141141
prefix := "autoscaling_policy.0."
142142

143-
scaler.AutoscalingPolicy = &autoscaler.AutoscalingPolicy{
143+
scaler.AutoscalingPolicy = &compute.AutoscalingPolicy{
144144
MaxNumReplicas: int64(d.Get(prefix + "max_replicas").(int)),
145145
MinNumReplicas: int64(d.Get(prefix + "min_replicas").(int)),
146146
CoolDownPeriodSec: int64(d.Get(prefix + "cooldown_period").(int)),
@@ -156,7 +156,7 @@ func buildAutoscaler(d *schema.ResourceData) (*autoscaler.Autoscaler, error) {
156156
return nil, fmt.Errorf("The autoscaling_policy must have exactly one cpu_utilization, found %d.", cpuUtilCount)
157157
}
158158
policyCounter++
159-
scaler.AutoscalingPolicy.CpuUtilization = &autoscaler.AutoscalingPolicyCpuUtilization{
159+
scaler.AutoscalingPolicy.CpuUtilization = &compute.AutoscalingPolicyCpuUtilization{
160160
UtilizationTarget: d.Get(prefix + "cpu_utilization.0.target").(float64),
161161
}
162162
}
@@ -168,7 +168,7 @@ func buildAutoscaler(d *schema.ResourceData) (*autoscaler.Autoscaler, error) {
168168
if metricCount != 1 {
169169
return nil, fmt.Errorf("The autoscaling_policy must have exactly one metric, found %d.", metricCount)
170170
}
171-
scaler.AutoscalingPolicy.CustomMetricUtilizations = []*autoscaler.AutoscalingPolicyCustomMetricUtilization{
171+
scaler.AutoscalingPolicy.CustomMetricUtilizations = []*compute.AutoscalingPolicyCustomMetricUtilization{
172172
{
173173
Metric: d.Get(prefix + "metric.0.name").(string),
174174
UtilizationTarget: d.Get(prefix + "metric.0.target").(float64),
@@ -185,7 +185,7 @@ func buildAutoscaler(d *schema.ResourceData) (*autoscaler.Autoscaler, error) {
185185
if lbuCount != 1 {
186186
return nil, fmt.Errorf("The autoscaling_policy must have exactly one load_balancing_utilization, found %d.", lbuCount)
187187
}
188-
scaler.AutoscalingPolicy.LoadBalancingUtilization = &autoscaler.AutoscalingPolicyLoadBalancingUtilization{
188+
scaler.AutoscalingPolicy.LoadBalancingUtilization = &compute.AutoscalingPolicyLoadBalancingUtilization{
189189
UtilizationTarget: d.Get(prefix + "load_balancing_utilization.0.target").(float64),
190190
}
191191
}
@@ -198,7 +198,7 @@ func buildAutoscaler(d *schema.ResourceData) (*autoscaler.Autoscaler, error) {
198198
return scaler, nil
199199
}
200200

201-
func resourceAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
201+
func resourceComputeAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
202202
config := meta.(*Config)
203203

204204
// Get the zone
@@ -215,7 +215,7 @@ func resourceAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
215215
return err
216216
}
217217

218-
op, err := config.clientAutoscaler.Autoscalers.Insert(
218+
op, err := config.clientCompute.Autoscalers.Insert(
219219
config.Project, zone.Name, scaler).Do()
220220
if err != nil {
221221
return fmt.Errorf("Error creating Autoscaler: %s", err)
@@ -225,10 +225,11 @@ func resourceAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
225225
d.SetId(scaler.Name)
226226

227227
// Wait for the operation to complete
228-
w := &AutoscalerOperationWaiter{
229-
Service: config.clientAutoscaler,
228+
w := &OperationWaiter{
229+
Service: config.clientCompute,
230230
Op: op,
231231
Project: config.Project,
232+
Type: OperationWaitZone,
232233
Zone: zone.Name,
233234
}
234235
state := w.Conf()
@@ -238,23 +239,23 @@ func resourceAutoscalerCreate(d *schema.ResourceData, meta interface{}) error {
238239
if err != nil {
239240
return fmt.Errorf("Error waiting for Autoscaler to create: %s", err)
240241
}
241-
op = opRaw.(*autoscaler.Operation)
242+
op = opRaw.(*compute.Operation)
242243
if op.Error != nil {
243244
// The resource didn't actually create
244245
d.SetId("")
245246

246247
// Return the error
247-
return AutoscalerOperationError(*op.Error)
248+
return OperationError(*op.Error)
248249
}
249250

250-
return resourceAutoscalerRead(d, meta)
251+
return resourceComputeAutoscalerRead(d, meta)
251252
}
252253

253-
func resourceAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
254+
func resourceComputeAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
254255
config := meta.(*Config)
255256

256257
zone := d.Get("zone").(string)
257-
scaler, err := config.clientAutoscaler.Autoscalers.Get(
258+
scaler, err := config.clientCompute.Autoscalers.Get(
258259
config.Project, zone, d.Id()).Do()
259260
if err != nil {
260261
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
@@ -272,7 +273,7 @@ func resourceAutoscalerRead(d *schema.ResourceData, meta interface{}) error {
272273
return nil
273274
}
274275

275-
func resourceAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
276+
func resourceComputeAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
276277
config := meta.(*Config)
277278

278279
zone := d.Get("zone").(string)
@@ -282,7 +283,7 @@ func resourceAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
282283
return err
283284
}
284285

285-
op, err := config.clientAutoscaler.Autoscalers.Patch(
286+
op, err := config.clientCompute.Autoscalers.Patch(
286287
config.Project, zone, d.Id(), scaler).Do()
287288
if err != nil {
288289
return fmt.Errorf("Error updating Autoscaler: %s", err)
@@ -292,10 +293,11 @@ func resourceAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
292293
d.SetId(scaler.Name)
293294

294295
// Wait for the operation to complete
295-
w := &AutoscalerOperationWaiter{
296-
Service: config.clientAutoscaler,
296+
w := &OperationWaiter{
297+
Service: config.clientCompute,
297298
Op: op,
298299
Project: config.Project,
300+
Type: OperationWaitZone,
299301
Zone: zone,
300302
}
301303
state := w.Conf()
@@ -305,30 +307,31 @@ func resourceAutoscalerUpdate(d *schema.ResourceData, meta interface{}) error {
305307
if err != nil {
306308
return fmt.Errorf("Error waiting for Autoscaler to update: %s", err)
307309
}
308-
op = opRaw.(*autoscaler.Operation)
310+
op = opRaw.(*compute.Operation)
309311
if op.Error != nil {
310312
// Return the error
311-
return AutoscalerOperationError(*op.Error)
313+
return OperationError(*op.Error)
312314
}
313315

314-
return resourceAutoscalerRead(d, meta)
316+
return resourceComputeAutoscalerRead(d, meta)
315317
}
316318

317-
func resourceAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
319+
func resourceComputeAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
318320
config := meta.(*Config)
319321

320322
zone := d.Get("zone").(string)
321-
op, err := config.clientAutoscaler.Autoscalers.Delete(
323+
op, err := config.clientCompute.Autoscalers.Delete(
322324
config.Project, zone, d.Id()).Do()
323325
if err != nil {
324326
return fmt.Errorf("Error deleting autoscaler: %s", err)
325327
}
326328

327329
// Wait for the operation to complete
328-
w := &AutoscalerOperationWaiter{
329-
Service: config.clientAutoscaler,
330+
w := &OperationWaiter{
331+
Service: config.clientCompute,
330332
Op: op,
331333
Project: config.Project,
334+
Type: OperationWaitZone,
332335
Zone: zone,
333336
}
334337
state := w.Conf()
@@ -338,10 +341,10 @@ func resourceAutoscalerDelete(d *schema.ResourceData, meta interface{}) error {
338341
if err != nil {
339342
return fmt.Errorf("Error waiting for Autoscaler to delete: %s", err)
340343
}
341-
op = opRaw.(*autoscaler.Operation)
344+
op = opRaw.(*compute.Operation)
342345
if op.Error != nil {
343346
// Return the error
344-
return AutoscalerOperationError(*op.Error)
347+
return OperationError(*op.Error)
345348
}
346349

347350
d.SetId("")

0 commit comments

Comments
 (0)