-
Notifications
You must be signed in to change notification settings - Fork 53
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
Draft: Add tests for importing templates #357
Conversation
What is the status of this PR ? are you experiencing some difficulties or do you need some help ? |
Hello @treywelsh, As far as I can see, the code for this PR should be ready, since the tests are passing. However, since this is my first pull-request, I am not sure whether the code makes sense from the perspective of an Opennebula-expert or Terraform-expert:
Of course I would like to write comments and/or documentation, but before this I would like to know whether this PR is good enough to start adding documentation, and whether anything needs to be written in the changelog. |
Hi, Thanks for your PR, it's an interesting one for someone who didn't contribute before. I asked you the state of this PR because at first glance I didn't get some changes.
|
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.
interesting first shot, thanks for your efforts
getTags[strings.ToLower(k)] = v | ||
} | ||
} | ||
d.Set("tags", getTags) |
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.
I think we don't need this, tags
are already read in the flattenVMUserTemplate
method called later in the code.
There is some parts of the code that are in methods due to the fact that are shared between resources.
The template resource share some code with the resources: VM, virtual router instance, and virtual router instance template
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.
In the method flattenVMUserTemplate
there was a line if tagsInterface, ok := d.GetOk("tags"); ok {
. Because of this, only tags which already were present in the state will be processed. I will add a commit where the method flattenVMUserTemplate
is updated: it will have a loop over all elements in the template, instead of over all elements in the state.
for _, pair := range vector.Pairs { | ||
getOS[strings.ToLower(pair.XMLName.Local)] = pair.Value | ||
} | ||
d.Set(strings.Replace(strings.ToLower(vector.XMLName.Local), "_", "", -1), append(make([]interface{}, 0), getOS)) |
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.
Please don't call to much methods in the same line this make you code more difficult to read, it's not a problem to add more code lines to make the code more readable/maintainable.
However you get how to read TypeSet
elements OS
, FEATURES
etc. that's a good point
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.
This long line has been separated in multiple lines, and an explanation has been added.
for _, pair := range vector.Pairs { | ||
switch pair.XMLName.Local { | ||
case "SET_HOSTNAME", "NETWORK": | ||
getOS[pair.XMLName.Local] = pair.Value |
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.
Reuse getOS
for the context make this code part less understandable.
You could just rename getOS
for something more generic like getVector
Or you could instead define a new map in each case of the switch.
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 for the suggestion, now the name getVector
is used.
d.Set("reg_time", tpl.RegTime) | ||
d.Set("description", "") |
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.
I'm surprised that you set an empty value, you could retrieve the description from the template via
desc, err := tpl.Get(vmk.Description)
...
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 fields description
and sched_requirements
were difficult to handle: if this field is removed from the Terraform-resource-configuration, it does not get removed from Terraform-state (it has an empty value, which gives problems in the unit-tests). I will write a new commit with an update of the method flattenVMUserTemplate
: in this method, the description
-field is already taken care of
@@ -233,7 +273,7 @@ resource "opennebula_template" "template" { | |||
|
|||
context = { | |||
dns_hostname = "yes" | |||
network = "YES" | |||
NETWORK = "YES" |
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 did you change the case ? is it for test purposes ?
Not a big deal but this just add a bit of noise for this PR.
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.
In the file opennebula/resource_opennebula_virtual_machine_test.go
, there are a lot of resources with the key context
. All these context-definitions have uppercase-keys, so it was easier to update this file opennebula/resource_opennebula_template_test.go
. This is also an example of conflicts between uppercase-text (Opennebula) and lowercase-text (Terraform).
I didn't used Next step, you could update your PR, discuss a bit with me and I'll make a new review to move forward. |
Hello, Thanks for your response. In have followed up on the detailed reviews, and written a response on some of them. About the general points:
|
Some automated tests are failing, which is related to #330. In this issue, the result was that tags in a virtual-machine-template will not be used in the Terraform-state for a virtual machine. This adds some complexity when a Terraform-managed virtual machines is imported into the Terraform-state. |
I saw some diffs in the CI test results so I tried to quickly dig around theses ones to understand what's happening.
But maybe that you are aware of all these points so what's the status, are you stuck ? |
Thanks for your response. Below are some concerns, for which I unfortunately do not have an easy solution.
|
The diffs we see are due to keys that are read by the provider but not described in the current resource being read (but added by OpenNebula, inherited, or due to the provider architecture ( First, I think this PR would need a list of keys to exclude when reading the template section of several resources (the template resource has also a template section it's a bit confusing). Here are some keys to intialize this list:
We need to make the more exhaustive list of attribute to make this list of keys to exclude. Each key we'll forget will be a potential future issue created by another user. Then we'll need to exclude virtual machine template keys inherithed from a template (when the VM has been instantiated from a template via the If we merge this PR, as you can see, there is a risk that the provider show some diffs for a bunch of users (in case they use an other hypervisor for instance etc.) and we may end up we a bunch of small issues to fix. Even if I'm trying to be careful I may still miss some cases. Thank you for this discussion |
I still need to check the failing tests. It seems that the changes from #362 have caused an extra test-failure, because the parameter |
FYI: in #378 ( |
FIX: typo - letter order in vCPU abbreviation
This pull request is stale because it has been open for 30 days with no activity and it is not in a milestone. Remove 'status: stale' label or comment, or this will be closed in 5 days. |
Community Note
Description
In this merge-request, it is attempted to add automated tests for importing templates. Since there are some differences between the output received from Opennebula and the data written into the Terraform-state, some workarounds were necessary.
References
#356
New or Affected Resource(s)
opennebula_template
Checklist
References
go fmt
)