Skip to content

Commit

Permalink
Allow show-cloud --include-config to work for vsphere.
Browse files Browse the repository at this point in the history
Fixes LP:1884794

Use environschema for configSchema, providing enhanced information
about possible config.  Implement ConfigSchema, ConfigDefaults from
the ConfigSchemaSource interface. Implement Schema.
  • Loading branch information
hmlanigan committed May 28, 2021
1 parent 293097d commit aa60d33
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
62 changes: 55 additions & 7 deletions provider/vsphere/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package vsphere
import (
"github.com/juju/errors"
"github.com/juju/schema"
"gopkg.in/juju/environschema.v1"

"github.com/juju/juju/environs/config"
"github.com/juju/juju/provider/vsphere/internal/vsphereclient"
Expand All @@ -23,13 +24,31 @@ const (

// configFields is the spec for each vmware config value's type.
var (
configFields = schema.Fields{
cfgExternalNetwork: schema.String(),
cfgDatastore: schema.String(),
cfgPrimaryNetwork: schema.String(),
cfgForceVMHardwareVersion: schema.ForceInt(),
cfgEnableDiskUUID: schema.Bool(),
cfgDiskProvisioningType: schema.String(),
configSchema = environschema.Fields{
cfgExternalNetwork: {
Description: "An external network that VMs will be connected to. The resulting IP address for a VM will be used as its public address.",
Type: environschema.Tstring,
},
cfgDatastore: {
Description: "The datastore in which to create VMs. If this is not specified, the process will abort unless there is only one datastore available.",
Type: environschema.Tstring,
},
cfgPrimaryNetwork: {
Description: "The primary network that VMs will be connected to. If this is not specified, Juju will look for a network named VM Network.",
Type: environschema.Tstring,
},
cfgForceVMHardwareVersion: {
Description: "The HW compatibility version to use when cloning a VM template to create a VM. The version must be supported by the remote compute resource, and greater or equal to the template’s version.",
Type: environschema.Tint,
},
cfgEnableDiskUUID: {
Description: "Expose consistent disk UUIDs to the VM, equivalent to disk.EnableUUID. The default is True.",
Type: environschema.Tbool,
},
cfgDiskProvisioningType: {
Description: "Specify how the disk should be provisioned when cloning the VM template. Allowed values are: thickEagerZero (default), thick and thin.",
Type: environschema.Tstring,
},
}

configDefaults = schema.Defaults{
Expand Down Expand Up @@ -59,6 +78,14 @@ func newConfig(cfg *config.Config) *environConfig {
}
}

var configFields = func() schema.Fields {
fs, _, err := configSchema.ValidationSchema()
if err != nil {
panic(err)
}
return fs
}()

// newValidConfig builds a new environConfig from the provided Config
// and returns it. The resulting config values are validated.
func newValidConfig(cfg *config.Config) (*environConfig, error) {
Expand Down Expand Up @@ -139,6 +166,27 @@ func (c *environConfig) diskProvisioningType() vsphereclient.DiskProvisioningTyp
return vsphereclient.DiskProvisioningType(provTypeStr)
}

// Schema returns the configuration schema for an environment.
func (environProvider) Schema() environschema.Fields {
fields, err := config.Schema(configSchema)
if err != nil {
panic(err)
}
return fields
}

// ConfigSchema returns extra config attributes specific
// to this provider only.
func (p environProvider) ConfigSchema() schema.Fields {
return configFields
}

// ConfigDefaults returns the default values for the
// provider specific config attributes.
func (p environProvider) ConfigDefaults() schema.Defaults {
return configDefaults
}

// validate checks vmware-specific config values.
func (c environConfig) validate() error {
// All fields must be populated, even with just the default.
Expand Down
13 changes: 13 additions & 0 deletions provider/vsphere/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,16 @@ func (s *ConfigSuite) TestSetConfig(c *gc.C) {
}
}
}

func (s *ConfigSuite) TestSchema(c *gc.C) {
ps, ok := s.provider.(environs.ProviderSchema)
c.Assert(ok, jc.IsTrue)

fields := ps.Schema()

globalFields, err := config.Schema(nil)
c.Assert(err, gc.IsNil)
for name, field := range globalFields {
c.Check(fields[name], jc.DeepEquals, field)
}
}
2 changes: 2 additions & 0 deletions provider/vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type environProvider struct {
dial DialFunc
}

var _ config.ConfigSchemaSource = (*environProvider)(nil)

// EnvironProviderConfig contains configuration for the EnvironProvider.
type EnvironProviderConfig struct {
// Dial is a function used for dialing connections to vCenter/ESXi.
Expand Down

0 comments on commit aa60d33

Please sign in to comment.