Skip to content

Commit

Permalink
Implement get and delete for attributes (flyteorg#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
katrogan authored Jan 6, 2020
1 parent e0ac32e commit 1f516ba
Show file tree
Hide file tree
Showing 33 changed files with 807 additions and 59 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
[[override]]
name = "github.com/lyft/flyteidl"
source = "https://github.com/lyft/flyteidl"
version = "^0.16.x"
version = "^0.16.5"

[[constraint]]
name = "github.com/lyft/flytepropeller"
Expand Down
29 changes: 29 additions & 0 deletions pkg/manager/impl/project_attributes_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ func (m *ProjectAttributesManager) UpdateProjectAttributes(
return &admin.ProjectAttributesUpdateResponse{}, nil
}

func (m *ProjectAttributesManager) GetProjectAttributes(ctx context.Context, request admin.ProjectAttributesGetRequest) (
*admin.ProjectAttributesGetResponse, error) {
if err := validation.ValidateProjectAttributesGetRequest(request); err != nil {
return nil, err
}
projectAttributesModel, err := m.db.ProjectAttributesRepo().Get(ctx, request.Project, request.ResourceType.String())
if err != nil {
return nil, err
}
projectAttributes, err := transformers.FromProjectAttributesModel(projectAttributesModel)
if err != nil {
return nil, err
}
return &admin.ProjectAttributesGetResponse{
Attributes: &projectAttributes,
}, nil
}

func (m *ProjectAttributesManager) DeleteProjectAttributes(ctx context.Context,
request admin.ProjectAttributesDeleteRequest) (*admin.ProjectAttributesDeleteResponse, error) {
if err := validation.ValidateProjectAttributesDeleteRequest(request); err != nil {
return nil, err
}
if err := m.db.ProjectAttributesRepo().Delete(ctx, request.Project, request.ResourceType.String()); err != nil {
return nil, err
}
return &admin.ProjectAttributesDeleteResponse{}, nil
}

func NewProjectAttributesManager(db repositories.RepositoryInterface) interfaces.ProjectAttributesInterface {
return &ProjectAttributesManager{
db: db,
Expand Down
45 changes: 45 additions & 0 deletions pkg/manager/impl/project_attributes_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,48 @@ func TestUpdateProjectAttributes(t *testing.T) {
assert.Nil(t, err)
assert.True(t, createOrUpdateCalled)
}

func TestGetProjectAttributes(t *testing.T) {
request := admin.ProjectAttributesGetRequest{
Project: "project",
ResourceType: admin.MatchableResource_EXECUTION_QUEUE,
}
db := mocks.NewMockRepository()
db.ProjectAttributesRepo().(*mocks.MockProjectAttributesRepo).GetFunction = func(
ctx context.Context, project, resource string) (models.ProjectAttributes, error) {
assert.Equal(t, "project", project)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), resource)
expectedSerializedAttrs, _ := proto.Marshal(testutils.ExecutionQueueAttributes)
return models.ProjectAttributes{
Project: project,
Resource: resource,
Attributes: expectedSerializedAttrs,
}, nil
}
manager := NewProjectAttributesManager(db)
response, err := manager.GetProjectAttributes(context.Background(), request)
assert.Nil(t, err)
assert.True(t, proto.Equal(&admin.ProjectAttributesGetResponse{
Attributes: &admin.ProjectAttributes{
Project: "project",
MatchingAttributes: testutils.ExecutionQueueAttributes,
},
}, response))
}

func TestDeleteProjectAttributes(t *testing.T) {
request := admin.ProjectAttributesDeleteRequest{
Project: "project",
ResourceType: admin.MatchableResource_EXECUTION_QUEUE,
}
db := mocks.NewMockRepository()
db.ProjectAttributesRepo().(*mocks.MockProjectAttributesRepo).DeleteFunction = func(
ctx context.Context, project, resource string) error {
assert.Equal(t, "project", project)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), resource)
return nil
}
manager := NewProjectAttributesManager(db)
_, err := manager.DeleteProjectAttributes(context.Background(), request)
assert.Nil(t, err)
}
32 changes: 32 additions & 0 deletions pkg/manager/impl/project_domain_attributes_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ func (m *ProjectDomainAttributesManager) UpdateProjectDomainAttributes(
return &admin.ProjectDomainAttributesUpdateResponse{}, nil
}

func (m *ProjectDomainAttributesManager) GetProjectDomainAttributes(
ctx context.Context, request admin.ProjectDomainAttributesGetRequest) (
*admin.ProjectDomainAttributesGetResponse, error) {
if err := validation.ValidateProjectDomainAttributesGetRequest(request); err != nil {
return nil, err
}
projectAttributesModel, err := m.db.ProjectDomainAttributesRepo().Get(
ctx, request.Project, request.Domain, request.ResourceType.String())
if err != nil {
return nil, err
}
projectAttributes, err := transformers.FromProjectDomainAttributesModel(projectAttributesModel)
if err != nil {
return nil, err
}
return &admin.ProjectDomainAttributesGetResponse{
Attributes: &projectAttributes,
}, nil
}

func (m *ProjectDomainAttributesManager) DeleteProjectDomainAttributes(ctx context.Context,
request admin.ProjectDomainAttributesDeleteRequest) (*admin.ProjectDomainAttributesDeleteResponse, error) {
if err := validation.ValidateProjectDomainAttributesDeleteRequest(request); err != nil {
return nil, err
}
if err := m.db.ProjectDomainAttributesRepo().Delete(
ctx, request.Project, request.Domain, request.ResourceType.String()); err != nil {
return nil, err
}
return &admin.ProjectDomainAttributesDeleteResponse{}, nil
}

func NewProjectDomainAttributesManager(
db repositories.RepositoryInterface) interfaces.ProjectDomainAttributesInterface {
return &ProjectDomainAttributesManager{
Expand Down
51 changes: 51 additions & 0 deletions pkg/manager/impl/project_domain_attributes_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,54 @@ func TestUpdateProjectDomainAttributes(t *testing.T) {
assert.Nil(t, err)
assert.True(t, createOrUpdateCalled)
}

func TestGetProjectDomainAttributes(t *testing.T) {
request := admin.ProjectDomainAttributesGetRequest{
Project: "project",
Domain: "domain",
ResourceType: admin.MatchableResource_EXECUTION_QUEUE,
}
db := mocks.NewMockRepository()
db.ProjectDomainAttributesRepo().(*mocks.MockProjectDomainAttributesRepo).GetFunction = func(
ctx context.Context, project, domain, resource string) (models.ProjectDomainAttributes, error) {
assert.Equal(t, "project", project)
assert.Equal(t, "domain", domain)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), resource)
expectedSerializedAttrs, _ := proto.Marshal(testutils.ExecutionQueueAttributes)
return models.ProjectDomainAttributes{
Project: project,
Domain: domain,
Resource: resource,
Attributes: expectedSerializedAttrs,
}, nil
}
manager := NewProjectDomainAttributesManager(db)
response, err := manager.GetProjectDomainAttributes(context.Background(), request)
assert.Nil(t, err)
assert.True(t, proto.Equal(&admin.ProjectDomainAttributesGetResponse{
Attributes: &admin.ProjectDomainAttributes{
Project: "project",
Domain: "domain",
MatchingAttributes: testutils.ExecutionQueueAttributes,
},
}, response))
}

func TestDeleteProjectDomainAttributes(t *testing.T) {
request := admin.ProjectDomainAttributesDeleteRequest{
Project: "project",
Domain: "domain",
ResourceType: admin.MatchableResource_EXECUTION_QUEUE,
}
db := mocks.NewMockRepository()
db.ProjectDomainAttributesRepo().(*mocks.MockProjectDomainAttributesRepo).DeleteFunction = func(
ctx context.Context, project, domain, resource string) error {
assert.Equal(t, "project", project)
assert.Equal(t, "domain", domain)
assert.Equal(t, admin.MatchableResource_EXECUTION_QUEUE.String(), resource)
return nil
}
manager := NewProjectDomainAttributesManager(db)
_, err := manager.DeleteProjectDomainAttributes(context.Background(), request)
assert.Nil(t, err)
}
66 changes: 66 additions & 0 deletions pkg/manager/impl/validation/attributes_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ func ValidateProjectAttributesUpdateRequest(request admin.ProjectAttributesUpdat
return validateMatchingAttributes(request.Attributes.MatchingAttributes, request.Attributes.Project)
}

func ValidateProjectAttributesGetRequest(request admin.ProjectAttributesGetRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}

return nil
}

func ValidateProjectAttributesDeleteRequest(request admin.ProjectAttributesDeleteRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}

return nil
}

func ValidateProjectDomainAttributesUpdateRequest(request admin.ProjectDomainAttributesUpdateRequest) (
admin.MatchableResource, error) {
if request.Attributes == nil {
Expand All @@ -54,6 +70,28 @@ func ValidateProjectDomainAttributesUpdateRequest(request admin.ProjectDomainAtt
fmt.Sprintf("%s-%s", request.Attributes.Project, request.Attributes.Domain))
}

func ValidateProjectDomainAttributesGetRequest(request admin.ProjectDomainAttributesGetRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}

return nil
}

func ValidateProjectDomainAttributesDeleteRequest(request admin.ProjectDomainAttributesDeleteRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}

return nil
}

func ValidateWorkflowAttributesUpdateRequest(request admin.WorkflowAttributesUpdateRequest) (
admin.MatchableResource, error) {
if request.Attributes == nil {
Expand All @@ -72,3 +110,31 @@ func ValidateWorkflowAttributesUpdateRequest(request admin.WorkflowAttributesUpd
return validateMatchingAttributes(request.Attributes.MatchingAttributes,
fmt.Sprintf("%s-%s-%s", request.Attributes.Project, request.Attributes.Domain, request.Attributes.Workflow))
}

func ValidateWorkflowAttributesGetRequest(request admin.WorkflowAttributesGetRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Workflow, shared.Name); err != nil {
return err
}

return nil
}

func ValidateWorkflowAttributesDeleteRequest(request admin.WorkflowAttributesDeleteRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Workflow, shared.Name); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 1f516ba

Please sign in to comment.