Skip to content

Commit

Permalink
feat: added some resources tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ArshiaBP committed Dec 1, 2024
1 parent b431ec0 commit 4312860
Show file tree
Hide file tree
Showing 10 changed files with 663 additions and 133 deletions.
6 changes: 5 additions & 1 deletion provider/configs/credentials.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package configs

import "encoding/json"

type IntegrationCredentials struct {
Token string `json:"token"`
AdminEmail string `json:"admin_email"`
CustomerID string `json:"customer_id"`
KeyFile json.RawMessage `json:"key_file"`
}
1 change: 1 addition & 0 deletions provider/describer/directory_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package describer
103 changes: 103 additions & 0 deletions provider/describer/group_members.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package describer

import (
"context"
"fmt"
"github.com/opengovern/og-describer-googleworkspace/pkg/sdk/models"
"github.com/opengovern/og-describer-googleworkspace/provider/model"
admin "google.golang.org/api/admin/directory/v1"
"sync"
)

func ListGroupMembers(ctx context.Context, handler *GoogleWorkspaceAPIHandler, stream *models.StreamSender) ([]models.Resource, error) {
var wg sync.WaitGroup
GoogleWorkspaceChan := make(chan models.Resource)
errorChan := make(chan error, 1) // Buffered channel to capture errors

groups, err := getGroups(ctx, handler)
if err != nil {
return nil, err
}

go func() {
defer close(GoogleWorkspaceChan)
defer close(errorChan)
for _, group := range groups {
if err = processGroupMembers(ctx, handler, group.Id, GoogleWorkspaceChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
}
}
wg.Wait()
}()

var values []models.Resource
for {
select {
case value, ok := <-GoogleWorkspaceChan:
if !ok {
return values, nil
}
if stream != nil {
if err = (*stream)(value); err != nil {
return nil, err
}
} else {
values = append(values, value)
}
case err = <-errorChan:
return nil, err
}
}
}

func processGroupMembers(ctx context.Context, handler *GoogleWorkspaceAPIHandler, groupID string, openaiChan chan<- models.Resource, wg *sync.WaitGroup) error {
var groupMembers []*admin.Member
var groupMembersResp *admin.Members
pageToken := ""

for {
req := handler.Service.Members.List(groupID).MaxResults(MaxPageResultsGroupMembers)
if pageToken != "" {
req.PageToken(pageToken)
}

requestFunc := func() (*int, error) {
var e error
groupMembersResp, e = req.Do()
if e != nil {
return nil, fmt.Errorf("request execution failed: %w", e)
}

groupMembers = append(groupMembers, groupMembersResp.Members...)
return &groupMembersResp.HTTPStatusCode, nil
}

err := handler.DoRequest(ctx, requestFunc)
if err != nil {
return fmt.Errorf("error during request handling: %w", err)
}

if groupMembersResp.NextPageToken == "" {
break
}
pageToken = groupMembersResp.NextPageToken
}

for _, groupMember := range groupMembers {
wg.Add(1)
go func(member *admin.Member) {
defer wg.Done()
value := models.Resource{
ID: member.Id,
Name: member.Email,
Description: JSONAllFieldsMarshaller{
Value: model.GroupMemberDescription{
Member: *member,
},
},
}
openaiChan <- value
}(groupMember)
}
return nil
}
143 changes: 143 additions & 0 deletions provider/describer/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package describer

import (
"context"
"errors"
"fmt"
"github.com/opengovern/og-describer-googleworkspace/pkg/sdk/models"
"github.com/opengovern/og-describer-googleworkspace/provider/model"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/googleapi"
"net/http"
"sync"
)

func ListGroups(ctx context.Context, handler *GoogleWorkspaceAPIHandler, stream *models.StreamSender) ([]models.Resource, error) {
var wg sync.WaitGroup
GoogleWorkspaceChan := make(chan models.Resource)
errorChan := make(chan error, 1) // Buffered channel to capture errors

go func() {
defer close(GoogleWorkspaceChan)
defer close(errorChan)
if err := processGroups(ctx, handler, GoogleWorkspaceChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
}
wg.Wait()
}()

var values []models.Resource
for {
select {
case value, ok := <-GoogleWorkspaceChan:
if !ok {
return values, nil
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
} else {
values = append(values, value)
}
case err := <-errorChan:
return nil, err
}
}
}

func GetGroup(ctx context.Context, handler *GoogleWorkspaceAPIHandler, resourceID string) (*models.Resource, error) {
group, err := processGroup(ctx, handler, resourceID)
if err != nil {
return nil, err
}
value := models.Resource{
ID: group.Id,
Name: group.Name,
Description: JSONAllFieldsMarshaller{
Value: model.GroupDescription{
Group: *group,
},
},
}
return &value, nil
}

func processGroups(ctx context.Context, handler *GoogleWorkspaceAPIHandler, openaiChan chan<- models.Resource, wg *sync.WaitGroup) error {
var groups []*admin.Group
var groupsResp *admin.Groups
pageToken := ""

for {
req := handler.Service.Groups.List().Customer(handler.CustomerID).MaxResults(MaxPageResultsGroups)
if pageToken != "" {
req.PageToken(pageToken)
}

requestFunc := func() (*int, error) {
var e error
groupsResp, e = req.Do()
if e != nil {
return nil, fmt.Errorf("request execution failed: %w", e)
}

groups = append(groups, groupsResp.Groups...)
return &groupsResp.HTTPStatusCode, nil
}

err := handler.DoRequest(ctx, requestFunc)
if err != nil {
return fmt.Errorf("error during request handling: %w", err)
}

if groupsResp.NextPageToken == "" {
break
}
pageToken = groupsResp.NextPageToken
}

for _, group := range groups {
wg.Add(1)
go func(group *admin.Group) {
defer wg.Done()
value := models.Resource{
ID: group.Id,
Name: group.Name,
Description: JSONAllFieldsMarshaller{
Value: model.GroupDescription{
Group: *group,
},
},
}
openaiChan <- value
}(group)
}
return nil
}

func processGroup(ctx context.Context, handler *GoogleWorkspaceAPIHandler, resourceID string) (*admin.Group, error) {
var group *admin.Group
var status *int

req := handler.Service.Groups.Get(resourceID)

requestFunc := func() (*int, error) {
var e error
group, e = req.Do()
if e != nil {
var apiErr *googleapi.Error
if errors.As(e, &apiErr) {
*status = apiErr.Code
}
return status, fmt.Errorf("request execution failed: %w", e)
}
*status = http.StatusOK
return status, e
}

err := handler.DoRequest(ctx, requestFunc)
if err != nil {
return nil, fmt.Errorf("error during request handling: %w", err)
}
return group, nil
}
131 changes: 131 additions & 0 deletions provider/describer/org_units.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package describer

import (
"context"
"errors"
"fmt"
"github.com/opengovern/og-describer-googleworkspace/pkg/sdk/models"
"github.com/opengovern/og-describer-googleworkspace/provider/model"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/googleapi"
"net/http"
"sync"
)

func ListOrgUnits(ctx context.Context, handler *GoogleWorkspaceAPIHandler, stream *models.StreamSender) ([]models.Resource, error) {
var wg sync.WaitGroup
GoogleWorkspaceChan := make(chan models.Resource)
errorChan := make(chan error, 1) // Buffered channel to capture errors

go func() {
defer close(GoogleWorkspaceChan)
defer close(errorChan)
if err := processOrgUnits(ctx, handler, GoogleWorkspaceChan, &wg); err != nil {
errorChan <- err // Send error to the error channel
}
wg.Wait()
}()

var values []models.Resource
for {
select {
case value, ok := <-GoogleWorkspaceChan:
if !ok {
return values, nil
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
} else {
values = append(values, value)
}
case err := <-errorChan:
return nil, err
}
}
}

func GetOrgUnit(ctx context.Context, handler *GoogleWorkspaceAPIHandler, resourceID string) (*models.Resource, error) {
orgUnit, err := processOrgUnit(ctx, handler, resourceID)
if err != nil {
return nil, err
}
value := models.Resource{
ID: orgUnit.OrgUnitId,
Name: orgUnit.Name,
Description: JSONAllFieldsMarshaller{
Value: model.OrgUnitDescription{
OrgUnit: *orgUnit,
},
},
}
return &value, nil
}

func processOrgUnits(ctx context.Context, handler *GoogleWorkspaceAPIHandler, openaiChan chan<- models.Resource, wg *sync.WaitGroup) error {
var orgUnits []*admin.OrgUnit
var orgUnitsResp *admin.OrgUnits
req := handler.Service.Orgunits.List(handler.CustomerID)

requestFunc := func() (*int, error) {
var e error
orgUnitsResp, e = req.Do()
if e != nil {
return nil, fmt.Errorf("request execution failed: %w", e)
}

orgUnits = append(orgUnits, orgUnitsResp.OrganizationUnits...)
return &orgUnitsResp.HTTPStatusCode, nil
}

err := handler.DoRequest(ctx, requestFunc)
if err != nil {
return fmt.Errorf("error during request handling: %w", err)
}

for _, orgUnit := range orgUnits {
wg.Add(1)
go func(orgUnit *admin.OrgUnit) {
defer wg.Done()
value := models.Resource{
ID: orgUnit.OrgUnitId,
Name: orgUnit.Name,
Description: JSONAllFieldsMarshaller{
Value: model.OrgUnitDescription{
OrgUnit: *orgUnit,
},
},
}
openaiChan <- value
}(orgUnit)
}
return nil
}

func processOrgUnit(ctx context.Context, handler *GoogleWorkspaceAPIHandler, resourceID string) (*admin.OrgUnit, error) {
var orgUnit *admin.OrgUnit
var status *int

req := handler.Service.Orgunits.Get(handler.CustomerID, resourceID)

requestFunc := func() (*int, error) {
var e error
orgUnit, e = req.Do()
if e != nil {
var apiErr *googleapi.Error
if errors.As(e, &apiErr) {
*status = apiErr.Code
}
return status, fmt.Errorf("request execution failed: %w", e)
}
*status = http.StatusOK
return status, e
}

err := handler.DoRequest(ctx, requestFunc)
if err != nil {
return nil, fmt.Errorf("error during request handling: %w", err)
}
return orgUnit, nil
}
Loading

0 comments on commit 4312860

Please sign in to comment.