github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/gce/provider.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package gce 5 6 import ( 7 stdcontext "context" 8 9 "github.com/juju/errors" 10 "github.com/juju/jsonschema" 11 "github.com/juju/schema" 12 "gopkg.in/juju/environschema.v1" 13 14 "github.com/juju/juju/cloud" 15 "github.com/juju/juju/environs" 16 environscloudspec "github.com/juju/juju/environs/cloudspec" 17 "github.com/juju/juju/environs/config" 18 "github.com/juju/juju/environs/context" 19 ) 20 21 const ( 22 // provider version 1 introduces labels for disks, 23 // for associating them with a model and controller. 24 providerVersion1 = 1 25 26 currentProviderVersion = providerVersion1 27 ) 28 29 type environProvider struct { 30 environProviderCredentials 31 } 32 33 var providerInstance environProvider 34 35 // Version is part of the EnvironProvider interface. 36 func (environProvider) Version() int { 37 return currentProviderVersion 38 } 39 40 // Open implements environs.EnvironProvider. 41 func (environProvider) Open(_ stdcontext.Context, args environs.OpenParams) (environs.Environ, error) { 42 if err := validateCloudSpec(args.Cloud); err != nil { 43 return nil, errors.Annotate(err, "validating cloud spec") 44 } 45 env, err := newEnviron(args.Cloud, args.Config) 46 return env, errors.Trace(err) 47 } 48 49 // CloudSchema returns the schema used to validate input for add-cloud. Since 50 // this provider does not support custom clouds, this always returns nil. 51 func (p environProvider) CloudSchema() *jsonschema.Schema { 52 return nil 53 } 54 55 // Ping tests the connection to the cloud, to verify the endpoint is valid. 56 func (p environProvider) Ping(ctx context.ProviderCallContext, endpoint string) error { 57 return errors.NotImplementedf("Ping") 58 } 59 60 // PrepareConfig implements environs.EnvironProvider. 61 func (p environProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) { 62 if err := validateCloudSpec(args.Cloud); err != nil { 63 return nil, errors.Annotate(err, "validating cloud spec") 64 } 65 return configWithDefaults(args.Config) 66 } 67 68 func validateCloudSpec(spec environscloudspec.CloudSpec) error { 69 if err := spec.Validate(); err != nil { 70 return errors.Trace(err) 71 } 72 if spec.Credential == nil { 73 return errors.NotValidf("missing credential") 74 } 75 switch authType := spec.Credential.AuthType(); authType { 76 case cloud.OAuth2AuthType, cloud.JSONFileAuthType: 77 default: 78 return errors.NotSupportedf("%q auth-type", authType) 79 } 80 return nil 81 } 82 83 // Schema returns the configuration schema for an environment. 84 func (environProvider) Schema() environschema.Fields { 85 fields, err := config.Schema(configSchema) 86 if err != nil { 87 panic(err) 88 } 89 return fields 90 } 91 92 // ConfigSchema returns extra config attributes specific 93 // to this provider only. 94 func (p environProvider) ConfigSchema() schema.Fields { 95 return configFields 96 } 97 98 // ConfigDefaults returns the default values for the 99 // provider specific config attributes. 100 func (p environProvider) ConfigDefaults() schema.Defaults { 101 return configDefaults 102 } 103 104 // UpgradeModelConfig is specified in the ModelConfigUpgrader interface. 105 func (environProvider) UpgradeConfig(cfg *config.Config) (*config.Config, error) { 106 return configWithDefaults(cfg) 107 } 108 109 func configWithDefaults(cfg *config.Config) (*config.Config, error) { 110 defaults := make(map[string]interface{}) 111 if _, ok := cfg.StorageDefaultBlockSource(); !ok { 112 // Set the default block source. 113 defaults[config.StorageDefaultBlockSourceKey] = storageProviderType 114 } 115 if len(defaults) == 0 { 116 return cfg, nil 117 } 118 return cfg.Apply(defaults) 119 } 120 121 // Validate implements environs.EnvironProvider.Validate. 122 func (environProvider) Validate(cfg, old *config.Config) (*config.Config, error) { 123 newCfg, err := newConfig(cfg, old) 124 if err != nil { 125 return nil, errors.Annotate(err, "invalid config") 126 } 127 return newCfg.config, nil 128 }