Skip to content
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

Add product resource support #447

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

bulat-khusnimardanov
Copy link
Contributor

@bulat-khusnimardanov bulat-khusnimardanov commented Nov 6, 2023

This merge request adds support for creating and managing Product resources.

Mock server extension is a prerequisite: labd/commercetools-node-mock#122

NEW FEATURES | UPGRADE NOTES | ENHANCEMENTS | BUG FIXES | EXPERIMENTS

  • Addition of Product resource creation and management capabilities.

@bulat-khusnimardanov bulat-khusnimardanov marked this pull request as ready for review November 15, 2023 08:16
@bulat-khusnimardanov bulat-khusnimardanov requested a review from a team as a code owner November 15, 2023 08:16
@demeyerthom demeyerthom self-assigned this Nov 15, 2023
Copy link
Member

@demeyerthom demeyerthom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big PR :). I left a few comments on things i would like to see changed.

I am curious at your usecase here though. I would never recommend putting an actual product into terraform state, as the data is normally much to ephermal to be considered configuration (same with stuff like customers, prices etc).

Mind you, I am not opposed to merging this pr to add this to the provider, but I am trying to understand why it is necessary

Optional: true,
},
},
Blocks: map[string]schema.Block{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to follow the terraform suggestion here to not use blocks but instead nested attributes: https://developer.hashicorp.com/terraform/plugin/framework/handling-data/blocks. This is not something we have been doing consistently in the current code base, but is something I want to change to at some point (in a newer version of the provider). Would it be possible to make that change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I tried this one, but I encountered an error. I assumed that it may be due to the current "semi-migrated" state, but I could be mistaken.

2023/11/15 15:46:05 error retrieving schema for *proto5server.Server:
	Attribute: 
	Summary: Error converting resource schema
	Detail: The schema for the resource "commercetools_product" couldn't be converted into a usable type. This is always a problem with the provider. Please report the following to the provider developer:
AttributeName("example_attribute"): protocol version 5 cannot have Attributes set

)

// Product represents the main schema data.
type Product struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure about this data model. The actual implementation for a product has the masterData field, which contains both the current and staged versions of the product. I am uncertain if we want to mirror this data model in the provider directly, or if we want to reduce the complexity a bit by making the assumption that a product that is managed in terraform should always be in a published state (so we can basically ignore staged, as it will always be the same as published). What is your usecase here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have implemented the approach you described where the stage and current values are always the same.
Personally, I do not see the need to manage these values separately in the Terraform template.

func marshalAttributeValue(o platform.Attribute) string {
val, err := json.Marshal(o.Value)
if err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we return this error instead of panic-ing? It allows terraform to deal with the failing resource a bit more elegantly (this might stop the whole application midway processing, which might cause side-effects when other processes are running in parallel)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will fix this.


type Attribute struct {
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to model this value as a custom tfsdk value instead: https://developer.hashicorp.com/terraform/plugin/framework/handling-data/types/custom. That way we can still use the power of defined types within the sdk, without hiding the complexity as marshalled json strings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look at this one as well.

@demeyerthom demeyerthom added the enhancement New feature or request label Nov 15, 2023
@bulat-khusnimardanov
Copy link
Contributor Author

Big PR :). I left a few comments on things i would like to see changed.

I am curious at your usecase here though. I would never recommend putting an actual product into terraform state, as the data is normally much to ephermal to be considered configuration (same with stuff like customers, prices etc).

Mind you, I am not opposed to merging this pr to add this to the provider, but I am trying to understand why it is necessary

The use case is to create discount templates that need to be populated along with the configuration.
They are necessary for testing purposes and can serve as examples for customers.
So we are not going to store real products in terraform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants