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

Pass full state to GetExternalNameFn function to access field other than ID #316

Merged

Conversation

sergenyalcin
Copy link
Member

Description of your changes

This PR passes full state to GetExternalNameFn function to access field other than ID.

I have:

  • Read and followed Upjet's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Tested against keyVault.AccessPolicy resource in provider-azure

…han ID

Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
if newState.ID == "" {
func (n *noForkExternal) setExternalName(mg xpresource.Managed, stateValueMap map[string]interface{}) (bool, error) {
id, ok := stateValueMap["id"]
if !ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

To be precise, we also need to make sure that the Terraform ID is not an empty string, i.e., it may be in the map but its value could be the empty string:

Suggested change
if !ok {
if !ok || id.(string) == "" {

, with the assumption that if the id key exists, its value is a string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

if err != nil {
return false, errors.Wrapf(err, "failed to get the external-name from ID: %s", newState.ID)
return false, errors.Wrapf(err, "failed to get the external-name from ID: %s", id)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
return false, errors.Wrapf(err, "failed to get the external-name from ID: %s", id)
return false, errors.Wrapf(err, "failed to compute the external-name from the state map of the resource with the ID %s", id)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

Comment on lines 604 to 605
stateValueMap, err := n.fromInstanceStateToJSONMap(newState)
if _, err := n.setExternalName(mg, stateValueMap); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make sure we check any errors returned from n.fromInstanceStateToJSONMap:

Suggested change
stateValueMap, err := n.fromInstanceStateToJSONMap(newState)
if _, err := n.setExternalName(mg, stateValueMap); err != nil {
stateValueMap, err := n.fromInstanceStateToJSONMap(newState)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, "failed to create the resource")
}
if _, err := n.setExternalName(mg, stateValueMap); err != nil {

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

Comment on lines 608 to 610
if err != nil {
return managed.ExternalCreation{}, err
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This block would be moved:

Suggested change
if err != nil {
return managed.ExternalCreation{}, err
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

- Move the error check to the correct place

Signed-off-by: Sergen Yalçın <yalcinsergen97@gmail.com>
Copy link
Collaborator

@ulucinar ulucinar left a comment

Choose a reason for hiding this comment

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

Thank you @sergenyalcin, lgtm.

@sergenyalcin sergenyalcin merged commit 0bc8185 into crossplane:main Dec 22, 2023
7 checks passed
@sergenyalcin sergenyalcin deleted the pass-allstate-to-getexternalnamefn branch December 22, 2023 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants