Skip to content

Commit

Permalink
git issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
AngieCris committed Sep 25, 2017
1 parent 78cb773 commit 81111c0
Show file tree
Hide file tree
Showing 4 changed files with 565 additions and 13 deletions.
10 changes: 3 additions & 7 deletions lib/apiservers/service/restapi/configure_vic_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func configureAPI(api *operations.VicMachineAPI) http.Handler {
})

// GET /container/target/{target}/vch/{vch-id}
api.GetTargetTargetVchVchIDHandler = operations.GetTargetTargetVchVchIDHandlerFunc(func(params operations.GetTargetTargetVchVchIDParams, principal interface{}) middleware.Responder {
return middleware.NotImplemented("operation .GetTargetTargetVchVchID has not yet been implemented")
})
api.GetTargetTargetVchVchIDHandler = &handlers.VCHGet{}

// PUT /container/target/{target}/vch/{vch-id}
api.PutTargetTargetVchVchIDHandler = operations.PutTargetTargetVchVchIDHandlerFunc(func(params operations.PutTargetTargetVchVchIDParams, principal interface{}) middleware.Responder {
Expand Down Expand Up @@ -114,9 +112,7 @@ func configureAPI(api *operations.VicMachineAPI) http.Handler {
})

// GET /container/target/{target}/datacenter/{datacenter}/vch/{vch-id}
api.GetTargetTargetDatacenterDatacenterVchVchIDHandler = operations.GetTargetTargetDatacenterDatacenterVchVchIDHandlerFunc(func(params operations.GetTargetTargetDatacenterDatacenterVchVchIDParams, principal interface{}) middleware.Responder {
return middleware.NotImplemented("operation .GetTargetTargetDatacenterDatacenterVchVchID has not yet been implemented")
})
api.GetTargetTargetDatacenterDatacenterVchVchIDHandler = &handlers.VCHDatacenterGet{}

// PUT /container/target/{target}/datacenter/{datacenter}/vch/{vch-id}
api.PutTargetTargetDatacenterDatacenterVchVchIDHandler = operations.PutTargetTargetDatacenterDatacenterVchVchIDHandlerFunc(func(params operations.PutTargetTargetDatacenterDatacenterVchVchIDParams, principal interface{}) middleware.Responder {
Expand Down Expand Up @@ -165,4 +161,4 @@ func setupMiddlewares(handler http.Handler) http.Handler {
// So this is a good place to plug in a panic handling middleware, logging and metrics
func setupGlobalMiddleware(handler http.Handler) http.Handler {
return handler
}
}
124 changes: 124 additions & 0 deletions lib/apiservers/service/restapi/handlers/common.go
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"
}
Loading

0 comments on commit 81111c0

Please sign in to comment.