Skip to content

Commit 3656e6d

Browse files
committed
[WIP] Save/load instance groups with project, members
1 parent bf3997d commit 3656e6d

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

commands.go

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ type configRoot struct {
8686
type cache struct {
8787
CacheDir string
8888
Instances []*compute.Instance
89-
InstanceGroups []*compute.InstanceGroup
89+
InstanceGroups []*instanceGroupExt
9090
Projects []*cloudresourcemanager.Project
9191
}
9292

93+
type instanceGroupExt struct {
94+
InstanceGroup *compute.InstanceGroup
95+
Project *cloudresourcemanager.Project
96+
Members []*compute.Instance
97+
}
98+
9399
func loadCache() (*cache, error) {
94100
cachedir, err := homedir.Expand("~/.cache/geco/")
95101
if err != nil {
@@ -182,6 +188,7 @@ func doCache(cliCtx *cli.Context) {
182188
}
183189
c.Projects = []*cloudresourcemanager.Project{}
184190
c.Instances = []*compute.Instance{}
191+
c.InstanceGroups = []*instanceGroupExt{}
185192

186193
ctx := oauth2.NoContext
187194
scopes := []string{compute.ComputeReadonlyScope}
@@ -271,11 +278,11 @@ func doCache(cliCtx *cli.Context) {
271278
}
272279

273280
// gcloud compute instance-groups list (in parallel)
274-
instanceGroupNotify := make(chan []*compute.InstanceGroup)
281+
instanceGroupNotify := make(chan []*instanceGroupExt)
275282
for _, project := range c.Projects {
276-
go func(project *cloudresourcemanager.Project, notify chan<- []*compute.InstanceGroup) {
283+
go func(project *cloudresourcemanager.Project, notify chan<- []*instanceGroupExt) {
277284
semaphore <- 0
278-
var instanceGroups []*compute.InstanceGroup
285+
var ret []*instanceGroupExt
279286

280287
log.Printf("loading instance groups in %s (%s)...\n", project.Name, project.ProjectId)
281288
service, err := compute.New(client)
@@ -298,7 +305,12 @@ func doCache(cliCtx *cli.Context) {
298305
}
299306

300307
for _, instanceGroupsScopedList := range res.Items {
301-
instanceGroups = append(instanceGroups, instanceGroupsScopedList.InstanceGroups...)
308+
for _, instanceGroup := range instanceGroupsScopedList.InstanceGroups {
309+
ret = append(ret, &instanceGroupExt{
310+
InstanceGroup: instanceGroup,
311+
Project: project,
312+
})
313+
}
302314
}
303315

304316
if res.NextPageToken != "" {
@@ -310,9 +322,9 @@ func doCache(cliCtx *cli.Context) {
310322
}
311323

312324
<-semaphore
313-
notify <- instanceGroups
325+
notify <- ret
314326

315-
log.Printf("loaded instances in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(instanceGroups))
327+
log.Printf("loaded instance groups in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(ret))
316328
}(project, instanceGroupNotify)
317329
}
318330
for _ = range c.Projects {
@@ -322,16 +334,13 @@ func doCache(cliCtx *cli.Context) {
322334
}
323335
}
324336

325-
// sort projects, instances, instance-groups
337+
// sort projects, instances
326338
sort.Slice(c.Projects, func(i, j int) bool {
327339
return c.Projects[i].ProjectId < c.Projects[j].ProjectId
328340
})
329341
sort.Slice(c.Instances, func(i, j int) bool {
330342
return c.Instances[i].SelfLink < c.Instances[j].SelfLink
331343
})
332-
sort.Slice(c.InstanceGroups, func(i, j int) bool {
333-
return c.InstanceGroups[i].SelfLink < c.InstanceGroups[j].SelfLink
334-
})
335344

336345
saveCache(c)
337346
log.Println("saved cache.")

0 commit comments

Comments
 (0)