Skip to content

Commit

Permalink
Use meta.GetExternalName to get external resoruce name
Browse files Browse the repository at this point in the history
and wrap all errors

Signed-off-by: Zheng Xi Zhou <[email protected]>
  • Loading branch information
zzxwill committed Apr 5, 2021
1 parent c54ff01 commit 2b650c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
26 changes: 20 additions & 6 deletions pkg/clients/sls/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ import (
"fmt"

sdk "github.com/aliyun/aliyun-log-go-sdk"
"github.com/pkg/errors"

"github.com/crossplane/provider-alibaba/apis/sls/v1alpha1"
)

var (
// ErrCodeProjectNotExist error code of ServerError when Project not found
ErrCodeProjectNotExist = "ProjectNotExist"
// ErrFailedToGetSLSProject is the error of failing to get an SLS project
ErrFailedToGetSLSProject = "FailedToGetSLSProject"
// ErrFailedToCreateSLSProject is the error of failing to create an SLS project
ErrFailedToCreateSLSProject = "FailedToCreateSLSProject"
// ErrFailedToUpdateSLSProject is the error of failing to update an SLS project
ErrFailedToUpdateSLSProject = "FailedToUpdateSLSProject"
// ErrFailedToDeleteSLSProject is the error of failing to delete an SLS project
ErrFailedToDeleteSLSProject = "FailedToDeleteSLSProject"
)

// LogClientInterface will help fakeOSSClient in unit tests
Expand All @@ -53,22 +62,27 @@ func NewClient(accessKeyID, accessKeySecret, region string) *LogClient {

// Describe describes SLS project
func (c *LogClient) Describe(name string) (*sdk.LogProject, error) {
return c.Client.GetProject(name)
logProject, err := c.Client.GetProject(name)
return logProject, errors.Wrap(err, ErrFailedToGetSLSProject)
}

// Create creates SLS project
func (c *LogClient) Create(name, description string) (*sdk.LogProject, error) {
return c.Client.CreateProject(name, description)
logProject, err := c.Client.CreateProject(name, description)
return logProject, errors.Wrap(err, ErrFailedToCreateSLSProject)
}

// Update updates SLS project's description
func (c *LogClient) Update(name, description string) (*sdk.LogProject, error) {
return c.Client.UpdateProject(name, description)
logProject, err := c.Client.UpdateProject(name, description)
return logProject, errors.Wrap(err, ErrFailedToUpdateSLSProject)

}

// Delete deletes SLS project
func (c *LogClient) Delete(name string) error {
return c.Client.DeleteProject(name)
err := c.Client.DeleteProject(name)
return errors.Wrap(err, ErrFailedToDeleteSLSProject)
}

// GenerateObservation is used to produce v1alpha1.ProjectObservation
Expand All @@ -87,10 +101,10 @@ func IsNotFoundError(err error) bool {
if err == nil {
return false
}
if e, ok := err.(sdk.Error); ok && (e.Code == ErrCodeProjectNotExist) {
if e, ok := errors.Cause(err).(sdk.Error); ok && (e.Code == ErrCodeProjectNotExist) {
return true
}
if e, ok := err.(*sdk.Error); ok && (e.Code == ErrCodeProjectNotExist) {
if e, ok := errors.Cause(err).(*sdk.Error); ok && (e.Code == ErrCodeProjectNotExist) {
return true
}
return false
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/sls/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/pkg/errors"
Expand All @@ -38,7 +39,7 @@ func BaseObserve(mg resource.Managed, c slsclient.LogClientInterface) (managed.E
if !ok {
return managed.ExternalObservation{}, errors.New(errNotProject)
}
projectName := cr.Spec.ForProvider.ProjectName
projectName := meta.GetExternalName(cr)
project, err := c.Describe(projectName)
if slsclient.IsNotFoundError(err) {
return managed.ExternalObservation{
Expand Down Expand Up @@ -70,7 +71,7 @@ func BaseCreate(mg resource.Managed, c slsclient.LogClientInterface) (managed.Ex
if !ok {
return managed.ExternalCreation{}, errors.New(errNotProject)
}
name := cr.Spec.ForProvider.ProjectName
name := meta.GetExternalName(cr)
description := cr.Spec.ForProvider.Description
cr.SetConditions(xpv1.Creating())
project, err := c.Create(name, description)
Expand All @@ -86,7 +87,7 @@ func BaseUpdate(mg resource.Managed, client slsclient.LogClientInterface) (manag
if !ok {
return managed.ExternalUpdate{}, errors.New(errNotProject)
}
name := cr.Spec.ForProvider.ProjectName
name := meta.GetExternalName(cr)
description := cr.Spec.ForProvider.Description
cr.Status.SetConditions(xpv1.Creating())
got, err := client.Update(name, description)
Expand All @@ -106,7 +107,7 @@ func BaseDelete(mg resource.Managed, client slsclient.LogClientInterface) error
if !ok {
return errors.New(errNotProject)
}
name := cr.Spec.ForProvider.ProjectName
name := meta.GetExternalName(cr)
cr.SetConditions(xpv1.Deleting())
if err := client.Delete(name); err != nil && !slsclient.IsNotFoundError(err) {
return err
Expand Down

0 comments on commit 2b650c2

Please sign in to comment.