Skip to content

Commit

Permalink
created universal controller that reconciles all resources related to… (
Browse files Browse the repository at this point in the history
#7)

* created universal controller that reconciles all resources related to magento.web7.md, allowing manage of resources from magento API based on standard RESTful API definitions

* use package constants for multiple uses of same value, move all types to beginning of file

* make desired structure and use separator as constant to build url

* Add in memory cache for Magento client (#9)

* added group, version, separator to constants
  • Loading branch information
ca7alindev authored May 30, 2024
1 parent 20721e2 commit 02ff1f5
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 354 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.27.4 // indirect
k8s.io/apiextensions-apiserver v0.27.4 // indirect
k8s.io/apiextensions-apiserver v0.27.4
k8s.io/component-base v0.27.4 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230525220651-2546d827e515 // indirect
Expand Down
115 changes: 0 additions & 115 deletions internal/client/categories/categories.go

This file was deleted.

1 change: 1 addition & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Client struct {
BaseURL string
AccessToken string
Path string
}

// NewClient initializes a new Magento API client configuration
Expand Down
91 changes: 91 additions & 0 deletions internal/client/magento.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package magento

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/go-resty/resty/v2"
)

const (
separator = "/"
)

// 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) (*Desired, error) {
if id == "" {
return nil, errors.New("resource with ID" + id + " in " + c.Path + " not found")
}
resp, _ := c.Create().R().Get(c.Path + separator + id)
if resp.StatusCode() != http.StatusOK {
return nil, errors.New("resource in " + c.Path + " not found")
}

var resource *Desired
err := json.Unmarshal(resp.Body(), &resource)
if err != nil {
return nil, errors.New("failed to unmarshal response body")
}

return resource, nil
}

// CreateResource creates a new resource at specified api endpoint.
func CreateResource(c *Client, observed map[string]interface{}) (map[string]interface{}, *resty.Response, error) {

requestBody := map[string]interface{}{
strings.ToLower(observed["kind"].(string)): observed["spec"].(map[string]interface{})["forProvider"],
}
resp, err := c.Create().R().SetHeader("Content-Type", "application/json").SetBody(requestBody).Post(c.Path)
if err != nil {
return nil, nil, err
}

var desired map[string]interface{}
err = json.Unmarshal(resp.Body(), &desired)
if err != nil {
return nil, nil, err
}

return desired, resp, nil
}

// UpdateResourceByID updates a resource by its ID at specified api endpoint.
func UpdateResourceByID(c *Client, id string, observed map[string]interface{}) error {
requestBody := map[string]interface{}{
strings.ToLower(observed["kind"].(string)): observed["spec"].(map[string]interface{})["forProvider"],
}
_, err := c.Create().R().SetBody(requestBody).Put(c.Path + separator + id)
if err != nil {
return err
}

return nil
}

// DeleteResourceByID deletes a resource by its ID at specified api endpoint.
func DeleteResourceByID(c *Client, id string) error {
_, err := c.Create().R().Delete(c.Path + separator + id)
return err
}

// IsUpToDate checks if the observed resource is up to date with the desired resource.
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 {
return false, nil
}

return true, nil
}
Loading

0 comments on commit 02ff1f5

Please sign in to comment.