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

created adapter_type variable for network interfaces #193

Merged
merged 4 commits into from Oct 11, 2017

Conversation

shelson
Copy link
Contributor

@shelson shelson commented Oct 10, 2017

I needed this functionality and noticed that it was already commented with "TODO". It's my first foray into go so the following might be better worked out another way:

adapter_type_string := fmt.Sprintf("%T", device)
if adapter_type_string == "*types.VirtualE1000" {

Otherwise though it seems to work just fine.

Copy link
Contributor

@vancluever vancluever left a comment

Choose a reason for hiding this comment

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

Hey @shelson - thanks for this PR! This definitely seems like an easy win ahead of the refactor coming soon.

I put some general feedback in the PR. There's a few things you can do using both the TF API and Go to make this better.

Thanks!

@@ -382,6 +387,21 @@ func resourceVSphereVirtualMachine() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "vmxnet3",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use StringInSlice from the validation package in TF core to make this much easier on the eyes.

The syntax would be here:

ValidateFunc: validation.StringInSlice(virtualMachineNetworkAdapterTypeAllowedValues, false)

@@ -36,6 +36,11 @@ var DiskControllerTypes = []string{
"ide",
}

var NetworkAdapterTypes = []string{
Copy link
Contributor

Choose a reason for hiding this comment

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

This variable should be unexported (starts with a lowercase letter, ensures that no package outside of vsphere can use it - basically a private variable), and be specific enough to avoid collisions.

Suggestion: virtualMachineNetworkAdapterTypeAllowedValues.

Also, you might want to check here to see if there is any appropriate constants you can use.

@@ -1144,6 +1167,12 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
log.Printf("[DEBUG] Device list %+v", deviceList)
for _, device := range deviceList {
networkInterface := make(map[string]interface{})
adapter_type_string := fmt.Sprintf("%T", device)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, rather than printing the type out, you should be using the more spec-native type assertion.

The code below would look like this:

if _, ok := device.(*types.VirtualE1000); ok {
  networkInterface["adapter_type"] = "e1000"
} else {
  networkInterface["adapter_type"] = "vmxnet3"
}

You can also use the switch type assertion format as well.

…nd shortened validation function drastically
@vancluever
Copy link
Contributor

Hey @shelson, thanks for the update!

Next issue: the tests are failing because you haven't run the changes through go fmt.

Depending on the IDE/editor you are using this is set up different ways - if you can tell me I might be able to help.

Also see here: https://blog.golang.org/go-fmt-your-code

@shelson
Copy link
Contributor Author

shelson commented Oct 10, 2017

Thanks for the awesome feedback - I really appreciate you taking the time to help me get this in a shippable state.

I'll work on the go-fmt thing in the morning as it's getting late here now.

Thanks again.

shelson and others added 2 commits October 10, 2017 22:25
Now that adapter_type has been exposed properly, document it.

The adapter types will be improved on during the refactor.
@vancluever
Copy link
Contributor

LGTM to me now @shelson! Merging now. Thanks for the contribution!

@vancluever vancluever merged commit 6f4416a into hashicorp:master Oct 11, 2017
@hashicorp hashicorp locked and limited conversation to collaborators Apr 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants