-
Notifications
You must be signed in to change notification settings - Fork 144
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
fix: add hashing function on region attributes #286
Conversation
Tested and now we have all the regions displayed in Before:
After:
|
ovh/helpers/helpers.go
Outdated
func HashMapRegionAttributes(v interface{}) int { | ||
attributes, ok := v.(map[string]interface{}) | ||
if !ok { | ||
return 0 |
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.
Why are you returning 0?
In which case should we obtain ok == false?
Is it possible to return an error message or something?
Thanks
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.
Actually, I'm returning 0
to be conformed to the hashing function signature, that is func(interface{}) int
. Said that, the conversion check regarding the v interface
is just to ensure that everything goes well but, in case of any failure, the only returning option for this signature is an integer and not an error. Maybe, a -1
is a better option in place of returning 0
.
However, since the schema should not change, that conversion check is just a stretch. Let me know the way is better to modify. Also, having a code like this is an option:
// HashMapRegionAttributes creates an hash for the region attributes.
func HashMapRegionAttributes(v interface{}) int {
attributes := v.(map[string]interface{})
builder := strings.Builder{}
for _, key := range []string{"status", "region", "openstackid"} {
value, inMap := attributes[key]
if inMap {
stringValue, ok := value.(string)
if ok {
builder.WriteString(stringValue)
}
}
}
return schema.HashString(builder.String())
}
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.
Currently, I opted for that last option. I hope it is the best way to approach the problem.
The error must be handled. For example, the method can return an int and an error:
|
@scraly it is not possible to handle the error in that function because in a Terraform Schema the type SchemaSetFunc func(interface{}) int Consequently, returning a tuple instead of an integer is not an option at this level. |
Another option that doesn't involve many code changes in resource creation could be something like this: "regions_attributes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"openstackid": {
Type: schema.TypeString,
Computed: true,
},
},
},
} With such a solution, the helper function Which one is better for this case @scraly ? PS: of course, in the case of |
Hi, after thinking,
Thanks |
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.
Hi, after thinking,
is it possible to:
- move the function to
cloud_project_network_private_helpers.go
because the function should. be only used by the resource - rename the function to
RegionAttributesHash
for example - add an error log when the cast is failing and a panic
Thanks
All the requirements should be satisfied, let me know if something else is missing or if the error description is not conforming to the others in the module. |
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.
The only thing left to comply with the whole list is to put the function in a file next to the resource, for example: cloud_project_network_private_helpers.go :)
Right... I just misunderstood the previous message... |
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.
Thanks, now it's perfect :)
With this fix is resolved the #281