Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions apis/resources/v1alpha1/route_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ type RouteStatus struct {
AtProvider RouteObservation `json:"atProvider,omitempty"`
}

// External-Name Configuration:
// - Follows Standard: yes
// - Format: Route GUID (UUID format)
// - How to find:
// - UI: Not available in the BTP Cockpit
// - CLI: Use CF CLI: `cf routes` and find the GUID in the output
// +kubebuilder:object:root=true
// +kubebuilder:storageversion

Expand Down
36 changes: 36 additions & 0 deletions internal/clients/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package clients

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestIsValidGUID(t *testing.T) {
cases := map[string]struct {
guid string
valid bool
}{
"valid GUID": {
guid: "33fd5b0b-4f3b-4b1b-8b3d-3b5f7b4b3b4b",
valid: true,
},
"invalid GUID": {
guid: "not-a-valid-guid",
valid: false,
},
"empty string": {
guid: "",
valid: false,
},
}

for n, tc := range cases {
t.Run(n, func(t *testing.T) {
result := IsValidGUID(tc.guid)
if diff := cmp.Diff(tc.valid, result); diff != "" {
t.Errorf("IsValidGUID(...): -want, +got:\n%s", diff)
}
})
}
}
57 changes: 33 additions & 24 deletions internal/clients/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/SAP/crossplane-provider-cloudfoundry/apis/resources/v1alpha1"
"github.com/SAP/crossplane-provider-cloudfoundry/internal/clients"
"github.com/SAP/crossplane-provider-cloudfoundry/internal/clients/job"
)

// Route is the interface that defines the methods that a Route client should implement.
Expand All @@ -25,36 +26,41 @@ type Client struct {
Route
}

// NewClient creates a new cf client and return interfaces for Route and RouteFeatures
func NewClient(cf *client.Client) *Client {
// NewClient creates a new cf client and returns the Route client and Job client.
func NewClient(cf *client.Client) (*Client, job.Job) {
return &Client{
Route: cf.Routes,
}
}, cf.Jobs
}

// GetByIDOrSpec returns a Route by ID or by forProvider Spec.
func (c *Client) GetByIDOrSpec(ctx context.Context, guid string, forProvider v1alpha1.RouteParameters) (*v1alpha1.RouteObservation, error) {
var r *resource.Route
var err error
if clients.IsValidGUID(guid) {
r, err = c.Get(ctx, guid)
} else {
opts, e := FormatListOption(forProvider)
if e != nil {
return nil, e
}
r, err = c.Single(ctx, opts)
// FindRouteBySpec looks up a route by spec fields (backwards compatibility).
func (c *Client) FindRouteBySpec(ctx context.Context, forProvider v1alpha1.RouteParameters) (*v1alpha1.RouteObservation, bool, error) {
opts, err := FormatListOption(forProvider)
if err != nil {
return nil, false, err
}
r, err := c.Single(ctx, opts)
if err != nil {
if clients.ErrorIsNotFound(err) {
return nil, nil
return nil, false, nil
}
return nil, err
return nil, false, err
}

atProvider := GenerateObservation(r)
return &atProvider, nil
return &atProvider, true, nil
}

// GetRouteByGUID fetches a route by its GUID.
func (c *Client) GetRouteByGUID(ctx context.Context, guid string) (*v1alpha1.RouteObservation, bool, error) {
r, err := c.Get(ctx, guid)
if err != nil {
if clients.ErrorIsNotFound(err) {
return nil, false, nil
}
return nil, false, err
}
atProvider := GenerateObservation(r)
return &atProvider, true, nil
}

// Create creates a Route and returns the GUID or error
Expand Down Expand Up @@ -86,16 +92,19 @@ func (c *Client) Update(ctx context.Context, guid string, forProvider v1alpha1.R
return err
}

func (c *Client) Delete(ctx context.Context, guid string) error {
func (c *Client) Delete(ctx context.Context, guid string) (string, error) {
if !clients.IsValidGUID(guid) {
return fmt.Errorf("invalid Route GUID")
return "", fmt.Errorf("invalid Route GUID")
}

_, err := c.Route.Delete(ctx, guid)
jobGUID, err := c.Route.Delete(ctx, guid)
if err != nil {
return err
if clients.ErrorIsNotFound(err) {
return "", nil
}
return "", err
}
return nil
return jobGUID, nil
}

// FormatListOption generates the list options for the client.
Expand Down
Loading
Loading