Skip to content

Commit acaa887

Browse files
committed
refactor
1 parent 59275d3 commit acaa887

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

test/remote/instance.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
137137
curInst, _ := i.computeService.Instances.Get(i.project, i.zone, newInst.Name).Do()
138138
if curInst != nil {
139139
if !strings.Contains(curInst.MachineType, newInst.MachineType) {
140-
klog.V(4).Infof("Instance machine type doesn't match the required one. Remove instance.")
140+
klog.V(4).Infof("Instance machine type doesn't match the required one. Delete instance.")
141141
if _, err := i.computeService.Instances.Delete(i.project, i.zone, i.name).Do(); err != nil {
142142
return err
143143
}
@@ -153,17 +153,23 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
153153
if err != nil {
154154
return err
155155
}
156+
}
157+
}
156158

157-
if err := insertInstance(i, newInst); err != nil {
158-
return err
159+
if _, err := i.computeService.Instances.Get(i.project, i.zone, newInst.Name).Do(); err != nil {
160+
op, err := i.computeService.Instances.Insert(i.project, i.zone, newInst).Do()
161+
klog.V(4).Infof("Inserted instance %v in project: %v, zone: %v", newInst.Name, i.project, i.zone)
162+
if err != nil {
163+
ret := fmt.Sprintf("could not create instance %s: API error: %v", i.name, err)
164+
if op != nil {
165+
ret = fmt.Sprintf("%s. op error: %v", ret, op.Error)
159166
}
160-
} else {
161-
klog.V(4).Infof("Compute service GOT instance %v, skipping instance creation", newInst.Name)
167+
return errors.New(ret)
168+
} else if op.Error != nil {
169+
return fmt.Errorf("could not create instance %s: %+v", i.name, op.Error)
162170
}
163171
} else {
164-
if err := insertInstance(i, newInst); err != nil {
165-
return err
166-
}
172+
klog.V(4).Infof("Compute service GOT instance %v, skipping instance creation", newInst.Name)
167173
}
168174

169175
then := time.Now()
@@ -205,21 +211,6 @@ func (i *InstanceInfo) CreateOrGetInstance(imageURL, serviceAccount string) erro
205211
return nil
206212
}
207213

208-
func insertInstance(i *InstanceInfo, newInst *compute.Instance) error {
209-
op, err := i.computeService.Instances.Insert(i.project, i.zone, newInst).Do()
210-
klog.V(4).Infof("Inserted instance %v in project: %v, zone: %v", newInst.Name, i.project, i.zone)
211-
if err != nil {
212-
ret := fmt.Sprintf("could not create instance %s: API error: %v", i.name, err)
213-
if op != nil {
214-
ret = fmt.Sprintf("%s. op error: %v", ret, op.Error)
215-
}
216-
return errors.New(ret)
217-
} else if op.Error != nil {
218-
return fmt.Errorf("could not create instance %s: %+v", i.name, op.Error)
219-
}
220-
return nil
221-
}
222-
223214
func (i *InstanceInfo) DeleteInstance() {
224215
klog.V(4).Infof("Deleting instance %q", i.name)
225216
_, err := i.computeService.Instances.Delete(i.project, i.zone, i.name).Do()

0 commit comments

Comments
 (0)