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

r/virtual_machine: Ignore validation when interpolation not available #784

Merged
merged 1 commit into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions vsphere/internal/helper/structure/structure_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,16 @@ func (s MoRefSorter) Less(i, j int) bool {
func (s MoRefSorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// ValuesAvailable takes a subresource path and a list of keys and checks that
// the value for each key is available at CustomizeDiff time. This function
// will return false if any of they values are based on computed values from
// other new or updated resources.
func ValuesAvailable(base string, keys []string, d *schema.ResourceDiff) bool {
for _, k := range keys {
if !d.NewValueKnown(fmt.Sprintf("%s%s", base, k)) {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ func CdromDiffOperation(d *schema.ResourceDiff, c *govmomi.Client) error {
for ci, ce := range cr.([]interface{}) {
cm := ce.(map[string]interface{})
r := NewCdromSubresource(c, d, cm, nil, ci)
if !structure.ValuesAvailable(fmt.Sprintf("%s.%d.", subresourceTypeCdrom, ci), []string{"datastore_id", "path"}, d) {
log.Printf("[DEBUG] CdromDiffOperation: Cdrom contains a value that depends on a computed value from another resource. Skipping validation")
return nil
}
if err := r.ValidateDiff(); err != nil {
return fmt.Errorf("%s: %s", r.Addr(), err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,9 @@ func ExpandCustomizationSpec(d *schema.ResourceData, family string) types.Custom
// spec. It should be called during diff customization to veto invalid configs.
func ValidateCustomizationSpec(d *schema.ResourceDiff, family string) error {
// Validate that the proper section exists for OS family suboptions.
linuxExists := len(d.Get(cKeyPrefix+"."+"linux_options").([]interface{})) > 0
windowsExists := len(d.Get(cKeyPrefix+"."+"windows_options").([]interface{})) > 0
sysprepExists := d.Get(cKeyPrefix+"."+"windows_sysprep_text").(string) != ""
linuxExists := len(d.Get(cKeyPrefix+"."+"linux_options").([]interface{})) > 0 || !structure.ValuesAvailable(cKeyPrefix+"."+"linux_options.", []string{"host_name", "domain"}, d)
windowsExists := len(d.Get(cKeyPrefix+"."+"windows_options").([]interface{})) > 0 || !structure.ValuesAvailable(cKeyPrefix+"."+"windows_options.", []string{"computer_name"}, d)
sysprepExists := d.Get(cKeyPrefix+"."+"windows_sysprep_text").(string) != "" || !structure.ValuesAvailable(cKeyPrefix+".", []string{"windows_sysprep_text"}, d)
switch {
case family == string(types.VirtualMachineGuestOsFamilyLinuxGuest) && !linuxExists:
return errors.New("linux_options must exist in VM customization options for Linux operating systems")
Expand Down
4 changes: 4 additions & 0 deletions vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,10 @@ func resourceVSphereVirtualMachineCustomizeDiffResourcePoolOperation(d *schema.R
}

func datastoreClusterDiffOperation(d *schema.ResourceDiff, client *govmomi.Client) error {
if !structure.ValuesAvailable("", []string{"datastore_cluster_id", "datastore_id"}, d) {
log.Printf("[DEBUG] DatastoreClusterDiffOperation: datastore_id or datastore_cluster_id value depends on a computed value from another resource. Skipping validation.")
return nil
}
podID, podOk := d.GetOk("datastore_cluster_id")
podKnown := d.NewValueKnown("datastore_cluster_id")
dsID, dsOk := d.GetOk("datastore_id")
Expand Down
Loading