// EnsureDefaultStorage ensures that the input profile is configured with a
// disk device, creating a new storage pool and a device if required.
962099
func (s *Server) EnsureDefaultStorage(profile *api.Profile, eTag string) error {
	// If there is already a "/" device, we have nothing to do.
	for _, dev := range profile.Devices {
		if dev["path"] == "/" {
			return nil
		}
	}

	// If there is a "default" pool, use it.
	// Otherwise if there are other pools available, choose the first.
	pools, err := s.GetStoragePoolNames()
	if err != nil {
		return errors.Trace(err)
	}
	poolName := ""
	for _, p := range pools {
		if p == "default" {
			poolName = p
		}
	}
	if poolName == "" && len(pools) > 0 {
		poolName = pools[0]
	}

	// We need to create a new storage pool.
	if poolName == "" {
		poolName = "default"
		req := api.StoragePoolsPost{
			Name:   poolName,
			Driver: "dir",
		}
		err := s.CreateStoragePool(req)
		if err != nil {
			return errors.Trace(err)
		}
	}

	// Create a new disk device in the input profile.
	if profile.Devices == nil {
		profile.Devices = map[string]device{}
	}
	profile.Devices["root"] = map[string]string{
		"type": "disk",
		"path": "/",
		"pool": poolName,
	}

	if err := s.UpdateProfile(profile.Name, profile.Writable(), eTag); err != nil {
		return errors.Trace(err)
	}
	logger.Debugf("created new disk device \"root\" in profile %q", profile.Name)
	return nil
}
1192981
func CreateDefaultESX(f *Folder) {
	dc := NewDatacenter(f)

	host := NewHostSystem(esx.HostSystem)

	summary := new(types.ComputeResourceSummary)
	addComputeResource(summary, host)

	cr := &mo.ComputeResource{
		Summary: summary,
		Network: esx.Datacenter.Network,
	}
	cr.EnvironmentBrowser = newEnvironmentBrowser()
	cr.Self = *host.Parent
	cr.Name = host.Name
	cr.Host = append(cr.Host, host.Reference())
	host.Network = cr.Network
	Map.PutEntity(cr, host)

	pool := NewResourcePool()
	cr.ResourcePool = &pool.Self
	Map.PutEntity(cr, pool)
	pool.Owner = cr.Self

	Map.Get(dc.HostFolder).(*Folder).putChild(cr)
}
255337
func (p *SdkPolicyManager) SetDefault(
	ctx context.Context,
	req *api.SdkOpenStoragePolicySetDefaultRequest,
) (*api.SdkOpenStoragePolicySetDefaultResponse, error) {
	if req.GetName() == "" {
		return nil, status.Error(codes.InvalidArgument, "Must supply a Storage Policy Name")
	}

	// verify policy exists, before setting it as default
	policy, err := p.Inspect(ctx,
		&api.SdkOpenStoragePolicyInspectRequest{
			Name: req.GetName(),
		},
	)
	if err != nil {
		return nil, err
	}

	// Only administrator can set policy as default storage
	// policy restriction
	user, _ := auth.NewUserInfoFromContext(ctx)
	if !policy.GetStoragePolicy().GetOwnership().IsAdminByUser(user) {
		return nil, status.Errorf(codes.PermissionDenied, "Only the storage system admin can set storage policy as default %v", req.GetName())
	}

	policyStr, err := json.Marshal(policy.GetStoragePolicy().GetName())
	if err != nil {
		return nil, status.Errorf(codes.Internal, "Json marshal failed for policy %s :%v", req.GetName(), err)
	}

	_, err = p.kv.Update(defaultPath, policyStr, 0)
	if err == kvdb.ErrNotFound {
		if _, err := p.kv.Create(defaultPath, policyStr, 0); err != nil {
			return nil, status.Errorf(codes.Internal, "Unable to save default policy details %v", err)
		}
	} else if err != nil {
		return nil, status.Errorf(codes.Internal, "Failed to set default policy: %v", err)
	}
	logrus.Infof("Storage Policy %v is set as default", policy.GetStoragePolicy().GetName())
	return &api.SdkOpenStoragePolicySetDefaultResponse{}, nil
}
469307
func (e *Environment) ensurePrepared(c context.Context) {
	e.prepareOnce.Do(func() {
		e.processCacheData = caching.NewProcessCacheData()
		e.globalSettings = settings.New(gaesettings.Storage{})
		if e.Prepare != nil {
			e.Prepare(c)
		}
	})
}
934171
func (env *environ) StorageProvider(t storage.ProviderType) (storage.Provider, error) {
	if env.storageSupported() && t == lxdStorageProviderType {
		return &lxdStorageProvider{env}, nil
	}
	return nil, errors.NotFoundf("storage provider %q", t)
}
758588
func GetOrDefaultString(envVar string, defaultValue string) string {
	v := GetOrFile(envVar)
	if len(v) == 0 {
		return defaultValue
	}

	return v
}
1318249
func (p *StorageProvider) DefaultPools() []*storage.Config {
	p.MethodCall(p, "DefaultPools")
	return p.DefaultPools_
}
481050
func (k *kubernetesClient) CheckDefaultWorkloadStorage(cluster string, storageProvisioner *caas.StorageProvisioner) error {
	preferredStorage, ok := jujuPreferredWorkloadStorage[cluster]
	if !ok {
		return errors.NotFoundf("cluster %q", cluster)
	}
	return storageClassMatches(preferredStorage, storageProvisioner)
}
753027
func EnableOrDisableStorageClasses(name, val string) error {
	enable, err := strconv.ParseBool(val)
	if err != nil {
		return errors.Wrap(err, "Error parsing boolean")
	}

	class := constants.DefaultStorageClassProvisioner
	if name == "storage-provisioner-gluster" {
		class = "glusterfile"
	}

	if enable {
		// Only StorageClass for 'name' should be marked as default
		err := storageclass.SetDefaultStorageClass(class)
		if err != nil {
			return errors.Wrapf(err, "Error making %s the default storage class", class)
		}
	} else {
		// Unset the StorageClass as default
		err := storageclass.DisableDefaultStorageClass(class)
		if err != nil {
			return errors.Wrapf(err, "Error disabling %s as the default storage class", class)
		}
	}

	return EnableOrDisableAddon(name, val)
}
716798
func (*maasEnviron) StorageProvider(t storage.ProviderType) (storage.Provider, error) {
	if t == maasStorageProviderType {
		return maasStorageProvider{}, nil
	}
	return nil, errors.NotFoundf("storage provider %q", t)
}
85469
func (s *CreateFleetInput) SetEnableDefaultInternetAccess(v bool) *CreateFleetInput {
	s.EnableDefaultInternetAccess = &v
	return s
}
296342
func createDefaultCookie() {
	settingMutex.Lock()
	defer settingMutex.Unlock()
	defaultCookieJar, _ = cookiejar.New(nil)
}
534189
func (sam *SAM) EnsureKeyfile(fname string) (keys I2PKeys, err error) {
	if fname == "" {
		// transient
		keys, err = sam.NewKeys()
		if err == nil {
			sam.keys = &keys
		}
	} else {
		// persistant
		_, err = os.Stat(fname)
		if os.IsNotExist(err) {
			// make the keys
			keys, err = sam.NewKeys()
			if err == nil {
				sam.keys = &keys
				// save keys
				var f io.WriteCloser
				f, err = os.OpenFile(fname, os.O_WRONLY|os.O_CREATE, 0600)
				if err == nil {
					err = StoreKeysIncompat(keys, f)
					f.Close()
				}
			}
		} else if err == nil {
			// we haz key file
			var f *os.File
			f, err = os.Open(fname)
			if err == nil {
				keys, err = LoadKeysIncompat(f)
				if err == nil {
					sam.keys = &keys
				}
			}
		}
	}
	return
}
127401
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
	configFile, err := Load(Dir())
	if err != nil {
		fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
	}
	if !configFile.ContainsAuth() {
		configFile.CredentialsStore = credentials.DetectDefaultStore(configFile.CredentialsStore)
	}
	return configFile
}
569761
func (store *LimitedStore) ensureSpace(size int64) error {
	if (store.usedSize + size) <= store.StoreSize {
		// Enough space is available to store the new upload
		return nil
	}

	sortedUploads := make(pairlist, len(store.uploads))
	i := 0
	for u, h := range store.uploads {
		sortedUploads[i] = pair{u, h}
		i++
	}
	sort.Sort(sort.Reverse(sortedUploads))

	// Forward traversal through the uploads in terms of size, biggest upload first
	for _, k := range sortedUploads {
		id := k.key

		if err := store.terminate(id); err != nil {
			return err
		}

		if (store.usedSize + size) <= store.StoreSize {
			// Enough space has been freed to store the new upload
			return nil
		}
	}

	return nil
}
490530
func (c *cache) ensureCapacity(toAdd uint64) {
	mustRemove := int64(c.size+toAdd) - int64(c.cap)
	for mustRemove > 0 {
		key := c.keyList.Back().Value.(string)
		mustRemove -= int64(c.items[key].item.Size())
		c.remove(key)
	}
}
43516
func (c *NodeupModelContext) EnsureDirectory(path string) error {
	st, err := os.Stat(path)
	if err != nil {
		if os.IsNotExist(err) {
			return os.MkdirAll(path, 0755)
		}

		return err
	}

	if !st.IsDir() {
		return fmt.Errorf("path: %s already exists but is not a directory", path)
	}

	return nil
}