Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Update to new API and refactor that shit
Browse files Browse the repository at this point in the history
  • Loading branch information
fionera committed Jun 24, 2020
1 parent 6e2b89c commit a0e478e
Show file tree
Hide file tree
Showing 43 changed files with 708 additions and 833 deletions.
91 changes: 91 additions & 0 deletions api/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package api

import (
"context"

"github.com/pkg/errors"
"golang.org/x/oauth2/jwt"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
)

func NewAdminService(jwtConfig *jwt.Config) (*admin.Service, error) {
jwtConfig.Scopes = append(jwtConfig.Scopes, admin.AdminDirectoryGroupScope)
return admin.NewService(context.TODO(), option.WithTokenSource(jwtConfig.TokenSource(context.TODO())))
}

func ListMembers(a *admin.Service, groupAddress string) ([]*admin.Member, error) {
var members []*admin.Member

err := a.Members.List(groupAddress).Pages(context.Background(), func(list *admin.Members) error {
members = append(members, list.Members...)

return nil
})
if err != nil {
return nil, errors.Errorf("Error listing group members: %s", err)
}

return members, nil
}

func AddMember(a *admin.Service, groupAddress, memberAddress string) (*admin.Member, error) {
return a.Members.Insert(groupAddress, &admin.Member{
Email: memberAddress,
DeliverySettings: "NONE",
}).Do()
}

func RemoveMember(a *admin.Service, groupAddress, memberAddress string) error {
err := a.Members.Delete(groupAddress, memberAddress).Do()

if err != nil {
return errors.Errorf("Error removing group member: %s", err)
}

return nil
}

func CreateGroup(a *admin.Service, name, address string) (*admin.Group, error) {
group, err := a.Groups.Insert(&admin.Group{
Name: name,
Email: address,
Description: "Created by TeamDriveManager",
}).Do()
if err != nil {
return nil, errors.Errorf("Error creating group: %s", err)
}

return group, nil
}

func ListGroups(a *admin.Service, domain string) ([]*admin.Group, error) {
var groups []*admin.Group

err := a.Groups.List().Domain(domain).Pages(context.Background(), func(list *admin.Groups) error {
groups = append(groups, list.Groups...)

return nil
})

if err != nil {
return nil, errors.Errorf("Error listing groups: %s", err)
}

return groups, nil
}

// GroupExists needs the full email address as parameter and returns true if it can find a group for it
func GroupExists(a *admin.Service, address string) (bool, error) {
_, err := a.Groups.Get(address).Do()
if err != nil {
if err.(*googleapi.Error).Code == 404 {
return false, nil
}

return false, err
}

return true, nil
}
28 changes: 0 additions & 28 deletions api/admin/admin.go

This file was deleted.

52 changes: 0 additions & 52 deletions api/admin/group.go

This file was deleted.

46 changes: 0 additions & 46 deletions api/admin/member.go

This file was deleted.

12 changes: 4 additions & 8 deletions api/client.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package api

import (
"context"
"io/ioutil"
"net/http"
"os"

"github.com/mitchellh/go-homedir"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
)

func CreateClient(serviceAccountFile, impersonate string, scopes []string) (*http.Client, error) {
func NewTokenSource(serviceAccountFile, impersonate string) (*jwt.Config, error) {
path, err := homedir.Expand(os.ExpandEnv(serviceAccountFile))
if err != nil {
return nil, err
Expand All @@ -22,7 +20,7 @@ func CreateClient(serviceAccountFile, impersonate string, scopes []string) (*htt
return nil, err
}

conf, err := google.JWTConfigFromJSON(loadedCreds, scopes...)
conf, err := google.JWTConfigFromJSON(loadedCreds)
if err != nil {
return nil, err
}
Expand All @@ -31,7 +29,5 @@ func CreateClient(serviceAccountFile, impersonate string, scopes []string) (*htt
conf.Subject = impersonate
}

ctx := context.Background()

return oauth2.NewClient(ctx, conf.TokenSource(ctx)), nil
return conf, nil
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package cloudresourcemanager
package api

import (
"context"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/option"
)

func (a *Api) CreateProject(projectId, organization string) error {
projectCreation, err := a.crm.Projects.Create(&cloudresourcemanager.Project{
func NewCloudResourceManagerService(jwtConfig *jwt.Config) (*cloudresourcemanager.Service, error) {
jwtConfig.Scopes = append(jwtConfig.Scopes, cloudresourcemanager.CloudPlatformScope)
return cloudresourcemanager.NewService(context.Background(), option.WithTokenSource(jwtConfig.TokenSource(context.TODO())))
}

func CreateProject(crm *cloudresourcemanager.Service, projectId, organization string) error {
projectCreation, err := crm.Projects.Create(&cloudresourcemanager.Project{
ProjectId: projectId,
Name: projectId,
Parent: &cloudresourcemanager.ResourceId{
Expand All @@ -25,7 +32,7 @@ func (a *Api) CreateProject(projectId, organization string) error {
logrus.Infof("Creating Project %s", projectId)

for {
operation, err := a.crm.Operations.Get(projectCreation.Name).Do()
operation, err := crm.Operations.Get(projectCreation.Name).Do()
if err != nil {
logrus.Panic(err)
return err
Expand All @@ -43,9 +50,9 @@ func (a *Api) CreateProject(projectId, organization string) error {
return nil
}

func (a *Api) ListProjects(organization string) ([]*cloudresourcemanager.Project, error) {
func ListProjects(crm *cloudresourcemanager.Service, organization string) ([]*cloudresourcemanager.Project, error) {
var projects []*cloudresourcemanager.Project
err := a.crm.Projects.List().Pages(context.Background(), func(list *cloudresourcemanager.ListProjectsResponse) error {
err := crm.Projects.List().Pages(context.Background(), func(list *cloudresourcemanager.ListProjectsResponse) error {
projects = append(projects, list.Projects...)

return nil
Expand All @@ -57,8 +64,8 @@ func (a *Api) ListProjects(organization string) ([]*cloudresourcemanager.Project
return projects, nil
}

func (a *Api) DeleteProject(projectId string) error {
_, err := a.crm.Projects.Delete(projectId).Do()
func DeleteProject(crm *cloudresourcemanager.Service, projectId string) error {
_, err := crm.Projects.Delete(projectId).Do()

if err != nil {
return errors.Errorf("Error listing projects: %s", err)
Expand Down
29 changes: 0 additions & 29 deletions api/cloudresourcemanager/cloudresourcemanager.go

This file was deleted.

Loading

0 comments on commit a0e478e

Please sign in to comment.