generated from crossplane/provider-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement reconciliation of category for create/update/delete categor… #5
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
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 |
---|---|---|
@@ -1,20 +1,21 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
namespace: crossplane-system | ||
namespace: default | ||
name: example-provider-secret | ||
type: Opaque | ||
data: | ||
# credentials: BASE64ENCODED_PROVIDER_CREDS | ||
creds: "" | ||
--- | ||
apiVersion: magento.crossplane.io/v1alpha1 | ||
apiVersion: magento.web7.md/v1alpha1 | ||
kind: ProviderConfig | ||
metadata: | ||
name: example | ||
name: category-provider-config | ||
spec: | ||
hostName: "" | ||
credentials: | ||
source: Secret | ||
secretRef: | ||
namespace: crossplane-system | ||
namespace: default | ||
name: example-provider-secret | ||
key: credentials | ||
key: creds |
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,13 @@ | ||
apiVersion: magento.web7.md/v1alpha1 | ||
kind: Category | ||
metadata: | ||
name: example-category | ||
spec: | ||
forProvider: | ||
name: "Example Category" | ||
level: 2 | ||
position: 1 | ||
isActive: true | ||
includeInMenu: false | ||
providerConfigRef: | ||
name: category-provider-config |
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
ca7alindev marked this conversation as resolved.
Show resolved
Hide resolved
|
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,115 @@ | ||
// Package categories provides functions for interacting with Magento categories. | ||
package categories | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/url" | ||
"strconv" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/web-seven/provider-magento/apis/category/v1alpha1" | ||
|
||
magento "github.com/web-seven/provider-magento/internal/client" | ||
) | ||
|
||
// Client is a struct that embeds the magento.Client struct. | ||
type Client struct { | ||
magento.Client | ||
} | ||
|
||
// categoriesPath is the path for the Magento categories API. | ||
const categoriesPath = "/rest/default/V1/categories/" | ||
|
||
// GetCategoryByID retrieves a category by its ID. | ||
func GetCategoryByID(c *magento.Client, id string) (*v1alpha1.Category, error) { | ||
queryParams := map[string]string{ | ||
"searchCriteria[filterGroups][0][filters][0][field]": "entity_id", | ||
"searchCriteria[filterGroups][0][filters][0][value]": id, | ||
} | ||
query := url.Values{} | ||
for key, value := range queryParams { | ||
query.Add(key, value) | ||
} | ||
resp, err := c.Create().R().SetQueryParams(queryParams).Get(categoriesPath + "list") | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var searchResults struct { | ||
Items []v1alpha1.CategoryParameters `json:"items"` | ||
} | ||
err = json.Unmarshal(resp.Body(), &searchResults) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(searchResults.Items) > 0 { | ||
category := v1alpha1.Category{ | ||
Spec: v1alpha1.CategorySpec{ | ||
ForProvider: searchResults.Items[0], | ||
}, | ||
} | ||
return &category, nil | ||
} else { | ||
return nil, errors.New("category not found") | ||
} | ||
} | ||
|
||
// CreateCategory creates a new category. | ||
func CreateCategory(c *magento.Client, category *v1alpha1.Category) (*v1alpha1.CategoryObservation, *resty.Response, error) { | ||
requestBody := map[string]interface{}{ | ||
"category": category.Spec.ForProvider, | ||
} | ||
|
||
resp, err := c.Create().R().SetHeader("Content-Type", "application/json").SetBody(requestBody).Post(categoriesPath) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
var categoryObservation *v1alpha1.CategoryObservation | ||
err = json.Unmarshal(resp.Body(), &categoryObservation) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return categoryObservation, resp, nil | ||
} | ||
|
||
// UpdateCategoryByID updates a category by its ID. | ||
func UpdateCategoryByID(c *magento.Client, id int, observed *v1alpha1.Category) error { | ||
requestBody := map[string]interface{}{ | ||
"category": observed.Spec.ForProvider, | ||
} | ||
|
||
_, err := c.Create().R().SetBody(requestBody).Put(categoriesPath + strconv.Itoa(id)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteCategoryByID deletes a category by its ID. | ||
func DeleteCategoryByID(c *magento.Client, id int) error { | ||
_, err := c.Create().R().Delete(categoriesPath + strconv.Itoa(id)) | ||
return err | ||
} | ||
|
||
// IsUpToDate checks if the observed category is up to date with the desired category. | ||
func IsUpToDate(observed *v1alpha1.Category, desired *v1alpha1.Category) (bool, error) { | ||
if observed == nil || desired == nil { | ||
return false, errors.New("observed or desired category is nil") | ||
} | ||
|
||
if observed.Spec.ForProvider.Name != desired.Spec.ForProvider.Name || | ||
observed.Spec.ForProvider.IncludeInMenu != desired.Spec.ForProvider.IncludeInMenu || | ||
observed.Spec.ForProvider.ParentID != desired.Spec.ForProvider.ParentID || | ||
observed.Spec.ForProvider.Position != desired.Spec.ForProvider.Position || | ||
observed.Spec.ForProvider.Level != desired.Spec.ForProvider.Level { | ||
return false, nil | ||
} | ||
|
||
return true, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ca7alindev Rename file to simple apis/v1alpha1/category_types.go
Why we need category word twice here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evghen1 in apis directory already exists v1alpha1 where are all magento provider level apis