-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
565 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright 2017 VMware, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/vmware/govmomi/object" | ||
"github.com/vmware/govmomi/vim25/types" | ||
"github.com/vmware/vic/cmd/vic-machine/common" | ||
"github.com/vmware/vic/lib/apiservers/service/restapi/handlers/util" | ||
"github.com/vmware/vic/lib/install/data" | ||
"github.com/vmware/vic/lib/install/validate" | ||
"github.com/vmware/vic/pkg/version" | ||
"github.com/vmware/vic/pkg/vsphere/vm" | ||
) | ||
|
||
func buildData(ctx context.Context, url url.URL, user string, pass string, thumbprint *string, datacenter *string, computeResource *string) (*data.Data, error) { | ||
d := data.Data{ | ||
Target: &common.Target{ | ||
URL: &url, | ||
User: user, | ||
Password: &pass, | ||
}, | ||
} | ||
|
||
if thumbprint != nil { | ||
d.Thumbprint = *thumbprint | ||
} | ||
|
||
if datacenter != nil { | ||
validator, err := validateTarget(ctx, &d) | ||
if err != nil { | ||
return nil, util.WrapError(500, err) | ||
} | ||
|
||
datacenterManagedObjectReference := types.ManagedObjectReference{Type: "Datacenter", Value: *datacenter} | ||
|
||
datacenterObject, err := validator.Session.Finder.ObjectReference(ctx, datacenterManagedObjectReference) | ||
if err != nil { | ||
return nil, util.WrapError(500, err) | ||
} | ||
|
||
d.Target.URL.Path = datacenterObject.(*object.Datacenter).InventoryPath | ||
} | ||
|
||
if computeResource != nil { | ||
d.ComputeResourcePath = *computeResource | ||
} | ||
|
||
return &d, nil | ||
} | ||
|
||
func validateTarget(ctx context.Context, d *data.Data) (*validate.Validator, error) { | ||
if err := d.HasCredentials(); err != nil { | ||
return nil, fmt.Errorf("Invalid Credentials: %s", err) | ||
} | ||
|
||
validator, err := validate.NewValidator(ctx, d) | ||
if err != nil { | ||
return nil, fmt.Errorf("Validation Error: %s", err) | ||
} | ||
// If dc is not set, and multiple datacenter is available, vic-machine ls will list VCHs under all datacenters. | ||
validator.AllowEmptyDC() | ||
|
||
_, err = validator.ValidateTarget(ctx, d) | ||
if err != nil { | ||
return nil, fmt.Errorf("Target validation failed: %s", err) | ||
} | ||
_, err = validator.ValidateCompute(ctx, d, false) | ||
if err != nil { | ||
return nil, fmt.Errorf("Compute resource validation failed: %s", err) | ||
} | ||
|
||
return validator, nil | ||
} | ||
|
||
// Copied from list.go, and appears to be present other places. TODO: deduplicate | ||
func upgradeStatusMessage(ctx context.Context, vch *vm.VirtualMachine, installerVer *version.Build, vchVer *version.Build) string { | ||
if sameVer := installerVer.Equal(vchVer); sameVer { | ||
return "Up to date" | ||
} | ||
|
||
upgrading, err := vch.VCHUpdateStatus(ctx) | ||
if err != nil { | ||
return fmt.Sprintf("Unknown: %s", err) | ||
} | ||
if upgrading { | ||
return "Upgrade in progress" | ||
} | ||
|
||
canUpgrade, err := installerVer.IsNewer(vchVer) | ||
if err != nil { | ||
return fmt.Sprintf("Unknown: %s", err) | ||
} | ||
if canUpgrade { | ||
return fmt.Sprintf("Upgradeable to %s", installerVer.ShortVersion()) | ||
} | ||
|
||
oldInstaller, err := installerVer.IsOlder(vchVer) | ||
if err != nil { | ||
return fmt.Sprintf("Unknown: %s", err) | ||
} | ||
if oldInstaller { | ||
return fmt.Sprintf("VCH has newer version") | ||
} | ||
|
||
// can't get here | ||
return "Invalid upgrade status" | ||
} |
Oops, something went wrong.