Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Adding changes for project level matchable attr api #357

Merged
merged 2 commits into from
Nov 8, 2022
Merged
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
17 changes: 13 additions & 4 deletions cmd/delete/matchable_attribute_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ func deleteMatchableAttr(ctx context.Context, project, domain, workflowName stri
if dryRun {
fmt.Print("skipping DeleteProjectDomainAttributes request (dryRun)\n")
} else {
err := deleter.DeleteProjectDomainAttributes(ctx, project, domain, rsType)
if err != nil {
return err
if len(domain) == 0 {
err := deleter.DeleteProjectAttributes(ctx, project, rsType)
if err != nil {
return err
}
fmt.Printf("Deleted matchable resources from %v project \n", project)
} else {
err := deleter.DeleteProjectDomainAttributes(ctx, project, domain, rsType)
if err != nil {
return err
}
fmt.Printf("Deleted matchable resources from %v project and domain %v\n", project, domain)
}
}
fmt.Printf("Deleted matchable resources from %v project and domain %v\n", project, domain)

}
return nil
}
2 changes: 1 addition & 1 deletion cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func CreateGetCommand() *cobra.Command {
Long: pluginOverrideLong, PFlagProvider: pluginoverride.DefaultFetchConfig},
"workflow-execution-config": {CmdFunc: getWorkflowExecutionConfigFunc, Aliases: []string{"workflow-execution-config"},
Short: workflowExecutionConfigShort,
Long: workflowExecutionConfigLong, PFlagProvider: workflowexecutionconfig.DefaultFetchConfig},
Long: workflowExecutionConfigLong, PFlagProvider: workflowexecutionconfig.DefaultFetchConfig, ProjectDomainNotRequired: true},
}

cmdcore.AddCommands(getCmd, getResourcesFuncs)
Expand Down
21 changes: 15 additions & 6 deletions cmd/get/matchable_attribute_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ func FetchAndUnDecorateMatchableAttr(ctx context.Context, project, domain, workf
// Update the shadow config with the fetched taskResourceAttribute which can then be written to a file which can then be called for an update.
unDecorator.UnDecorate(workflowAttr.GetAttributes().GetMatchingAttributes())
} else {
// Fetch the project domain attribute from the admin
projectDomainAttr, err := fetcher.FetchProjectDomainAttributes(ctx, project, domain, rsType)
if err != nil {
return err
if len(domain) == 0 {
projectAttr, err := fetcher.FetchProjectAttributes(ctx, project, rsType)
if err != nil {
return err
}
// Update the shadow config with the fetched taskResourceAttribute which can then be written to a file which can then be called for an update.
unDecorator.UnDecorate(projectAttr.GetAttributes().GetMatchingAttributes())
} else {
// Fetch the project domain attribute from the admin
projectDomainAttr, err := fetcher.FetchProjectDomainAttributes(ctx, project, domain, rsType)
if err != nil {
return err
}
// Update the shadow config with the fetched taskResourceAttribute which can then be written to a file which can then be called for an update.
unDecorator.UnDecorate(projectDomainAttr.GetAttributes().GetMatchingAttributes())
}
// Update the shadow config with the fetched taskResourceAttribute which can then be written to a file which can then be called for an update.
unDecorator.UnDecorate(projectDomainAttr.GetAttributes().GetMatchingAttributes())
}
return nil
}
17 changes: 13 additions & 4 deletions cmd/update/matchable_attribute_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ func DecorateAndUpdateMatchableAttr(ctx context.Context, project, domain, workfl
if dryRun {
fmt.Printf("skipping UpdateProjectDomainAttributes request (dryRun)\n")
} else {
err := updater.UpdateProjectDomainAttributes(ctx, project, domain, matchingAttr)
if err != nil {
return err
if len(domain) == 0 {
err := updater.UpdateProjectAttributes(ctx, project, matchingAttr)
if err != nil {
return err
}
fmt.Printf("Updated attributes from %v project\n", project)
} else {
err := updater.UpdateProjectDomainAttributes(ctx, project, domain, matchingAttr)
if err != nil {
return err
}
fmt.Printf("Updated attributes from %v project and domain %v\n", project, domain)
}
}
fmt.Printf("Updated attributes from %v project and domain %v\n", project, domain)

}
return nil
}
14 changes: 14 additions & 0 deletions pkg/ext/attribute_match_deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ func TestDeleteProjectDomainAttributesError(t *testing.T) {
err := adminDeleterExt.DeleteProjectDomainAttributes(ctx, "dummyProject", "domainValue", admin.MatchableResource_TASK_RESOURCE)
assert.Equal(t, fmt.Errorf("failed"), err)
}

func TestDeleteProjectAttributes(t *testing.T) {
deleteAttributeMatchFetcherSetup()
adminClient.OnDeleteProjectAttributesMatch(mock.Anything, mock.Anything).Return(nil, nil)
err := adminDeleterExt.DeleteProjectAttributes(ctx, "dummyProject", admin.MatchableResource_TASK_RESOURCE)
assert.Nil(t, err)
}

func TestDeleteProjectAttributesError(t *testing.T) {
deleteAttributeMatchFetcherSetup()
adminClient.OnDeleteProjectAttributesMatch(mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed"))
err := adminDeleterExt.DeleteProjectAttributes(ctx, "dummyProject", admin.MatchableResource_TASK_RESOURCE)
assert.Equal(t, fmt.Errorf("failed"), err)
}
16 changes: 16 additions & 0 deletions pkg/ext/attribute_match_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ func (a *AdminFetcherExtClient) FetchProjectDomainAttributes(ctx context.Context
}
return projectDomainAttr, nil
}

func (a *AdminFetcherExtClient) FetchProjectAttributes(ctx context.Context, project string,
rsType admin.MatchableResource) (*admin.ProjectAttributesGetResponse, error) {
projectDomainAttr, err := a.AdminServiceClient().GetProjectAttributes(ctx,
&admin.ProjectAttributesGetRequest{
Project: project,
ResourceType: rsType,
})
if err != nil {
return nil, err
}
if projectDomainAttr.GetAttributes() == nil || projectDomainAttr.GetAttributes().GetMatchingAttributes() == nil {
return nil, fmt.Errorf("attribute doesn't exist")
}
return projectDomainAttr, nil
}
17 changes: 17 additions & 0 deletions pkg/ext/attribute_match_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,20 @@ func TestFetchProjectDomainAttributesError(t *testing.T) {
assert.Equal(t, fmt.Errorf("attribute doesn't exist"), err)
})
}

func TestFetchProjectAttributesError(t *testing.T) {
t.Run("failed api", func(t *testing.T) {
getAttributeMatchFetcherSetup()
adminClient.OnGetProjectAttributesMatch(mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed"))
_, err := adminFetcherExt.FetchProjectAttributes(ctx, "dummyProject", admin.MatchableResource_TASK_RESOURCE)
assert.Equal(t, fmt.Errorf("failed"), err)
})
t.Run("empty data from api", func(t *testing.T) {
getAttributeMatchFetcherSetup()
pResp := &admin.ProjectAttributesGetResponse{}
adminClient.OnGetProjectAttributesMatch(mock.Anything, mock.Anything).Return(pResp, nil)
_, err := adminFetcherExt.FetchProjectAttributes(ctx, "dummyProject", admin.MatchableResource_TASK_RESOURCE)
assert.NotNil(t, err)
assert.Equal(t, fmt.Errorf("attribute doesn't exist"), err)
})
}
11 changes: 11 additions & 0 deletions pkg/ext/attribute_match_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ func (a *AdminUpdaterExtClient) UpdateProjectDomainAttributes(ctx context.Contex
})
return err
}

func (a *AdminUpdaterExtClient) UpdateProjectAttributes(ctx context.Context, project string, matchingAttr *admin.MatchingAttributes) error {
_, err := a.AdminServiceClient().UpdateProjectAttributes(ctx,
&admin.ProjectAttributesUpdateRequest{
Attributes: &admin.ProjectAttributes{
Project: project,
MatchingAttributes: matchingAttr,
},
})
return err
}
17 changes: 17 additions & 0 deletions pkg/ext/attribute_match_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ func TestUpdateProjectDomainAttributesError(t *testing.T) {
err := adminUpdaterExt.UpdateProjectDomainAttributes(ctx, "dummyProject", "domainValue", nil)
assert.Equal(t, fmt.Errorf("failed"), err)
}

func TestUpdateProjectAttributes(t *testing.T) {
updateAttributeMatchFetcherSetup()
matchingAttr := &admin.MatchingAttributes{
Target: &admin.MatchingAttributes_TaskResourceAttributes{},
}
adminClient.OnUpdateProjectAttributesMatch(mock.Anything, mock.Anything).Return(nil, nil)
err := adminUpdaterExt.UpdateProjectAttributes(ctx, "dummyProject", matchingAttr)
assert.Nil(t, err)
}

func TestUpdateProjectAttributesError(t *testing.T) {
updateAttributeMatchFetcherSetup()
adminClient.OnUpdateProjectAttributesMatch(mock.Anything, mock.Anything).Return(nil, fmt.Errorf("failed"))
err := adminUpdaterExt.UpdateProjectAttributes(ctx, "dummyProject", nil)
assert.Equal(t, fmt.Errorf("failed"), err)
}
8 changes: 8 additions & 0 deletions pkg/ext/attribute_matcher_deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ func (a *AdminDeleterExtClient) DeleteProjectDomainAttributes(ctx context.Contex
})
return err
}

func (a *AdminDeleterExtClient) DeleteProjectAttributes(ctx context.Context, project string, rsType admin.MatchableResource) error {
_, err := a.AdminServiceClient().DeleteProjectAttributes(ctx, &admin.ProjectAttributesDeleteRequest{
Project: project,
ResourceType: rsType,
})
return err
}
3 changes: 3 additions & 0 deletions pkg/ext/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type AdminDeleterExtInterface interface {

// DeleteProjectDomainAttributes deletes project domain attributes for a particular matchable resource
DeleteProjectDomainAttributes(ctx context.Context, project, domain string, rsType admin.MatchableResource) error

// DeleteProjectAttributes deletes project attributes for a particular matchable resource
DeleteProjectAttributes(ctx context.Context, project string, rsType admin.MatchableResource) error
}

// AdminDeleterExtClient is used for interacting with extended features used for deleting/archiving data in admin service
Expand Down
3 changes: 3 additions & 0 deletions pkg/ext/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type AdminFetcherExtInterface interface {
// FetchProjectDomainAttributes fetches project domain attributes particular resource type in a project, domain
FetchProjectDomainAttributes(ctx context.Context, project, domain string, rsType admin.MatchableResource) (*admin.ProjectDomainAttributesGetResponse, error)

// FetchProjectAttributes fetches project attributes particular resource type in a project
FetchProjectAttributes(ctx context.Context, project string, rsType admin.MatchableResource) (*admin.ProjectAttributesGetResponse, error)

// ListProjects fetches all projects
ListProjects(ctx context.Context, filter filters.Filters) (*admin.Projects, error)
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/ext/mocks/admin_deleter_ext_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions pkg/ext/mocks/admin_fetcher_ext_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions pkg/ext/mocks/admin_updater_ext_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/ext/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type AdminUpdaterExtInterface interface {

// UpdateProjectDomainAttributes updates project domain attributes for a particular matchable resource
UpdateProjectDomainAttributes(ctx context.Context, project, domain string, matchingAttr *admin.MatchingAttributes) error

// UpdateProjectAttributes updates project attributes for a particular matchable resource
UpdateProjectAttributes(ctx context.Context, project string, matchingAttr *admin.MatchingAttributes) error
}

// AdminUpdaterExtClient is used for interacting with extended features used for updating data in admin service
Expand Down