Skip to content

Commit

Permalink
[WIP] Save/load instance groups with project, members
Browse files Browse the repository at this point in the history
  • Loading branch information
minimum2scp committed Feb 22, 2017
1 parent bf3997d commit dbde979
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,16 @@ type configRoot struct {
type cache struct {
CacheDir string
Instances []*compute.Instance
InstanceGroups []*compute.InstanceGroup
InstanceGroups []*instanceGroupExt
Projects []*cloudresourcemanager.Project
}

type instanceGroupExt struct {
InstanceGroup *compute.InstanceGroup
Project *cloudresourcemanager.Project
Members []*compute.Instance
}

func loadCache() (*cache, error) {
cachedir, err := homedir.Expand("~/.cache/geco/")
if err != nil {
Expand Down Expand Up @@ -271,11 +277,11 @@ func doCache(cliCtx *cli.Context) {
}

// gcloud compute instance-groups list (in parallel)
instanceGroupNotify := make(chan []*compute.InstanceGroup)
instanceGroupNotify := make(chan []*instanceGroupExt)
for _, project := range c.Projects {
go func(project *cloudresourcemanager.Project, notify chan<- []*compute.InstanceGroup) {
go func(project *cloudresourcemanager.Project, notify chan<- []*instanceGroupExt) {
semaphore <- 0
var instanceGroups []*compute.InstanceGroup
var ret []*instanceGroupExt

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

for _, instanceGroupsScopedList := range res.Items {
instanceGroups = append(instanceGroups, instanceGroupsScopedList.InstanceGroups...)
for _, instanceGroup := range instanceGroupsScopedList.InstanceGroups {
ret = append(ret, &instanceGroupExt{
InstanceGroup: instanceGroup,
Project: project,
})
}
}

if res.NextPageToken != "" {
Expand All @@ -310,9 +321,9 @@ func doCache(cliCtx *cli.Context) {
}

<-semaphore
notify <- instanceGroups
notify <- ret

log.Printf("loaded instances in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(instanceGroups))
log.Printf("loaded instance groups in %s (%s), %d instances found.\n", project.Name, project.ProjectId, len(ret))
}(project, instanceGroupNotify)
}
for _ = range c.Projects {
Expand All @@ -322,16 +333,13 @@ func doCache(cliCtx *cli.Context) {
}
}

// sort projects, instances, instance-groups
// sort projects, instances
sort.Slice(c.Projects, func(i, j int) bool {
return c.Projects[i].ProjectId < c.Projects[j].ProjectId
})
sort.Slice(c.Instances, func(i, j int) bool {
return c.Instances[i].SelfLink < c.Instances[j].SelfLink
})
sort.Slice(c.InstanceGroups, func(i, j int) bool {
return c.InstanceGroups[i].SelfLink < c.InstanceGroups[j].SelfLink
})

saveCache(c)
log.Println("saved cache.")
Expand Down

0 comments on commit dbde979

Please sign in to comment.