Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 0 additions & 62 deletions collector/gcp/collector/admin/group.go

This file was deleted.

17 changes: 11 additions & 6 deletions collector/gcp/collector/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,17 @@ func (s *Services) InitServices(cloudAccountParam schema.CloudAccountParam) (err
log.CtxLogger(ctx).Warn("Failed to create sql admin client:", zap.Error(err))
}
s.CloudSQL = svc
//case GoogleGroup, User:
// svc, err := admin.NewService(ctx, clientOption)
// if err != nil {
// log.GetWLogger().Warn(fmt.Sprintf("Failed to create cloud identity client: %v", err))
// }
// s.Admin = svc
case GoogleGroup:
svc, err := admin.NewService(ctx, clientOption)
if err != nil {
log.GetWLogger().Warn(fmt.Sprintf("Failed to create admin client: %v", err))
}
s.Admin = svc

s.OrganizationsClient, err = resourcemanager.NewOrganizationsClient(ctx, clientOption)
if err != nil {
log.GetWLogger().Warn(fmt.Sprintf("Failed to create Organizations client: %v", err))
}
}

return nil
Expand Down
99 changes: 99 additions & 0 deletions collector/gcp/collector/workspace/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package workspace

import (
"cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/cloudrec/gcp/collector"
"github.com/cloudrec/gcp/collector/cloudresourcemanager"
"github.com/core-sdk/constant"
"github.com/core-sdk/log"
"github.com/core-sdk/schema"
"github.com/yalp/jsonpath"
"go.uber.org/zap"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/iterator"
)

func GetGroupResource() schema.Resource {
return schema.Resource{
ResourceType: collector.GoogleGroup,
ResourceTypeName: collector.GoogleGroup,
ResourceGroupType: constant.IDENTITY,
Desc: ``,
ResourceDetailFunc: func(ctx context.Context, service schema.ServiceInterface, res chan<- any) error {
svc := service.(*collector.Services).Admin
for organization, err := range cloudresourcemanager.SearchOrganizations(ctx, service.(*collector.Services).OrganizationsClient) {
if errors.Is(err, iterator.Done) {
log.CtxLogger(ctx).Warn("SearchOrganizations error", zap.Error(fmt.Errorf("get 0 organization")))
return err
}
if err != nil {
log.CtxLogger(ctx).Warn("SearchOrganizations error", zap.Error(err))
return err
}

customerId, ok := getCustomerId(ctx, organization).(string)
if ok {
if err = svc.Groups.List().Customer(customerId).Pages(ctx, func(resp *admin.Groups) error {
for _, group := range resp.Groups {
res <- GroupDetail{
Group: group,
}
}
return nil
},
); err != nil {
log.CtxLogger(ctx).Warn(fmt.Sprintf("ListGroups error in", organization.DisplayName), zap.Error(err))
}
} else {
log.CtxLogger(ctx).Warn("`DirectoryCustomerId` is not string", zap.Error(err))
}
}

return nil
},
RowField: schema.RowField{
ResourceId: "$.Group.id",
ResourceName: "$.Group.name",
},
Dimension: schema.Global,
}
}

func getCustomerId(ctx context.Context, organization *resourcemanagerpb.Organization) interface{} {
jsonBytes, err := json.Marshal(organization)
if err != nil {
log.CtxLogger(ctx).Warn("json Marshal Organization error", zap.Error(err))
}

var o interface{}
_ = json.Unmarshal(jsonBytes, &o)
directoryCustomerId, err := jsonpath.Read(o, "$.Owner.DirectoryCustomerId")
if err != nil {
log.CtxLogger(ctx).Warn("Read `DirectoryCustomerId` error", zap.Error(err))
}

return directoryCustomerId
}

type GroupDetail struct {
Group *admin.Group
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package admin
package workspace

//import (
// "github.com/core-sdk/constant"
Expand Down
2 changes: 2 additions & 0 deletions collector/gcp/platform/platfrom_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cloudrec/gcp/collector/iam"
"github.com/cloudrec/gcp/collector/k8sengine"
"github.com/cloudrec/gcp/collector/vpc"
"github.com/cloudrec/gcp/collector/workspace"
"github.com/core-sdk/constant"
"github.com/core-sdk/schema"
)
Expand Down Expand Up @@ -60,6 +61,7 @@ func GetPlatformConfig() *schema.Platform {
cloudresourcemanager.GetOrganizationResource(),
cloudresourcemanager.GetProjectResource(),
cloudsql.GetInstanceResource(),
workspace.GetGroupResource(),
},

Service: &collector.Services{},
Expand Down