Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
rebase with late initializer and fix some linter errors
Browse files Browse the repository at this point in the history
Signed-off-by: Muvaffak Onus <me@muvaf.com>
  • Loading branch information
muvaf committed Oct 5, 2021
1 parent 56ee498 commit 09cbb70
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 577 deletions.
21 changes: 14 additions & 7 deletions pkg/controller/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ type external struct {
async bool
}

func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.ExternalObservation, error) {
func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.ExternalObservation, error) { // nolint:gocyclo
// We skip the gocyclo check because most of the operations are straight-forward
// and serial.
// TODO(muvaf): Look for ways to reduce the cyclomatic complexity without
// increasing the difficulty of understanding the flow.
tr, ok := mg.(resource.Terraformed)
if !ok {
return managed.ExternalObservation{}, errors.New(errUnexpectedObject)
Expand Down Expand Up @@ -143,11 +147,14 @@ func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.
return managed.ExternalObservation{}, errors.Wrap(err, "cannot set observation")
}

// TODO(hasan): Handle late initialization of parameters.
lateInited, err := lateInitializeAnnotations(tr, attr, string(res.State.GetPrivateRaw()))
lateInitedAnn, err := lateInitializeAnnotations(tr, attr, string(res.State.GetPrivateRaw()))
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot late initialize annotations")
}
lateInitedParams, err := tr.LateInitialize(res.State.GetAttributes())
if err != nil {
return managed.ExternalObservation{}, errors.Wrap(err, "cannot late initialize parameters")
}

// During creation (i.e. apply), Terraform already waits until resource is
// ready. So, I believe it would be safe to assume it is available if create
Expand All @@ -163,7 +170,7 @@ func (e *external) Observe(ctx context.Context, mg xpresource.Managed) (managed.
return managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: plan.UpToDate,
ResourceLateInitialized: lateInited,
ResourceLateInitialized: lateInitedAnn || lateInitedParams,
}, nil
}

Expand Down Expand Up @@ -230,11 +237,11 @@ func lateInitializeAnnotations(tr resource.Terraformed, attr map[string]interfac

// Terraform stores id for the external resource as an attribute in the
// resource state. Key for the attribute holding external identifier is
// resource specific. We rely on GetTerraformResourceIdField() function
// resource specific. We rely on GetTerraformResourceIDField() function
// to find out that key.
id, exists := attr[tr.GetTerraformResourceIdField()]
id, exists := attr[tr.GetTerraformResourceIDField()]
if !exists {
return false, errors.Errorf("no value for id field: %s", tr.GetTerraformResourceIdField())
return false, errors.Errorf("no value for id field: %s", tr.GetTerraformResourceIDField())
}
extID, ok := id.(string)
if !ok {
Expand Down
23 changes: 8 additions & 15 deletions pkg/controller/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,8 @@ func TestConnect(t *testing.T) {

func TestObserve(t *testing.T) {
type args struct {
kube client.Client
w Workspace
obj xpresource.Managed
w Workspace
obj xpresource.Managed
}
type want struct {
obs managed.ExternalObservation
Expand Down Expand Up @@ -275,7 +274,7 @@ func TestObserve(t *testing.T) {
args: args{
obj: &fake.Terraformed{
MetadataProvider: fake.MetadataProvider{
IdField: "id",
IDField: "id",
},
},
w: WorkspaceFns{
Expand All @@ -297,7 +296,7 @@ func TestObserve(t *testing.T) {
args: args{
obj: &fake.Terraformed{
MetadataProvider: fake.MetadataProvider{
IdField: "id",
IDField: "id",
},
},
w: WorkspaceFns{
Expand All @@ -322,7 +321,7 @@ func TestObserve(t *testing.T) {
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
e := &external{kube: tc.kube, workspace: tc.w}
e := &external{workspace: tc.w}
_, err := e.Observe(context.TODO(), tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nObserve(...): -want error, +got error:\n%s", tc.reason, diff)
Expand All @@ -333,13 +332,11 @@ func TestObserve(t *testing.T) {

func TestCreate(t *testing.T) {
type args struct {
kube client.Client
w Workspace
async bool
obj xpresource.Managed
}
type want struct {
obs managed.ExternalObservation
err error
}
cases := map[string]struct {
Expand Down Expand Up @@ -387,7 +384,7 @@ func TestCreate(t *testing.T) {
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
e := &external{kube: tc.kube, workspace: tc.w, async: tc.async}
e := &external{workspace: tc.w, async: tc.async}
_, err := e.Create(context.TODO(), tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nCreate(...): -want error, +got error:\n%s", tc.reason, diff)
Expand All @@ -398,13 +395,11 @@ func TestCreate(t *testing.T) {

func TestUpdate(t *testing.T) {
type args struct {
kube client.Client
w Workspace
async bool
obj xpresource.Managed
}
type want struct {
obs managed.ExternalObservation
err error
}
cases := map[string]struct {
Expand Down Expand Up @@ -452,7 +447,7 @@ func TestUpdate(t *testing.T) {
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
e := &external{kube: tc.kube, workspace: tc.w, async: tc.async}
e := &external{workspace: tc.w, async: tc.async}
_, err := e.Update(context.TODO(), tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nCreate(...): -want error, +got error:\n%s", tc.reason, diff)
Expand All @@ -463,13 +458,11 @@ func TestUpdate(t *testing.T) {

func TestDelete(t *testing.T) {
type args struct {
kube client.Client
w Workspace
async bool
obj xpresource.Managed
}
type want struct {
obs managed.ExternalObservation
err error
}
cases := map[string]struct {
Expand Down Expand Up @@ -509,7 +502,7 @@ func TestDelete(t *testing.T) {
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
e := &external{kube: tc.kube, workspace: tc.w, async: tc.async}
e := &external{workspace: tc.w, async: tc.async}
err := e.Delete(context.TODO(), tc.args.obj)
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
t.Errorf("\n%s\nCreate(...): -want error, +got error:\n%s", tc.reason, diff)
Expand Down
230 changes: 0 additions & 230 deletions pkg/conversion/cli.go

This file was deleted.

Loading

0 comments on commit 09cbb70

Please sign in to comment.