Skip to content

Commit

Permalink
Define REST API handlers for inspecting VCHs
Browse files Browse the repository at this point in the history
Introduce a pair of handlers for accessing information about the VCH
with a given identifier within a vSphere target or named datacenter.

There are ways in which this work is incomplete, marked by TODO:
 - We accept datacenters by name, not ID
 - The name of the VCH's compute resource is returned, not its ID
 - The names of portgroups used by the VCH are returned, not the IDs
 - Malformed or unexpected PKI material is ignored
  • Loading branch information
zjs committed Sep 22, 2017
1 parent 1a3e917 commit 6e38a9b
Show file tree
Hide file tree
Showing 6 changed files with 584 additions and 111 deletions.
28 changes: 14 additions & 14 deletions cmd/vic-machine/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ OPTIONS:
type Create struct {
common.Networks
*data.Data
certs common.CertFactory
Certs common.CertFactory
containerNetworks common.CNetworks
registries common.Registries

Expand All @@ -77,7 +77,7 @@ type Create struct {
advancedOptions bool
BridgeIPRange string

proxies common.Proxies
Proxies common.Proxies

syslogAddr string

Expand Down Expand Up @@ -243,12 +243,12 @@ func (c *Create) Flags() []cli.Flag {
Destination: &c.NumCPUs,
})

tls := c.certs.CertFlags()
tls := c.Certs.CertFlags()

tls = append(tls, cli.BoolFlag{
Name: "no-tls, k",
Usage: "Disable TLS support completely",
Destination: &c.certs.NoTLS,
Destination: &c.Certs.NoTLS,
Hidden: true,
})

Expand Down Expand Up @@ -321,7 +321,7 @@ func (c *Create) Flags() []cli.Flag {
iso := c.ImageFlags(true)
cNetwork := c.containerNetworks.CNetworkFlags(true)
dns := c.dns.DNSFlags(true)
proxies := c.proxies.ProxyFlags(true)
proxies := c.Proxies.ProxyFlags(true)
debug := c.DebugFlags(true)

// flag arrays are declared, now combined
Expand Down Expand Up @@ -409,7 +409,7 @@ func (c *Create) processParams() error {
c.WhitelistRegistries = c.registries.WhitelistRegistries
c.RegistryCAs = c.registries.RegistryCAs

hproxy, sproxy, err := c.proxies.ProcessProxies()
hproxy, sproxy, err := c.Proxies.ProcessProxies()
if err != nil {
return err
}
Expand All @@ -433,16 +433,16 @@ func (c *Create) processCertificates() error {
debug = *c.Debug.Debug
}

c.certs.Networks = c.Networks
c.Certs.Networks = c.Networks

if err := c.certs.ProcessCertificates(c.DisplayName, c.Force, debug); err != nil {
if err := c.Certs.ProcessCertificates(c.DisplayName, c.Force, debug); err != nil {
return err
}

// copy a few things out of seed because ProcessCertificates has side effects
c.KeyPEM = c.certs.KeyPEM
c.CertPEM = c.certs.CertPEM
c.ClientCAs = c.certs.ClientCAs
c.KeyPEM = c.Certs.KeyPEM
c.CertPEM = c.Certs.CertPEM
c.ClientCAs = c.Certs.ClientCAs

return nil
}
Expand Down Expand Up @@ -757,9 +757,9 @@ func (c *Create) Run(clic *cli.Context) (err error) {
}
}()

if err = executor.CheckServiceReady(ctx, vchConfig, c.certs.ClientCert); err != nil {
if err = executor.CheckServiceReady(ctx, vchConfig, c.Certs.ClientCert); err != nil {
executor.CollectDiagnosticLogs()
cmd, _ := executor.GetDockerAPICommand(vchConfig, c.certs.Ckey, c.certs.Ccert, c.certs.Cacert, c.certs.CertPath)
cmd, _ := executor.GetDockerAPICommand(vchConfig, c.Certs.Ckey, c.Certs.Ccert, c.Certs.Cacert, c.Certs.CertPath)
log.Info("\tAPI may be slow to start - try to connect to API after a few minutes:")
if cmd != "" {
log.Infof("\t\tRun command: %s", cmd)
Expand All @@ -774,7 +774,7 @@ func (c *Create) Run(clic *cli.Context) (err error) {

// We must check for the volume stores that are present after the portlayer presents.

executor.ShowVCH(vchConfig, c.certs.Ckey, c.certs.Ccert, c.certs.Cacert, c.certs.EnvFile, c.certs.CertPath)
executor.ShowVCH(vchConfig, c.Certs.Ckey, c.Certs.Ccert, c.Certs.Cacert, c.Certs.EnvFile, c.Certs.CertPath)
log.Infof("Installer completed successfully")

return nil
Expand Down
8 changes: 2 additions & 6 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
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 6e38a9b

Please sign in to comment.