Skip to content

Commit

Permalink
make desired structure and use separator as constant to build url
Browse files Browse the repository at this point in the history
  • Loading branch information
ca7alindev committed May 28, 2024
1 parent f3f2cf9 commit e8da8c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 10 additions & 4 deletions internal/client/magento.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import (
"github.com/go-resty/resty/v2"
)

// Desired represents the desired state of a resource.
type Desired struct {
ID string `json:"id"`
Name string `json:"name"`
}

// GetResourceByID retrieves a resource by its ID at specified api endpoint.
func GetResourceByID(c *Client, id string) (map[string]interface{}, error) {
func GetResourceByID(c *Client, id string) (*Desired, error) {
if id == "" {
return nil, errors.New("resource with ID" + id + " in " + c.Path + " not found")
}
Expand All @@ -19,7 +25,7 @@ func GetResourceByID(c *Client, id string) (map[string]interface{}, error) {
return nil, errors.New("resource in " + c.Path + " not found")
}

var resource map[string]interface{}
var resource *Desired
err := json.Unmarshal(resp.Body(), &resource)
if err != nil {
return nil, errors.New("failed to unmarshal response body")
Expand Down Expand Up @@ -68,12 +74,12 @@ func DeleteResourceByID(c *Client, id string) error {
}

// IsUpToDate checks if the observed resource is up to date with the desired resource.
func IsUpToDate(observed map[string]interface{}, desired map[string]interface{}) (bool, error) {
func IsUpToDate(observed map[string]interface{}, desired *Desired) (bool, error) {

if observed == nil || desired == nil {
return false, errors.New("observed or desired resource is nil")
}
if observed["spec"].(map[string]interface{})["forProvider"].(map[string]interface{})["name"] != desired["name"] {
if observed["spec"].(map[string]interface{})["forProvider"].(map[string]interface{})["name"] != desired.Name {
return false, nil
}

Expand Down
11 changes: 5 additions & 6 deletions internal/controller/magento.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ const (
errGetCreds = "cannot get credentials"

errNewClient = "cannot create new Service"
api = "/rest/"
api = "/rest"
id = "external-id"
separator = "/"
)

// MagentoService is a service that can connect to Magento API.
Expand Down Expand Up @@ -192,8 +193,8 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
crds := &v1.CustomResourceDefinitionList{}
_ = c.kube.List(ctx, crds)
crd := getDesiredCRD(crds, mg.GetObjectKind().GroupVersionKind().Group, mg.GetObjectKind().GroupVersionKind().Kind)
c.service.client.Path = api + strings.ToUpper(mg.GetObjectKind().GroupVersionKind().Version) + "/" + crd.Spec.Names.Plural

c.service.client.Path = strings.Join([]string{api, strings.ToUpper(mg.GetObjectKind().GroupVersionKind().Version), crd.Spec.Names.Plural}, separator)
externalID := mg.GetAnnotations()[id]
desired, err := magento.GetResourceByID(c.service.client, externalID)
if err != nil {
Expand All @@ -208,8 +209,8 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
}

if desired != nil {
observed["status"].(map[string]interface{})["atProvider"].(map[string]interface{})["id"] = desired["id"]
observed["status"].(map[string]interface{})["atProvider"].(map[string]interface{})["name"] = desired["name"]
observed["status"].(map[string]interface{})["atProvider"].(map[string]interface{})["id"] = desired.ID
observed["status"].(map[string]interface{})["atProvider"].(map[string]interface{})["name"] = desired.Name
}

err = runtime.DefaultUnstructuredConverter.FromUnstructured(observed, mg)
Expand All @@ -219,9 +220,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
mg.SetConditions(xpv1.Available())

isUpToDate, _ := magento.IsUpToDate(observed, desired)

return managed.ExternalObservation{

ResourceExists: true,
ResourceUpToDate: isUpToDate,
ConnectionDetails: managed.ConnectionDetails{},
Expand Down

0 comments on commit e8da8c1

Please sign in to comment.