Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table deprecation support #307

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NAME = "github.com/goto/optimus"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
OPMS_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "04fa895e5b1a1c79f2bb9c0cad63617062c55495"
PROTON_COMMIT := "5e7cf7119e5ef2007c8323712c9d08c11863813d"


.PHONY: build test test-ci generate-proto unit-test-ci integration-test vet coverage clean install lint
Expand Down
Empty file added client.yaml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it accidently commited ?

Empty file.
47 changes: 35 additions & 12 deletions client/local/model/resource_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,55 @@ package model

import (
"fmt"
"time"

"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"

pb "github.com/goto/optimus/protos/gotocompany/optimus/core/v1beta1"
)

type Deprecation struct {
Reason string `yaml:"reason"`
Date string `yaml:"date"`
ReplacementTable string `yaml:"replacement_table"`
}

type ResourceSpec struct {
Version int `yaml:"version"`
Name string `yaml:"name"`
Type string `yaml:"type"`
Labels map[string]string `yaml:"labels"`
Spec map[string]interface{} `yaml:"spec"`
Path string `yaml:"-"`
Version int `yaml:"version"`
Name string `yaml:"name"`
Type string `yaml:"type"`
Labels map[string]string `yaml:"labels"`
Spec map[string]interface{} `yaml:"spec"`
Path string `yaml:"-"`
Deprecation *Deprecation `yaml:"deprecation"`
}

func (r ResourceSpec) ToProto() (*pb.ResourceSpecification, error) {
specPb, err := structpb.NewStruct(r.Spec)
if err != nil {
return nil, fmt.Errorf("error constructing spec pb: %w", err)
}

var deprecation *pb.Deprecation
if r.Deprecation != nil && len(r.Deprecation.Date) > 0 {
deprecationDate, err := time.Parse(time.DateOnly, r.Deprecation.Date)
if err != nil {
return nil, fmt.Errorf("error parsing deprivation Date, for resource: %s, expected_format: [YYYY-MM-DD], got: [%s], err: %w", r.Name, r.Deprecation.Date, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there was some typo in the message

Suggested change
return nil, fmt.Errorf("error parsing deprivation Date, for resource: %s, expected_format: [YYYY-MM-DD], got: [%s], err: %w", r.Name, r.Deprecation.Date, err)
return nil, fmt.Errorf("error parsing deprecation Date, for resource: %s, expected_format: [YYYY-MM-DD], got: [%s], err: %w", r.Name, r.Deprecation.Date, err)

}
deprecation = &pb.Deprecation{
Reason: r.Deprecation.Reason,
Date: timestamppb.New(deprecationDate),
ReplacementTable: r.Deprecation.ReplacementTable,
}
}
return &pb.ResourceSpecification{
Version: int32(r.Version),
Name: r.Name,
Type: r.Type,
Labels: r.Labels,
Spec: specPb,
Assets: nil, // TODO: check if we really need assets
Version: int32(r.Version),
Name: r.Name,
Type: r.Type,
Labels: r.Labels,
Spec: specPb,
Assets: nil, // TODO: check if we really need assets
Deprecation: deprecation,
}, nil
}
1 change: 1 addition & 0 deletions client/local/specio/resource_spec_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ spec:
- mode: nullable
name: name
type: string
deprecation: null

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add unit test for deprecation is exists ?

`)
r.Assert().EqualValues(expectedContent, actualContent)
})
Expand Down
4 changes: 2 additions & 2 deletions core/job/service/job_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4586,7 +4586,7 @@ func TestJobService(t *testing.T) {
resourceExistenceChecker.On("GetByURN", ctx, sampleTenant, resourceURNA).Return(nil, errors.New("unexpected get by urn error"))
resourceExistenceChecker.On("ExistInStore", ctx, sampleTenant, resourceURNA).Return(false, errors.New("unexpected exist in store error"))

rsc, err := resource.NewResource("resource_1", "table", resource.Bigquery, sampleTenant, &resource.Metadata{Description: "table for test"}, map[string]any{"version": 1})
rsc, err := resource.NewResource("resource_1", "table", resource.Bigquery, sampleTenant, &resource.Metadata{Description: "table for test"}, map[string]any{"version": 1}, nil)
assert.NoError(t, err)
deletedRsc := resource.FromExisting(rsc, resource.ReplaceStatus(resource.StatusDeleted))

Expand Down Expand Up @@ -4744,7 +4744,7 @@ func TestJobService(t *testing.T) {

jobRunInputCompiler.On("Compile", ctx, mock.Anything, mock.Anything, mock.Anything).Return(nil, nil)

rsc, err := resource.NewResource("resource_1", "table", resource.Bigquery, sampleTenant, &resource.Metadata{Description: "table for test"}, map[string]any{"version": 1})
rsc, err := resource.NewResource("resource_1", "table", resource.Bigquery, sampleTenant, &resource.Metadata{Description: "table for test"}, map[string]any{"version": 1}, nil)
assert.NoError(t, err)

resourceExistenceChecker.On("GetByURN", ctx, sampleTenant, resourceURNA).Return(rsc, nil)
Expand Down
35 changes: 27 additions & 8 deletions core/resource/handler/v1beta1/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/goto/optimus/core/resource"
"github.com/goto/optimus/core/tenant"
Expand Down Expand Up @@ -515,8 +516,16 @@ func fromResourceProto(rs *pb.ResourceSpecification, tnnt tenant.Tenant, store r
Description: description,
Labels: rs.Labels,
}

return resource.NewResource(rs.Name, rs.GetType(), store, tnnt, &metadata, spec)
var deprecation *resource.Deprecated
deprecationPb := rs.GetDeprecation()
if deprecationPb != nil {
deprecation = &resource.Deprecated{
Reason: deprecationPb.Reason,
Date: deprecationPb.Date.AsTime(),
ReplacementTable: deprecationPb.ReplacementTable,
}
}
return resource.NewResource(rs.Name, rs.GetType(), store, tnnt, &metadata, spec, deprecation)
}

func toResourceProto(res *resource.Resource) (*pb.ResourceSpecification, error) {
Expand All @@ -529,14 +538,24 @@ func toResourceProto(res *resource.Resource) (*pb.ResourceSpecification, error)
if err != nil {
return nil, errors.InvalidArgument(resource.EntityResource, "unable to convert spec to proto struct")
}
deprecation := res.GetDeprecationInfo()
var deprecationPb *pb.Deprecation
if deprecation != nil {
deprecationPb = &pb.Deprecation{
Reason: deprecation.Reason,
Date: timestamppb.New(deprecation.Date),
ReplacementTable: deprecation.ReplacementTable,
}
}

return &pb.ResourceSpecification{
Version: meta.Version,
Name: res.FullName(),
Type: res.Kind(),
Spec: pbStruct,
Assets: nil,
Labels: meta.Labels,
Version: meta.Version,
Name: res.FullName(),
Type: res.Kind(),
Spec: pbStruct,
Assets: nil,
Labels: meta.Labels,
Deprecation: deprecationPb,
}, nil
}

Expand Down
8 changes: 4 additions & 4 deletions core/resource/handler/v1beta1/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestResourceHandler(t *testing.T) {
t.Run("lists the resources successfully", func(t *testing.T) {
spec := map[string]any{"a": "b"}
dbRes, err := resource.NewResource("proj.set.table", "table", resource.Bigquery, tnnt,
&resource.Metadata{}, spec)
&resource.Metadata{}, spec, nil)
assert.Nil(t, err)

service := new(resourceService)
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestResourceHandler(t *testing.T) {
invalidKey := "a\xc5z"
specWithInvalidUTF := map[string]any{invalidKey: "value"}
dbRes, err := resource.NewResource("proj.set.table", "table", resource.Bigquery, tnnt,
&resource.Metadata{}, specWithInvalidUTF)
&resource.Metadata{}, specWithInvalidUTF, nil)
assert.Nil(t, err)
service := new(resourceService)
name := "proj.set.table"
Expand All @@ -571,7 +571,7 @@ func TestResourceHandler(t *testing.T) {
t.Run("returns the resource successfully", func(t *testing.T) {
spec := map[string]any{"a": "b"}
dbRes, err := resource.NewResource("proj.set.table", "table", resource.Bigquery, tnnt,
&resource.Metadata{}, spec)
&resource.Metadata{}, spec, nil)
assert.Nil(t, err)

service := new(resourceService)
Expand Down Expand Up @@ -997,7 +997,7 @@ func TestResourceHandler(t *testing.T) {
t.Run("DeleteResource", func(t *testing.T) {
resourceName := "project.dataset.test_table"
spec := map[string]any{"a": "b"}
existing, _ := resource.NewResource(resourceName, "table", resource.Bigquery, tnnt, &resource.Metadata{}, spec)
existing, _ := resource.NewResource(resourceName, "table", resource.Bigquery, tnnt, &resource.Metadata{}, spec, nil)

t.Run("success", func(t *testing.T) {
var (
Expand Down
50 changes: 34 additions & 16 deletions core/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (n Name) String() string {
return string(n)
}

type Deprecated struct {
Reason string
Date time.Time
ReplacementTable string
}

type Resource struct {
name Name

Expand All @@ -99,6 +105,12 @@ type Resource struct {
metadata *Metadata

status Status

deprecation *Deprecated
}

func (r *Resource) IsDeprecated() bool {
return r.deprecation != nil
}

func (r *Resource) GetUpdateImpact(incoming *Resource) UpdateImpact {
Expand All @@ -108,7 +120,7 @@ func (r *Resource) GetUpdateImpact(incoming *Resource) UpdateImpact {
return UnspecifiedImpactChange
}

func NewResource(fullName, kind string, store Store, tnnt tenant.Tenant, meta *Metadata, spec map[string]any) (*Resource, error) {
func NewResource(fullName, kind string, store Store, tnnt tenant.Tenant, meta *Metadata, spec map[string]any, deprecation *Deprecated) (*Resource, error) {
name, err := NameFrom(fullName)
if err != nil {
return nil, err
Expand All @@ -124,13 +136,14 @@ func NewResource(fullName, kind string, store Store, tnnt tenant.Tenant, meta *M
}

return &Resource{
name: name,
kind: kind,
store: store,
tenant: tnnt,
spec: spec,
metadata: meta,
status: StatusUnknown,
name: name,
kind: kind,
store: store,
tenant: tnnt,
spec: spec,
metadata: meta,
status: StatusUnknown,
deprecation: deprecation,
}, nil
}

Expand All @@ -151,6 +164,10 @@ func (r *Resource) URN() URN {
return r.urn
}

func (r *Resource) GetDeprecationInfo() *Deprecated {
return r.deprecation
}

func (r *Resource) UpdateURN(urn URN) error {
if r.urn.IsZero() {
r.urn = urn
Expand Down Expand Up @@ -234,14 +251,15 @@ func ReplaceStatus(status Status) FromExistingOpt {

func FromExisting(existing *Resource, opts ...FromExistingOpt) *Resource {
output := &Resource{
name: existing.name,
kind: existing.kind,
store: existing.store,
tenant: existing.tenant,
spec: existing.spec,
metadata: existing.metadata,
urn: existing.urn,
status: existing.status,
name: existing.name,
kind: existing.kind,
store: existing.store,
tenant: existing.tenant,
spec: existing.spec,
metadata: existing.metadata,
urn: existing.urn,
status: existing.status,
deprecation: existing.deprecation,
}
for _, opt := range opts {
opt(output)
Expand Down
Loading
Loading