/
folder_repository.go
78 lines (63 loc) · 2.29 KB
/
folder_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org
import (
"context"
"errors"
"fmt"
"strings"
"cloud.google.com/go/iam/apiv1/iampb"
resourcemanager "cloud.google.com/go/resourcemanager/apiv3"
"cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb"
"github.com/googleapis/gax-go/v2"
"google.golang.org/api/iterator"
"github.com/raito-io/cli-plugin-gcp/internal/common"
"github.com/raito-io/cli-plugin-gcp/internal/iam"
)
type folderClient interface {
ListFolders(ctx context.Context, req *resourcemanagerpb.ListFoldersRequest, opts ...gax.CallOption) *resourcemanager.FolderIterator
GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error)
SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error)
}
type FolderRepository struct {
folderClient folderClient
}
func NewFolderRepository(folderClient folderClient) *FolderRepository {
return &FolderRepository{
folderClient: folderClient,
}
}
func (r *FolderRepository) GetFolders(ctx context.Context, parentName string, parent *GcpOrgEntity, fn func(ctx context.Context, folder *GcpOrgEntity) error) error {
folderIterator := r.folderClient.ListFolders(ctx, &resourcemanagerpb.ListFoldersRequest{
Parent: parentName,
})
for {
folder, err := folderIterator.Next()
if errors.Is(err, iterator.Done) {
break
} else if common.IsGoogle400Error(err) {
common.Logger.Warn(fmt.Sprintf("Encountered 4xx error while listing folders: %s", err.Error()))
continue
} else if err != nil {
return fmt.Errorf("folder iterator: %w", err)
}
id := strings.Split(folder.Name, "/")[1]
res := GcpOrgEntity{
EntryName: folder.Name,
Name: folder.DisplayName,
Id: id,
FullName: id,
Type: TypeFolder,
Parent: parent,
}
err = fn(ctx, &res)
if err != nil {
return err
}
}
return nil
}
func (r *FolderRepository) GetIamPolicy(ctx context.Context, folderId string) ([]iam.IamBinding, error) {
return getAndParseBindings(ctx, r.folderClient, TypeFolder, folderId)
}
func (r *FolderRepository) UpdateBinding(ctx context.Context, dataObject *iam.DataObjectReference, bindingsToAdd []iam.IamBinding, bindingsToDelete []iam.IamBinding) error {
return updateBindings(ctx, r.folderClient, dataObject, bindingsToAdd, bindingsToDelete)
}