Skip to content

Commit

Permalink
use project key as external name
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Mota committed Oct 26, 2022
1 parent b9b27e9 commit cdf5b0f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
7 changes: 1 addition & 6 deletions internal/bitbucket/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ type Project struct {

// GetProjectRequest contains the fields required to fetch a project
type GetProjectRequest struct {
Key string `json:"key"`
Name string `json:"name"`
Key string `json:"key"`
}

func (ps *projectService) GetProject(ctx context.Context, getReq *GetProjectRequest) (*Project, error) {
Expand All @@ -45,10 +44,6 @@ func (ps *projectService) GetProject(ctx context.Context, getReq *GetProjectRequ
if err != nil {
return nil, fmt.Errorf("error fetching projects: %w", err)
}
// We only consider that this project exists if both key and name match
if p.Name != getReq.Name {
return nil, ErrNotFound
}
return &p, nil
}

Expand Down
16 changes: 13 additions & 3 deletions internal/controller/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package project

import (
"context"
"fmt"
"log"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/connection"
"github.com/crossplane/crossplane-runtime/pkg/controller"
"github.com/crossplane/crossplane-runtime/pkg/event"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
Expand Down Expand Up @@ -146,14 +148,18 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.New(errNotProject)
}

log.Printf("Observing Project %s\n", cr.Name)
if meta.GetExternalName(cr) == "" {
return managed.ExternalObservation{
ResourceExists: false,
}, nil
}

p, err := c.service.Client.Projects.GetProject(ctx, &bitbucket.GetProjectRequest{
Key: cr.Spec.ForProvider.Key,
Name: cr.Name,
Key: meta.GetExternalName(cr),
})
if err != nil {
if errors.Is(err, bitbucket.ErrNotFound) {
log.Printf("Project with key (%s) does not exist\n", meta.GetExternalName(cr))
return managed.ExternalObservation{ResourceExists: false}, nil
}
return managed.ExternalObservation{}, errors.Wrap(err, "error fetching Bitbucket project")
Expand Down Expand Up @@ -206,9 +212,11 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

p, err := c.service.Client.Projects.CreateProject(ctx, createReq)
if err != nil {
log.Println(err)
return managed.ExternalCreation{}, err
}
log.Printf("Finished creating Project %+v\n", p)
meta.SetExternalName(cr, fmt.Sprint(p.Key))

return managed.ExternalCreation{ConnectionDetails: managed.ConnectionDetails{}}, nil
}
Expand All @@ -229,6 +237,7 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext

p, err := c.service.Client.Projects.UpdateProject(ctx, updateReq)
if err != nil {
log.Println(err)
return managed.ExternalUpdate{}, err
}

Expand All @@ -251,6 +260,7 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) error {
Key: cr.Spec.ForProvider.Key,
})
if err != nil {
log.Println(err)
return err
}

Expand Down

0 comments on commit cdf5b0f

Please sign in to comment.