generated from crossplane/provider-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created universal controller that reconciles all resources related to… (
#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
1 parent
20721e2
commit 02ff1f5
Showing
7 changed files
with
349 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.